-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_test.grace
354 lines (335 loc) · 17.2 KB
/
parser_test.grace
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// vi:set ai sm nu ts=4 sw=4 expandtab:
//
// Interactive Fiction demo program -- parser test cases
// CS 420 term project to demonstrate an object-oriented parser design
// Steve Willoughby, Portland State University, Spring 2016
//
// ©2016 Steven L. Willoughby, All Rights Reserved. May be distributed and used
// under the terms of the Simplified (2-clause) BSD open-source license (See the
// LICENSE file for details.)
//
import "gUnit" as gu
import "parser" as parser
def parserTest = object {
class forMethod(m) {
inherits gu.testCaseNamed(m)
method testTrim {
assert(parser.trimLeadingSpace(" hello")) shouldBe ("hello")
assert(parser.trimLeadingSpace("hello")) shouldBe ("hello")
assert(parser.trimLeadingSpace("hello ")) shouldBe ("hello ")
assert(parser.trimLeadingSpace(" hello ")) shouldBe ("hello ")
assert(parser.trimLeadingSpace(" hello world ")) shouldBe ("hello world ")
}
method testBreakOut {
assert(parser.breakOutNextWord("a b c")) shouldBe ("a")
assert(parser.breakOutNextWord("magic word xyxzzy")) shouldBe ("magic")
assert(parser.breakOutNextWord(" magic word")) shouldBe ("")
assert(parser.breakOutNextWord(" ")) shouldBe ("")
assert(parser.breakOutNextWord("")) shouldBe ("")
}
method testRemoveLeading {
assert(parser.removeLeading (1) charactersFrom ("xyzzy")) shouldBe ("yzzy")
assert(parser.removeLeading (3) charactersFrom ("xyzzy")) shouldBe ("zy")
assert(parser.removeLeading (5) charactersFrom ("xyzzy")) shouldBe ("")
assert(parser.removeLeading (10) charactersFrom ("xyzzy")) shouldBe ("")
}
method testAsLower {
assert(parser.asLower ("HeLlOWorLD")) shouldBe "helloworld"
assert(parser.asLower ("éÉHeLlOWorLD")) shouldBe "éÉhelloworld"
}
method testNonsense {
def me = object {
var error is readable := ""
method complain (message) {
error := message
}
}
parser.parse("magic word xyzzy")forPlayer(me)
assert(me.error) shouldBe ("I have no idea what you mean by “magic”.")
}
method testSimpleVerbs {
def me = object {
var error is readable := ""
var verb is readable := ""
method complain (message) {
error := message
}
method quit { verb := "◊quit" }
method takeAction (verb') { verb := verb' }
}
parser.parse("quit") forPlayer(me)
assert (me.verb) shouldBe "◊quit"
parser.parse("HELP") forPlayer(me)
assert (me.error) shouldBe ‹Helpful Hints:
To move around, type compass directions such as GO NORTH, GO SOUTH, etc.
To see your progress, type SCORE.
To give up entirely, type QUIT.
Most commands can be abbreviated in reasonably obvious ways.
Good Luck!›
parser.parse("Inventory") forPlayer(me)
assert (me.verb) shouldBe "inventory"
parser.parse("examine") forPlayer(me)
assert (me.verb) shouldBe "examine"
parser.parse("take") forPlayer(me)
assert (me.verb) shouldBe "take"
parser.parse("get") forPlayer(me)
assert (me.verb) shouldBe "take"
parser.parse("drop") forPlayer(me)
assert (me.verb) shouldBe "drop"
parser.parse("score") forPlayer(me)
assert (me.verb) shouldBe "score"
}
method testDirections {
def me = object {
var verb is readable := ""
method complain (message) { verb := "err◊"++message }
method quit { verb := "◊quit" }
method takeAction (verb') { verb := verb' }
method moveDirection (dir) { verb := "dir◊"++dir }
}
parser.parse "go" forPlayer(me)
assert(me.verb) shouldBe "err◊Which direction do you want to move?"
parser.parse "MOVE" forPlayer(me)
assert(me.verb) shouldBe "err◊Which direction do you want to move?"
parser.parse "MOVE TO" forPlayer(me)
assert(me.verb) shouldBe "err◊Which direction do you want to move?"
parser.parse "MOVE TO SOUTH" forPlayer(me)
assert(me.verb) shouldBe "dir◊south"
parser.parse "MOVE TO THE WEST" forPlayer(me)
assert(me.verb) shouldBe "dir◊west"
parser.parse "MOVE EAST" forPlayer(me)
assert(me.verb) shouldBe "dir◊east"
parser.parse "WEST" forPlayer(me)
assert(me.verb) shouldBe "dir◊west"
}
method testLooks {
def me = object {
var verb is readable := ""
method complain (message) { verb := "err◊"++message }
method quit { verb := "◊quit" }
method takeAction (verb') { verb := verb' }
method moveDirection (dir) { verb := "dir◊"++dir }
method look { verb := "◊look" }
}
parser.parse "LOOK" forPlayer(me)
assert(me.verb) shouldBe "◊look"
parser.parse "LOOK AT" forPlayer(me)
assert(me.verb) shouldBe "examine"
parser.parse "LOOK AT OBJECT" forPlayer(me)
assert(me.verb) shouldBe "err◊I'm not really sure what object you're referring to here."
}
method testNull {
def me = object {} // should be NO method calls at all into me
assert({parser.parse "" forPlayer (me)}) shouldntRaise (Exception)
}
method testVerbObject {
def me = object {
var verb is readable := ""
method complain (message) { verb := "err◊"++message }
method quit { verb := "◊quit" }
method takeAction (verb') { verb := verb' }
method takeAction (verb') on (dobj) { verb := "VO◊"++verb'++"◊"++(dobj.name) }
method moveDirection (dir) { verb := "dir◊"++dir }
method look { verb := "◊look" }
}
def obj = object {
def name is readable = "#red obj"
method shortDescription { "red object" }
}
def obj2 = object {
def name is readable = "#blue obj"
method shortDescription { "blue object" }
}
def obj3 = object {
def name is readable = "#red thing"
method shortDescription { "red thing" }
}
def obj4 = object {
def name is readable = "#the cat"
method shortDescription { "cat" }
}
parser.vocabulary.clear
parser.vocabulary.addNoun "object" withAdjective "red" forObject (obj)
parser.vocabulary.addNoun "object" withAdjective "blue" forObject (obj2)
parser.vocabulary.addNoun "thing" withAdjective "red" forObject (obj3)
parser.vocabulary.addNoun "cat" forObject (obj4)
parser.vocabulary.addVerb "frob"
parser.parse "LOOK AT Object" forPlayer (me)
assert (me.verb) shouldBe "err◊ambiguous noun: Which object are you referring to?"
parser.parse "LOOK AT THE RED OBJECT" forPlayer (me)
assert (me.verb) shouldBe "VO◊examine◊#red obj"
parser.parse "LOOK AT BLUE OBJECT" forPlayer (me)
assert (me.verb) shouldBe "VO◊examine◊#blue obj"
parser.parse "LOOK AT THE THING" forPlayer (me)
assert (me.verb) shouldBe "VO◊examine◊#red thing"
parser.parse "LOOK AT BLUE" forPlayer (me)
assert (me.verb) shouldBe "VO◊examine◊#blue obj"
parser.parse "LOOK AT RED" forPlayer (me)
assert (me.verb) shouldBe "err◊ambiguous noun: Which red thing are you referring to?"
parser.parse "LOOK AT RED CAT" forPlayer (me)
assert (me.verb) shouldBe "err◊no such noun: I don't see any red cat here."
parser.parse "LOOK AT THE CAT" forPlayer (me)
assert (me.verb) shouldBe "VO◊examine◊#the cat"
parser.parse "FROB THE RED THING" forPlayer (me)
assert (me.verb) shouldBe "VO◊frob◊#red thing"
parser.parse "FROB A" forPlayer (me)
assert (me.verb) shouldBe "err◊You didn't specify a complete object to frob"
}
method testVerbIndirectObject {
def me = object {
var verb is readable := ""
method complain (message) { verb := "err◊"++message }
method quit { verb := "◊quit" }
method takeAction (verb') { verb := verb' }
method takeAction (verb') on (dobj) { verb := "VO◊"++verb'++"◊"++(dobj.name) }
method takeAction (verb') indirect (prep) with (iobj) { verb := "VPO◊"++verb'++"◊"++prep++"◊"++(iobj.name) }
method moveDirection (dir) { verb := "dir◊"++dir }
method look { verb := "◊look" }
}
def obj = object {
def name is readable = "#red obj"
method shortDescription { "red object" }
}
def obj2 = object {
def name is readable = "#blue obj"
method shortDescription { "blue object" }
}
def obj3 = object {
def name is readable = "#red thing"
method shortDescription { "red thing" }
}
def obj4 = object {
def name is readable = "#the cat"
method shortDescription { "cat" }
}
parser.vocabulary.clear
parser.vocabulary.addNoun "object" withAdjective "red" forObject (obj)
parser.vocabulary.addNoun "object" withAdjective "blue" forObject (obj2)
parser.vocabulary.addNoun "thing" withAdjective "red" forObject (obj3)
parser.vocabulary.addNoun "cat" forObject (obj4)
parser.vocabulary.addVerb "frob"
parser.parse "FROB WITH Object" forPlayer (me)
assert (me.verb) shouldBe "err◊ambiguous noun: Which object are you referring to?"
parser.parse "FROB WITH THE RED OBJECT" forPlayer (me)
assert (me.verb) shouldBe "VPO◊frob◊with◊#red obj"
parser.parse "FROB WITH BLUE OBJECT" forPlayer (me)
assert (me.verb) shouldBe "VPO◊frob◊with◊#blue obj"
parser.parse "FROB WITH THE THING" forPlayer (me)
assert (me.verb) shouldBe "VPO◊frob◊with◊#red thing"
parser.parse "FROB WITH BLUE" forPlayer (me)
assert (me.verb) shouldBe "VPO◊frob◊with◊#blue obj"
parser.parse "FROB WITH RED" forPlayer (me)
assert (me.verb) shouldBe "err◊ambiguous noun: Which red thing are you referring to?"
parser.parse "FROB WITH RED CAT" forPlayer (me)
assert (me.verb) shouldBe "err◊no such noun: I don't see any red cat here."
parser.parse "FROB WITH THE CAT" forPlayer (me)
assert (me.verb) shouldBe "VPO◊frob◊with◊#the cat"
parser.parse "FROB WITH A" forPlayer (me)
assert (me.verb) shouldBe "err◊You didn't specify a complete object to frob with"
}
method testVerbObjectIndirectObject {
def me = object {
var verb is readable := ""
method complain (message) { verb := "err◊"++message }
method quit { verb := "◊quit" }
method takeAction (verb') { verb := verb' }
method takeAction (verb') on (dobj) { verb := "VO◊"++verb'++"◊"++(dobj.name) }
method takeAction (verb') indirect (prep) with (iobj) { verb := "VPO◊"++verb'++"◊"++prep++"◊"++(iobj.name) }
method takeAction (verb') on (dobj) indirect (prep) with (iobj) {
verb := "VOPO◊"++verb'++"◊"++(dobj.name)++"◊"++prep++"◊"++(iobj.name)
}
method moveDirection (dir) { verb := "dir◊"++dir }
method look { verb := "◊look" }
}
def obj = object {
def name is readable = "#red obj"
method shortDescription { "red object" }
}
def obj2 = object {
def name is readable = "#blue obj"
method shortDescription { "blue object" }
}
def obj3 = object {
def name is readable = "#red thing"
method shortDescription { "red thing" }
}
def obj4 = object {
def name is readable = "#the cat"
method shortDescription { "cat" }
}
parser.vocabulary.clear
parser.vocabulary.addNoun "object" withAdjective "red" forObject (obj)
parser.vocabulary.addNoun "object" withAdjective "blue" forObject (obj2)
parser.vocabulary.addNoun "thing" withAdjective "red" forObject (obj3)
parser.vocabulary.addNoun "cat" forObject (obj4)
parser.vocabulary.addVerb "frob"
parser.parse "FROB THING WITH Object" forPlayer (me)
assert (me.verb) shouldBe "err◊ambiguous noun: Which object are you referring to?"
parser.parse "FROB A THING WITH THE RED OBJECT" forPlayer (me)
assert (me.verb) shouldBe "VOPO◊frob◊#red thing◊with◊#red obj"
parser.parse "FROB A THING WITH BLUE OBJECT" forPlayer (me)
assert (me.verb) shouldBe "VOPO◊frob◊#red thing◊with◊#blue obj"
parser.parse "FROB THE CAT WITH THE THING" forPlayer (me)
assert (me.verb) shouldBe "VOPO◊frob◊#the cat◊with◊#red thing"
parser.parse "FROB THE CAT WITH BLUE" forPlayer (me)
assert (me.verb) shouldBe "VOPO◊frob◊#the cat◊with◊#blue obj"
parser.parse "FROB A CAT WITH RED" forPlayer (me)
assert (me.verb) shouldBe "err◊ambiguous noun: Which red thing are you referring to?"
parser.parse "FROB A CAT WITH RED CAT" forPlayer (me)
assert (me.verb) shouldBe "err◊no such noun: I don't see any red cat here."
parser.parse "FROB A BLUE OBJECT WITH THE CAT" forPlayer (me)
assert (me.verb) shouldBe "VOPO◊frob◊#blue obj◊with◊#the cat"
parser.parse "FROB THING WITH A" forPlayer (me)
assert (me.verb) shouldBe "err◊You didn't specify a complete object to frob with"
}
method testPickUpPutDown {
def me = object {
var verb is readable := ""
method complain (message) { verb := "err◊"++message }
method quit { verb := "◊quit" }
method takeAction (verb') { verb := verb' }
method takeAction (verb') on (dobj) { verb := "VO◊"++verb'++"◊"++(dobj.name) }
method takeAction (verb') indirect (prep) with (iobj) { verb := "VPO◊"++verb'++"◊"++prep++"◊"++(iobj.name) }
method takeAction (verb') on (dobj) indirect (prep) with (iobj) {
verb := "VOPO◊"++verb'++"◊"++(dobj.name)++"◊"++prep++"◊"++(iobj.name)
}
method moveDirection (dir) { verb := "dir◊"++dir }
method look { verb := "◊look" }
}
def obj = object {
def name is readable = "#red obj"
method shortDescription { "red object" }
}
def obj2 = object {
def name is readable = "#blue obj"
method shortDescription { "blue object" }
}
def obj3 = object {
def name is readable = "#red thing"
method shortDescription { "red thing" }
}
def obj4 = object {
def name is readable = "#the cat"
method shortDescription { "cat" }
}
parser.vocabulary.clear
parser.vocabulary.addNoun "object" withAdjective "red" forObject (obj)
parser.vocabulary.addNoun "object" withAdjective "blue" forObject (obj2)
parser.vocabulary.addNoun "thing" withAdjective "red" forObject (obj3)
parser.vocabulary.addNoun "cat" forObject (obj4)
parser.parse "PICK THE RED OBJECT" forPlayer(me)
assert (me.verb) shouldBe "VO◊pick◊#red obj"
parser.parse "PICK UP THE RED OBJECT" forPlayer(me)
assert (me.verb) shouldBe "VO◊take◊#red obj"
parser.parse "PUT THE RED OBJECT" forPlayer(me)
assert (me.verb) shouldBe "VO◊put◊#red obj"
parser.parse "PUT DOWN THE RED OBJECT" forPlayer(me)
assert (me.verb) shouldBe "VO◊drop◊#red obj"
parser.parse "PICK IT UP" forPlayer(me)
assert (me.verb) shouldBe "VO◊take◊#red obj"
parser.parse "PUT IT DOWN" forPlayer(me)
assert (me.verb) shouldBe "VO◊drop◊#red obj"
}
}
}
gu.testSuite.fromTestMethodsIn(parserTest).runAndPrintResults