-
-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathgen.go
More file actions
88 lines (76 loc) · 2.08 KB
/
gen.go
File metadata and controls
88 lines (76 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"../../../../gen"
"log"
"text/template"
)
func main() {
t, err := template.New("").Parse(tmpl)
if err != nil {
log.Fatal(err)
}
j := map[string]any{
"roll": &[]Case{},
"score": &[]Case{},
}
if err := gen.Gen("bowling", j, t); err != nil {
log.Fatal(err)
}
}
type Case struct {
Description string `json:"description"`
Input struct {
PreviousRolls []int `json:"previousRolls"`
Roll int `json:"roll"`
} `json:"input"`
Expected any `json:"expected"`
}
func (t Case) Score() int {
score, ok := t.Expected.(float64)
if !ok {
return 0
}
return int(score)
}
func (t Case) Valid() bool {
_, ok := t.Expected.(float64)
return ok
}
func (t Case) ExplainText() string {
return gen.ErrorMessage(t.Expected)
}
// Template to generate two sets of test cases, one for Score tests and one for Roll tests.
var tmpl = `{{.Header}}
type testCaseScore struct {
description string
previousRolls []int // bowling rolls to do before the Score() test
valid bool // true => no error, false => error expected
score int // when .valid == true, the expected score value
explainText string // when .valid == false, error explanation text
}
var scoreTestCases = []testCaseScore { {{range .J.score}}
{
description: {{printf "%q" .Description}},
previousRolls: {{printf "%#v" .Input.PreviousRolls}},
valid: {{printf "%v" .Valid}},
score: {{printf "%d" .Score}},
explainText: {{printf "%q" .ExplainText}},
},{{end}}
}
type testCaseRoll struct {
description string
previousRolls []int // bowling rolls to do before the Roll(roll) test
valid bool // true => no error, false => error expected
roll int // pin count for the test roll
explainText string // when .valid == false, error explanation text
}
var rollTestCases = []testCaseRoll { {{range .J.roll}}
{
description: {{printf "%q" .Description}},
previousRolls: {{printf "%#v" .Input.PreviousRolls}},
valid: {{printf "%v" .Valid}},
roll: {{printf "%d" .Input.Roll}},
explainText: {{printf "%q" .ExplainText}},
},{{end}}
}
`