Skip to content

Update of testcase generator to latest JSON format #2279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
0b3bed2
DRAFT: test implementation
eklatzer Jul 2, 2022
30ce019
draft implementation
eklatzer Jul 3, 2022
f7d8ce0
removed versioning
eklatzer Jul 3, 2022
9081bf7
first draft
eklatzer Jul 3, 2022
ec22bc8
added code from PR #2276 to exclude tests with include=false in tests…
eklatzer Jul 3, 2022
7d515db
removed property.go as this seems to be unneeded with the new structure
eklatzer Jul 3, 2022
03dfd91
updated gen.go to new structure for testing the new structure
eklatzer Jul 3, 2022
2c0821c
removed duplicate print
eklatzer Jul 3, 2022
8e73f2d
updated generator of forth to new structure
eklatzer Jul 3, 2022
5fd0148
removed classifyByProperty
eklatzer Jul 3, 2022
7296fac
Merge branch 'main' into test-generator
eklatzer Jul 4, 2022
bcca49b
adjusted tests to new structure
eklatzer Jul 4, 2022
5c5302a
added additional test-cases
eklatzer Jul 4, 2022
eeae582
adjusted comments
eklatzer Jul 4, 2022
bc7783c
refactoring of test case generator
eklatzer Jul 4, 2022
685364d
added comment
eklatzer Jul 8, 2022
116479f
added test-case generator and usage of new test-cases
eklatzer Jul 8, 2022
041fc35
fixed formatting of files and fixed formatting of generated cases for…
eklatzer Jul 10, 2022
21e251a
updated templates to contain field names and regenerated test-cases
eklatzer Jul 10, 2022
8e939ac
fixed template for diamond to see structure of output in tests
eklatzer Jul 10, 2022
a6980e3
removed reset to analyze formatting error in output file
eklatzer Jul 10, 2022
10ac4ea
added space
eklatzer Jul 10, 2022
0f7b4c4
replaced isJSON by json.Valid
eklatzer Jul 10, 2022
0ba5fe3
added space
eklatzer Jul 10, 2022
d125634
fixed order of imports
eklatzer Jul 10, 2022
b9a734b
fixed validation of json
eklatzer Jul 10, 2022
834bf9d
adjusted example solution to meet requirements of problem-specifications
eklatzer Jul 11, 2022
750e816
added eklatzter to contributors
eklatzer Jul 11, 2022
455e1f0
improved error wrapping
eklatzer Jul 12, 2022
ff23161
Improved comment of getAllTestCasesFiltered
eklatzer Jul 12, 2022
98b03f6
fixed comment
eklatzer Jul 12, 2022
c198750
Update gen/gen.go
eklatzer Jul 12, 2022
da22d18
fixed comment
eklatzer Jul 12, 2022
86756de
renamed variable and added comment
eklatzer Jul 12, 2022
1972efb
removed fields that are not needed
eklatzer Jul 12, 2022
ac695ea
removed line
eklatzer Jul 12, 2022
fca1787
fixed import order
eklatzer Jul 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exercises/practice/bowling/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"ferhatelmas",
"hilary",
"robphoenix",
"sebito91"
"sebito91",
"eklatzer"
],
"files": {
"solution": [
Expand Down
117 changes: 49 additions & 68 deletions exercises/practice/bowling/.meta/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,70 +12,50 @@ func main() {
if err != nil {
log.Fatal(err)
}
var j js
if err := gen.Gen("bowling", &j, t); err != nil {
var j = map[string]interface{}{
"roll": &[]Case{},
"score": &[]Case{},
}
if err := gen.Gen("bowling", j, t); err != nil {
log.Fatal(err)
}
}

// The JSON structure we expect to be able to unmarshal into
type js struct {
Exercise string
Version string
Comments []string
Cases []OneCase
}

// template applied to above data structure generates the Go test cases

type OneCase struct {
Description string
Property string
type Case struct {
Description string `json:"description"`
Input struct {
PreviousRolls []int
Roll int
}
Expected interface{}
}

// ScoreTest and RollTest help determine which type of test case
// to generate in the template.
func (c OneCase) ScoreTest() bool { return c.Property == "score" }
func (c OneCase) RollTest() bool { return c.Property == "roll" }

func (c OneCase) Valid() bool {
valid, _, _ := determineExpected(c.Expected)
return valid
PreviousRolls []int `json:"previousRolls"`
Roll int `json:"roll"`
} `json:"input"`
Expected interface{} `json:"expected"`
}

func (c OneCase) Score() int {
_, score, _ := determineExpected(c.Expected)
return score
func (t Case) Score() int {
score, ok := t.Expected.(float64)
if !ok {
return 0
}
return int(score)
}

func (c OneCase) ExplainText() string {
_, _, explainText := determineExpected(c.Expected)
return explainText
func (t Case) Valid() bool {
_, ok := t.Expected.(float64)
return ok
}

// determineExpected examines an .Expected interface{} object and determines
// whether a test case is valid(bool), has a score field, and/or has an expected error,
// returning valid, score, and error explanation text.
func determineExpected(expected interface{}) (bool, int, string) {
score, ok := expected.(float64)
if ok {
return ok, int(score), ""
}
m, ok := expected.(map[string]interface{})
if !ok {
return false, 0, ""
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
}
iError, ok := m["error"].(interface{})
if !ok {
return false, 0, ""
}
explainText, ok := iError.(string)
return false, 0, explainText
return ""
}

// Template to generate two sets of test cases, one for Score tests and one for Roll tests.
Expand All @@ -89,29 +69,30 @@ var scoreTestCases = []struct {
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.Cases}}
{{if .ScoreTest}}{
{{printf "%q" .Description}},
{{printf "%#v" .Input.PreviousRolls}},
{{printf "%v" .Valid}},
{{printf "%d" .Score}},
{{printf "%q" .ExplainText}},
},{{- end}}{{end}}
}{ {{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.Cases}}
{{if .RollTest}}{
{{printf "%q" .Description}},
{{printf "%#v" .Input.PreviousRolls}},
{{printf "%v" .Valid}},
{{printf "%d" .Input.Roll}},
{{printf "%q" .ExplainText}},
},{{- end}}{{end}}
}{ {{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}}
}
`
Loading