Skip to content

Commit

Permalink
book-store: create test case generator
Browse files Browse the repository at this point in the history
Add .meta/gen.go to generate cases_test.go.

Update test program to use generated test case array.

For exercism#605.
  • Loading branch information
leenipper committed Nov 12, 2017
1 parent 6bf7038 commit aa34a9b
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 67 deletions.
54 changes: 54 additions & 0 deletions exercises/book-store/.meta/gen.go
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}}
`
67 changes: 0 additions & 67 deletions exercises/book-store/book_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,6 @@ import (
"testing"
)

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: "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: "groups of four plus group of two is 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 of 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 fo 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,
},
}

func TestCost(t *testing.T) {
for _, testCase := range testCases {
cost := Cost(testCase.basket)
Expand Down
77 changes: 77 additions & 0 deletions exercises/book-store/cases_test.go
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,
},
}

0 comments on commit aa34a9b

Please sign in to comment.