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.
book-store: create test case generator
Add .meta/gen.go to generate cases_test.go. Update test program to use generated test case array. For exercism#605.
- Loading branch information
Showing
3 changed files
with
131 additions
and
67 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("book-store", &j, t); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// The JSON structure we expect to be able to unmarshal into | ||
type js struct { | ||
Cases []OneCase | ||
Groups []TestGroup `json:"cases"` | ||
} | ||
|
||
type TestGroup struct { | ||
Description string | ||
Cases []OneCase | ||
} | ||
|
||
type OneCase struct { | ||
Description string | ||
Basket []int | ||
Expected float64 | ||
} | ||
|
||
// template applied to above data structure generates the Go test cases | ||
var tmpl = `package bookstore | ||
{{.Header}} | ||
var testCases = []struct { | ||
description string | ||
basket []int | ||
expected float64 | ||
}{{range .J.Groups}}{ | ||
{{range .Cases}} { | ||
description: "{{.Description}}", | ||
basket: {{printf "%#v" .Basket}}, | ||
expected: {{printf "%.2f" .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
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,77 @@ | ||
package bookstore | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: 087ad69 book-store: Add missing trailing zero | ||
// Problem Specifications Version: 1.0.1 | ||
|
||
var testCases = []struct { | ||
description string | ||
basket []int | ||
expected float64 | ||
}{ | ||
{ | ||
description: "Only a single book", | ||
basket: []int{1}, | ||
expected: 8.00, | ||
}, | ||
{ | ||
description: "Two of the same book", | ||
basket: []int{2, 2}, | ||
expected: 16.00, | ||
}, | ||
{ | ||
description: "Empty basket", | ||
basket: []int{}, | ||
expected: 0.00, | ||
}, | ||
{ | ||
description: "Two different books", | ||
basket: []int{1, 2}, | ||
expected: 15.20, | ||
}, | ||
{ | ||
description: "Three different books", | ||
basket: []int{1, 2, 3}, | ||
expected: 21.60, | ||
}, | ||
{ | ||
description: "Four different books", | ||
basket: []int{1, 2, 3, 4}, | ||
expected: 25.60, | ||
}, | ||
{ | ||
description: "Five different books", | ||
basket: []int{1, 2, 3, 4, 5}, | ||
expected: 30.00, | ||
}, | ||
{ | ||
description: "Two groups of four is cheaper than group of five plus group of three", | ||
basket: []int{1, 1, 2, 2, 3, 3, 4, 5}, | ||
expected: 51.20, | ||
}, | ||
{ | ||
description: "Group of four plus group of two is cheaper than two groups of three", | ||
basket: []int{1, 1, 2, 2, 3, 4}, | ||
expected: 40.80, | ||
}, | ||
{ | ||
description: "Two each of first 4 books and 1 copy each of rest", | ||
basket: []int{1, 1, 2, 2, 3, 3, 4, 4, 5}, | ||
expected: 55.60, | ||
}, | ||
{ | ||
description: "Two copies of each book", | ||
basket: []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5}, | ||
expected: 60.00, | ||
}, | ||
{ | ||
description: "Three copies of first book and 2 each of remaining", | ||
basket: []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1}, | ||
expected: 68.00, | ||
}, | ||
{ | ||
description: "Three each of first 2 books and 2 each of remaining books", | ||
basket: []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2}, | ||
expected: 75.20, | ||
}, | ||
} |