@@ -94,3 +94,131 @@ func TestRoom_FindCorpse(t *testing.T) {
9494 found , ok = r .FindCorpse ("NonExistent" )
9595 assert .False (t , ok , "Expected not to find a missing corpse" )
9696}
97+
98+ func TestFindNoun (t * testing.T ) {
99+ // Create a room with various noun mappings (including aliases).
100+ r := & Room {
101+ Nouns : map [string ]string {
102+ "torch" : "a fiery torch" ,
103+ "torchAlias" : ":torch" , // alias -> :torch means "torch"
104+ "lamp" : "an illuminating lamp" ,
105+ "lampAlias" : ":lamp" , // alias -> :lamp means "lamp"
106+ "candles" : "some wax candles" ,
107+ "pony" : "a small horse" ,
108+ "mystery" : "just a riddle" ,
109+ "secret" : ":mystery" , // chain alias -> :mystery
110+ "projector screen" : "a wide, matte-white screen" , // multi-word matching
111+ },
112+ }
113+
114+ // Table-driven tests.
115+ tests := []struct {
116+ name string
117+ inputNoun string
118+ wantFoundNoun string
119+ wantDesc string
120+ }{
121+ {
122+ name : "Direct match (torch)" ,
123+ inputNoun : "torch" ,
124+ wantFoundNoun : "torch" ,
125+ wantDesc : "a fiery torch" ,
126+ },
127+ {
128+ name : "Direct match (candles)" ,
129+ inputNoun : "candles" ,
130+ wantFoundNoun : "candles" ,
131+ wantDesc : "some wax candles" ,
132+ },
133+ {
134+ name : "Direct match (pony)" ,
135+ inputNoun : "pony" ,
136+ wantFoundNoun : "pony" ,
137+ wantDesc : "a small horse" ,
138+ },
139+ {
140+ name : "Alias match (torchAlias -> :torch)" ,
141+ inputNoun : "torchAlias" ,
142+ wantFoundNoun : "torch" ,
143+ wantDesc : "a fiery torch" ,
144+ },
145+ {
146+ name : "Alias match (lampAlias -> :lamp)" ,
147+ inputNoun : "lampAlias" ,
148+ wantFoundNoun : "lamp" ,
149+ wantDesc : "an illuminating lamp" ,
150+ },
151+ {
152+ name : "Chain alias (secret -> :mystery)" ,
153+ inputNoun : "secret" ,
154+ wantFoundNoun : "mystery" ,
155+ wantDesc : "just a riddle" ,
156+ },
157+ {
158+ name : "Plural form by adding s (pony -> ponies)" ,
159+ inputNoun : "ponies" ,
160+ wantFoundNoun : "pony" ,
161+ wantDesc : "a small horse" ,
162+ },
163+ {
164+ name : "Plural form by adding es (torches)" ,
165+ inputNoun : "torches" ,
166+ wantFoundNoun : "torch" ,
167+ wantDesc : "a fiery torch" ,
168+ },
169+
170+ {
171+ name : "Multi-word input, second word valid (red torches)" ,
172+ inputNoun : "red torches" ,
173+ wantFoundNoun : "torch" ,
174+ wantDesc : "a fiery torch" ,
175+ },
176+ {
177+ name : "Multi-word input, multi-word match" ,
178+ inputNoun : "projector screen" ,
179+ wantFoundNoun : "projector screen" ,
180+ wantDesc : "a wide, matte-white screen" ,
181+ },
182+ {
183+ name : "Single-word input, multi-word match 1" ,
184+ inputNoun : "projector" ,
185+ wantFoundNoun : "projector screen" ,
186+ wantDesc : "a wide, matte-white screen" ,
187+ },
188+ {
189+ name : "Single-word input, multi-word match 2" ,
190+ inputNoun : "screen" ,
191+ wantFoundNoun : "projector screen" ,
192+ wantDesc : "a wide, matte-white screen" ,
193+ },
194+ {
195+ name : "Multi-word input, first word valid (torch something)" ,
196+ inputNoun : "torch something" ,
197+ wantFoundNoun : "torch" ,
198+ wantDesc : "a fiery torch" ,
199+ },
200+ {
201+ name : "No match" ,
202+ inputNoun : "gibberish" ,
203+ wantFoundNoun : "" ,
204+ wantDesc : "" ,
205+ },
206+ {
207+ name : "Multi-word no match (foo bar)" ,
208+ inputNoun : "foo bar" ,
209+ wantFoundNoun : "" ,
210+ wantDesc : "" ,
211+ },
212+ }
213+
214+ // Run each sub-test.
215+ for _ , tt := range tests {
216+ t .Run (tt .name , func (t * testing.T ) {
217+ gotFound , gotDesc := r .FindNoun (tt .inputNoun )
218+ if gotFound != tt .wantFoundNoun || gotDesc != tt .wantDesc {
219+ t .Errorf ("FindNoun(%q) = (%q, %q), want (%q, %q)" ,
220+ tt .inputNoun , gotFound , gotDesc , tt .wantFoundNoun , tt .wantDesc )
221+ }
222+ })
223+ }
224+ }
0 commit comments