forked from aichaos/rivescript-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rivescript.py
More file actions
685 lines (535 loc) · 20.3 KB
/
Copy pathtest_rivescript.py
File metadata and controls
685 lines (535 loc) · 20.3 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
import unittest
from rivescript.rivescript import RS_ERR_MATCH
from .config import RiveScriptTestCase
class BeginBlockTests(RiveScriptTestCase):
"""BEGIN Block Tests"""
def test_no_begin_block(self):
self.new("""
+ hello bot
- Hello human.
""")
self.reply("Hello bot", "Hello human.")
def test_simple_begin_block(self):
self.new("""
> begin
+ request
- {ok}
< begin
+ hello bot
- Hello human.
""")
self.reply("Hello bot", "Hello human.")
def test_blocked_begin_block(self):
self.new("""
> begin
+ request
- Nope.
< begin
+ hello bot
- Hello human.
""")
self.reply("Hello bot", "Nope.")
def test_conditional_begin_block(self):
self.new("""
> begin
+ request
* <get met> == undefined => <set met=true>{ok}
* <get name> != undefined => <get name>: {ok}
- {ok}
< begin
+ hello bot
- Hello human.
+ my name is *
- <set name=<formal>>Hello, <get name>.
""")
self.reply("Hello bot.", "Hello human.")
self.uservar("met", "true")
self.uservar("name", "undefined")
self.reply("My name is bob", "Hello, Bob.")
self.uservar("name", "Bob")
self.reply("Hello Bot", "Bob: Hello human.")
class BotvarTests(RiveScriptTestCase):
"""Test bot variables."""
def test_bot_variables(self):
self.new("""
! var name = Aiden
! var age = 5
+ what is your name
- My name is <bot name>.
+ how old are you
- I am <bot age>.
+ what are you
- I'm <bot gender>.
+ happy birthday
- <bot age=6>Thanks!
""")
self.reply("What is your name?", "My name is Aiden.")
self.reply("How old are you?", "I am 5.")
self.reply("What are you?", "I'm undefined.")
self.reply("Happy birthday!", "Thanks!")
self.reply("How old are you?", "I am 6.")
def test_global_variables(self):
self.new("""
! global debug = false
+ debug mode
- Debug mode is: <env debug>
+ set debug mode *
- <env debug=<star>>Switched to <star>.
""")
self.reply("Debug mode.", "Debug mode is: false")
self.reply("Set debug mode true", "Switched to true.")
self.reply("Debug mode?", "Debug mode is: true")
class SubstitutionTests(RiveScriptTestCase):
"""Test substitutions."""
def test_substitutions(self):
self.new("""
+ whats up
- nm.
+ what is up
- Not much.
""")
self.reply("whats up", "nm.")
self.reply("what's up?", "nm.")
self.reply("what is up?", "Not much.")
self.extend("""
! sub whats = what is
! sub what's = what is
""")
self.reply("whats up", "Not much.")
self.reply("what's up?", "Not much.")
self.reply("What is up?", "Not much.")
def test_person_substitutions(self):
self.new("""
+ say *
- <person>
""")
self.reply("say I am cool", "i am cool")
self.reply("say You are dumb", "you are dumb")
self.extend("""
! person i am = you are
! person you are = I am
""")
self.reply("say I am cool", "you are cool")
self.reply("say You are dumb", "I am dumb")
class TriggerTests(RiveScriptTestCase):
"""Test triggers."""
def test_atomic_triggers(self):
self.new("""
+ hello bot
- Hello human.
+ what are you
- I am a RiveScript bot.
""")
self.reply("Hello bot", "Hello human.")
self.reply("What are you?", "I am a RiveScript bot.")
def test_wildcard_triggers(self):
self.new("""
+ my name is *
- Nice to meet you, <star>.
+ * told me to say *
- Why did <star1> tell you to say <star2>?
+ i am # years old
- A lot of people are <star>.
+ i am _ years old
- Say that with numbers.
+ i am * years old
- Say that with fewer words.
""")
self.reply("my name is Bob", "Nice to meet you, bob.")
self.reply("bob told me to say hi", "Why did bob tell you to say hi?")
self.reply("i am 5 years old", "A lot of people are 5.")
self.reply("i am five years old", "Say that with numbers.")
self.reply("i am twenty five years old", "Say that with fewer words.")
def test_alternatives_and_optionals(self):
self.new("""
+ what (are|is) you
- I am a robot.
+ what is your (home|office|cell) [phone] number
- It is 555-1234.
+ [please|can you] ask me a question
- Why is the sky blue?
+ (aa|bb|cc) [bogus]
- Matched.
+ (yo|hi) [computer|bot] *
- Matched.
""")
self.reply("What are you?", "I am a robot.")
self.reply("What is you?", "I am a robot.")
for kind in ["home", "office", "cell"]:
self.reply("What is your {} phone number?".format(kind), "It is 555-1234.")
self.reply("What is your {} number?".format(kind), "It is 555-1234.")
self.reply("Can you ask me a question?", "Why is the sky blue?")
self.reply("Please ask me a question?", "Why is the sky blue?")
self.reply("Ask me a question?", "Why is the sky blue?")
self.reply("aa", "Matched.")
self.reply("bb", "Matched.")
self.reply("aa bogus", "Matched.")
self.reply("aabogus", RS_ERR_MATCH)
self.reply("bogus", RS_ERR_MATCH)
self.reply("hi Aiden", "Matched.")
self.reply("hi bot how are you?", "Matched.")
self.reply("yo computer what time is it?", "Matched.")
self.reply("yoghurt is yummy", RS_ERR_MATCH)
self.reply("hide and seek is fun", RS_ERR_MATCH)
self.reply("hip hip hurrah", RS_ERR_MATCH)
def test_trigger_arrays(self):
self.new("""
! array colors = red blue green yellow white
^ dark blue|light blue
+ what color is my (@colors) *
- Your <star2> is <star1>.
+ what color was * (@colors) *
- It was <star2>.
+ i have a @colors *
- Tell me more about your <star>.
""")
self.reply("What color is my red shirt?", "Your shirt is red.")
self.reply("What color is my blue car?", "Your car is blue.")
self.reply("What color is my pink house?", RS_ERR_MATCH)
self.reply("What color is my dark blue jacket?", "Your jacket is dark blue.")
self.reply("What color was Napoleon's white horse?", "It was white.")
self.reply("What color was my red shirt?", "It was red.")
self.reply("I have a blue car.", "Tell me more about your car.")
self.reply("I have a cyan car.", RS_ERR_MATCH)
def test_weighted_triggers(self):
self.new("""
+ * or something{weight=10}
- Or something. <@>
+ can you run a google search for *
- Sure!
+ hello *{weight=20}
- Hi there!
""")
self.reply("Hello robot.", "Hi there!")
self.reply("Hello or something", "Hi there!")
self.reply("Can you run a Google search for Python", "Sure!")
self.reply("Can you run a Google search for Python or something", "Or something. Sure!")
class ReplyTests(RiveScriptTestCase):
"""Response tests."""
def test_previous(self):
self.new("""
! sub who's = who is
! sub it's = it is
! sub didn't = did not
+ knock knock
- Who's there?
+ *
% who is there
- <sentence> who?
+ *
% * who
- Haha! <sentence>!
+ *
- I don't know.
""")
self.reply("knock knock", "Who's there?")
self.reply("Canoe", "Canoe who?")
self.reply("Canoe help me with my homework?", "Haha! Canoe help me with my homework!")
self.reply("hello", "I don't know.")
def test_continuations(self):
self.new("""
+ tell me a poem
- There once was a man named Tim,\\s
^ who never quite learned how to swim.\\s
^ He fell off a dock, and sank like a rock,\\s
^ and that was the end of him.
""")
self.reply("Tell me a poem.",
"There once was a man named Tim, " \
+ "who never quite learned how to swim. " \
+ "He fell off a dock, and sank like a rock, " \
+ "and that was the end of him.")
def test_redirects(self):
self.new("""
+ hello
- Hi there!
+ hey
@ hello
+ hi there
- {@hello}
""")
for greeting in ["hello", "hey", "hi there"]:
self.reply(greeting, "Hi there!")
def test_conditionals(self):
self.new("""
+ i am # years old
- <set age=<star>>OK.
+ what can i do
* <get age> == undefined => I don't know.
* <get age> > 25 => Anything you want.
* <get age> == 25 => Rent a car for cheap.
* <get age> >= 21 => Drink.
* <get age> >= 18 => Vote.
* <get age> < 18 => Not much of anything.
+ am i your master
* <get master> == true => Yes.
- No.
""")
age_q = "What can I do?"
self.reply(age_q, "I don't know.")
ages = {
'16' : "Not much of anything.",
'18' : "Vote.",
'20' : "Vote.",
'22' : "Drink.",
'24' : "Drink.",
'25' : "Rent a car for cheap.",
'27' : "Anything you want.",
}
for age in sorted(ages.keys()):
self.reply("I am {} years old.".format(age), "OK.")
self.reply(age_q, ages[age])
self.reply("Am I your master?", "No.")
self.rs.set_uservar(self.username, "master", "true")
self.reply("Am I your master?", "Yes.")
def test_embedded_tags(self):
self.new("""
+ my name is *
* <get name> != undefined => <set oldname=<get name>>I thought\s
^ your name was <get oldname>?
^ <set name=<formal>>
- <set name=<formal>>OK.
+ what is my name
- Your name is <get name>, right?
+ html test
- <set name=<b>Name</b>>This has some non-RS <em>tags</em> in it.
""")
self.reply("What is my name?", "Your name is undefined, right?")
self.reply("My name is Alice", "OK.")
self.reply("My name is Bob.", "I thought your name was Alice?")
self.reply("What is my name?", "Your name is Bob, right?")
self.reply("HTML Test", "This has some non-RS <em>tags</em> in it.")
class ObjectMacroTests(RiveScriptTestCase):
"""Test object macros."""
def test_python_objects(self):
self.new("""
> object nolang
return "Test w/o language."
< object
> object wlang python
return "Test w/ language."
< object
> object reverse python
msg = " ".join(args)
return msg[::-1]
< object
> object broken python
return "syntax error
< object
> object foreign javascript
return "JavaScript checking in!"
< object
+ test nolang
- Nolang: <call>nolang</call>
+ test wlang
- Wlang: <call>wlang</call>
+ reverse *
- <call>reverse <star></call>
+ test broken
- Broken: <call>broken</call>
+ test fake
- Fake: <call>fake</call>
+ test js
- JS: <call>foreign</call>
""")
self.reply("Test nolang", "Nolang: Test w/o language.")
self.reply("Test wlang", "Wlang: Test w/ language.")
self.reply("Reverse hello world", "dlrow olleh")
self.reply("Test broken", "Broken: [ERR: Object Not Found]")
self.reply("Test fake", "Fake: [ERR: Object Not Found]")
self.reply("Test js", "JS: [ERR: Object Not Found]")
def test_disabled_python_language(self):
self.new("""
> object test python
return "Python here!"
< object
+ test
- Result: <call>test</call>
""", debug=True)
self.reply("test", "Result: Python here!")
self.rs.set_handler("python", None)
self.reply("test", "Result: [ERR: No Object Handler]")
class TopicTest(RiveScriptTestCase):
"""Topic tests."""
def test_punishment_topic(self):
self.new("""
+ hello
- Hi there!
+ swear word
- How rude! Apologize or I won't talk to you again.{topic=sorry}
+ *
- Catch-all.
> topic sorry
+ sorry
- It's ok!{topic=random}
+ *
- Say you're sorry!
< topic
""")
self.reply("hello", "Hi there!")
self.reply("How are you?", "Catch-all.")
self.reply("Swear word!", "How rude! Apologize or I won't talk to you again.")
self.reply("hello", "Say you're sorry!")
self.reply("How are you?", "Say you're sorry!")
self.reply("Sorry!", "It's ok!")
self.reply("hello", "Hi there!")
self.reply("How are you?", "Catch-all.")
def test_topic_inheritence(self):
self.new("""
> topic colors
+ what color is the sky
- Blue.
+ what color is the sun
- Yellow.
< topic
> topic linux
+ name a red hat distro
- Fedora.
+ name a debian distro
- Ubuntu.
< topic
> topic stuff includes colors linux
+ say stuff
- "Stuff."
< topic
> topic override inherits colors
+ what color is the sun
- Purple.
< topic
> topic morecolors includes colors
+ what color is grass
- Green.
< topic
> topic evenmore inherits morecolors
+ what color is grass
- Blue, sometimes.
< topic
""")
self.rs.set_uservar(self.username, "topic", "colors")
self.reply("What color is the sky?", "Blue.")
self.reply("What color is the sun?", "Yellow.")
self.reply("What color is grass?", RS_ERR_MATCH)
self.reply("Name a Red Hat distro.", RS_ERR_MATCH)
self.reply("Name a Debian distro.", RS_ERR_MATCH)
self.reply("Say stuff.", RS_ERR_MATCH)
self.rs.set_uservar(self.username, "topic", "linux")
self.reply("What color is the sky?", RS_ERR_MATCH)
self.reply("What color is the sun?", RS_ERR_MATCH)
self.reply("What color is grass?", RS_ERR_MATCH)
self.reply("Name a Red Hat distro.", "Fedora.")
self.reply("Name a Debian distro.", "Ubuntu.")
self.reply("Say stuff.", RS_ERR_MATCH)
self.rs.set_uservar(self.username, "topic", "stuff")
self.reply("What color is the sky?", "Blue.")
self.reply("What color is the sun?", "Yellow.")
self.reply("What color is grass?", RS_ERR_MATCH)
self.reply("Name a Red Hat distro.", "Fedora.")
self.reply("Name a Debian distro.", "Ubuntu.")
self.reply("Say stuff.", '"Stuff."')
self.rs.set_uservar(self.username, "topic", "override")
self.reply("What color is the sky?", "Blue.")
self.reply("What color is the sun?", "Purple.")
self.reply("What color is grass?", RS_ERR_MATCH)
self.reply("Name a Red Hat distro.", RS_ERR_MATCH)
self.reply("Name a Debian distro.", RS_ERR_MATCH)
self.reply("Say stuff.", RS_ERR_MATCH)
self.rs.set_uservar(self.username, "topic", "morecolors")
self.reply("What color is the sky?", "Blue.")
self.reply("What color is the sun?", "Yellow.")
self.reply("What color is grass?", "Green.")
self.reply("Name a Red Hat distro.", RS_ERR_MATCH)
self.reply("Name a Debian distro.", RS_ERR_MATCH)
self.reply("Say stuff.", RS_ERR_MATCH)
self.rs.set_uservar(self.username, "topic", "evenmore")
self.reply("What color is the sky?", "Blue.")
self.reply("What color is the sun?", "Yellow.")
self.reply("What color is grass?", "Blue, sometimes.")
self.reply("Name a Red Hat distro.", RS_ERR_MATCH)
self.reply("Name a Debian distro.", RS_ERR_MATCH)
self.reply("Say stuff.", RS_ERR_MATCH)
class ParserOptionTest(RiveScriptTestCase):
"""File scoped parser option tests."""
def test_concat(self):
self.new("""
// Default concat mode = none
+ test concat default
- Hello
^ world!
! local concat = space
+ test concat space
- Hello
^ world!
! local concat = none
+ test concat none
- Hello
^ world!
! local concat = newline
+ test concat newline
- Hello
^ world!
// invalid concat setting is equivalent to `none`
! local concat = foobar
+ test concat foobar
- Hello
^ world!
// the option is file scoped so it can be left at
// any setting and won't affect subsequent parses
! local concat = newline
""")
self.extend("""
// concat mode should be restored to the default in a
// separate file/stream parse
+ test concat second file
- Hello
^ world!
""")
self.reply("test concat default", "Helloworld!")
self.reply("test concat space", "Hello world!")
self.reply("test concat none", "Helloworld!")
self.reply("test concat newline", "Hello\nworld!")
self.reply("test concat second file", "Helloworld!")
class UnicodeTest(RiveScriptTestCase):
"""UTF-8 Tests."""
def test_unicode(self):
self.new("""
! sub who's = who is
+ äh
- What's the matter?
+ ブラッキー
- エーフィ
// Make sure %Previous continues working in UTF-8 mode.
+ knock knock
- Who's there?
+ *
% who is there
- <sentence> who?
+ *
% * who
- Haha! <sentence>!
// And with UTF-8.
+ tëll më ä pöëm
- Thërë öncë wäs ä män nämëd Tïm
+ more
% thërë öncë wäs ä män nämëd tïm
- Whö nëvër qüïtë lëärnëd höw tö swïm
+ more
% whö nëvër qüïtë lëärnëd höw tö swïm
- Hë fëll öff ä döck, änd sänk lïkë ä röck
+ more
% hë fëll öff ä döck änd sänk lïkë ä röck
- Änd thät wäs thë ënd öf hïm.
""", utf8=True)
self.reply("äh", "What's the matter?")
self.reply("ブラッキー", "エーフィ")
self.reply("knock knock", "Who's there?")
self.reply("Orange", "Orange who?")
self.reply("banana", "Haha! Banana!")
self.reply("tëll më ä pöëm", "Thërë öncë wäs ä män nämëd Tïm")
self.reply("more", "Whö nëvër qüïtë lëärnëd höw tö swïm")
self.reply("more", "Hë fëll öff ä döck, änd sänk lïkë ä röck")
self.reply("more", "Änd thät wäs thë ënd öf hïm.")
if __name__ == "__main__":
unittest.main()