diff --git a/example/calculator/calc.go b/example/calculator/calc.go new file mode 100644 index 0000000..1d63e0f --- /dev/null +++ b/example/calculator/calc.go @@ -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 } diff --git a/example/calculator/calc_steps_test.go b/example/calculator/calc_steps_test.go new file mode 100644 index 0000000..bbd36c7 --- /dev/null +++ b/example/calculator/calc_steps_test.go @@ -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) + } + } + +} diff --git a/example/calculator/calc_test.go b/example/calculator/calc_test.go new file mode 100644 index 0000000..46ae3b3 --- /dev/null +++ b/example/calculator/calc_test.go @@ -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) +} diff --git a/example/calculator/go.mod b/example/calculator/go.mod new file mode 100644 index 0000000..cdc2972 --- /dev/null +++ b/example/calculator/go.mod @@ -0,0 +1,3 @@ +module github.com/mpwalkerdine/elicit/example/calculator + +require github.com/mpwalkerdine/elicit v0.0.0-20180815004046-a55089967cf7 diff --git a/example/calculator/go.sum b/example/calculator/go.sum new file mode 100644 index 0000000..0d6d29b --- /dev/null +++ b/example/calculator/go.sum @@ -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= diff --git a/example/calculator/specs/arithmetic.md b/example/calculator/specs/arithmetic.md new file mode 100644 index 0000000..6ed9974 --- /dev/null +++ b/example/calculator/specs/arithmetic.md @@ -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` \ No newline at end of file