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. Update test program to use generated test case array. Output FAIL and PASS in test output. For exercism#605.
Showing
3 changed files
with
123 additions
and
18 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("nth-prime", &j, t); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
type OneCase struct { | ||
Description string | ||
Input int | ||
Expected interface{} | ||
} | ||
|
||
// The JSON structure we expect to be able to unmarshal into | ||
type js struct { | ||
Cases []OneCase | ||
} | ||
|
||
func (c OneCase) HasPrimeAnswer() bool { | ||
hasPrimeAnswer, _ := determineExpected(c.Expected) | ||
return hasPrimeAnswer | ||
} | ||
|
||
func (c OneCase) PrimeAnswer() int { | ||
_, answer := determineExpected(c.Expected) | ||
return answer | ||
} | ||
|
||
// determineExpected examines an .Expected interface{} object and determines | ||
// whether a test case has a Prime answer or expects an error, | ||
// returning true and the answer, or false and zero. | ||
func determineExpected(expected interface{}) (bool, int) { | ||
value, ok := expected.(float64) | ||
if ok { | ||
return true, int(value) | ||
} | ||
return false, 0 | ||
} | ||
|
||
// template applied to above data structure generates the Go test cases | ||
var tmpl = `package prime | ||
{{.Header}} | ||
var tests = []struct { | ||
description string | ||
n int | ||
p int | ||
ok bool | ||
}{ | ||
{{range .J.Cases}}{ | ||
"{{.Description}}", | ||
{{.Input}}, | ||
{{- if .HasPrimeAnswer}} | ||
{{.PrimeAnswer}}, | ||
true, | ||
{{- else}} | ||
0, | ||
false, | ||
{{- 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,43 @@ | ||
package prime | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: e82cbcd nth-prime: Use object instead of bool for invalid value (#969) | ||
// Problem Specifications Version: 2.0.0 | ||
|
||
var tests = []struct { | ||
description string | ||
n int | ||
p int | ||
ok bool | ||
}{ | ||
{ | ||
"first prime", | ||
1, | ||
2, | ||
true, | ||
}, | ||
{ | ||
"second prime", | ||
2, | ||
3, | ||
true, | ||
}, | ||
{ | ||
"sixth prime", | ||
6, | ||
13, | ||
true, | ||
}, | ||
{ | ||
"big prime", | ||
10001, | ||
104743, | ||
true, | ||
}, | ||
{ | ||
"there is no zeroth prime", | ||
0, | ||
0, | ||
false, | ||
}, | ||
} |
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