forked from exercism/go
-
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.
Add .meta/gen.go to generate cases_test.go. Make use of zero-value of expectError bool. Update test program to use generated test case array. The test case value for the 'Total()' is retained in TestTotal() since there wasn't an array of cases in the canonical-data.json for the 'total' property, and there is only one valid return value. For exercism#605.
- Loading branch information
Showing
3 changed files
with
171 additions
and
20 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,103 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"strings" | ||
"text/template" | ||
|
||
"../../../gen" | ||
) | ||
|
||
func main() { | ||
t, err := template.New("").Parse(tmpl) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var j js | ||
if err := gen.Gen("grains", &j, t); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// The JSON structure we expect to be able to unmarshal into | ||
type js struct { | ||
Groups []testGroup `json:"Cases"` | ||
} | ||
|
||
type testGroup struct { | ||
Description string | ||
Cases []json.RawMessage `property:"RAW"` | ||
SquareCases []SquareCase `property:"square"` | ||
// Note: canonical-data.json has a element of "cases" | ||
// which includes a test for property 'total', but it is ignored here, | ||
// since "expected" is a single known value. | ||
} | ||
|
||
type SquareCase struct { | ||
Description string | ||
Input int | ||
Expected interface{} | ||
} | ||
|
||
func (c SquareCase) Valid() bool { | ||
valid, _ := determineExpected(c.Expected) | ||
return valid | ||
} | ||
|
||
func (c SquareCase) Answer() uint64 { | ||
_, answer := determineExpected(c.Expected) | ||
return answer | ||
} | ||
|
||
func (c SquareCase) EditedDescription() string { | ||
// Go doesn't raise exceptions, so replace the wording in .Description. | ||
return strings.Replace(c.Description, "raises an exception", "returns an error", 1) | ||
} | ||
|
||
// 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, uint64) { | ||
ans, ok := expected.(float64) | ||
if ok { | ||
if ans == float64(-1) { | ||
return false, 0 | ||
} | ||
return ok, uint64(ans) | ||
} | ||
return false, 0 | ||
} | ||
|
||
func (groups testGroup) GroupShortName() string { | ||
return strings.ToLower(strings.Fields(groups.Description)[0]) | ||
} | ||
|
||
var tmpl = `package grains | ||
{{.Header}} | ||
{{range .J.Groups}} | ||
{{- if .SquareCases }} | ||
// {{ .Description }} | ||
var squareTests = []struct { | ||
description string | ||
input int | ||
expectedVal uint64 | ||
expectError bool | ||
}{ | ||
{{- range .SquareCases}} | ||
{ | ||
description: "{{.EditedDescription}}", | ||
input: {{.Input}}, | ||
{{- if .Valid}} | ||
expectedVal: {{.Answer}}, | ||
{{- else}} | ||
expectError: true, | ||
{{- end}} | ||
}, | ||
{{- end }} | ||
} | ||
{{- 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,64 @@ | ||
package grains | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: d4554e6 grains: fix a typo in comments of canonical data | ||
// Problem Specifications Version: 1.0.0 | ||
|
||
// returns the number of grains on the square | ||
var squareTests = []struct { | ||
description string | ||
input int | ||
expectedVal uint64 | ||
expectError bool | ||
}{ | ||
{ | ||
description: "1", | ||
input: 1, | ||
expectedVal: 1, | ||
}, | ||
{ | ||
description: "2", | ||
input: 2, | ||
expectedVal: 2, | ||
}, | ||
{ | ||
description: "3", | ||
input: 3, | ||
expectedVal: 4, | ||
}, | ||
{ | ||
description: "4", | ||
input: 4, | ||
expectedVal: 8, | ||
}, | ||
{ | ||
description: "16", | ||
input: 16, | ||
expectedVal: 32768, | ||
}, | ||
{ | ||
description: "32", | ||
input: 32, | ||
expectedVal: 2147483648, | ||
}, | ||
{ | ||
description: "64", | ||
input: 64, | ||
expectedVal: 9223372036854775808, | ||
}, | ||
{ | ||
description: "square 0 returns an error", | ||
input: 0, | ||
expectError: true, | ||
}, | ||
{ | ||
description: "negative square returns an error", | ||
input: -1, | ||
expectError: true, | ||
}, | ||
{ | ||
description: "square greater than 64 returns an error", | ||
input: 65, | ||
expectError: true, | ||
}, | ||
} |
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