Skip to content

Commit c4f1a98

Browse files
authored
[house] Add test generator and update tests for runner (#3121)
* Improve runner "TEST CODE" by following the expected test function pattern. * Add a test generator and move tests into `cases_test.go`. [no important files changed]
1 parent 7148336 commit c4f1a98

4 files changed

Lines changed: 174 additions & 106 deletions

File tree

exercises/practice/house/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"house.go"
2121
],
2222
"test": [
23-
"house_test.go"
23+
"house_test.go",
24+
"cases_test.go"
2425
],
2526
"example": [
2627
".meta/example.go"
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"../../../../gen"
5+
"log"
6+
"strings"
7+
"text/template"
8+
)
9+
10+
func main() {
11+
t, err := template.New("").Parse(tmpl)
12+
if err != nil {
13+
log.Fatal(err)
14+
}
15+
j := map[string]any{
16+
"recite": &[]testCase{},
17+
}
18+
if err := gen.Gen("house", j, t); err != nil {
19+
log.Fatal(err)
20+
}
21+
}
22+
23+
type testCase struct {
24+
Description string `json:"description"`
25+
Input struct {
26+
StartVerse int `json:"startVerse"`
27+
EndVerse int `json:"endVerse"`
28+
} `json:"input"`
29+
Expected []string `json:"expected"`
30+
}
31+
32+
// wrapVerse inserts newlines before the "that" section separators.
33+
func wrapVerse(verse string) string {
34+
var sb strings.Builder
35+
for part := range strings.SplitSeq(verse, " that ") {
36+
if strings.HasPrefix(part, "Jack") || strings.HasPrefix(part, "crowed") {
37+
sb.WriteString(" that ")
38+
} else if ! strings.HasPrefix(part, "This") {
39+
sb.WriteString("\nthat ")
40+
}
41+
sb.WriteString(part)
42+
}
43+
return sb.String()
44+
}
45+
46+
func (tc testCase) WrappedVerses() []string {
47+
out := make([]string, len(tc.Expected))
48+
for i, verse := range tc.Expected {
49+
out[i] = wrapVerse(verse)
50+
}
51+
return out
52+
}
53+
54+
var tmpl = `{{.Header}}
55+
56+
type testCase struct {
57+
description string
58+
verse int
59+
expected string
60+
}
61+
62+
var testCases = []testCase { {{range .J.recite}} {{ if eq .Input.StartVerse .Input.EndVerse }}
63+
{
64+
description: {{printf "%q" .Description}},
65+
verse: {{.Input.StartVerse}},
66+
expected: {{index .WrappedVerses 0 | printf "%q"}},
67+
},{{end}}{{end}}
68+
}
69+
70+
var song = {{range .J.recite}}{{if eq .Input.StartVerse 1}}{{ if eq .Input.EndVerse 12 }}[]string{ {{range .WrappedVerses}}
71+
{{printf "%q" .}},{{end}}
72+
}{{end}}{{end}}{{end}}
73+
`
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package house
2+
3+
// This is an auto-generated file. Do not change it manually. Run the generator to update the file.
4+
// See https://github.com/exercism/go#synchronizing-tests-and-instructions
5+
// Source: exercism/problem-specifications
6+
// Commit: b820099 Allow prettier to format more files (#1966)
7+
8+
type testCase struct {
9+
description string
10+
verse int
11+
expected string
12+
}
13+
14+
var testCases = []testCase{
15+
{
16+
description: "verse one - the house that jack built",
17+
verse: 1,
18+
expected: "This is the house that Jack built.",
19+
},
20+
{
21+
description: "verse two - the malt that lay",
22+
verse: 2,
23+
expected: "This is the malt\nthat lay in the house that Jack built.",
24+
},
25+
{
26+
description: "verse three - the rat that ate",
27+
verse: 3,
28+
expected: "This is the rat\nthat ate the malt\nthat lay in the house that Jack built.",
29+
},
30+
{
31+
description: "verse four - the cat that killed",
32+
verse: 4,
33+
expected: "This is the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
34+
},
35+
{
36+
description: "verse five - the dog that worried",
37+
verse: 5,
38+
expected: "This is the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
39+
},
40+
{
41+
description: "verse six - the cow with the crumpled horn",
42+
verse: 6,
43+
expected: "This is the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
44+
},
45+
{
46+
description: "verse seven - the maiden all forlorn",
47+
verse: 7,
48+
expected: "This is the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
49+
},
50+
{
51+
description: "verse eight - the man all tattered and torn",
52+
verse: 8,
53+
expected: "This is the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
54+
},
55+
{
56+
description: "verse nine - the priest all shaven and shorn",
57+
verse: 9,
58+
expected: "This is the priest all shaven and shorn\nthat married the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
59+
},
60+
{
61+
description: "verse 10 - the rooster that crowed in the morn",
62+
verse: 10,
63+
expected: "This is the rooster that crowed in the morn\nthat woke the priest all shaven and shorn\nthat married the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
64+
},
65+
{
66+
description: "verse 11 - the farmer sowing his corn",
67+
verse: 11,
68+
expected: "This is the farmer sowing his corn\nthat kept the rooster that crowed in the morn\nthat woke the priest all shaven and shorn\nthat married the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
69+
},
70+
{
71+
description: "verse 12 - the horse and the hound and the horn",
72+
verse: 12,
73+
expected: "This is the horse and the hound and the horn\nthat belonged to the farmer sowing his corn\nthat kept the rooster that crowed in the morn\nthat woke the priest all shaven and shorn\nthat married the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
74+
},
75+
}
76+
77+
var song = []string{
78+
"This is the house that Jack built.",
79+
"This is the malt\nthat lay in the house that Jack built.",
80+
"This is the rat\nthat ate the malt\nthat lay in the house that Jack built.",
81+
"This is the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
82+
"This is the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
83+
"This is the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
84+
"This is the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
85+
"This is the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
86+
"This is the priest all shaven and shorn\nthat married the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
87+
"This is the rooster that crowed in the morn\nthat woke the priest all shaven and shorn\nthat married the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
88+
"This is the farmer sowing his corn\nthat kept the rooster that crowed in the morn\nthat woke the priest all shaven and shorn\nthat married the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
89+
"This is the horse and the hound and the horn\nthat belonged to the farmer sowing his corn\nthat kept the rooster that crowed in the morn\nthat woke the priest all shaven and shorn\nthat married the man all tattered and torn\nthat kissed the maiden all forlorn\nthat milked the cow with the crumpled horn\nthat tossed the dog\nthat worried the cat\nthat killed the rat\nthat ate the malt\nthat lay in the house that Jack built.",
90+
}

exercises/practice/house/house_test.go

Lines changed: 9 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -11,129 +11,33 @@
1111
package house
1212

1313
import (
14-
"fmt"
1514
"strings"
1615
"testing"
1716
)
1817

19-
var (
20-
// song copied from README
21-
expectedSong = `This is the house that Jack built.
22-
23-
This is the malt
24-
that lay in the house that Jack built.
25-
26-
This is the rat
27-
that ate the malt
28-
that lay in the house that Jack built.
29-
30-
This is the cat
31-
that killed the rat
32-
that ate the malt
33-
that lay in the house that Jack built.
34-
35-
This is the dog
36-
that worried the cat
37-
that killed the rat
38-
that ate the malt
39-
that lay in the house that Jack built.
40-
41-
This is the cow with the crumpled horn
42-
that tossed the dog
43-
that worried the cat
44-
that killed the rat
45-
that ate the malt
46-
that lay in the house that Jack built.
47-
48-
This is the maiden all forlorn
49-
that milked the cow with the crumpled horn
50-
that tossed the dog
51-
that worried the cat
52-
that killed the rat
53-
that ate the malt
54-
that lay in the house that Jack built.
55-
56-
This is the man all tattered and torn
57-
that kissed the maiden all forlorn
58-
that milked the cow with the crumpled horn
59-
that tossed the dog
60-
that worried the cat
61-
that killed the rat
62-
that ate the malt
63-
that lay in the house that Jack built.
64-
65-
This is the priest all shaven and shorn
66-
that married the man all tattered and torn
67-
that kissed the maiden all forlorn
68-
that milked the cow with the crumpled horn
69-
that tossed the dog
70-
that worried the cat
71-
that killed the rat
72-
that ate the malt
73-
that lay in the house that Jack built.
74-
75-
This is the rooster that crowed in the morn
76-
that woke the priest all shaven and shorn
77-
that married the man all tattered and torn
78-
that kissed the maiden all forlorn
79-
that milked the cow with the crumpled horn
80-
that tossed the dog
81-
that worried the cat
82-
that killed the rat
83-
that ate the malt
84-
that lay in the house that Jack built.
85-
86-
This is the farmer sowing his corn
87-
that kept the rooster that crowed in the morn
88-
that woke the priest all shaven and shorn
89-
that married the man all tattered and torn
90-
that kissed the maiden all forlorn
91-
that milked the cow with the crumpled horn
92-
that tossed the dog
93-
that worried the cat
94-
that killed the rat
95-
that ate the malt
96-
that lay in the house that Jack built.
97-
98-
This is the horse and the hound and the horn
99-
that belonged to the farmer sowing his corn
100-
that kept the rooster that crowed in the morn
101-
that woke the priest all shaven and shorn
102-
that married the man all tattered and torn
103-
that kissed the maiden all forlorn
104-
that milked the cow with the crumpled horn
105-
that tossed the dog
106-
that worried the cat
107-
that killed the rat
108-
that ate the malt
109-
that lay in the house that Jack built.`
110-
111-
expectedVerses = strings.Split(expectedSong, "\n\n")
112-
)
113-
11418
func TestVerse(t *testing.T) {
115-
for v := 0; v < len(expectedVerses); v++ {
116-
t.Run(fmt.Sprintf("verse %d", v+1), func(t *testing.T) {
117-
if got := Verse(v + 1); got != expectedVerses[v] {
118-
t.Fatalf("Verse(%d)\ngot:\n%q\nwant:\n%q", v+1, got, expectedVerses[v])
19+
for _, tc := range testCases {
20+
t.Run(tc.description, func(t *testing.T) {
21+
if got := Verse(tc.verse); got != tc.expected {
22+
t.Fatalf("Verse(%d)\ngot:\n%q\nwant:\n%q", tc.verse, got, tc.expected)
11923
}
12024
})
12125
}
12226
}
12327

12428
func TestSong(t *testing.T) {
12529
s := Song()
126-
if s == expectedSong {
30+
if s == strings.Join(song, "\n\n") {
12731
return
12832
}
12933
// a little help in locating an error
13034
gotStanzas := len(strings.Split(s, "\n\n"))
131-
wantStanzas := len(expectedVerses)
35+
wantStanzas := len(song)
13236
if wantStanzas != gotStanzas {
13337
t.Fatalf("Song() has %d verse(s), want %d verses", gotStanzas, wantStanzas)
13438
}
13539
got := strings.Split(s, "\n")
136-
want := strings.Split(expectedSong, "\n")
40+
want := strings.Split(strings.Join(song, "\n\n"), "\n")
13741
var g, w string
13842
var i int
13943
for i, w = range want {
@@ -150,8 +54,8 @@ func TestSong(t *testing.T) {
15054

15155
func BenchmarkVerse(b *testing.B) {
15256
for range b.N {
153-
for v := 0; v < len(expectedVerses); v++ {
154-
Verse(v + 1)
57+
for _, tc := range testCases {
58+
Verse(tc.verse)
15559
}
15660
}
15761
}

0 commit comments

Comments
 (0)