public void SetBadRemoteChatProperty() { var code = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name"; code += "\nCHAT c2\nSET $WRONG.staleness=2"; var rt = new ChatRuntime(null); rt.ParseText(code, true); var chat = rt["c1"]; Assert.That(chat, Is.Not.Null); Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Set))); Assert.That(chat.commands[1].GetType(), Is.EqualTo(typeof(Say))); chat.Resolve(globals); var chat2 = rt["c2"]; Assert.That(chat2, Is.Not.Null); Assert.That(chat2.commands[0].GetType(), Is.EqualTo(typeof(Set))); Assert.That(chat.Staleness(), Is.EqualTo(0)); // throw b/c $WRONG.staleness doesn't exist in any scope Assert.Throws <BindException>(() => chat2.Resolve(globals)); }
public void SaveAsync() { var blocker = new AutoResetEvent(false); var file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Util.EpochMs() + ".ser"); var lines = new[] { "CHAT switch {type=a,stage=b,other=c}", "SAY async", }; ChatRuntime rt = new ChatRuntime(Client.AppConfig.TAC); rt.ParseText(String.Join("\n", lines)); rt.SaveAsync(serializer, file, (bytes) => { blocker.Set(); //Console.WriteLine("CB: " + (bytes != null ? bytes.Length + " bytes" : "Failed")); Assert.That(bytes, Is.Not.Null); Assert.That(bytes.Length, Is.GreaterThan(0)); // create a new runtime from the bytes var rt2 = ChatRuntime.Create(serializer, bytes, AppConfig.TAC); // and verify they are the same CheckEquals(rt, rt2); }); blocker.WaitOne(); }
public void SearchHardDashedConstraint() { string[] lines = { "CHAT CORE_Shake {type=sha-ke, stage=CO-RE}", "SAY Core shake!", "CHAT CORE_Tap {type=ta-p, stage=CO-RE}", "SAY Core tap!", "CHAT CORE_Stale_Fast {type=cri-tic, stage=CO-RE}", "SAY Core critic!", "CHAT NV_Shake {type=sha-ke, stage=N-V}", "SAY NV shake!", "CHAT NV_Tap {type=ta-p, stage=N-V}", "SAY NV tap!", "CHAT NV_Stale_Fast {type=cri-tic, stage=N-V}", "SAY NV critic!", }; string contents = String.Join("\n", lines); ChatRuntime rt = new ChatRuntime(AppConfig.TAC); rt.ParseText(contents); var finder = new Find().Init("{!!type=ta-p,!stage=CO-RE}"); var chat = rt.DoFind((Dialogic.Find)finder); Assert.That(chat.text, Is.EqualTo("CORE_Tap")); }
// -------------–-------------–-------------–-------------–-------------–----------- private static Result ParseScript(IDictionary <string, string> kvs) { var code = kvs.ContainsKey("code") ? kvs["code"] : null; var validators = kvs.ContainsKey("useValidators") && kvs["useValidators"].Equals("true"); //if (kvs.ContainsKey("selectionStart")) // TODO: handle in JS //{ // code = kvs["selection"]; // var startIdx = kvs["selectionStart"]; // var endIdx = kvs["selectionEnd"]; //} try { globals = new Dictionary <string, object>(); runtime = new ChatRuntime(Client.AppConfig.TAC); runtime.strictMode = false; // allow unbound symbols/functions runtime.ParseText(code, !validators); // true to disable validators } catch (ParseException ex) { return(Result.Error(code, ex, ex.lineNumber)); } catch (Exception e) { return(Result.Error(code, e, -1)); } return(Result.Success(code)); }
public void SetRemoteChatProperty() { var code = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name"; code += "\nCHAT c2\nSET $c1.staleness=2"; var rt = new ChatRuntime(null); rt.ParseText(code, true); var chat = rt["c1"]; Assert.That(chat, Is.Not.Null); Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Set))); Assert.That(chat.commands[1].GetType(), Is.EqualTo(typeof(Say))); chat.Resolve(globals); var chat2 = rt["c2"]; Assert.That(chat2, Is.Not.Null); Assert.That(chat2.commands[0].GetType(), Is.EqualTo(typeof(Set))); Assert.That(chat.Staleness(), Is.EqualTo(0)); chat2.Resolve(globals); Assert.That(chat.Staleness(), Is.EqualTo(2)); }
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 RawCharacters() { ChatRuntime rt; string s; Resolver.DBUG = false; string[] test = new[] { "SAY The = op", "SAY cute**", "SAY <wait>", "SAY ^-^", "SAY =_=" }; for (int i = 0; i < test.Length; i++) { rt = new ChatRuntime(); rt.ParseText(test[i], true); s = rt.InvokeImmediate(globals); Console.WriteLine(i + ": '" + s + "' ?= '" + test[i].Substring(4) + "'"); Assert.That(s.Equals(test[i].Substring(4))); } }
public void SerializeToJSON() { ChatRuntime rt = new ChatRuntime(); rt.ParseText("CHAT Test { type = a,stage = b}"); Assert.That(rt.ToJSON(serializer), Is.Not.Null); }
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.INV), 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 SetRemoteChatNonProperty() { var code = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name"; code += "\nCHAT c2\nSET $c1.happiness=2"; var rt = new ChatRuntime(null); rt.ParseText(code, true); var chat = rt["c1"]; Assert.That(chat, Is.Not.Null); Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Set))); Assert.That(chat.commands[1].GetType(), Is.EqualTo(typeof(Say))); chat.Resolve(globals); var chat2 = rt["c2"]; Assert.That(chat2, Is.Not.Null); Assert.That(chat2.commands[0].GetType(), Is.EqualTo(typeof(Set))); Assert.That(chat.Staleness(), Is.EqualTo(0)); // throw b/c we only allow setting of persistent properties // (staleness, etc) on remote chats Assert.Throws <BindException>(() => chat2.Resolve(globals)); }
public void MergeAsync() { var blocker = new AutoResetEvent(false); var lines = new[] { "CHAT Test {type=a,stage=b}", "SET ab = hello", "DO flip", "SAY ab", }; var file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Util.EpochMs() + ".ser"); ChatRuntime rt = new ChatRuntime(Client.AppConfig.TAC); rt.ParseText(String.Join("\n", lines)); rt.Save(serializer, file); ChatRuntime rt2 = new ChatRuntime(Client.AppConfig.TAC); rt2.MergeAsync(serializer, file, () => { blocker.Set(); //Console.WriteLine("CALLBACK: "+ (rt2.chats != null ? rt2.chats.Count + " chats" : "Failed")); Assert.That(rt2.chats, Is.Not.Null); Assert.That(rt2.chats.Count, Is.GreaterThan(0)); }); blocker.WaitOne(); }
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 ImmediateGoTest() { string[] lines = { "CHAT c", "SAY hello $animal.", "SAY ok.", "GO c1", "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 dog.\nok.\ngoodbye.")); s = rt.InvokeImmediate(globals, "c"); Assert.That(s, Is.EqualTo("hello dog.\nok.\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.\nok.\ngoodbye.")); }
public void RuntimeIndexTest() { var rt = new ChatRuntime(); rt.ParseText("CHAT c1\nSAY OK"); Assert.That(rt.Chats()[0], Is.Not.Null); Assert.That(rt["c1"], Is.Not.Null); // ??? }
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 SaveMultipleAsync() { var blocker1 = new AutoResetEvent(false); var file1 = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Util.EpochMs() + ".ser"); var lines1 = new[] { "CHAT switch {type=a1,stage=b1,other=async1}", "SAY async1", }; ChatRuntime rt1 = new ChatRuntime(Client.AppConfig.TAC); rt1.ParseText(String.Join("\n", lines1)); //var blocker2 = new AutoResetEvent(false); var file2 = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Util.EpochMs() + ".ser"); var lines2 = new[] { "CHAT switch2 {type=a2,stage=2,other=async2}", "DO #flip", "SAY async2", }; ChatRuntime rt2 = new ChatRuntime(Client.AppConfig.TAC); rt2.ParseText(String.Join("\n", lines2)); rt1.SaveAsync(serializer, file1, (bytes) => { Thread.Sleep(50); blocker1.Set(); //Console.WriteLine("CB1: " + (bytes != null ? bytes.Length + " bytes" : "Failed")); Assert.That(bytes, Is.Not.Null); Assert.That(bytes.Length, Is.GreaterThan(0)); // create a new runtime from the bytes var rtDeser1 = ChatRuntime.Create(serializer, bytes, AppConfig.TAC); // and verify they are the same CheckEquals(rt1, rtDeser1); }); rt2.SaveAsync(serializer, file2, (bytes) => { //blocker2.Set(); //Console.WriteLine("CB2: " + (bytes != null ? bytes.Length + " bytes" : "Failed")); Assert.That(bytes, Is.Not.Null); Assert.That(bytes.Length, Is.GreaterThan(0)); // create a new runtime from the bytes var rtDeser2 = ChatRuntime.Create(serializer, bytes, AppConfig.TAC); // and verify they are the same CheckEquals(rt2, rtDeser2); }); blocker1.WaitOne(); //blocker2.WaitOne(); }
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 SaveAndRestoreChatWithAsk() { 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 SetChatLocalPath() { var code = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name"; code += "\nCHAT c2\nSET $c1.staleness=2"; var rt = new ChatRuntime(null); rt.ParseText(code, true); var chat = rt["c1"]; Assert.That(chat, Is.Not.Null); Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Set))); Assert.That(chat.commands[1].GetType(), Is.EqualTo(typeof(Say))); chat.Resolve(globals); var chat2 = rt["c2"]; Assert.That(chat2, Is.Not.Null); Assert.That(chat2.commands[0].GetType(), Is.EqualTo(typeof(Set))); Assert.That(chat.Staleness(), Is.EqualTo(Defaults.CHAT_STALENESS)); Assert.That(Convert.ToDouble(chat.GetMeta(Meta.STALENESS)), Is.EqualTo(Defaults.CHAT_STALENESS)); chat2.Resolve(globals); Assert.That(chat.Staleness(), Is.EqualTo(2)); Assert.That(Convert.ToDouble(chat.GetMeta(Meta.STALENESS)), Is.EqualTo(2)); code = "CHAT c1\nSET $fish.name=Mary\nSAY Hi $fish.name"; code += "\nCHAT c2\nSET $c1.stalenessIncr=2"; rt = new ChatRuntime(null); rt.ParseText(code, true); chat = rt["c1"]; Assert.That(chat, Is.Not.Null); Assert.That(chat.commands[0].GetType(), Is.EqualTo(typeof(Set))); Assert.That(chat.commands[1].GetType(), Is.EqualTo(typeof(Say))); chat.Resolve(globals); chat2 = rt["c2"]; Assert.That(chat2, Is.Not.Null); Assert.That(chat2.commands[0].GetType(), Is.EqualTo(typeof(Set))); // no need to check metadata, except for staleness Assert.That(chat.StalenessIncr(), Is.EqualTo(Defaults.CHAT_STALENESS_INCR)); chat2.Resolve(globals); Assert.That(chat.StalenessIncr(), Is.EqualTo(2)); }
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" })); } }
private static string DoSay(ChatRuntime rt, string s) { var globs = new Dictionary <string, object> { { "ant", "hello" } }; rt.chats = new Dictionary <string, Chat>(); rt.ParseText(s); Say say = (Dialogic.Say)rt.Chats().First().commands.First(); say.Resolve(globs); s = say.Text(); //Console.WriteLine(s); return(s); }
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 ContinueAskTest() { string[] lines = { "CHAT c", "ASK Hello?", "OPT Yes", "OPT No", "SAY Ok" }; ChatRuntime rt = new ChatRuntime(); rt.ParseText(String.Join("\n", lines)); var s = rt.InvokeImmediate(globals); Assert.That(s, Is.EqualTo("Hello?\n[No]\nOk").Or.EqualTo("Hello?\n[Yes]\nOk")); }
public void ImmediateAskTest() { string[] lines = { "CHAT c", "SAY hello $animal.", "ASK are you a $animal?", "OPT Yes #c2", "OPT No #c2", "CHAT c1 {a=b}", "SAY nope.", "CHAT c2 {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.")); //Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\ngoodbye.")); Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\n[No]\ngoodbye.") .Or.EqualTo("hello dog.\nare you a dog?\n[Yes]\ngoodbye.")); s = rt.InvokeImmediate(globals, "c"); Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\n[No]\ngoodbye.") .Or.EqualTo("hello dog.\nare you a dog?\n[Yes]\ngoodbye.")); //Assert.That(s, Is.EqualTo("hello dog.\nare you a dog?\ngoodbye.")); s = rt.InvokeImmediate(globals, "c2"); 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?\n[No]\ngoodbye.") .Or.EqualTo("hello $animal.\nare you a $animal?\n[Yes]\ngoodbye.")); }
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")); }