-
-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wordy: create test case generator (#949)
Add .meta/gen.go to generate cases_test.go. Update test program to use generated test case array. Put FAIL and PASS in test result output. For #605.
- Loading branch information
Showing
3 changed files
with
194 additions
and
29 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,76 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"text/template" | ||
|
||
"../../../gen" | ||
) | ||
|
||
func main() { | ||
t, err := template.New("").Parse(tmpl) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var j js | ||
if err := gen.Gen("wordy", &j, t); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
type OneCase struct { | ||
Description string | ||
Input string | ||
Expected interface{} | ||
} | ||
|
||
// The JSON structure we expect to be able to unmarshal into | ||
type js struct { | ||
Cases []OneCase | ||
} | ||
|
||
func (c OneCase) Valid() bool { | ||
valid, _ := determineExpected(c.Expected) | ||
return valid | ||
} | ||
|
||
func (c OneCase) Answer() int { | ||
_, answer := determineExpected(c.Expected) | ||
return answer | ||
} | ||
|
||
// determineExpected examines an .Expected interface{} object and determines | ||
// whether a test case is valid(bool) and has an answer or expects an error. | ||
// returning valid and answer. | ||
func determineExpected(expected interface{}) (bool, int) { | ||
ans, ok := expected.(float64) | ||
if ok { | ||
return ok, int(ans) | ||
} | ||
return false, 0 | ||
} | ||
|
||
// template applied to above data structure generates the Go test cases | ||
var tmpl = `package wordy | ||
{{.Header}} | ||
type wordyTest struct { | ||
description string | ||
question string | ||
ok bool | ||
answer int | ||
} | ||
var tests = []wordyTest { | ||
{{range .J.Cases}}{ | ||
"{{.Description}}", | ||
"{{.Input}}", | ||
{{if .Valid}} true, | ||
{{.Answer}}, | ||
{{- else}} false, | ||
0, | ||
{{- end}} | ||
}, | ||
{{end}}} | ||
` |
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,111 @@ | ||
package wordy | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: 5b8ad58 wordy: Fix canonical-data.json formatting | ||
// Problem Specifications Version: 1.0.0 | ||
|
||
type wordyTest struct { | ||
description string | ||
question string | ||
ok bool | ||
answer int | ||
} | ||
|
||
var tests = []wordyTest{ | ||
{ | ||
"addition", | ||
"What is 1 plus 1?", | ||
true, | ||
2, | ||
}, | ||
{ | ||
"more addition", | ||
"What is 53 plus 2?", | ||
true, | ||
55, | ||
}, | ||
{ | ||
"addition with negative numbers", | ||
"What is -1 plus -10?", | ||
true, | ||
-11, | ||
}, | ||
{ | ||
"large addition", | ||
"What is 123 plus 45678?", | ||
true, | ||
45801, | ||
}, | ||
{ | ||
"subtraction", | ||
"What is 4 minus -12?", | ||
true, | ||
16, | ||
}, | ||
{ | ||
"multiplication", | ||
"What is -3 multiplied by 25?", | ||
true, | ||
-75, | ||
}, | ||
{ | ||
"division", | ||
"What is 33 divided by -3?", | ||
true, | ||
-11, | ||
}, | ||
{ | ||
"multiple additions", | ||
"What is 1 plus 1 plus 1?", | ||
true, | ||
3, | ||
}, | ||
{ | ||
"addition and subtraction", | ||
"What is 1 plus 5 minus -2?", | ||
true, | ||
8, | ||
}, | ||
{ | ||
"multiple subtraction", | ||
"What is 20 minus 4 minus 13?", | ||
true, | ||
3, | ||
}, | ||
{ | ||
"subtraction then addition", | ||
"What is 17 minus 6 plus 3?", | ||
true, | ||
14, | ||
}, | ||
{ | ||
"multiple multiplication", | ||
"What is 2 multiplied by -2 multiplied by 3?", | ||
true, | ||
-12, | ||
}, | ||
{ | ||
"addition and multiplication", | ||
"What is -3 plus 7 multiplied by -2?", | ||
true, | ||
-8, | ||
}, | ||
{ | ||
"multiple division", | ||
"What is -12 divided by 2 divided by -3?", | ||
true, | ||
2, | ||
}, | ||
{ | ||
"unknown operation", | ||
"What is 52 cubed?", | ||
false, | ||
0, | ||
}, | ||
{ | ||
"Non math question", | ||
"Who is the President of the United States?", | ||
false, | ||
0, | ||
}, | ||
} |
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