-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a550899
commit 63e20c4
Showing
6 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |