public void ImmediateFindTest() { string[] lines = { "CHAT c", "SAY hello $animal.", "ASK are you a $animal?", "FIND {a=b}", "CHAT c1 {a=b}", "SAY goodbye." }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines)); var s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\ngoodbye.")); s = rt.InvokeImmediate(globals, "c"); Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\ngoodbye.")); s = rt.InvokeImmediate(globals, "c1"); Assert.That(s, Is.EqualTo("goodbye.")); Assert.Throws <UnboundSymbol>(() => rt.InvokeImmediate(null)); ChatRuntime.SILENT = true; rt.strictMode = false; s = rt.InvokeImmediate(null); ChatRuntime.SILENT = false; Assert.That(s, Is.EqualTo("hello $animal.\nare you a $animal?\ngoodbye.")); }
public void GroupsWithMissingSymbol() { ChatRuntime rt; string txt; txt = "a | b | c"; rt = new ChatRuntime(); rt.ParseText(txt); Resolver.DBUG = false; for (int i = 0; i < 5; i++) { var s = rt.InvokeImmediate(globals); //Console.WriteLine("#" + i + ": " + s); Assert.That(s.IsOneOf(new[] { "a", "b", "c" })); } txt = "CHAT c1\nSET a = $object | $object.Call() | honk\nSAY $a"; rt = new ChatRuntime(); rt.ParseText(txt); //Resolver.DBUG = true; rt.strictMode = false; ChatRuntime.SILENT = true; for (int i = 0; i < 5; i++) { var s = rt.InvokeImmediate(globals); //Console.WriteLine("#"+i+": "+s); Assert.That(s.IsOneOf(new[] { "$object", "$object.Call()", "honk" })); } ChatRuntime.SILENT = false; }
public void MultiTransformResolution() { var str = "(well|well).cap() don't you look (anger).emoadj()."; var rt = new ChatRuntime(Client.AppConfig.TAC); rt.ParseText(str); //rt.strictMode = false; var s = rt.InvokeImmediate(globals); //Console.WriteLine(s); Assert.That(s.StartsWith("Well don't you look ", Util.IC), Is.True); Assert.That(s.Contains("anger"), Is.False); Assert.That(s.Contains("emoadj"), Is.False); str = "The (dog|).Cap() ran."; rt = new ChatRuntime(Client.AppConfig.TAC); rt.ParseText(str); //rt.strictMode = false; for (int i = 0; i < 5; i++) { s = rt.InvokeImmediate(globals); //Console.WriteLine(i + ") " + s); Assert.That(s.IsOneOf("The Dog ran.", "The ran.")); } }
public void SearchFailWithConstraint() { var lines = new[] { "CHAT Test {type=a,stage=b}", "SAY Find", "FIND {type=a,x=b}", "CHAT next {type=a,stage=b}", "SAY Done", }; ChatRuntime rt = new ChatRuntime(AppConfig.TAC); rt.ParseText(String.Join("\n", lines)); var s = rt.InvokeImmediate(null); Assert.That(s, Is.EqualTo("Find\nDone")); //FuzzySearch.DBUG = true; lines = new[] { "CHAT Test {type=a,stage=b}", "SAY Find", "FIND {type=a,staleness<5}", "CHAT next {type=a,stage=b,staleness=5}", "SAY Done", }; rt = new ChatRuntime(AppConfig.TAC); rt.ParseText(String.Join("\n", lines)); s = rt.InvokeImmediate(null); Assert.That(s, Is.EqualTo("Find\nDone")); }
public void BoundedVarSolutions() { ChatRuntime rt; string[] lines; string s; lines = new[] { "CHAT c1", "SET doop = bop", "SET deep = beep", "SAY [a=$deep][b=$doop]" }; rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("beepbop")); lines = new[] { "CHAT c1", "SET doop = bop", "SET deep = beep", "SAY ($deep)-($doop)" }; rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("beep-bop")); lines = new[] { "CHAT c1", "SET doop = bop", "SET deep = beep", "SAY ($deep)($doop)" }; rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("beepbop")); lines = new[] { "CHAT c1", "SET doop = bop", "SET deep = beep", "SAY [a=$deep]+[b=$doop]" }; rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("beep+bop")); }
public void SubstringIssue() { ChatRuntime rt = new ChatRuntime(); //Resolver.DBUG = true; rt.ParseText("SET $a = A\nSET $b = $a1\nSET $a1 = B\nSAY $a $b\n", true); var s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("A B")); }
public void SaveRestoreChatWithAsk() { var lines = new[] { "CHAT Test {type=a,stage=b}", "ASK Is it ok?", "OPT yes #next ", "OPT no #next", "CHAT next {type=a,stage=b}", "SAY Done", }; Chat c1, c2; ChatRuntime rtOut, rtIn; var text = String.Join("\n", lines); rtIn = new ChatRuntime(Client.AppConfig.TAC); rtIn.ParseText(text); // serialize the runtime to bytes var bytes = serializer.ToBytes(rtIn); // create a new runtime from the bytes rtOut = ChatRuntime.Create(serializer, bytes, AppConfig.TAC); // check they are identical Assert.That(rtIn, Is.EqualTo(rtOut)); // double-check the chats themselves c1 = rtIn.Chats().First(); c2 = rtOut.Chats().First(); //Console.WriteLine(c1.ToTree()+"\n\n"+c2.ToTree()); Assert.That(c1, Is.EqualTo(c2)); Assert.That(c1.ToTree(), Is.EqualTo(c2.ToTree())); Assert.That(c1.text, Is.EqualTo(c2.text)); for (int i = 0; i < c1.commands.Count; i++) { var cmd1 = c1.commands[i]; Assert.That(cmd1.parent, Is.Not.Null); var cmd2 = c2.commands[i]; Assert.That(cmd2.parent, Is.Not.Null); Assert.That(c1.commands[i], Is.EqualTo(c2.commands[i])); } // no dynamics, so output should be the same var res1 = rtIn.InvokeImmediate(globals); var res2 = rtOut.InvokeImmediate(globals); Assert.That(res1, Is.EqualTo(res2)); }
public void ValidateParensTest() { string[] lines = new[] { "CHAT c1", "SET ab = (a | b))", "SAY $ab" }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); Assert.Throws <MismatchedParens>(() => rt.InvokeImmediate(globals)); }
public void ImmediateTransformFails() { string[] lines = { "CHAT c", "SAY hello (a | a).noFun(),", "SAY ok.", }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines)); Assert.Throws <UnboundFunction>(() => rt.InvokeImmediate(null)); //ChatRuntime.SILENT = true; rt.strictMode = false; var s = rt.InvokeImmediate(globals); //ChatRuntime.SILENT = false; Assert.That(s, Is.EqualTo("hello a.noFun(),\nok.")); }
public void PartialTransformIssue() { ChatRuntime rt = new ChatRuntime(); rt.ParseText("SET $test = (a) (b)\nSAY $test.Cap()", true); Assert.That(rt.InvokeImmediate(globals), Is.EqualTo("A b")); Resolver.DBUG = false; rt = new ChatRuntime(); rt.ParseText("SET $test = (a | a) (b | b)\nSAY $test.Cap()", true); Assert.That(rt.InvokeImmediate(globals), Is.EqualTo("A b")); }
public void ATransformWithinAChoice() { var txt = "CHAT c1\nSET a = a ($animal.Cap() | $prep.Cap())\nSAY $a"; ChatRuntime rt = new ChatRuntime(); rt.ParseText(txt); Resolver.DBUG = false; rt.strictMode = false; for (int i = 0; i < 1; i++) { var s = rt.InvokeImmediate(globals); Assert.That(s.IsOneOf(new[] { "a Dog", "a Then" })); } }
public void AppendNewChats() { var lines = new[] { "CHAT Test {type=a,stage=b}", "SAY Find", "FIND {type=a,stage=b,other=c}", "CHAT next {type=a,stage=b}", "SAY Done", }; ChatRuntime rt; rt = new ChatRuntime(Client.AppConfig.TAC); rt.ParseText(String.Join("\n", lines)); var s = rt.InvokeImmediate(null); Assert.That(s, Is.EqualTo("Find\nDone")); // Add more chats via Update, with higher search score var lines2 = new[] { "CHAT switch {type=a,stage=b,other=c,statelness=}", "SAY Added", }; ChatRuntime rt2 = new ChatRuntime(Client.AppConfig.TAC); rt2.ParseText(String.Join("\n", lines2)); // append the 2nd runtime to the first rt.UpdateFrom(serializer, rt2); s = rt.InvokeImmediate(null); Assert.That(s, Is.EqualTo("Find\nAdded")); }
public void ImmediateSimpleLoop() { var lines = new[] { "CHAT Test {type=a,stage=b}", "SAY Find", "GO #Test" }; ChatRuntime rt = new ChatRuntime(Client.AppConfig.TAC); rt.ParseText(String.Join("\n", lines)); var s = rt.InvokeImmediate(null); // Note: exits before GO to avoid infinite loop Assert.That(s, Is.EqualTo("Find\nFind\n\nGO #Test looped")); }
public void PreloadingTest() { string[] lines = new[] { "CHAT c1", "SET ab = hello", "SAY $ab $de", "CHAT c2 {preload=true}", "SET $de = preload", }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); rt.Preload(globals); var s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("hello preload")); }
public void ImmediateModeResume() { string[] lines = { "CHAT c", "SAY hello", "GO #c1", "SAY ok", "CHAT c1", "SAY goodbye" }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines)); var s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("hello\ngoodbye\nok")); }
public void ImmediateTripleLoop() { string[] lines = { "CHAT c1", "SAY C1", "GO #c2", "CHAT c2", "SAY C2", "GO #c1", }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines)); var s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("C1\nC2\nC1\n\nGO #c2 looped")); }
public void PreloadingBindingFunc() { string[] lines = new[] { "CHAT c1", "SAY $d $e", "CHAT c2 {preload=true}", "SET $d = hello", "SET $e = $emotion.Cap()", }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); rt.Preload(globals); globals.Add("emotion", "darkness"); var s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("hello Darkness")); }
public void ImmediateWithDynamicGo() { string[] lines = { "CHAT c1", "GO #(line1|line2)", "CHAT line1", "Done", "CHAT line2", "Done" }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines)); var s = rt.InvokeImmediate(globals); //Console.WriteLine(s); Assert.That(s, Is.EqualTo("Done")); }
public void ImmediateGoFails() { string[] lines = { "CHAT c", "SAY hello $animal.", "SAY ok.", "GO c2", "CHAT c1", "SAY goodbye." }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines)); ChatRuntime.SILENT = true; rt.strictMode = false; var s = rt.InvokeImmediate(globals); ChatRuntime.SILENT = false; Assert.That(s, Is.EqualTo("hello dog.\nok.\n\nGO #c2 failed")); }
public void ParensCheck() { string[] lines; ChatRuntime rt; string s; Resolver.DBUG = false; lines = new[] { "CHAT c", "SET $aa = Word", "SET $bb = (A | B) | (A | B)", "SAY $aa $bb" }; rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); for (int i = 0; i < 10; i++) { s = rt.InvokeImmediate(globals); //Console.WriteLine(i+") "+s); Assert.That(s.IsOneOf("Word A", "Word B")); } }
public void DynamicRules() { string[] lines; ChatRuntime rt; string s; Resolver.DBUG = false; lines = new[] { "CHAT c", "SET $emo = anger", "SAY ($emo)_rule" }; rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("anger_rule")); lines = new[] { "CHAT c", "SET $emo = a|a", "SAY $$emo" }; rt = new ChatRuntime(); rt.strictMode = false; rt.ParseText(String.Join("\n", lines), true); s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("A")); lines = new[] { "CHAT c", "SET $emo = a|a", "SAY $($emo)." }; rt = new ChatRuntime(); rt.strictMode = false; rt.ParseText(String.Join("\n", lines), true); s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("A.")); lines = new[] { "CHAT c", "SET $emo = anger", "SAY $($emo)_rule" }; rt = new ChatRuntime(); rt.strictMode = false; rt.ParseText(String.Join("\n", lines), true); ChatRuntime.SILENT = true; s = rt.InvokeImmediate(globals); ChatRuntime.SILENT = false; Assert.That(s, Is.EqualTo("$anger_rule")); lines = new[] { "CHAT c", "SET $emo = anger", "SET start = $($emo)_rule", "SET anger_rule = I am angry", "SET happy_rule = I am happy", "SAY $start" }; rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines), true); s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("I am angry")); }