Skip to content

scrabble-score: create test case generator #665

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 1 commit into from
May 5, 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
45 changes: 45 additions & 0 deletions exercises/scrabble-score/.meta/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// +build ignore

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("scrabble-score", &j, t); err != nil {
log.Fatal(err)
}
}

// The JSON structure we expect to be able to unmarshal into
type js struct {
Cases []struct {
Description string
Input string
Expected int
}
}

// template applied to above data structure generates the Go test cases
var tmpl = `package scrabble

{{.Header}}

type scrabbleTest struct {
input string
expected int
}

var scrabbleScoreTests = []scrabbleTest {
{{range .J.Cases}}{ "{{.Input}}", {{.Expected}}}, // {{.Description}}
{{end}}}
`
24 changes: 24 additions & 0 deletions exercises/scrabble-score/cases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scrabble

// Source: exercism/x-common
// Commit: 11ed503 scrabble-score: Make canonical-data.json compliant
// x-common version: 1.0.0

type scrabbleTest struct {
input string
expected int
}

var scrabbleScoreTests = []scrabbleTest{
{"a", 1}, // lowercase letter
{"A", 1}, // uppercase letter
{"f", 4}, // valuable letter
{"at", 2}, // short word
{"zoo", 12}, // short, valuable word
{"street", 6}, // medium word
{"quirky", 22}, // medium, valuable word
{"OxyphenButazone", 41}, // long, mixed-case word
{"pinata", 8}, // english-like word
{"", 0}, // empty input
{"abcdefghijklmnopqrstuvwxyz", 87}, // entire alphabet available
}
2 changes: 1 addition & 1 deletion exercises/scrabble-score/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// testVersion tracks the version of the exercise.
const testVersion = 4
const testVersion = 5

var letterValues = map[rune]int{
'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1,
Expand Down
20 changes: 3 additions & 17 deletions exercises/scrabble-score/scrabble_score_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@ package scrabble

import "testing"

const targetTestVersion = 4

var tests = []struct {
input string
expected int
}{
{"", 0},
{" \t\n", 0},
{"a", 1},
{"f", 4},
{"street", 6},
{"quirky", 22},
{"OXYPHENBUTAZONE", 41},
{"alacrity", 13},
}
const targetTestVersion = 5

func TestTestVersion(t *testing.T) {
if testVersion != targetTestVersion {
Expand All @@ -25,7 +11,7 @@ func TestTestVersion(t *testing.T) {
}

func TestScore(t *testing.T) {
for _, test := range tests {
for _, test := range scrabbleScoreTests {
if actual := Score(test.input); actual != test.expected {
t.Errorf("Score(%q) expected %d, Actual %d", test.input, test.expected, actual)
}
Expand All @@ -34,7 +20,7 @@ func TestScore(t *testing.T) {

func BenchmarkScore(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range tests {
for _, test := range scrabbleScoreTests {
Score(test.input)
}
}
Expand Down