summaryrefslogtreecommitdiff
path: root/go/house/house_test.go
blob: d702dd050b0da7b96a8af39c4a73113b2f71c473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Embed embeds a noun phrase as the object of relative clause with a
// transitive verb.
//
// Argument relPhrase is a phrase with a relative clause, minus the object
// of the clause.  That is, relPhrase consists of a subject, a relative
// pronoun, a transitive verb, possibly a preposition, but then no object.
//
//    func Embed(relPhrase, nounPhrase string) string

// Verse generates a verse of a song with relative clauses that have
// a recursive structure.
//
//    func Verse(subject string, relPhrases []string, nounPhrase string) string
//
// There are different ways to do this of course, but try using Embed as a
// subroutine and using programmatic recursion that reflects the grammatical
// recursion.

// Song generates the full text of "The House That Jack Built".  Oh yes, you
// could just return a string literal, but humor us; use Verse as a subroutine.
//
//    func Song() string

package house

import (
	"strings"
	"testing"
)

var (
	s = "This is"
	r = []string{
		"the cat that broke",
		"the vase that was on",
	}
	p    = "the shelf."
	last = len(r) - 1
	// song copied from readme
	song = `This is the house that Jack built.

This is the malt
that lay in the house that Jack built.

This is the rat
that ate the malt
that lay in the house that Jack built.

This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.

This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.`
)

func TestEmbed(t *testing.T) {
	l := r[last]
	want := l + " " + p
	if e := Embed(l, p); e != want {
		t.Fatalf("Embed(%q, %q) = %q, want %q.", l, p, e, want)
	}
}

func TestVerse(t *testing.T) {
	for i := len(r); i >= 0; i-- {
		ri := r[i:]
		want := s + " " + strings.Join(append(ri, p), " ")
		if v := Verse(s, ri, p); v != want {
			t.Fatalf("Verse(%q, %q, %q) = %q, want %q.", s, ri, p, v, want)
		}
	}
}

func TestSong(t *testing.T) {
	s := Song()
	if s == song {
		return
	}
	// a little help in locating an error
	got := strings.Split(s, "\n")
	want := strings.Split(song, "\n")
	var g, w string
	var i int
	for i, w = range want {
		if len(got) <= i {
			g = ""
			break
		}
		if g = got[i]; g != w {
			break
		}
	}
	t.Fatalf("Song() line %d = %q, want %q", i+1, g, w)
}