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.
word-search: create test case generator (exercism#871)
Add .meta/gen.go to generate cases_test.go. Update test program to use generated test case array. Put FAIL and PASS in test result output. For exercism#605.
- Loading branch information
1 parent
81461ce
commit 7bb5933
Showing
3 changed files
with
266 additions
and
41 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,96 @@ | ||
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("word-search", &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 Int int | ||
|
||
type Coordinates struct { | ||
Column Int | ||
Row Int | ||
} | ||
type Position struct { | ||
Start Coordinates | ||
End Coordinates | ||
} | ||
|
||
type OneCase struct { | ||
Description string | ||
Property string | ||
Grid []string | ||
WordsToSearchFor []string | ||
Expected map[string]Position | ||
} | ||
|
||
func (p Position) NullPosition() bool { | ||
if p.Start.Column == 0 && p.Start.Row == 0 && | ||
p.End.Column == 0 && p.End.Row == 0 { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (c OneCase) ErrorExpected() bool { | ||
// When any of the word positions have an null position, expect an error. | ||
for _, p := range c.Expected { | ||
if p.NullPosition() { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func (v Int) Minus1() int { | ||
return int(v) - 1 | ||
} | ||
|
||
// Template to generate test cases | ||
|
||
var tmpl = `package wordsearch | ||
{{.Header}} | ||
var testCases = []struct { | ||
description string | ||
puzzle []string // puzzle strings | ||
words []string // words to search for | ||
expected map[string][2][2]int // expected coordinates | ||
expectError bool | ||
}{ {{range .J.Cases}} | ||
{ | ||
{{printf "%q" .Description}}, | ||
{{printf "%#v" .Grid}}, | ||
{{printf "%#v" .WordsToSearchFor}}, | ||
{{if .ErrorExpected}} map[string][2][2]int{ }, | ||
true, | ||
{{else}} map[string][2][2]int{ {{ range $key, $value := .Expected }} "{{ $key }}": { { {{ $value.Start.Column.Minus1 }}, {{ $value.Start.Row.Minus1 }}, }, { {{ $value.End.Column.Minus1 }}, {{ $value.End.Row.Minus1 }} } }, {{ end }} }, | ||
false, | ||
{{- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package wordsearch | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: c741e35 Add tests to support TDD workflow (#899) | ||
// Problem Specifications Version: 1.1.0 | ||
|
||
var testCases = []struct { | ||
description string | ||
puzzle []string // puzzle strings | ||
words []string // words to search for | ||
expected map[string][2][2]int // expected coordinates | ||
expectError bool | ||
}{ | ||
{ | ||
"Should accept an initial game grid and a target search word", | ||
[]string{"jefblpepre"}, | ||
[]string{"clojure"}, | ||
map[string][2][2]int{}, | ||
true, | ||
}, | ||
{ | ||
"Should locate one word written left to right", | ||
[]string{"clojurermt"}, | ||
[]string{"clojure"}, | ||
map[string][2][2]int{"clojure": {{0, 0}, {6, 0}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate the same word written left to right in a different position", | ||
[]string{"mtclojurer"}, | ||
[]string{"clojure"}, | ||
map[string][2][2]int{"clojure": {{2, 0}, {8, 0}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate a different left to right word", | ||
[]string{"coffeelplx"}, | ||
[]string{"coffee"}, | ||
map[string][2][2]int{"coffee": {{0, 0}, {5, 0}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate that different left to right word in a different position", | ||
[]string{"xcoffeezlp"}, | ||
[]string{"coffee"}, | ||
map[string][2][2]int{"coffee": {{1, 0}, {6, 0}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate a left to right word in two line grid", | ||
[]string{"jefblpepre", "tclojurerm"}, | ||
[]string{"clojure"}, | ||
map[string][2][2]int{"clojure": {{1, 1}, {7, 1}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate a left to right word in three line grid", | ||
[]string{"camdcimgtc", "jefblpepre", "clojurermt"}, | ||
[]string{"clojure"}, | ||
map[string][2][2]int{"clojure": {{0, 2}, {6, 2}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate a left to right word in ten line grid", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"clojure"}, | ||
map[string][2][2]int{"clojure": {{0, 9}, {6, 9}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate that left to right word in a different position in a ten line grid", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "clojurermt", "jalaycalmp"}, | ||
[]string{"clojure"}, | ||
map[string][2][2]int{"clojure": {{0, 8}, {6, 8}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate a different left to right word in a ten line grid", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "fortranftw", "alxhpburyi", "clojurermt", "jalaycalmp"}, | ||
[]string{"fortran"}, | ||
map[string][2][2]int{"fortran": {{0, 6}, {6, 6}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate multiple words", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "fortranftw", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"fortran", "clojure"}, | ||
map[string][2][2]int{"clojure": {{0, 9}, {6, 9}}, "fortran": {{0, 6}, {6, 6}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate a single word written right to left", | ||
[]string{"rixilelhrs"}, | ||
[]string{"elixir"}, | ||
map[string][2][2]int{"elixir": {{5, 0}, {0, 0}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate multiple words written in different horizontal directions", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"elixir", "clojure"}, | ||
map[string][2][2]int{"clojure": {{0, 9}, {6, 9}}, "elixir": {{5, 4}, {0, 4}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate words written top to bottom", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"clojure", "elixir", "ecmascript"}, | ||
map[string][2][2]int{"clojure": {{0, 9}, {6, 9}}, "ecmascript": {{9, 0}, {9, 9}}, "elixir": {{5, 4}, {0, 4}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate words written bottom to top", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"clojure", "elixir", "ecmascript", "rust"}, | ||
map[string][2][2]int{"clojure": {{0, 9}, {6, 9}}, "ecmascript": {{9, 0}, {9, 9}}, "elixir": {{5, 4}, {0, 4}}, "rust": {{8, 4}, {8, 1}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate words written top left to bottom right", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"clojure", "elixir", "ecmascript", "rust", "java"}, | ||
map[string][2][2]int{"clojure": {{0, 9}, {6, 9}}, "ecmascript": {{9, 0}, {9, 9}}, "elixir": {{5, 4}, {0, 4}}, "java": {{0, 0}, {3, 3}}, "rust": {{8, 4}, {8, 1}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate words written bottom right to top left", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"clojure", "elixir", "ecmascript", "rust", "java", "lua"}, | ||
map[string][2][2]int{"clojure": {{0, 9}, {6, 9}}, "ecmascript": {{9, 0}, {9, 9}}, "elixir": {{5, 4}, {0, 4}}, "java": {{0, 0}, {3, 3}}, "lua": {{7, 8}, {5, 6}}, "rust": {{8, 4}, {8, 1}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate words written bottom left to top right", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"clojure", "elixir", "ecmascript", "rust", "java", "lua", "lisp"}, | ||
map[string][2][2]int{"clojure": {{0, 9}, {6, 9}}, "ecmascript": {{9, 0}, {9, 9}}, "elixir": {{5, 4}, {0, 4}}, "java": {{0, 0}, {3, 3}}, "lisp": {{2, 5}, {5, 2}}, "lua": {{7, 8}, {5, 6}}, "rust": {{8, 4}, {8, 1}}}, | ||
false, | ||
}, | ||
{ | ||
"Should locate words written top right to bottom left", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"clojure", "elixir", "ecmascript", "rust", "java", "lua", "lisp", "ruby"}, | ||
map[string][2][2]int{"clojure": {{0, 9}, {6, 9}}, "ecmascript": {{9, 0}, {9, 9}}, "elixir": {{5, 4}, {0, 4}}, "java": {{0, 0}, {3, 3}}, "lisp": {{2, 5}, {5, 2}}, "lua": {{7, 8}, {5, 6}}, "ruby": {{7, 5}, {4, 8}}, "rust": {{8, 4}, {8, 1}}}, | ||
false, | ||
}, | ||
{ | ||
"Should fail to locate a word that is not in the puzzle", | ||
[]string{"jefblpepre", "camdcimgtc", "oivokprjsm", "pbwasqroua", "rixilelhrs", "wolcqlirpc", "screeaumgr", "alxhpburyi", "jalaycalmp", "clojurermt"}, | ||
[]string{"clojure", "elixir", "ecmascript", "rust", "java", "lua", "lisp", "ruby", "haskell"}, | ||
map[string][2][2]int{}, | ||
true, | ||
}, | ||
} |
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