DefineTemplate() public method

public DefineTemplate ( string fullyQualifiedTemplateName, IToken nameT, List args, string template, IToken templateToken ) : CompiledTemplate
fullyQualifiedTemplateName string
nameT IToken
args List
template string
templateToken IToken
return Antlr4.StringTemplate.Compiler.CompiledTemplate
 public void TestIndirectMap()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("a", "[<x>]", new string[] { "x" });
     group.DefineTemplate("test", "hi <names:(templateName)()>!", new string[] { "names", "templateName" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", "Ter");
     st.Add("names", "Tom");
     st.Add("names", "Sumana");
     st.Add("templateName", "a");
     string expected =
         "hi [Ter][Tom][Sumana]!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 2
0
 public static string ToListString(this IList list)
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("listTemplate", "[<list:{x|<x>}; separator=\", \">]", new string[] { "list" });
     group.RegisterRenderer(typeof(IList), new CollectionRenderer());
     Template st = group.GetInstanceOf("listTemplate");
     st.Add("list", list);
     return st.Render();
 }
 public void TestMissingDictionaryValue2()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<if(m.foo)>[<m.foo>]<endif>", new string[] { "m" });
     Template t = group.GetInstanceOf("test");
     t.Add("m", new Dictionary<string, string>());
     string expecting = "";
     string result = t.Render();
     Assert.AreEqual(expecting, result);
 }
Esempio n. 4
0
        public void TestEvalSTIteratingSubtemplateInSTFromAnotherGroup()
        {
            ErrorBuffer errors = new ErrorBuffer();
            TemplateGroup innerGroup = new TemplateGroup();
            innerGroup.Listener = errors;
            innerGroup.DefineTemplate("test", "<m:samegroup()>", new string[] { "m" });
            innerGroup.DefineTemplate("samegroup", "hi ", new string[] { "x" });
            Template st = innerGroup.GetInstanceOf("test");
            st.Add("m", new int[] { 1, 2, 3 });

            TemplateGroup outerGroup = new TemplateGroup();
            outerGroup.DefineTemplate("errorMessage", "<x>", new string[] { "x" });
            Template outerST = outerGroup.GetInstanceOf("errorMessage");
            outerST.Add("x", st);

            string expected = "hi hi hi ";
            string result = outerST.Render();

            Assert.AreEqual(errors.Errors.Count, 0); // ignores no such prop errors

            Assert.AreEqual(expected, result);
        }
Esempio n. 5
0
 public void TestAttrSeparator()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "hi <name; separator=sep>!", new string[] { "name", "sep" });
     Template st = group.GetInstanceOf("test");
     st.Add("sep", ", ");
     st.Add("name", "Ter");
     st.Add("name", "Tom");
     st.Add("name", "Sumana");
     string expected = "hi Ter, Tom, Sumana!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
 public void TestEmptyListGetsNoOutput()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test",
         "begin\n" +
         "<users:{u | name: <u>}; separator=\", \">\n" +
         "end\n", new string[] { "users" });
     Template t = group.GetInstanceOf("test");
     t.Add("users", new List<string>());
     string expecting = "begin" + newline + "end";
     string result = t.Render();
     Assert.AreEqual(expecting, result);
 }
Esempio n. 7
0
        public void TestEvalSTFromAnotherGroup()
        {
            ErrorBuffer errors = new ErrorBuffer();
            TemplateGroup innerGroup = new TemplateGroup();
            innerGroup.Listener = errors;
            innerGroup.DefineTemplate("bob", "inner");
            Template st = innerGroup.GetInstanceOf("bob");

            TemplateGroup outerGroup = new TemplateGroup();
            outerGroup.Listener = errors;
            outerGroup.DefineTemplate("errorMessage", "<x>", new string[] { "x" });
            outerGroup.DefineTemplate("bob", "outer"); // should not be visible to test() in innerGroup
            Template outerST = outerGroup.GetInstanceOf("errorMessage");
            outerST.Add("x", st);

            string expected = "inner";
            string result = outerST.Render();

            Assert.AreEqual(errors.Errors.Count, 0); // ignores no such prop errors

            Assert.AreEqual(expected, result);
        }
Esempio n. 8
0
 public void TestIllegalOption()
 {
     ErrorBuffer errors = new ErrorBuffer();
     TemplateGroup group = new TemplateGroup();
     group.Listener = errors;
     group.DefineTemplate("test", "<name; bad=\"ugly\">", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     string expected = "Ter";
     string result = st.Render();
     Assert.AreEqual(expected, result);
     expected = "[test 1:7: no such option: bad]";
     Assert.AreEqual(expected, errors.Errors.ToListString());
 }
Esempio n. 9
0
 public void TestDoubleListApplyWithNullValueAndNullOption()
 {
     // first apply sends [Template, null, Template] to second apply, which puts [] around
     // the value.  This verifies that null not blank comes out of first apply
     // since we don't get [null].
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<name:{n | <n>}:{n | [<n>]}; null=\"n/a\">", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     st.Add("name", null);
     st.Add("name", "Sumana");
     string expected = "[Ter]n/a[Sumana]";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 10
0
 public void TestParallelMap()
 {
     TemplateGroup group = new TemplateGroup('$', '$');
     group.DefineTemplate("test", "hi $names,phones:{n,p | $n$:$p$;}$", new string[] { "names", "phones" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", "Ter");
     st.Add("names", "Tom");
     st.Add("names", "Sumana");
     st.Add("phones", "x5001");
     st.Add("phones", "x5002");
     st.Add("phones", "x5003");
     string expected =
         "hi Ter:x5001;Tom:x5002;Sumana:x5003;";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 11
0
 public void TestEmptyExpr2()
 {
     string template = "hi <> ";
     TemplateGroup group = new TemplateGroup();
     ErrorBuffer errors = new ErrorBuffer();
     group.Listener = errors;
     try
     {
         group.DefineTemplate("test", template);
     }
     catch (TemplateException)
     {
     }
     string result = errors.ToString();
     string expected = "test 1:3: doesn't look like an expression" + newline;
     Assert.AreEqual(expected, result);
 }
Esempio n. 12
0
 public void TestWeirdChar2()
 {
     string template = "\n<\\\n";
     TemplateGroup group = new TemplateGroup();
     ErrorBuffer errors = new ErrorBuffer();
     group.Listener = errors;
     try
     {
         group.DefineTemplate("test", template);
     }
     catch (TemplateException)
     {
     }
     string result = errors.ToString();
     string expected = "test 1:2: invalid escaped char: '<EOF>'" + newline +
                       "test 1:2: expecting '>', found '<EOF>'" + newline;
     Assert.AreEqual(expected, result);
 }
Esempio n. 13
0
 public void TestParallelMapWith3Versus2Elements()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "hi <names,phones:{n,p | <n>:<p>;}>", new string[] { "names", "phones" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", "Ter");
     st.Add("names", "Tom");
     st.Add("names", "Sumana");
     st.Add("phones", "x5001");
     st.Add("phones", "x5002");
     string expected =
         "hi Ter:x5001;Tom:x5002;Sumana:;";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 14
0
 public void TestUnterminatedExpr()
 {
     string template = "hi <t()$";
     TemplateGroup group = new TemplateGroup();
     ErrorBuffer errors = new ErrorBuffer();
     group.Listener = errors;
     try
     {
         group.DefineTemplate("test", template);
     }
     catch (TemplateException)
     {
     }
     string result = errors.ToString();
     string expected = "test 1:7: invalid character '$'" + newline +
         "test 1:7: invalid character '<EOF>'" + newline +
         "test 1:7: premature EOF" + newline;
     Assert.AreEqual(expected, result);
 }
Esempio n. 15
0
 public void TestWeirdChar()
 {
     string template = "   <*>";
     TemplateGroup group = new TemplateGroup();
     ErrorBuffer errors = new ErrorBuffer();
     group.Listener = errors;
     try
     {
         group.DefineTemplate("test", template);
     }
     catch (TemplateException)
     {
     }
     string result = errors.ToString();
     string expected = "test 1:4: invalid character '*'" + newline +
                       "test 1:0: this doesn't look like a template: \"   <*>\"" + newline;
     Assert.AreEqual(expected, result);
 }
Esempio n. 16
0
 public void TestTrimNewlineInSubtemplates()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<names:{n |\n" +
                                  "<n>}>!", new string[] { "names" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", "Ter");
     st.Add("names", "Tom");
     st.Add("names", "Sumana");
     string expected = "TerTomSumana!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 17
0
 public void TestTrimmedSubtemplatesNoArgs()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "[<foo({ foo })>]");
     group.DefineTemplate("foo", "<x>", new string[] { "x" });
     Template st = group.GetInstanceOf("test");
     string expected = "[ foo ]";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 18
0
 public void TestSeparatorWithSpaces()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "hi <name; separator= \", \">!", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     Console.WriteLine(st.impl.Ast.ToStringTree());
     st.Add("name", "Ter");
     st.Add("name", "Tom");
     st.Add("name", "Sumana");
     string expected = "hi Ter, Tom, Sumana!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 19
0
 public void TestSeparatorInList()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<names:{n | case <n>}; separator=\", \">", new string[] { "names" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", new List<string>() { "Ter", "Tom" });
     string expected =
         "case Ter, case Tom";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 20
0
 public void TestOptionDoesntApplyToNestedTemplate()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("foo", "<zippo>");
     group.DefineTemplate("test", "<foo(); null=\"n/a\">", new string[] { "zippo" });
     Template st = group.GetInstanceOf("test");
     st.Add("zippo", null);
     string expected = "";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 21
0
 public void TestSeparatorWithNullFirstValueAndNullOption()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "hi <name; null=\"n/a\", separator=\", \">!", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", null);
     st.Add("name", "Tom");
     st.Add("name", "Sumana");
     string expected = "hi n/a, Tom, Sumana!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 22
0
 public void TestDefineTemplate()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("inc", "<x>+1", new string[] { "x" });
     group.DefineTemplate("test", "hi <name>!", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     st.Add("name", "Tom");
     st.Add("name", "Sumana");
     string expected =
         "hi TerTomSumana!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 23
0
 public void TestMapIndexes2()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<name:{n | <i>:<n>}; separator=\", \">", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     st.Add("name", "Tom");
     st.Add("name", null); // don't count this one. still can't apply subtemplate to null value
     st.Add("name", "Sumana");
     string expected =
         "1:Ter, 2:Tom, 3:Sumana";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 24
0
 public void TestMapNullValueInList()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<name; separator=\", \">", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     st.Add("name", "Tom");
     st.Add("name", null); // don't print this one
     st.Add("name", "Sumana");
     string expected =
         "Ter, Tom, Sumana";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 25
0
 public void TestParallelMapThenMap()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("bold", "[<x>]", new string[] { "x" });
     group.DefineTemplate("test", "hi <names,phones:{n,p | <n>:<p>;}:bold()>", new string[] { "names", "phones" });
     Template st = group.GetInstanceOf("test");
     st.Add("names", "Ter");
     st.Add("names", "Tom");
     st.Add("names", "Sumana");
     st.Add("phones", "x5001");
     st.Add("phones", "x5002");
     string expected =
         "hi [Ter:x5001;][Tom:x5002;][Sumana:;]";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 26
0
 public void TestMapSingleValue()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("a", "[<x>]", new string[] { "x" });
     group.DefineTemplate("test", "hi <name:a()>!", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     string expected = "hi [Ter]!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
 public void TestIndirectTemplateIncludeWithArgs()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("foo", "<x><y>", new string[] { "x", "y" });
     string template = "<(name)({1},{2})>";
     group.DefineTemplate("test", template, new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "foo");
     string expected = "12";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 28
0
 public void TestRoundRobinMap()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("a", "[<x>]", new string[] { "x" });
     group.DefineTemplate("b", "(<x>)", new string[] { "x" });
     group.DefineTemplate("test", "hi <name:a(),b()>!", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", "Ter");
     st.Add("name", "Tom");
     st.Add("name", "Sumana");
     string expected =
         "hi [Ter](Tom)[Sumana]!";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
 public void TestIndirectTemplateIncludeViaTemplate()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("foo", "bar");
     group.DefineTemplate("tname", "foo");
     string template = "<(tname())()>";
     group.DefineTemplate("test", template, new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     string expected = "bar";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Esempio n. 30
0
 public void TestNullValueAndNullOption()
 {
     TemplateGroup group = new TemplateGroup();
     group.DefineTemplate("test", "<name; null=\"n/a\">", new string[] { "name" });
     Template st = group.GetInstanceOf("test");
     st.Add("name", null);
     string expected = "n/a";
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }