public void TestDictDefaultIsDefaultString() { string templates = "map ::= [default: \"default\"] " + newline + "t() ::= << <map.(\"1\")> >>" + newline ; WriteFile(tmpdir, "test.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg")); Template st = group.GetInstanceOf("t"); string expecting = " default "; string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestDefineRegionInSameGroup() { string dir = GetRandomDir(); string g = "a() ::= <<[<@r()>]>>\n" + "@a.r() ::= <<foo>>\n"; WriteFile(dir, "g.stg", g); TemplateGroup group = new TemplateGroupFile(dir + "/g.stg"); Template st = group.GetInstanceOf("a"); string expected = "[foo]"; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestDictDefaultValue() { string templates = "typeInit ::= [\"int\":\"0\", default:\"null\"] " + newline + "var(type,name) ::= \"<type> <name> = <typeInit.(type)>;\"" + newline ; WriteFile(tmpdir, "test.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg")); Template st = group.GetInstanceOf("var"); st.Add("type", "UserRecord"); st.Add("name", "x"); string expecting = "UserRecord x = null;"; string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestDictDefaultStringAsKey() { string templates = "typeInit ::= [\"default\":\"foo\"] " + newline + "var(type,name) ::= \"<type> <name> = <typeInit.(type)>;\"" + newline ; WriteFile(tmpdir, "test.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg")); Template st = group.GetInstanceOf("var"); st.Add("type", "default"); st.Add("name", "x"); string expecting = "default x = foo;"; string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestNumberRendererWithPrintfFormat() { string templates = "foo(x,y) ::= << <x; format=\"F0\"> <y; format=\"0.000\"> >>\n"; WriteFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); group.RegisterRenderer(typeof(int), new NumberRenderer()); group.RegisterRenderer(typeof(double), new NumberRenderer()); Template st = group.GetInstanceOf("foo"); st.Add("x", -2100); st.Add("y", 3.14159); string expecting = " -2100 3.142 "; string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestHiddenFieldNotError() { ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; String templates = "t(u) ::= \"<u.name>\"" + newline; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); ST st = group.GetInstanceOf("t"); st.Add("u", new UserHiddenNameField("parrt")); st.Render(); String expected = ""; String result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestAttribute() { String templates = "t(x) ::= << <x> >>" + newline; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(tmpdir + "/" + "t.stg"); group.Debug = true; DebugTemplate template = (DebugTemplate)group.GetInstanceOf("t"); IList<InterpEvent> events = template.GetEvents(); String expected = "[EvalExprEvent{self=t(),start=0,stop=-1,expr=<x>}," + " EvalExprEvent{self=t(),start=0,stop=0,expr= }," + " EvalTemplateEvent{self=t(),start=0,stop=0}]"; String result = ((IList)events).ToElementString(); Assert.AreEqual(expected, result); }
public void TestLocaleWithNumberRenderer() { string templates = "foo(x,y) ::= << <x; format=\"N0\"> <y; format=\"0.000\"> >>\n"; WriteFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); group.RegisterRenderer(typeof(int), new NumberRenderer()); group.RegisterRenderer(typeof(double), new NumberRenderer()); Template st = group.GetInstanceOf("foo"); st.Add("x", -2100); st.Add("y", 3.14159); // Polish uses ' ' for ',' and ',' for '.' string expecting = " -2 100 3,142 "; string result = st.Render(CultureInfo.GetCultureInfo("pl-PL")); Assert.AreEqual(expecting, result); }
public void TestUndefinedArg() { ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; string templates = "t() ::= \"<u()>\"\n" + "u() ::= \"<x>\"\n"; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); group.Debug = true; ST st = group.GetInstanceOf("t"); st.Render(); String expected = "context [t, u] 1:1 attribute x isn't defined" + newline; String result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestPassUnknownAttr() { String templates = "t() ::= \"<u(x={Ter})>\"\n" + "u(y) ::= <<hi <x>!>>\n"; ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); ST st = group.GetInstanceOf("t"); String expected = "hi Ter!"; String result = st.Render(); Assert.AreEqual(expected, result); // check error now expected = "context [t u] can't set attribute x; template u has no such attribute" + newline; result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestDefineRegionInSubgroup2() { string dir = GetRandomDir(); string g1 = "a() ::= <<[<@r()>]>>\n"; WriteFile(dir, "g1.stg", g1); string g2 = "@a.r() ::= <<foo>>>\n"; WriteFile(dir, "g2.stg", g2); TemplateGroup group1 = new TemplateGroupFile(dir + "/g1.stg"); TemplateGroup group2 = new TemplateGroupFile(dir + "/g2.stg"); group1.ImportTemplates(group2); // opposite of previous; g1 imports g2 Template st = group1.GetInstanceOf("a"); string expected = "[]"; // @a.r implicitly defined in g1; can't see g2's string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestParallelAttributeIterationWithDifferentSizesTemplateRefInsideToo() { String templates = "page(names,phones,salaries) ::= " + newline + " << <names,phones,salaries:{n,p,s | <value(n)>@<value(p)>: <value(s)>}; separator=\", \"> >>" + newline + "value(x=\"n/a\") ::= \"<x>\"" + newline; WriteFile(tmpdir, "g.stg", templates); STGroup group = new STGroupFile(tmpdir + "/g.stg"); ST p = group.GetInstanceOf("page"); p.Add("names", "Ter"); p.Add("names", "Tom"); p.Add("names", "Sriram"); p.Add("phones", "1"); p.Add("phones", "2"); p.Add("salaries", "big"); String expecting = "Ter@1: big, Tom@2: n/a, Sriram@n/a: n/a"; Assert.AreEqual(expecting, p.Render()); }
public void TestDictViaEnclosingTemplates2() { string templates = "typeInit ::= [\"int\":\"0\", \"float\":\"0.0\"] " + newline + "intermediate(stuff) ::= \"<stuff>\"" + newline + "var(type,name) ::= \"<type> <name> = <typeInit.(type)>;\"" + newline ; WriteFile(tmpdir, "test.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg")); Template interm = group.GetInstanceOf("intermediate"); Template var = group.GetInstanceOf("var"); var.Add("type", "int"); var.Add("name", "x"); interm.Add("stuff", var); string expecting = "int x = 0;"; string result = interm.Render(); Assert.AreEqual(expecting, result); }
public void TestDictValuesAreTemplates() { string templates = "typeInit ::= [\"int\":{0<w>}, \"float\":{0.0<w>}] " + newline + "var(type,w,name) ::= \"<type> <name> = <typeInit.(type)>;\"" + newline ; WriteFile(tmpdir, "test.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg")); Template st = group.GetInstanceOf("var"); st.Add("w", "L"); st.Add("type", "int"); st.Add("name", "x"); string expecting = "int x = 0L;"; string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestDictMissingDefaultValueIsEmpty() { string templates = "typeInit ::= [\"int\":\"0\", \"float\":\"0.0\"] " + newline + "var(type,w,name) ::= \"<type> <name> = <typeInit.(type)>;\"" + newline ; WriteFile(tmpdir, "test.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg")); Template st = group.GetInstanceOf("var"); st.Add("w", "L"); st.Add("type", "double"); // double not in typeInit map st.Add("name", "x"); string expecting = "double x = ;"; // weird, but tests default value is key string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestDictKeyLookupViaTemplate() { // Make sure we try rendering stuff to string if not found as regular object string templates = "typeInit ::= [\"int\":{0<w>}, \"float\":{0.0<w>}] " + newline + "var(type,w,name) ::= \"<type> <name> = <typeInit.(type)>;\"" + newline ; WriteFile(tmpdir, "test.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg")); Template st = group.GetInstanceOf("var"); st.Add("w", "L"); st.Add("type", new Template("int")); st.Add("name", "x"); string expecting = "int x = 0L;"; string result = st.Render(); Assert.AreEqual(expecting, result); }
public void TestTemplateCall() { String templates = "t(x) ::= <<[<u()>]>>\n" + "u() ::= << <x> >>\n"; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(tmpdir + "/" + "t.stg"); group.Debug = true; DebugTemplate template = (DebugTemplate)group.GetInstanceOf("t"); IList<InterpEvent> events = template.GetEvents(); String expected = "[EvalExprEvent{self=t(),start=0,stop=0,expr=[}," + " EvalExprEvent{self=u(),start=1,stop=0,expr=<x>}," + " EvalExprEvent{self=u(),start=1,stop=1,expr= }," + " EvalTemplateEvent{self=u(),start=1,stop=1}," + " EvalExprEvent{self=t(),start=1,stop=1,expr=<u()>}," + " EvalExprEvent{self=t(),start=2,stop=2,expr=]}," + " EvalTemplateEvent{self=t(),start=0,stop=2}]"; String result = ((IList)events).ToElementString(); Assert.AreEqual(expected, result); }
public void TestIndentBetweenLeftJustifiedLiterals() { String templates = "list(names) ::= <<" + "Before:" + newline + " <names; separator=\"\\n\">" + newline + "after" + newline + ">>" + newline; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); Template t = group.GetInstanceOf("list"); t.Add("names", "Terence"); t.Add("names", "Jim"); t.Add("names", "Sriram"); String expecting = "Before:" + newline + " Terence" + newline + " Jim" + newline + " Sriram" + newline + "after" + newline; Assert.AreEqual(expecting, t.Render()); }
public void TestRegion() { string dir = GetRandomDir(); string groupFile = "a() ::= <<\n" + "[<@r()>]\n" + ">>\n"; WriteFile(dir, "group.stg", groupFile); TemplateGroup group = new TemplateGroupFile(dir + "/group.stg"); Template st = group.GetInstanceOf("a"); string expected = "[]" + newline; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestIndentOfMultilineAttributes() { String templates = "list(names) ::= <<" + " <names; separator=\"\n\">" + newline + ">>" + newline; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); Template t = group.GetInstanceOf("list"); t.Add("names", "Terence\nis\na\nmaniac"); t.Add("names", "Jim"); t.Add("names", "Sriram\nis\ncool"); String expecting = " Terence" + newline + " is" + newline + " a" + newline + " maniac" + newline + " Jim" + newline + " Sriram" + newline + " is" + newline + " cool" + newline; Assert.AreEqual(expecting, t.Render()); }
public void TestDefineRegionInSubgroup() { string dir = GetRandomDir(); string g1 = "a() ::= <<[<@r()>]>>\n"; WriteFile(dir, "g1.stg", g1); string g2 = "@a.r() ::= <<foo>>\n"; WriteFile(dir, "g2.stg", g2); TemplateGroup group1 = new TemplateGroupFile(dir + "/g1.stg"); TemplateGroup group2 = new TemplateGroupFile(dir + "/g2.stg"); group2.ImportTemplates(group1); // define r in g2 Template st = group2.GetInstanceOf("a"); string expected = "[foo]"; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestIndentOfMultipleBlankLines() { String templates = "list(names) ::= <<" + " <names>" + newline + ">>" + newline; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); Template t = group.GetInstanceOf("list"); t.Add("names", "Terence\n\nis a maniac"); String expecting = " Terence" + newline + "" + newline + // no indent on blank line " is a maniac" + newline; Assert.AreEqual(expecting, t.Render()); }
public void TestMissingSuperTemplate() { ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; String templates = "t() ::= \"<super.t()>\"" + newline; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); String templates2 = "u() ::= \"blech\"" + newline; WriteFile(tmpdir, "t2.stg", templates2); STGroup group2 = new STGroupFile(Path.Combine(tmpdir, "t2.stg")); group.ImportTemplates(group2); ST st = group.GetInstanceOf("t"); st.Render(); String expected = "context [t] 1:1 no such template: super.t" + newline; String result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestNestedIndent() { String templates = "method(name,stats) ::= <<" + "void <name>() {" + newline + "\t<stats; separator=\"\\n\">" + newline + "}" + newline + ">>" + newline + "ifstat(expr,stats) ::= <<" + newline + "if (<expr>) {" + newline + " <stats; separator=\"\\n\">" + newline + "}" + ">>" + newline + "assign(lhs,expr) ::= <<<lhs>=<expr>;>>" + newline ; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); Template t = group.GetInstanceOf("method"); t.Add("name", "foo"); Template s1 = group.GetInstanceOf("assign"); s1.Add("lhs", "x"); s1.Add("expr", "0"); Template s2 = group.GetInstanceOf("ifstat"); s2.Add("expr", "x>0"); Template s2a = group.GetInstanceOf("assign"); s2a.Add("lhs", "y"); s2a.Add("expr", "x+y"); Template s2b = group.GetInstanceOf("assign"); s2b.Add("lhs", "z"); s2b.Add("expr", "4"); s2.Add("stats", s2a); s2.Add("stats", s2b); t.Add("stats", s1); t.Add("stats", s2); String expecting = "void foo() {" + newline + "\tx=0;" + newline + "\tif (x>0) {" + newline + "\t y=x+y;" + newline + "\t z=4;" + newline + "\t}" + newline + "}" + newline; Assert.AreEqual(expecting, t.Render()); }
public void TestSoleArgUsingApplySyntax() { ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; String templates = "t() ::= \"<{9}:u()>\"\n" + "u(x,y) ::= \"<x>\"\n"; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); ST st = group.GetInstanceOf("t"); String expected = "9"; String result = st.Render(); Assert.AreEqual(expected, result); expected = "context [t] 1:1 expecting single arg in template reference u() (not 2 args)" + newline; result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestSimpleIndentOfAttributeList() { String templates = "list(names) ::= <<" + " <names; separator=\"\\n\">" + newline + ">>" + newline; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); Template t = group.GetInstanceOf("list"); t.Add("names", "Terence"); t.Add("names", "Jim"); t.Add("names", "Sriram"); String expecting = " Terence" + newline + " Jim" + newline + " Sriram" + newline; Assert.AreEqual(expecting, t.Render()); }
public void TestUndefinedArgNoProblemInCompatibilityMode() { ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; ErrorManager.CompatibilityMode = true; try { string templates = "t() ::= \"<u()>\"\n" + "u() ::= \"<x>\"\n"; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(tmpdir + "/" + "t.stg"); ST st = group.GetInstanceOf("t"); st.Render(); String expected = ""; String result = errors.ToString(); Assert.AreEqual(expected, result); } finally { ErrorManager.CompatibilityMode = false; } }
public void TestReUseOfCat() { string templates = "a(mine,yours) ::= \"<b([mine,yours])>\"" + newline + "b(x) ::= \"<x>, <x>\"" + newline ; WriteFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); Template e = group.GetInstanceOf("a"); var mine = new ArrayList(); mine.Add("Ter"); mine.Add("Tom"); e.Add("mine", mine); var yours = new ArrayList(); yours.Add("Foo"); e.Add("yours", yours); string expecting = "TerTomFoo, TerTomFoo"; Assert.AreEqual(expecting, e.Render()); }
public void TestMissingEmbeddedTemplate() { ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; String templates = "t() ::= \"<foo()>\"" + newline; WriteFile(tmpdir, "t.stg", templates); STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); ST st = group.GetInstanceOf("t"); st.Render(); String expected = "context [t] 1:0 no such template: foo" + newline; String result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestReUseOfRestResult() { string templates = "a(names) ::= \"<b(rest(names))>\"" + newline + "b(x) ::= \"<x>, <x>\"" + newline ; WriteFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); Template e = group.GetInstanceOf("a"); var names = new ArrayList(); names.Add("Ter"); names.Add("Tom"); e.Add("names", names); string expecting = "Tom, Tom"; Assert.AreEqual(expecting, e.Render()); }