55
66import re
77import unittest
8+ from collections import OrderedDict
89
910from rivescript .rivescript import RS_ERR_MATCH
1011
@@ -90,12 +91,22 @@ def test_bot_variables(self):
9091
9192 + happy birthday
9293 - <bot age=6>Thanks!
94+
95+ + who is your master
96+ - My master is <bot master>.
9397 """ )
98+ self .rs .set_variable ("master" , "kirsle" )
99+
94100 self .reply ("What is your name?" , "My name is Aiden." )
95101 self .reply ("How old are you?" , "I am 5." )
96102 self .reply ("What are you?" , "I'm undefined." )
97103 self .reply ("Happy birthday!" , "Thanks!" )
98104 self .reply ("How old are you?" , "I am 6." )
105+ self .reply ("Who is your master?" , "My master is kirsle." )
106+
107+ self .assertEqual (self .rs .get_variable ("age" ), "6" )
108+ self .assertEqual (self .rs .get_variable ("master" ), "kirsle" )
109+ self .assertEqual (self .rs .get_variable ("fake" ), "undefined" )
99110
100111
101112 def test_global_variables (self ):
@@ -107,10 +118,20 @@ def test_global_variables(self):
107118
108119 + set debug mode *
109120 - <env debug=<star>>Switched to <star>.
121+
122+ + are you testing
123+ - Testing: <env testing>
110124 """ )
125+ self .rs .set_global ("testing" , "true" )
126+
111127 self .reply ("Debug mode." , "Debug mode is: false" )
112128 self .reply ("Set debug mode true" , "Switched to true." )
113129 self .reply ("Debug mode?" , "Debug mode is: true" )
130+ self .reply ("Are you testing?" , "Testing: true" )
131+
132+ self .assertEqual (self .rs .get_global ("debug" ), "true" )
133+ self .assertEqual (self .rs .get_global ("testing" ), "true" )
134+ self .assertEqual (self .rs .get_global ("fake" ), "undefined" )
114135
115136
116137class SubstitutionTests (RiveScriptTestCase ):
@@ -680,6 +701,88 @@ def test_concat(self):
680701 self .reply ("test concat second file" , "Helloworld!" )
681702
682703
704+ class APITest (RiveScriptTestCase ):
705+ """Miscellaneous API tests."""
706+
707+ def test_set_uservars (self ):
708+ self .new ("""
709+ + who am i
710+ - You are <get name>, seeker!
711+
712+ + how old am i
713+ - You are <get age> years old.
714+ """ )
715+
716+ # Test the base case, with no vars set.
717+ self .reply ("Who am I?" , "You are undefined, seeker!" )
718+ self .reply ("How old am I?" , "You are undefined years old." )
719+
720+ # Set setting one variable at a time.
721+ self .rs .set_uservar ("localuser" , "name" , "Alice" )
722+ self .rs .set_uservar ("localuser" , "age" , "10" )
723+ self .reply ("Who am I?" , "You are Alice, seeker!" )
724+ self .reply ("How old am I?" , "You are 10 years old." )
725+
726+ # Test setting a dict of variables for one user.
727+ self .rs .set_uservars ("localuser" , {
728+ "name" : "Eliza" ,
729+ "age" : "20" ,
730+ })
731+ self .reply ("Who am I?" , "You are Eliza, seeker!" )
732+ self .reply ("How old am I?" , "You are 20 years old." )
733+
734+ # Test setting a partial dict which only updates named keys.
735+ self .rs .set_uservars ("localuser" , dict (name = "UltraHAL" ))
736+ self .reply ("Who am I?" , "You are UltraHAL, seeker!" )
737+ self .reply ("How old am I?" , "You are 20 years old." )
738+
739+ # Test setting a dict of many users to many keys.
740+ self .rs .set_uservars ({
741+ "localuser" : {
742+ "age" : "22" ,
743+ },
744+ "testuser" : {
745+ "name" : "Bob" ,
746+ }
747+ })
748+ self .reply ("Who am I?" , "You are UltraHAL, seeker!" )
749+ self .reply ("How old am I?" , "You are 22 years old." )
750+ self .assertEqual (self .rs .get_uservar ("testuser" , "name" ), "Bob" )
751+ self .assertEqual (self .rs .get_uservar ("testuser" , "age" ), "undefined" )
752+
753+ # Non-existing users return None, not "undefined"
754+ self .assertEqual (self .rs .get_uservar ("fake" , "name" ), None )
755+
756+ # Test calling with (str, None)
757+ with self .assertRaises (TypeError ):
758+ self .rs .set_uservars ("alice" )
759+
760+ # Test calling with (str, str)
761+ with self .assertRaises (TypeError ):
762+ self .rs .set_uservars ("alice" , "name" )
763+
764+ # Test calling with (dict, dict)
765+ with self .assertRaises (TypeError ):
766+ self .rs .set_uservars ({"localuser" : "hi" }, {"name" : "Alice" })
767+
768+ # Test calling it in many-users mode, where one of the users isn't
769+ # a dict.
770+ with self .assertRaises (TypeError ):
771+ self .rs .set_uservars ({
772+ "localuser" : {
773+ "name" : "Mary" ,
774+ },
775+ "testuser" : "not a dict" ,
776+ })
777+
778+ # Test calling with dict-like objects.
779+ with self .assertRaises (TypeError ):
780+ self .rs .set_uservars ("localuser" , OrderedDict (name = "Alice" ))
781+
782+ # But dict-like objects can be cast as dicts.
783+ testdict = OrderedDict (name = "Joe" )
784+ self .assertEqual (self .rs .set_uservars ("localuser" , dict (testdict )), None )
785+
683786class UnicodeTest (RiveScriptTestCase ):
684787 """UTF-8 Tests."""
685788
0 commit comments