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.
- Loading branch information
Showing
3 changed files
with
106 additions
and
19 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,54 @@ | ||
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("prime-factors", &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 []OneCase | ||
} | ||
|
||
type OneCase struct { | ||
Description string | ||
Input int64 | ||
Expected []int64 | ||
} | ||
|
||
// template applied to above data structure generates the Go test cases | ||
var tmpl = `package prime | ||
{{.Header}} | ||
var tests = []struct { | ||
description string | ||
input int64 | ||
expected []int64 | ||
}{ | ||
{{range .J.Groups}} | ||
{{range .Cases}} { | ||
"{{.Description}}", | ||
{{.Input}}, | ||
{{printf "%#v" .Expected}}, | ||
}, | ||
{{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,48 @@ | ||
package prime | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: bf5bb2b prime-factors: Fix canonical-data.json formatting | ||
// Problem Specifications Version: 1.0.0 | ||
|
||
var tests = []struct { | ||
description string | ||
input int64 | ||
expected []int64 | ||
}{ | ||
|
||
{ | ||
"no factors", | ||
1, | ||
[]int64{}, | ||
}, | ||
{ | ||
"prime number", | ||
2, | ||
[]int64{2}, | ||
}, | ||
{ | ||
"square of a prime", | ||
9, | ||
[]int64{3, 3}, | ||
}, | ||
{ | ||
"cube of a prime", | ||
8, | ||
[]int64{2, 2, 2}, | ||
}, | ||
{ | ||
"product of primes and non-primes", | ||
12, | ||
[]int64{2, 2, 3}, | ||
}, | ||
{ | ||
"product of primes", | ||
901255, | ||
[]int64{5, 17, 23, 461}, | ||
}, | ||
{ | ||
"factors include a large prime", | ||
93819012551, | ||
[]int64{11, 9539, 894119}, | ||
}, | ||
} |
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