コード例 #1
0
        public void TestBasic()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("2.lg"));

            var evaled  = lgFile.EvaluateTemplate("wPhrase");
            var options = new List <string> {
                "Hi", "Hello", "Hiya"
            };

            Assert.IsTrue(options.Contains(evaled), $"The result `{evaled}` is not in those options [{string.Join(",", options)}]");
        }
コード例 #2
0
        public void TestIfElseTemplate()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("5.lg"));

            string evaled = lgFile.EvaluateTemplate("time-of-day-readout", new { timeOfDay = "morning" }).ToString();

            Assert.IsTrue(evaled == "Good morning" || evaled == "Morning! ", $"Evaled is {evaled}");

            evaled = lgFile.EvaluateTemplate("time-of-day-readout", new { timeOfDay = "evening" }).ToString();
            Assert.IsTrue(evaled == "Good evening" || evaled == "Evening! ", $"Evaled is {evaled}");
        }
コード例 #3
0
        public void TestBasicTemplateReference()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("3.lg"));

            var evaled  = lgFile.EvaluateTemplate("welcome-user", null);
            var options = new List <string> {
                "Hi", "Hello", "Hiya", "Hi :)", "Hello :)", "Hiya :)"
            };

            Assert.IsTrue(options.Contains(evaled), $"The result {evaled} is not in those options [{string.Join(",", options)}]");
        }
コード例 #4
0
        public void TestBasicSwitchCaseTemplate()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("switchcase.lg"));

            string evaled = lgFile.EvaluateTemplate("greetInAWeek", new { day = "Saturday" }).ToString();

            Assert.IsTrue(evaled == "Happy Saturday!");

            evaled = lgFile.EvaluateTemplate("greetInAWeek", new { day = "Monday" }).ToString();
            Assert.IsTrue(evaled == "Work Hard!");
        }
コード例 #5
0
        public void TestBasicTemplateRefAndEntityRef()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("4.lg"));

            var userName = "******";
            var evaled   = lgFile.EvaluateTemplate("welcome-user", new { userName = userName }).ToString();
            var options  = new List <string> {
                "Hi", "Hello", "Hiya ", "Hi :)", "Hello :)", "Hiya  :)"
            };

            Assert.IsTrue(evaled.Contains(userName), $"The result {evaled} does not contiain `{userName}`");
        }
コード例 #6
0
        public void TestTemplateRef()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("TemplateRef.lg"));

            var scope = new
            {
                time = "morning",
                name = "Dong Lei"
            };

            Assert.AreEqual(lgFile.EvaluateTemplate("Hello", scope), "Good morning Dong Lei");
        }
コード例 #7
0
        public void TestAnalyzer()
        {
            var testData = new object[]
            {
                new
                {
                    name               = "orderReadOut",
                    variableOptions    = new string[] { "orderType", "userName", "base", "topping", "bread", "meat" },
                    templateRefOptions = new string[] { "wPhrase", "pizzaOrderConfirmation", "sandwichOrderConfirmation" }
                },
                new
                {
                    name               = "sandwichOrderConfirmation",
                    variableOptions    = new string[] { "bread", "meat" },
                    templateRefOptions = new string[] { }
                },
                new
                {
                    name = "template1",

                    // TODO: input.property should really be: customer.property but analyzer needs to be
                    variableOptions    = new string[] { "alarms", "customer", "tasks[0]", "age", "city" },
                    templateRefOptions = new string[] { "template2", "template3", "template4", "template5", "template6" }
                },
                new
                {
                    name               = "coffee-to-go-order",
                    variableOptions    = new string[] { "coffee", "userName", "size", "price" },
                    templateRefOptions = new string[] { "wPhrase", "LatteOrderConfirmation", "MochaOrderConfirmation", "CuppuccinoOrderConfirmation" }
                },
                new
                {
                    name               = "structureTemplate",
                    variableOptions    = new string[] { "text", "newText" },
                    templateRefOptions = new string[] { "ST2" }
                },
            };

            foreach (var testItem in testData)
            {
                var lgFile                = LGParser.ParseFile(GetExampleFilePath("analyzer.lg"));
                var evaled1               = lgFile.AnalyzeTemplate(testItem.GetType().GetProperty("name").GetValue(testItem).ToString());
                var variableEvaled        = evaled1.Variables;
                var variableEvaledOptions = testItem.GetType().GetProperty("variableOptions").GetValue(testItem) as string[];
                Assert.AreEqual(variableEvaledOptions.Length, variableEvaled.Count);
                variableEvaledOptions.ToList().ForEach(element => Assert.AreEqual(variableEvaled.Contains(element), true));
                var templateEvaled        = evaled1.TemplateReferences;
                var templateEvaledOptions = testItem.GetType().GetProperty("templateRefOptions").GetValue(testItem) as string[];
                Assert.AreEqual(templateEvaledOptions.Length, templateEvaled.Count);
                templateEvaledOptions.ToList().ForEach(element => Assert.AreEqual(templateEvaled.Contains(element), true));
            }
        }
コード例 #8
0
        public void TestAnalyzelgTemplateFunction()
        {
            var lgFile         = LGParser.ParseFile(GetExampleFilePath("lgTemplate.lg"));
            var evaled         = lgFile.AnalyzeTemplate("TemplateD");
            var variableEvaled = evaled.Variables;
            var options        = new List <string>()
            {
                "b"
            };

            Assert.AreEqual(variableEvaled.Count, options.Count);
            options.ForEach(e => Assert.AreEqual(variableEvaled.Contains(e), true));
        }
コード例 #9
0
        public void TestRegex()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("Regex.lg"));
            var evaled = lgFile.EvaluateTemplate("wPhrase", string.Empty);

            Assert.AreEqual(evaled, "Hi");

            evaled = lgFile.EvaluateTemplate("wPhrase", new { name = "jack" });
            Assert.AreEqual(evaled, "Hi jack");

            evaled = lgFile.EvaluateTemplate("wPhrase", new { name = "morethanfive" });
            Assert.AreEqual(evaled, "Hi");
        }
コード例 #10
0
        public void TestBasicTemplateRefWithParameters()
        {
            var    lgFile = LGParser.ParseFile(GetExampleFilePath("6.lg"));
            string evaled = lgFile.EvaluateTemplate("welcome", null).ToString();

            Assert.IsTrue(evaled == "Hi DongLei :)" ||
                          evaled == "Hey DongLei :)" ||
                          evaled == "Hello DongLei :)");

            evaled = lgFile.EvaluateTemplate("welcome", new { userName = "******" }).ToString();
            Assert.IsTrue(evaled == "Hi DL :)" ||
                          evaled == "Hey DL :)" ||
                          evaled == "Hello DL :)");
        }
コード例 #11
0
        public void TestIsTemplateFunction()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("IsTemplate.lg"));

            var evaled = lgFile.EvaluateTemplate("template2", new { templateName = "template1" });

            Assert.AreEqual("template template1 exists", evaled);

            evaled = lgFile.EvaluateTemplate("template2", new { templateName = "wPhrase" });
            Assert.AreEqual("template wPhrase exists", evaled);

            evaled = lgFile.EvaluateTemplate("template2", new { templateName = "xxx" });
            Assert.AreEqual("template xxx does not exist", evaled);
        }
コード例 #12
0
        public void TestLGResource()
        {
            var lgResource = LGParser.Parse(File.ReadAllText(GetExampleFilePath("2.lg")));

            Assert.AreEqual(lgResource.Templates.Count, 1);
            Assert.AreEqual(lgResource.Imports.Count, 0);
            Assert.AreEqual(lgResource.Templates[0].Name, "wPhrase");
            Assert.AreEqual(lgResource.Templates[0].Body.Replace("\r\n", "\n"), "- Hi\n- Hello\n- Hiya\n- Hi");

            lgResource = lgResource.AddTemplate("newtemplate", new List <string> {
                "age", "name"
            }, "- hi ");
            Assert.AreEqual(lgResource.Templates.Count, 2);
            Assert.AreEqual(lgResource.Imports.Count, 0);
            Assert.AreEqual(lgResource.Templates[1].Name, "newtemplate");
            Assert.AreEqual(lgResource.Templates[1].Parameters.Count, 2);
            Assert.AreEqual(lgResource.Templates[1].Parameters[0], "age");
            Assert.AreEqual(lgResource.Templates[1].Parameters[1], "name");
            Assert.AreEqual(lgResource.Templates[1].Body, "- hi ");

            lgResource = lgResource.AddTemplate("newtemplate2", null, "- hi2 ");
            Assert.AreEqual(lgResource.Templates.Count, 3);
            Assert.AreEqual(lgResource.Templates[2].Name, "newtemplate2");
            Assert.AreEqual(lgResource.Templates[2].Body, "- hi2 ");

            lgResource = lgResource.UpdateTemplate("newtemplate", "newtemplateName", new List <string> {
                "newage", "newname"
            }, "- new hi\r\n#hi");
            Assert.AreEqual(lgResource.Templates.Count, 3);
            Assert.AreEqual(lgResource.Imports.Count, 0);
            Assert.AreEqual(lgResource.Templates[1].Name, "newtemplateName");
            Assert.AreEqual(lgResource.Templates[1].Parameters.Count, 2);
            Assert.AreEqual(lgResource.Templates[1].Parameters[0], "newage");
            Assert.AreEqual(lgResource.Templates[1].Parameters[1], "newname");
            Assert.AreEqual(lgResource.Templates[1].Body, "- new hi\r\n- #hi");

            lgResource = lgResource.UpdateTemplate("newtemplate2", "newtemplateName2", new List <string> {
                "newage2", "newname2"
            }, "- new hi\r\n#hi2");
            Assert.AreEqual(lgResource.Templates.Count, 3);
            Assert.AreEqual(lgResource.Imports.Count, 0);
            Assert.AreEqual(lgResource.Templates[2].Name, "newtemplateName2");
            Assert.AreEqual(lgResource.Templates[2].Body, "- new hi\r\n- #hi2");

            lgResource = lgResource.DeleteTemplate("newtemplateName");
            Assert.AreEqual(lgResource.Templates.Count, 2);

            lgResource = lgResource.DeleteTemplate("newtemplateName2");
            Assert.AreEqual(lgResource.Templates.Count, 1);
        }
コード例 #13
0
        public void TestBasicConditionalTemplateWithoutDefault()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("5.lg"));

            string evaled = lgFile.EvaluateTemplate("time-of-day-readout-without-default", new { timeOfDay = "morning" }).ToString();

            Assert.IsTrue(evaled == "Good morning" || evaled == "Morning! ", $"Evaled is {evaled}");

            evaled = lgFile.EvaluateTemplate("time-of-day-readout-without-default2", new { timeOfDay = "morning" }).ToString();
            Assert.IsTrue(evaled == "Good morning" || evaled == "Morning! ", $"Evaled is {evaled}");

            object evaledNull = lgFile.EvaluateTemplate("time-of-day-readout-without-default2", new { timeOfDay = "evening" });

            Assert.IsNull(evaledNull, "Evaled is not null");
        }
コード例 #14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleMultiLangGenerator"/> class.
        /// </summary>
        /// <param name="localeLGFiles">A dictionary of locale and LG file.</param>
        public SimpleMultiLangGenerator(Dictionary <string, string> localeLGFiles)
        {
            lgFilesPerLocale       = new Dictionary <string, LGFile>(StringComparer.OrdinalIgnoreCase);
            languageFallbackPolicy = new LanguagePolicy();

            if (localeLGFiles == null)
            {
                throw new ArgumentNullException(nameof(localeLGFiles));
            }

            foreach (var filesPerLocale in localeLGFiles)
            {
                lgFilesPerLocale[filesPerLocale.Key] = LGParser.ParseFile(filesPerLocale.Value);
            }
        }
コード例 #15
0
        public void TestEvaluateOnce()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("EvaluateOnce.lg"));

            var evaled = lgFile.EvaluateTemplate("templateWithSameParams", new { param = "ms" });

            Assert.IsNotNull(evaled);

            var resultList = evaled.ToString().Split(" ");

            Assert.IsTrue(resultList.Length == 2);
            Assert.IsTrue(resultList[0] == resultList[1]);

            // may be has different values
            evaled = lgFile.EvaluateTemplate("templateWithDifferentParams", new { param1 = "ms", param2 = "newms" });
        }
コード例 #16
0
        public void TestNullTolerant()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("NullTolerant.lg"));

            var evaled = lgFile.EvaluateTemplate("template1");

            Assert.AreEqual("null", evaled);

            evaled = lgFile.EvaluateTemplate("template2");

            Assert.AreEqual("result is 'null'", evaled);

            var jObjEvaled = lgFile.EvaluateTemplate("template3") as JObject;

            Assert.AreEqual("null", jObjEvaled["key1"]);
        }
コード例 #17
0
        public void TestlgTemplateFunction()
        {
            var lgFile  = LGParser.ParseFile(GetExampleFilePath("lgTemplate.lg"));
            var evaled  = lgFile.EvaluateTemplate("TemplateC", string.Empty);
            var options = new List <string> {
                "Hi", "Hello"
            };

            Assert.AreEqual(options.Contains(evaled), true);

            evaled  = lgFile.EvaluateTemplate("TemplateD", new { b = "morning" });
            options = new List <string> {
                "Hi morning", "Hello morning"
            };
            Assert.AreEqual(options.Contains(evaled), true);
        }
コード例 #18
0
        public void TestLoopScope()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("LoopScope.lg"));

            var loopClass1 = new LoopClass();

            loopClass1.Name = "jack";

            var loopClass2 = new LoopClass();

            loopClass2.Name = "jones";

            loopClass1.LoopObj = loopClass2;
            loopClass2.LoopObj = loopClass1;

            lgFile.EvaluateTemplate("template1", new { scope = loopClass1 });
        }
コード例 #19
0
        public void TestImportLgFiles()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("import.lg"));

            // Assert 6.lg is imported only once when there are several relative paths which point to the same file.
            // Assert import cycle loop is handled well as expected when a file imports itself.
            Assert.AreEqual(14, lgFile.AllTemplates.Count());

            string evaled = lgFile.EvaluateTemplate("basicTemplate", null).ToString();

            Assert.IsTrue(evaled == "Hi" || evaled == "Hello");

            evaled = lgFile.EvaluateTemplate("welcome", null).ToString();
            Assert.IsTrue(evaled == "Hi DongLei :)" ||
                          evaled == "Hey DongLei :)" ||
                          evaled == "Hello DongLei :)");

            evaled = lgFile.EvaluateTemplate("template3", null).ToString();
            Assert.IsTrue(evaled == "Hi 2" || evaled == "Hello 2");

            evaled = lgFile.EvaluateTemplate("welcome", new { userName = "******" }).ToString();
            Assert.IsTrue(evaled == "Hi DL :)" ||
                          evaled == "Hey DL :)" ||
                          evaled == "Hello DL :)");

            evaled = lgFile.EvaluateTemplate("basicTemplate2", null).ToString();
            Assert.IsTrue(evaled == "Hi 2" || evaled == "Hello 2");

            // Assert 6.lg of relative path is imported from text.
            lgFile = LGParser.ParseText("# basicTemplate\r\n- Hi\r\n- Hello\r\n[import](./6.lg)", GetExampleFilePath("xx.lg"));

            Assert.AreEqual(8, lgFile.AllTemplates.Count());
            evaled = lgFile.EvaluateTemplate("basicTemplate", null).ToString();
            Assert.IsTrue(evaled == "Hi" || evaled == "Hello");

            evaled = lgFile.EvaluateTemplate("welcome", null).ToString();
            Assert.IsTrue(evaled == "Hi DongLei :)" ||
                          evaled == "Hey DongLei :)" ||
                          evaled == "Hello DongLei :)");

            evaled = lgFile.EvaluateTemplate("welcome", new { userName = "******" }).ToString();
            Assert.IsTrue(evaled == "Hi DL :)" ||
                          evaled == "Hey DL :)" ||
                          evaled == "Hello DL :)");
        }
コード例 #20
0
        public void TestCustomFunction()
        {
            var engine = new ExpressionEngine((string func) =>
            {
                if (func == "custom")
                {
                    return(ExpressionFunctions.Numeric("custom", (args) => args[0] + args[1]));
                }
                else
                {
                    return(ExpressionFunctions.Lookup(func));
                }
            });
            var lgFile = LGParser.ParseFile(GetExampleFilePath("CustomFunction.lg"), null, engine);
            var evaled = lgFile.EvaluateTemplate("template");

            Assert.AreEqual(3, evaled);
        }
コード例 #21
0
        public void TestInlineEvaluate()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("2.lg"));
            var evaled = lgFile.Evaluate("hello");

            Assert.AreEqual("hello", evaled);

            // test template reference
            evaled = lgFile.Evaluate("${wPhrase()}");
            var options = new List <string> {
                "Hi", "Hello", "Hiya"
            };

            Assert.IsTrue(options.Contains(evaled), $"The result `{evaled}` is not in those options [{string.Join(",", options)}]");

            var exception = Assert.ThrowsException <Exception>(() => lgFile.Evaluate("${ErrrorTemplate()}"));

            Assert.IsTrue(exception.Message.Contains("it's not a built-in function or a customized function"));
        }
コード例 #22
0
        public void TestExpandTemplateWithFunction()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("Expand.lg"));

            var alarms = new[]
            {
                new
                {
                    time = "7 am",
                    date = "tomorrow"
                },
                new
                {
                    time = "8 pm",
                    date = "tomorrow"
                }
            };

            var evaled      = lgFile.ExpandTemplate("ShowAlarmsWithForeach", new { alarms = alarms });
            var evalOptions = new List <string>()
            {
                "You have 2 alarms, 7 am at tomorrow and 8 pm at tomorrow",
                "You have 2 alarms, 7 am at tomorrow and 8 pm of tomorrow",
                "You have 2 alarms, 7 am of tomorrow and 8 pm at tomorrow",
                "You have 2 alarms, 7 am of tomorrow and 8 pm of tomorrow"
            };

            Assert.AreEqual(1, evaled.Count);
            Assert.AreEqual(true, evalOptions.Contains(evaled[0]));

            evaled = lgFile.ExpandTemplate("T2");
            Assert.AreEqual(1, evaled.Count);
            Assert.AreEqual(true, evaled[0] == "3" || evaled[0] == "5");

            evaled = lgFile.ExpandTemplate("T3");
            Assert.AreEqual(1, evaled.Count);
            Assert.AreEqual(true, evaled[0] == "3" || evaled[0] == "5");

            evaled = lgFile.ExpandTemplate("T4");
            Assert.AreEqual(1, evaled.Count);
            Assert.AreEqual(true, evaled[0] == "ey" || evaled[0] == "el");
        }
コード例 #23
0
        public void TestTemplateAsFunction()
        {
            var    lgFile = LGParser.ParseFile(GetExampleFilePath("TemplateAsFunction.lg"));
            string evaled = lgFile.EvaluateTemplate("Test2", string.Empty).ToString();

            Assert.AreEqual(evaled, "hello world");

            evaled = lgFile.EvaluateTemplate("Test3", string.Empty).ToString();
            Assert.AreEqual(evaled, "hello world");

            evaled = lgFile.EvaluateTemplate("Test4", string.Empty).ToString();

            Assert.AreEqual(evaled.Trim(), "hello world");

            evaled = lgFile.EvaluateTemplate("dupNameWithTemplate").ToString();
            Assert.AreEqual(evaled, "calculate length of ms by user's template");

            evaled = lgFile.EvaluateTemplate("dupNameWithBuiltinFunc").ToString();
            Assert.AreEqual(evaled, "2");
        }
コード例 #24
0
        public void TestConditionExpression()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("ConditionExpression.lg"));

            var evaled = lgFile.EvaluateTemplate("conditionTemplate", new { num = 1 });

            Assert.AreEqual(evaled, "Your input is one");

            evaled = lgFile.EvaluateTemplate("conditionTemplate", new { num = 2 });

            Assert.AreEqual(evaled, "Your input is two");

            evaled = lgFile.EvaluateTemplate("conditionTemplate", new { num = 3 });

            Assert.AreEqual(evaled, "Your input is three");

            evaled = lgFile.EvaluateTemplate("conditionTemplate", new { num = 4 });

            Assert.AreEqual(evaled, "Your input is not one, two or three");
        }
コード例 #25
0
        public void TestBasicExtendedFunctions()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("6.lg"));
            var alarms = new[]
            {
                new
                {
                    time = "7 am",
                    date = "tomorrow"
                },
                new
                {
                    time = "8 pm",
                    date = "tomorrow"
                }
            };

            // var alarmStrs = alarms.Select(x => engine.EvaluateTemplate("ShowAlarm", new { alarm = x })).ToList() ;
            // var evaled = engine.EvaluateTemplate("ShowAlarms", new { alarms = alarmStrs });
            // Assert.AreEqual("You have 2 alarms, 7 am at tomorrow and 8 pm at tomorrow", evaled);

            var evaled = lgFile.EvaluateTemplate("ShowAlarmsWithForeach", new { alarms = alarms });

            Assert.AreEqual("You have 2 alarms, 7 am at tomorrow and 8 pm at tomorrow", evaled);

            evaled = lgFile.EvaluateTemplate("ShowAlarmsWithLgTemplate", new { alarms = alarms });
            Assert.AreEqual("You have 2 alarms, 7 am at tomorrow and 8 pm at tomorrow", evaled);

            evaled = lgFile.EvaluateTemplate("ShowAlarmsWithDynamicLgTemplate", new { alarms = alarms, templateName = "ShowAlarm" });
            Assert.AreEqual("You have 2 alarms, 7 am at tomorrow and 8 pm at tomorrow", evaled);

            // var evaled = lgFile.EvaluateTemplate("ShowAlarmsWithMemberForeach", new { alarms = alarms });
            // Assert.AreEqual("You have 2 alarms, 7 am at tomorrow and 8 pm at tomorrow", evaled);

            // var evaled = lgFile.EvaluateTemplate("ShowAlarmsWithHumanize", new { alarms = alarms });
            // Assert.AreEqual("You have 2 alarms, 7 am at tomorrow and 8 pm at tomorrow", evaled);

            // var evaled = lgFile.EvaluateTemplate("ShowAlarmsWithMemberHumanize", new { alarms = alarms });
            // Assert.AreEqual("You have 2 alarms, 7 am at tomorrow and 8 pm at tomorrow", evaled);
        }
コード例 #26
0
        public void TestEscapeCharacter()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("EscapeCharacter.lg"));
            var evaled = lgFile.EvaluateTemplate("wPhrase", null);

            Assert.AreEqual(evaled, "Hi \r\n\t[]{}\\");

            evaled = lgFile.EvaluateTemplate("AtEscapeChar", null);
            Assert.AreEqual(evaled, "Hi{1+1}[wPhrase]{wPhrase()}${wPhrase()}2${1+1} ");

            evaled = lgFile.EvaluateTemplate("otherEscape", null);
            Assert.AreEqual(evaled, "Hi y ");

            evaled = lgFile.EvaluateTemplate("escapeInExpression", null);
            Assert.AreEqual(evaled, "Hi hello\\\\");

            evaled = lgFile.EvaluateTemplate("escapeInExpression2", null);
            Assert.AreEqual(evaled, "Hi hello'");

            evaled = lgFile.EvaluateTemplate("escapeInExpression3", null);
            Assert.AreEqual(evaled, "Hi hello\"");

            evaled = lgFile.EvaluateTemplate("escapeInExpression4", null);
            Assert.AreEqual(evaled, "Hi hello\"");

            evaled = lgFile.EvaluateTemplate("escapeInExpression5", null);
            Assert.AreEqual(evaled, "Hi hello\n");

            evaled = lgFile.EvaluateTemplate("escapeInExpression6", null);
            Assert.AreEqual(evaled, "Hi hello\n");

            var todos = new[] { "A", "B", "C" };

            evaled = lgFile.EvaluateTemplate("showTodo", new { todos });
            Assert.AreEqual(((string)evaled).Replace("\r\n", "\n"), "\n    Your most recent 3 tasks are\n    * A\n* B\n* C\n    ");

            evaled = lgFile.EvaluateTemplate("showTodo", null);
            Assert.AreEqual(((string)evaled).Replace("\r\n", "\n"), "\n    You don't have any \"t\\\\odo'\".\n    ");
        }
コード例 #27
0
        public void TestMemoryAccessPath()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("MemoryAccess.lg"));

            var memory = new
            {
                myProperty = new
                {
                    name = "p1"
                },

                turn = new
                {
                    properties = new Dictionary <string, object>
                    {
                        {
                            "p1", new Dictionary <string, object>()
                            {
                                { "enum", "p1enum" }
                            }
                        }
                    }
                }
            };

            // this evaulate will hit memory access twice
            // first for "property", and get "p1", from local
            // sencond for "turn.property[p1].enum" and get "p1enum" from global
            var result = lgFile.EvaluateTemplate("T1", memory);

            Assert.AreEqual(result, "p1enum");

            // this evaulate will hit memory access twice
            // first for "myProperty.name", and get "p1", from global
            // sencond for "turn.property[p1].enum" and get "p1enum" from global
            result = lgFile.EvaluateTemplate("T3", memory);
            Assert.AreEqual(result, "p1enum");
        }
コード例 #28
0
        public void TestStringInterpolation()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("StringInterpolation.lg"));

            var evaled = lgFile.EvaluateTemplate("simpleStringTemplate");

            Assert.AreEqual("say hi", evaled);

            evaled = lgFile.EvaluateTemplate("StringTemplateWithVariable", new { w = "world" });
            Assert.AreEqual("hello world", evaled);

            evaled = lgFile.EvaluateTemplate("StringTemplateWithMixing", new { name = "jack" });
            Assert.AreEqual("I know your name is jack", evaled);

            evaled = lgFile.EvaluateTemplate("StringTemplateWithJson", new { h = "hello", w = "world" });
            Assert.AreEqual("get 'h' value : hello", evaled);

            evaled = lgFile.EvaluateTemplate("StringTemplateWithEscape");
            Assert.AreEqual("just want to output ${bala`bala}", evaled);

            evaled = lgFile.EvaluateTemplate("StringTemplateWithTemplateRef");
            Assert.AreEqual("hello jack , welcome. nice weather!", evaled);
        }
コード例 #29
0
        public void TestExpressionextract()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("ExpressionExtract.lg"));

            var evaled1        = lgFile.EvaluateTemplate("templateWithBrackets");
            var evaled2        = lgFile.EvaluateTemplate("templateWithBrackets2");
            var evaled3        = lgFile.EvaluateTemplate("templateWithBrackets3").ToString().Trim();
            var espectedResult = "don't mix {} and '{}'";

            Assert.AreEqual(evaled1, espectedResult);
            Assert.AreEqual(evaled2, espectedResult);
            Assert.AreEqual(evaled3, espectedResult);

            evaled1        = lgFile.EvaluateTemplate("templateWithQuotationMarks");
            evaled2        = lgFile.EvaluateTemplate("templateWithQuotationMarks2");
            evaled3        = lgFile.EvaluateTemplate("templateWithQuotationMarks3").ToString().Trim();
            espectedResult = "don't mix {\"} and \"\"'\"";
            Assert.AreEqual(evaled1, espectedResult);
            Assert.AreEqual(evaled2, espectedResult);
            Assert.AreEqual(evaled3, espectedResult);

            evaled1        = lgFile.EvaluateTemplate("templateWithUnpairedBrackets1");
            evaled2        = lgFile.EvaluateTemplate("templateWithUnpairedBrackets12");
            evaled3        = lgFile.EvaluateTemplate("templateWithUnpairedBrackets13").ToString().Trim();
            espectedResult = "{prefix 5 sufix";
            Assert.AreEqual(evaled1, espectedResult);
            Assert.AreEqual(evaled2, espectedResult);
            Assert.AreEqual(evaled3, espectedResult);

            evaled1        = lgFile.EvaluateTemplate("templateWithUnpairedBrackets2");
            evaled2        = lgFile.EvaluateTemplate("templateWithUnpairedBrackets22");
            evaled3        = lgFile.EvaluateTemplate("templateWithUnpairedBrackets23").ToString().Trim();
            espectedResult = "prefix 5 sufix}";
            Assert.AreEqual(evaled1, espectedResult);
            Assert.AreEqual(evaled2, espectedResult);
            Assert.AreEqual(evaled3, espectedResult);
        }
コード例 #30
0
        public void TestExpandTemplateWithRef()
        {
            var lgFile = LGParser.ParseFile(GetExampleFilePath("Expand.lg"));

            var alarms = new[]
            {
                new
                {
                    time = "7 am",
                    date = "tomorrow"
                },
                new
                {
                    time = "8 pm",
                    date = "tomorrow"
                }
            };

            var evaled = lgFile.ExpandTemplate("ShowAlarmsWithLgTemplate", new { alarms = alarms });

            Assert.AreEqual(2, evaled.Count);
            Assert.AreEqual("You have 2 alarms, they are 8 pm at tomorrow", evaled[0]);
            Assert.AreEqual("You have 2 alarms, they are 8 pm of tomorrow", evaled[1]);
        }