Skip to content

Commit

Permalink
Add simple example project.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpwalkerdine committed Aug 19, 2018
1 parent a550899 commit 63e20c4
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
10 changes: 10 additions & 0 deletions example/calculator/calc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package calculator

// Calculator is a contrived example for specification purposes.
type Calculator struct{}

// Add is a + b.
func (c Calculator) Add(a, b int) int { return a + b }

// Sub is a - b.
func (c Calculator) Sub(a, b int) int { return a - b }
49 changes: 49 additions & 0 deletions example/calculator/calc_steps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package calculator_test

import (
"strconv"
"testing"

"github.com/mpwalkerdine/elicit"
sut "github.com/mpwalkerdine/elicit/example/calculator"
)

type operation struct {
left int
symbol rune
right int
}

var steps = elicit.Steps{}
var transforms = elicit.Transforms{}

func init() {
transforms["`(\\d+)\\s*([+-])\\s*(\\d+)`"] =
func(params []string) operation {
left, _ := strconv.Atoi(params[1])
op := []rune(params[2])[0]
right, _ := strconv.Atoi(params[3])

return operation{
left: left,
symbol: op,
right: right,
}
}

steps["When (.+) is entered the answer is `(\\d+)`"] =
func(t *testing.T, op operation, want int) {
calc := sut.Calculator{}
var got int
switch op.symbol {
case '+':
got = calc.Add(op.left, op.right)
case '-':
got = calc.Sub(op.left, op.right)
}
if got != want {
t.Errorf("got %d, want %d", got, want)
}
}

}
15 changes: 15 additions & 0 deletions example/calculator/calc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package calculator_test

import (
"testing"

"github.com/mpwalkerdine/elicit"
)

func Test(t *testing.T) {
elicit.New().
WithSpecsFolder("./specs").
WithTransforms(transforms).
WithSteps(steps).
RunTests(t)
}
3 changes: 3 additions & 0 deletions example/calculator/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/mpwalkerdine/elicit/example/calculator

require github.com/mpwalkerdine/elicit v0.0.0-20180815004046-a55089967cf7
4 changes: 4 additions & 0 deletions example/calculator/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/mpwalkerdine/elicit v0.0.0-20180815004046-a55089967cf7 h1:uGJI0LQoXQz/7+bkNV8ZsycZMmbwJlq+rrk3RfiVxFg=
github.com/mpwalkerdine/elicit v0.0.0-20180815004046-a55089967cf7/go.mod h1:C7FxQJFxxXx/O7QDHcCMFjYGaEPewwzg7Kzqt6IAyvw=
github.com/russross/blackfriday v1.5.1 h1:B8ZN6pD4PVofmlDCDUdELeYrbsVIDM/bpjW3v3zgcRc=
github.com/russross/blackfriday v1.5.1/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
15 changes: 15 additions & 0 deletions example/calculator/specs/arithmetic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Arithmetic

Calculators must perform basic arithmetic.

## Addition

Addition is `a + b = c`, for example:

+ When `2 + 2` is entered the answer is `4`

## Subtraction

Subtraction is `a - b = c`, for example:

+ When `4 - 1` is entered the answer is `3`

0 comments on commit 63e20c4

Please sign in to comment.