-
-
Notifications
You must be signed in to change notification settings - Fork 650
/
gen.go
98 lines (86 loc) · 2.17 KB
/
gen.go
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
89
90
91
92
93
94
95
96
97
98
package main
import (
"log"
"text/template"
"../../../../gen"
)
func main() {
t, err := template.New("").Parse(tmpl)
if err != nil {
log.Fatal(err)
}
j := map[string]interface{}{
"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 interface{} `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 {
if !t.Valid() {
m, ok := t.Expected.(map[string]interface{})
if !ok {
return ""
}
b, ok := m["error"].(string)
if !ok {
return ""
}
return b
}
return ""
}
// Template to generate two sets of test cases, one for Score tests and one for Roll tests.
var tmpl = `package bowling
{{.Header}}
var scoreTestCases = []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
}{ {{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}}
}
var rollTestCases = []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
}{ {{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}}
}
`