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); }
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); }
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()); }
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); }
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); }
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); }
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); }
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); }
public void TestSeparatorInPrimitiveArray2() { TemplateGroup group = new TemplateGroup(); group.DefineTemplate("test", "<names:{n | case <n>}; separator=\", \">", new string[] { "names" }); Template st = group.GetInstanceOf("test"); st.Add("names", 0); st.Add("names", new int[] { 1, 2 }); string expected = "case 0, case 1, case 2"; string result = st.Render(); Assert.AreEqual(expected, result); }
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); }
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); }
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); }
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); }
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 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); }
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); }
public void TestTabBeforeEndInSubtemplates() { // fails since it counts indent from outer too TemplateGroup group = new TemplateGroup(); group.DefineTemplate("test", " <names:{n |\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 = " Ter" + newline + " Tom" + newline + " Sumana" + newline + "!"; string result = st.Render(); st.impl.Dump(); Assert.AreEqual(expected, result); }
public void TestTrimmedSubtemplatesArgs() { TemplateGroup group = new TemplateGroup(); group.DefineTemplate("test", "<names:{x| foo }>", new string[] { "names" }); Template st = group.GetInstanceOf("test"); st.Add("names", "Ter"); st.Add("names", "Tom"); st.Add("names", "Sumana"); string expected = " foo foo foo "; string result = st.Render(); Assert.AreEqual(expected, result); }
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); }
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); }
public void TestRepeatedMapWithNullValueAndNullOption() { 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(); null={x}>!", new string[] { "name" }); Template st = group.GetInstanceOf("test"); st.Add("name", "Ter"); st.Add("name", null); st.Add("name", "Sumana"); string expected = "hi ([Ter])x([Sumana])!"; string result = st.Render(); Assert.AreEqual(expected, result); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }