Skip to content
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

book-store: create test case generator #954

Merged
merged 1 commit into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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,
},
}