Exemplo n.º 1
0
        public void Optional_Start_End_Full_Middle_Alternation()
        {
            var rs = new RiveScriptEngine(Config.Debug);

            rs.stream(new[] { "+ [*] (when were you born|what is your birthday|what is your bday) [*]",
                              "",
                              "- reply",
                              "" });

            rs.sortReplies();

            rs.reply("when were you born").AssertAreEqual("reply");
            rs.reply("tell when were you born").AssertAreEqual("reply");
            rs.reply("tell when were you born please").AssertAreEqual("reply");
            rs.reply("when were you born please").AssertAreEqual("reply");

            rs.reply("what is your birthday").AssertAreEqual("reply");
            rs.reply("tell what is your birthday").AssertAreEqual("reply");
            rs.reply("tell what is your birthday please").AssertAreEqual("reply");
            rs.reply("what is your birthday please").AssertAreEqual("reply");

            rs.reply("what is your bday").AssertAreEqual("reply");
            rs.reply("tell what is your bday").AssertAreEqual("reply");
            rs.reply("tell what is your bday please").AssertAreEqual("reply");
            rs.reply("what is your bday please").AssertAreEqual("reply");
        }
        public void CurrentUser_Is_ThreadSafe_On_Macro()
        {
            var rs = new RiveScriptEngine(Config.Debug);

            rs.stream(@"+ trigger
                        - <call>currentuser</call>

                        > object currentuser csharp
                            return rs.currentUser();
                        < object");

            rs.sortReplies();


            Action action = () =>
            {
                var threadid = Thread.CurrentThread.ManagedThreadId.ToString();

                var reply = rs.reply(threadid, "trigger");

                Assert.AreEqual(threadid, reply);
            };


            Parallel.Invoke(action, action, action);
        }
        public void RSTS_Substitutions__message_substitutions()
        {
            var rs = new RiveScriptEngine(Config.Debug);

            rs.stream(new[] { "+ whats up",
                              "- nm.",

                              "+ what is up",
                              "- Not much." });

            rs.sortReplies();

            rs.reply("whats up").AssertAreEqual("nm.");
            rs.reply("what's up?").AssertAreEqual("nm.");
            rs.reply("what is up?").AssertAreEqual("Not much.");


            rs.stream(new[] { "! sub whats  = what is",
                              "! sub what's = what is" });

            rs.sortReplies();

            rs.reply("whats up").AssertAreEqual("Not much.");
            rs.reply("what's up?").AssertAreEqual("Not much.");
            rs.reply("what is up?").AssertAreEqual("Not much.");
        }
Exemplo n.º 4
0
        public void Complex_Random_With_Conditionals_And_Arrays_Triggers_UTF8()
        {
            var rs = new RiveScriptEngine(Config.UTF8);

            rs.streamForTest(new[] { "! array oi = olá oi",
                                     "^ boa noite|bom dia",
                                     "",
                                     "+ meu nome é *",
                                     "- <set name=<formal>>Prazer em conhecer, <get name>",
                                     "",
                                     "+ @oi",
                                     "* <get name> != undefined => {random}",
                                     "^ Olá, <get name>.|",
                                     "^ Bom dia, <get name>.{/random}",
                                     "- Olá!",
                                     "- Como vai?", });

            //Without name

            rs.reply("olá")
            .AssertContains("Olá!", "Como vai?");

            //Set name
            rs.reply("meu nome é Fábio")
            .AssertAreEqual("Prazer em conhecer, Fábio");


            rs.reply("bom dia")
            .AssertContains("Olá, Fábio.", "Bom dia, Fábio.");
        }
Exemplo n.º 5
0
        public void Complex_Random_With_Conditionals()
        {
            var rs = new RiveScriptEngine();

            rs.streamForTest(new[] { "+ my name is *",
                                     "- <set name=<formal>>Nice to meet you, <get name>",
                                     "",
                                     "+ hello bot",
                                     "* <get name> != undefined => {random}",
                                     "^ Hello there, <get name>.|",
                                     "^ Hey, <get name>.{/random}",
                                     "- Hello there!",
                                     "- Hi there", });

            //Without name

            rs.reply("hello bot")
            .AssertContains("Hello there!", "Hi there");

            //Set name
            rs.reply("my name is John")
            .AssertAreEqual("Nice to meet you, John");


            rs.reply("hello bot")
            .AssertContains("Hello there, John.", "Hey, John.");
        }
Exemplo n.º 6
0
        public void Execute_Object_Call_Entry_Assembly_Without_Explicit_Reference()
        {
            //Mock entry assembly for test envirioment
            ContextHelper.SetEntryAssembly(typeof(CSharpObjectHandlerTest).Assembly);

            var rs = new RiveScriptEngine();

            rs.stream(new[] { "",
                              "",
                              "",
                              "+ show context data",
                              "- data is: <call>context</call>",
                              "",
                              "> object context csharp",
                              "",
                              "    using RiveScript.Tests.Helper;",
                              "",
                              "    return ContextHelper.Property;",
                              "< object",
                              "" });

            rs.sortReplies();


            rs.reply("show context data")
            .AssertAreEqual("data is: Context_Property");
        }
        public void Load_Older_Version_File()
        {
            var rs = new RiveScriptEngine(Config.Debug);

            var result = rs.stream("! version = 1.8");

            Assert.IsTrue(result);
        }
Exemplo n.º 8
0
        public string Call(string name, RiveScriptEngine rs, string[] args)
        {
            if (!macros.ContainsKey(name))
            {
                return("ERR: Could not find a object code for " + name + ".");
            }

            return(macros[name].Call(rs, args));
        }
Exemplo n.º 9
0
        public static RiveScriptEngine getEmptyStreamed(string[] code)
        {
            var rs = new RiveScriptEngine(Config.Default);

            rs.setDebug(true);
            rs.stream(code);
            rs.sortReplies();
            return(rs);
        }
Exemplo n.º 10
0
        public void Reply_RS_Instance()
        {
            var rs = new RiveScriptEngine();
            var oh = new CSharpObjectHandler(rs);

            oh.Load("test", new string[] { "return rs.GetHashCode().ToString(); " });

            var result = oh.Call("test", rs, new string[] { "" });

            Assert.AreEqual(rs.GetHashCode().ToString(), result);
        }
Exemplo n.º 11
0
        public void Simple_Reply()
        {
            var rs = new RiveScriptEngine(Config.DebugUTF8);

            rs.streamForTest(new[] { "+ olá bot",
                                     "- Olá humano!" });

            var reply = rs.reply("default", "olá bot");

            Assert.AreEqual(reply, "Olá humano!");
        }
Exemplo n.º 12
0
        public void Reply_CurrentUser_Not_Initialized()
        {
            var rs = new RiveScriptEngine();
            var oh = new CSharpObjectHandler(rs);

            oh.Load("test", new string[] { "return rs.currentUser();" });

            var result = oh.Call("test", rs, new string[] { "" });

            Assert.AreEqual(Constants.Undefined, result);
        }
Exemplo n.º 13
0
        public void Reply_Concatenet_Args_Id()
        {
            var rs = new RiveScriptEngine();
            var oh = new CSharpObjectHandler(rs);

            oh.Load("test", new string[] { "return String.Join(\",\", args); " });

            var result = oh.Call("test", rs, new string[] { "1", "2", "3" });

            Assert.AreEqual("1,2,3", result);
        }
Exemplo n.º 14
0
        public void Hello_World_Simple_Code_AndChsrpHandlerIsDefault()
        {
            var rs = new RiveScriptEngine();
            var oh = new CSharpObjectHandler(rs);

            oh.Load("test", new string[] { "return \"Hello world\"; " });

            var result = oh.Call("test", rs, new string[] { "" });

            Assert.AreEqual("Hello world", result);
        }
Exemplo n.º 15
0
        public void Alternation_With_Optional_And_Space_Between()
        {
            var rs = new RiveScriptEngine(Config.Debug);

            rs.stream(new[] { "+ (aa|bb|cc) [bogus]",
                              "- reply" });

            rs.sortReplies();

            rs.reply("aa bogus").AssertAreEqual("reply");
            rs.reply("aabogus").AssertAreNotEqual("reply");
        }
Exemplo n.º 16
0
        public void Simple_Random_Word_End()
        {
            var rs = new RiveScriptEngine();

            rs.streamForTest(new[] { "+ hello bot",
                                     "- Good {random}day|night{/random}" });

            var reply = rs.reply("hello bot");

            Assert.IsTrue(new[] { "Good day",
                                  "Good night" }.Contains(reply));
        }
Exemplo n.º 17
0
        public void Simple_Random_Word_Middle()
        {
            var rs = new RiveScriptEngine();

            rs.streamForTest(new[] { "+ hello bot",
                                     "- Hello {random}good|best{/random} day" });

            var reply = rs.reply("hello bot");

            Assert.IsTrue(new[] { "Hello good day",
                                  "Hello best day" }.Contains(reply));
        }
Exemplo n.º 18
0
        public void Simple_Random_Word_Start()
        {
            var rs = new RiveScriptEngine();

            rs.streamForTest(new[] { "+ hello bot",
                                     "- {random}Good|Best{/random} day" });

            var reply = rs.reply("hello bot");

            Assert.IsTrue(new[] { "Good day",
                                  "Best day" }.Contains(reply));
        }
Exemplo n.º 19
0
        public CSharpObjectHandler(RiveScriptEngine rs, bool tryAddEntryAssembly)
        {
            this.rs = rs ?? throw new ArgumentNullException(nameof(rs), "RiveScript instance muts not be null");

            //Get entry assembly name
            if (tryAddEntryAssembly && Assembly.GetEntryAssembly() != null)
            {
                currentAssembly = System.IO.Path.GetFileName(Assembly.GetEntryAssembly().Location);
            }

            riveAssembly = System.IO.Path.GetFileName(typeof(IObjectHandler).Assembly.Location);
        }
Exemplo n.º 20
0
        public void Load_Simple_File_Non_Common_Extension()
        {
            var rs = new RiveScriptEngine(Config.Debug);

            var result = rs.loadFile("TestData/basic_reply_3.txt");

            Assert.IsTrue(result);

            rs.sortReplies();

            rs.reply("trigger3").AssertAreEqual("reply3");
        }
Exemplo n.º 21
0
        public void Load_Stream_Single_String()
        {
            var rs = new RiveScriptEngine(Config.Debug);

            var result = rs.stream("+ hello bot\n - Hello human!");

            Assert.IsTrue(result);

            rs.sortReplies();

            rs.reply("hello bot").AssertAreEqual("Hello human!");
        }
Exemplo n.º 22
0
        public void RSTS_Options__concat()
        {
            //# The concat option is file scoped and doesn't persist across streams.
            var rs = new RiveScriptEngine(Config.Debug);

            rs.stream(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" });


            rs.stream(new[] { "// concat mode should be restored to the default in a",
                              "// separate file/stream parse",
                              "+ test concat second file",
                              "- Hello",
                              "^ world!" });


            rs.sortReplies();

            rs.reply("test concat default").AssertAreEqual("Helloworld!");
            rs.reply("test concat space").AssertAreEqual("Hello world!");
            rs.reply("test concat none").AssertAreEqual("Helloworld!");
            rs.reply("test concat newline").AssertAreEqual("Hello\nworld!");
            rs.reply("test concat foobar").AssertAreEqual("Helloworld!");
            rs.reply("test concat second file").AssertAreEqual("Helloworld!");
        }
Exemplo n.º 23
0
        public void Simple_Random_Reply()
        {
            var rs = new RiveScriptEngine();

            rs.streamForTest(new[] { "+ hello bot",
                                     "- Hello",
                                     "- Hi" });

            var reply = rs.reply("hello bot");

            Assert.IsTrue(new[] { "Hello",
                                  "Hi" }.Contains(reply));
        }
Exemplo n.º 24
0
        public void Simple_Random_Reply_Alternative_Syntax()
        {
            var rs = new RiveScriptEngine();

            rs.streamForTest(new[] { "+ hello bot",
                                     "- {random}Hello|Hi|Hello human{/random}" });

            var reply = rs.reply("hello bot");

            Assert.IsTrue(new[] { "Hello",
                                  "Hi",
                                  "Hello human" }.Contains(reply));
        }
Exemplo n.º 25
0
        public void RSTS_Unicode__unicode()
        {
            var rs = new RiveScriptEngine(Config.DebugUTF8);

            rs.stream(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." });

            rs.sortReplies();

            rs.reply("äh").AssertAreEqual("What's the matter?");
            rs.reply("ブラッキー").AssertAreEqual("エーフィ");
            rs.reply("knock knock").AssertAreEqual("Who's there?");
            rs.reply("orange").AssertAreEqual("Orange who?");
            rs.reply("banana").AssertAreEqual("Haha! Banana!");
            rs.reply("tëll më ä pöëm").AssertAreEqual("Thërë öncë wäs ä män nämëd Tïm");
            rs.reply("more").AssertAreEqual("Whö nëvër qüïtë lëärnëd höw tö swïm");
            rs.reply("more").AssertAreEqual("Hë fëll öff ä döck, änd sänk lïkë ä röck");
            rs.reply("more").AssertAreEqual("Änd thät wäs thë ënd öf hïm.");
        }
Exemplo n.º 26
0
        public void Load_All_Directory_With_Extension_Specify()
        {
            var rs = new RiveScriptEngine(Config.Debug);

            var result = rs.loadDirectory("TestData", new[] { ".rs" });

            Assert.IsTrue(result);

            rs.sortReplies();

            rs.reply("trigger1").AssertAreEqual("reply1");    //.rs
            rs.reply("trigger2").AssertAreNotEqual("reply2"); //.rive
            rs.reply("trigger3").AssertAreNotEqual("reply3"); //.txt
            rs.reply("trigger4").AssertAreEqual("reply4");    //.rs
        }
Exemplo n.º 27
0
        public void Load_All_Directory_No_Extension_Specify()
        {
            var rs = new RiveScriptEngine(Config.Debug);

            var result = rs.loadDirectory("TestData");

            Assert.IsTrue(result);

            rs.sortReplies();

            rs.reply("trigger1").AssertAreEqual("reply1");
            rs.reply("trigger2").AssertAreEqual("reply2");
            rs.reply("trigger4").AssertAreEqual("reply4");    //Inner folder
            rs.reply("trigger3").AssertAreNotEqual("reply3"); //Non-common extension
        }
Exemplo n.º 28
0
        public void Last_Input_Repetition_UTF8()
        {
            var rs = new RiveScriptEngine(Config.UTF8);

            rs.streamForTest(new[] { "+ <input>",
                                     "- Você não acabou de falar isso?",
                                     "+ olá",
                                     "- Oi" });

            rs.reply("Olá")
            .AssertAreEqual("Oi");

            rs.reply("Olá")
            .AssertAreEqual("Você não acabou de falar isso?");
        }
Exemplo n.º 29
0
        public void Last_Input_Repetition()
        {
            var rs = new RiveScriptEngine(Config.Default);

            rs.streamForTest(new[] { "+ <input>",
                                     "- Didn't you just say that?",
                                     "+ hi",
                                     "- Hello" });

            rs.reply("Hi")
            .AssertAreEqual("Hello");

            rs.reply("Hi")
            .AssertAreEqual("Didn't you just say that?");
        }
Exemplo n.º 30
0
        public void Last_Reply_Repetition()
        {
            var rs = new RiveScriptEngine(Config.Default);

            rs.streamForTest(new[] { "+ <reply>",
                                     "- I just say that",
                                     "+ hi",
                                     "- hello" });

            var r1 = rs.reply("hi");

            Assert.AreEqual("hello", r1);

            rs.reply("hello")
            .AssertAreEqual("I just say that");
        }