public void TestMissingSuperTemplate() { ErrorBuffer errors = new ErrorBuffer(); string templates = "t() ::= \"<super.t()>\"" + Environment.NewLine; writeFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); group.Listener = errors; string templates2 = "u() ::= \"blech\"" + Environment.NewLine; writeFile(tmpdir, "t2.stg", templates2); TemplateGroup group2 = new TemplateGroupFile(Path.Combine(tmpdir, "t2.stg")); group.ImportTemplates(group2); Template 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 TestArgWithSameNameAsEnclosing() { string templates = "t(x,y) ::= \"<u(x)>\"\n" + "u(y) ::= \"<x><y>\""; ErrorBuffer errors = new ErrorBuffer(); writeFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; Template st = group.GetInstanceOf("t"); st.Add("x", "x"); st.Add("y", "y"); string result = st.Render(); string expectedError = ""; Assert.AreEqual(expectedError, errors.ToString()); string expected = "xx"; Assert.AreEqual(expected, result); group.Listener = ErrorManager.DEFAULT_ERROR_LISTENER; }
public void TestEvalSTIteratingSubtemplateInSTFromAnotherGroupSingleValue() { 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", 10); TemplateGroup outerGroup = new TemplateGroup(); outerGroup.DefineTemplate("errorMessage", "<x>", new string[] { "x" }); Template outerST = outerGroup.GetInstanceOf("errorMessage"); outerST.Add("x", st); string expected = "hi "; string result = outerST.Render(); Assert.AreEqual(errors.Errors.Count, 0); // ignores no such prop errors 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 TestSuperRegionRefMissingOk() { string dir = tmpdir; string g = "a() ::= \"X<@r()>Y\"" + "@a.r() ::= \"foo\"" + newline; writeFile(dir, "g.stg", g); TemplateGroupFile group = new TemplateGroupFile(Path.Combine(dir, "g.stg")); string sub = "@a.r() ::= \"A<@super.q()>B\"" + newline; // allow this; trap at runtime ITemplateErrorListener errors = new ErrorBuffer(); group.Listener = errors; writeFile(dir, "sub.stg", sub); TemplateGroupFile subGroup = new TemplateGroupFile(dir + "/sub.stg"); subGroup.ImportTemplates(group); Template st = subGroup.GetInstanceOf("a"); string result = st.Render(); string expecting = "XABY"; Assert.AreEqual(expecting, result); }
public void TestSetUnknownAttr() { string templates = "t() ::= <<hi <name>!>>\n"; ErrorBuffer errors = new ErrorBuffer(); writeFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); group.Listener = errors; Template st = group.GetInstanceOf("t"); string result = null; try { st.Add("name", "Ter"); } catch (ArgumentException iae) { result = iae.Message; } string expected = "no such attribute: name"; Assert.AreEqual(expected, result); }
public void TestIndexAttrVisibleLocallyOnly() { string templates = "t(names) ::= \"<names:{n | <u(n)>}>\"\n" + "u(x) ::= \"<i>:<x>\""; ErrorBuffer errors = new ErrorBuffer(); writeFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); group.Listener = errors; Template st = group.GetInstanceOf("t"); st.Add("names", "Ter"); string result = st.Render(); group.GetInstanceOf("u").impl.Dump(); string expectedError = "t.stg 2:11: implicitly-defined attribute i not visible" + newline; Assert.AreEqual(expectedError, errors.ToString()); string expected = ":Ter"; Assert.AreEqual(expected, result); group.Listener = ErrorManager.DefaultErrorListener; }
public void TestSeesEnclosingAttr() { string templates = "t(x,y) ::= \"<u()>\"\n" + "u() ::= \"<x><y>\""; ErrorBuffer errors = new ErrorBuffer(); writeFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); group.Listener = errors; Template st = group.GetInstanceOf("t"); st.Add("x", "x"); st.Add("y", "y"); string result = st.Render(); string expectedError = ""; Assert.AreEqual(expectedError, errors.ToString()); string expected = "xy"; Assert.AreEqual(expected, result); }
public void TestParallelAttributeIterationWithMissingArgs() { ErrorBuffer errors = new ErrorBuffer(); TemplateGroup group = new TemplateGroup(); group.Listener = errors; Template e = new Template(group, "<names,phones,salaries:{n,p | <n>@<p>}; separator=\", \">" ); e.Add("names", "Ter"); e.Add("names", "Tom"); e.Add("phones", "1"); e.Add("phones", "2"); e.Add("salaries", "big"); e.Render(); string errorExpecting = "1:23: anonymous template has 2 arg(s) but mapped across 3 value(s)" + newline + "context [anonymous] 1:23 passed 3 arg(s) to template /_sub1 with 2 declared arg(s)" + newline + "context [anonymous] 1:1 iterating through 3 values in zip map but template has 2 declared arguments" + newline; Assert.AreEqual(errorExpecting, errors.ToString()); string expecting = "Ter@1, Tom@2"; Assert.AreEqual(expecting, e.Render()); }
public void TestCantDefineEmbeddedRegionAgainInTemplate() { string dir = tmpdir; string g = "a() ::= <<\n" + "[\n" + "<@r>foo<@end>\n" + "<@r()>\n" + "]\n" + ">>\n"; // error; dup writeFile(dir, "g.stg", g); TemplateGroupFile group = new TemplateGroupFile(Path.Combine(dir, "g.stg")); ErrorBuffer errors = new ErrorBuffer(); group.Listener = errors; group.Load(); Assert.AreEqual(0, errors.Errors.Count); Template template = group.GetInstanceOf("a"); string expected = "[" + newline + "foo" + newline + "foo" + newline + "]"; string result = template.Render(); Assert.AreEqual(expected, result); }
public void TestAnonIncludeArgMismatch3() { ITemplateErrorListener errors = new ErrorBuffer(); string template = "<a:{x|foo},{bar}>"; CompiledTemplate code = new TemplateCompiler(new TemplateGroup() { ErrorManager = new ErrorManager(errors) }).Compile(template); string expected = "1:11: anonymous template has 0 arg(s) but mapped across 1 value(s)" + newline; Assert.AreEqual(expected, errors.ToString()); }
public OpenDocumentTemplate(byte[] document, string arguments, char leftDelimiter = DefaultLeftDelimiter, char rightDelimiter = DefaultRightDelimiter) { this.fileByFileName = new Dictionary <string, byte[]>(); using (var zip = new MemoryStream(document)) { using (var archive = new ZipArchive(zip, ZipArchiveMode.Read)) { foreach (var entry in archive.Entries) { using (var memoryStream = new MemoryStream()) { using (var zipStream = entry.Open()) { zipStream.CopyTo(memoryStream); var file = memoryStream.ToArray(); if (entry.FullName.Equals(ContentFileName)) { var errorBuffer = new ErrorBuffer(); var content = new OpenDocumentTemplateContent(file, leftDelimiter, rightDelimiter); var stringTemplate = content.ToStringTemplate(); var group = MainTemplateName + "(" + arguments + ")" + stringTemplate; this.templateGroup = new TemplateGroupString(MainTemplateName, group, leftDelimiter, rightDelimiter) { ErrorManager = new ErrorManager(errorBuffer) }; // Force a compilation of the templates to check for errors this.templateGroup.GetInstanceOf(MainTemplateName); if (errorBuffer.Errors.Count > 0) { throw new TemplateException(errorBuffer.Errors); } } else if (entry.FullName.Equals(MetaFileName)) { this.manifest = file; } else { this.fileByFileName.Add(entry.FullName, file); } } } } } } }
public void TestStringTypeMismatch2() { ErrorBuffer errors = new ErrorBuffer(); TemplateGroup group = new TemplateGroup(); group.Listener = errors; Template e = new Template(group, "<strlen(s)>"); e.Add("s", 34); e.Render(); // generate the error string errorExpecting = "context [anonymous] 1:1 function strlen expects a string not System.Int32" + newline; Assert.AreEqual(errorExpecting, errors.ToString()); }
public void TestIllegalOption() { ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; STGroup group = new STGroup(); group.DefineTemplate(new TemplateName("test"), "<name; bad=\"ugly\">"); ST st = group.GetInstanceOf("test"); st.Add("name", "Ter"); String expected = "Ter"; String result = st.Render(); Assert.AreEqual(expected, result); expected = "1:7: no such option: bad" + newline; Assert.AreEqual(expected, errors.ToString()); }
public void TestArg() { String templates = "foo(a,) ::= << >>\n"; WriteFile(tmpdir, "t.stg", templates); STGroup group = null; var errors = new ErrorBuffer(); group = new STGroupFile(Path.Combine(tmpdir, "t.stg")); ErrorManager.ErrorListener = errors; group.Load(); // force load String expected = "t.stg 1:6: missing ID at ')'" + newline; String result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestCantDefineEmbeddedRegionAgain() { string dir = GetRandomDir(); string g = "a() ::= <<[<@r>foo<@end>]>>\n" + "@a.r() ::= <<bar>>\n"; // error; dup WriteFile(dir, "g.stg", g); TemplateGroup group = new TemplateGroupFile(Path.Combine(dir, "g.stg")); ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; group.Load(); string expected = "2:3: region a.r is embedded and thus already implicitly defined" + newline; string result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestEmptyDictionary() { string templates = "d ::= []\n"; writeFile(tmpdir, "t.stg", templates); TemplateGroupFile group = null; ErrorBuffer errors = new ErrorBuffer(); group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); group.Listener = errors; group.Load(); // force load Assert.AreEqual(0, errors.Errors.Count); }
public void TestLineBreakWithScarfedTrailingNewline() { writeFile(tmpdir, "t.stg", "a(x) ::= <<<\\\\>\r\n>>"); // \r\n removed as trailing whitespace ErrorBuffer errors = new ErrorBuffer(); TemplateGroup group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; Template st = group.GetInstanceOf("a"); Assert.AreEqual("t.stg 1:15: Missing newline after newline escape <\\\\>" + newline, errors.ToString()); st.Add("x", "parrt"); string expected = ""; string result = st.Render(); Assert.AreEqual(expected, result); }
public static void Visualize(this DebugTemplate template, ErrorManager errorManager, CultureInfo culture, int lineWidth) { ErrorBuffer errors = new ErrorBuffer(); template.impl.NativeGroup.Listener = errors; StringWriter @out = new StringWriter(); ITemplateWriter wr = new AutoIndentWriter(@out); wr.LineWidth = lineWidth; Interpreter interp = new Interpreter(template.groupThatCreatedThisInstance, culture); interp.Execute(wr, template); // Render and track events TemplateVisualizer visualizer = new TemplateVisualizer(errorManager, template, @out.ToString(), interp, interp.GetExecutionTrace(), errors.Errors); visualizer.Show(); }
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 TestValidButOutOfPlaceChar() { string templates = "foo() ::= <<hi <.> mom>>\n"; writeFile(tmpdir, "t.stg", templates); ITemplateErrorListener errors = new ErrorBuffer(); TemplateGroupFile group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); group.Listener = errors; group.Load(); // force load string expected = "t.stg 1:15: doesn't look like an expression" + newline; string result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestUnknownAttr() { string templates = "t() ::= \"<x>\"\n"; ErrorBuffer errors = new ErrorBuffer(); writeFile(tmpdir, "t.stg", templates); TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg")); group.Listener = errors; Template st = group.GetInstanceOf("t"); string result = st.Render(); string expectedError = "context [/t] 1:1 attribute x isn't defined" + newline; Assert.AreEqual(expectedError, errors.ToString()); }
public void TestMissingRegionName() { string dir = tmpdir; string g = "@t.() ::= \"\"\n"; writeFile(dir, "g.stg", g); TemplateGroupFile group = new TemplateGroupFile(Path.Combine(dir, "g.stg")); ErrorBuffer errors = new ErrorBuffer(); group.Listener = errors; group.Load(); string expected = "g.stg 1:3: missing ID at '('" + newline; string result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestHiddenPropertyNotError() { 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 UserHiddenName("parrt")); st.Render(); String expected = ""; String result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestMissingTemplate() { string templates = "foo() ::= \n"; writeFile(tmpdir, "t.stg", templates); TemplateGroupFile group = null; ITemplateErrorListener errors = new ErrorBuffer(); group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; group.Load(); // force load string expected = "t.stg 2:0: missing template at '<EOF>'" + newline; string result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestMap2() { string templates = "d ::= [\"k\":]\n"; writeFile(tmpdir, "t.stg", templates); TemplateGroupFile group = null; ErrorBuffer errors = new ErrorBuffer(); group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; group.Load(); // force load string expected = "[t.stg 1:11: missing value for key at ']']"; string result = errors.Errors.ToListString(); Assert.AreEqual(expected, result); }
public void TestUnterminatedString() { string templates = "f() ::= \""; // extra } writeFile(tmpdir, "t.stg", templates); TemplateGroupFile group = null; ErrorBuffer errors = new ErrorBuffer(); group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; group.Load(); // force load string expected = "[t.stg 1:9: unterminated string, t.stg 1:9: missing template at '<EOF>']"; string result = errors.Errors.ToListString(); Assert.AreEqual(expected, result); }
public void TestMap3() { string templates = "d ::= [\"k\":{dfkj}}]\n"; // extra } writeFile(tmpdir, "t.stg", templates); TemplateGroupFile group = null; ErrorBuffer errors = new ErrorBuffer(); group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; group.Load(); // force load string expected = "[t.stg 1:17: invalid character '}']"; string result = errors.Errors.ToListString(); Assert.AreEqual(expected, result); }
public void TestArg() { string templates = "foo(a,) ::= << >>\n"; writeFile(tmpdir, "t.stg", templates); TemplateGroupFile group = null; ITemplateErrorListener errors = new ErrorBuffer(); group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; group.Load(); // force load string expected = "t.stg 1:6: mismatched input ')' expecting ID" + newline; string result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestErrorWithinTemplate() { string templates = "foo(a) ::= \"<a b>\"\n"; writeFile(tmpdir, "t.stg", templates); TemplateGroupFile group = null; ErrorBuffer errors = new ErrorBuffer(); group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; group.Load(); // force load string expected = "[t.stg 1:15: 'b' came as a complete surprise to me]"; string result = errors.Errors.ToListString(); Assert.AreEqual(expected, result); }
public void TestParen() { string templates = "foo( ::= << >>\n"; writeFile(tmpdir, "t.stg", templates); TemplateGroupFile group = null; ITemplateErrorListener errors = new ErrorBuffer(); group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; group.Load(); // force load string expected = "t.stg 1:5: no viable alternative at input '::='" + newline; string result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestNewlineInString() { string templates = "foo() ::= \"\nfoo\"\n"; writeFile(tmpdir, "t.stg", templates); TemplateGroupFile group = null; ITemplateErrorListener errors = new ErrorBuffer(); group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; group.Load(); // force load string expected = "t.stg 1:11: \\n in string" + newline; string result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestImportNotString() { string templates = "import Super.stg\n" + "foo() ::= <<>>\n"; writeFile(tmpdir, "t.stg", templates); ITemplateErrorListener errors = new ErrorBuffer(); TemplateGroup group = new TemplateGroupFile(tmpdir + "/" + "t.stg"); group.Listener = errors; group.Load(); // force load string expected = "t.stg 1:7: mismatched input 'Super' expecting STRING" + newline; string result = errors.ToString(); Assert.AreEqual(expected, result); }
public void TestDictEmptyDefaultValue() { string templates = "typeInit ::= [\"int\":\"0\", default:] " + newline + "var(type,name) ::= \"<type> <name> = <typeInit.(type)>;\"" + newline ; writeFile(tmpdir, "test.stg", templates); ErrorBuffer errors = new ErrorBuffer(); TemplateGroupFile group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg")); group.Listener = errors; group.Load(); string expected = "[test.stg 1:33: missing value for key at ']']"; string result = errors.Errors.ToListString(); Assert.AreEqual(expected, result); }
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 virtual void testIndentOfMultipleBlankLines() { string templates = "" + "group test;" + NL + "list(names) ::= <<" + " $names$" + NL + ">>" + NL; IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(DefaultTemplateLexer), errors); StringTemplate t = group.GetInstanceOf("list"); t.SetAttribute("names", "Terence\n\nis a maniac"); string expecting = "" + " Terence\n\n" + " is a maniac"; Assert.AreEqual(expecting, t.ToString()); }
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 virtual void testNullListGetsNoOutput() { StringTemplateGroup group = new StringTemplateGroup("test"); IStringTemplateErrorListener errors = new ErrorBuffer(); group.ErrorListener = errors; StringTemplate t = new StringTemplate(group, "begin\n" + "$users:{name: $it$}; separator=\", \"$\n" + "end\n"); //t.setAttribute("users", new Duh()); string expecting = "begin\nend\n"; string result = t.ToString(); Assert.AreEqual(expecting, result); }
public virtual void testNestedIndent() { string templates = "" + "group test;" + NL + "method(name,stats) ::= <<" + "void $name$() {" + NL + "\t$stats; separator=\"\\n\"$" + NL + "}" + NL + ">>" + NL + "ifstat(expr,stats) ::= <<" + NL + "if ($expr$) {" + NL + " $stats; separator=\"\\n\"$" + NL + "}" + ">>" + NL + "assign(lhs,expr) ::= <<$lhs$=$expr$;>>" + NL; IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(DefaultTemplateLexer), errors); StringTemplate t = group.GetInstanceOf("method"); t.SetAttribute("name", "foo"); StringTemplate s1 = group.GetInstanceOf("assign"); s1.SetAttribute("lhs", "x"); s1.SetAttribute("expr", "0"); StringTemplate s2 = group.GetInstanceOf("ifstat"); s2.SetAttribute("expr", "x>0"); StringTemplate s2a = group.GetInstanceOf("assign"); s2a.SetAttribute("lhs", "y"); s2a.SetAttribute("expr", "x+y"); StringTemplate s2b = group.GetInstanceOf("assign"); s2b.SetAttribute("lhs", "z"); s2b.SetAttribute("expr", "4"); s2.SetAttribute("stats", s2a); s2.SetAttribute("stats", s2b); t.SetAttribute("stats", s1); t.SetAttribute("stats", s2); string expecting = "" + "void foo() {" + NL + "\tx=0;" + NL + "\tif (x>0) {" + NL + "\t y=x+y;" + NL + "\t z=4;" + NL + "\t}" + NL + "}"; Assert.AreEqual(expecting, t.ToString()); }
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 testNoDotsInTemplateNames() { IStringTemplateErrorListener errors = new ErrorBuffer(); string templates = "" + "group test;" + NL + "a.b() ::= <<foo>>" + NL ; //string error = null; StringTemplateGroup group = new StringTemplateGroup( new StringReader(templates), typeof(DefaultTemplateLexer), errors); string expecting = "template group parse error: line 2:1: unexpected token:"; Console.Error.WriteLine("errors.ToString() = '{0}'", errors.ToString()); Console.Error.WriteLine("expecting = '{0}'", expecting); Assert.IsTrue(errors.ToString().StartsWith(expecting)); }
public virtual void testGroupSatisfiesSingleInterface() { // this also tests the group loader IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup.RegisterGroupLoader(new CommonGroupLoader(errors, TEMPDIR)); string groupIStr = "" + "interface testI;" + NL + "t();" + NL + "bold(item);" + NL + "optional duh(a,b,c);" + NL; WriteFile(TEMPDIR, "testI.sti", groupIStr); string templates = "" + "group testG implements testI;" + NL + "t() ::= <<foo>>" + NL + "bold(item) ::= <<foo>>" + NL + "duh(a,b,c) ::= <<foo>>" + NL; WriteFile(TEMPDIR, "testG.stg", templates); file1 = new StreamReader(Path.Combine(TEMPDIR, "testG.stg")); StringTemplateGroup group = new StringTemplateGroup(file1, errors); string expecting = ""; // should be no errors Assert.AreEqual(expecting, errors.ToString()); }
public void testImplicitOverriddenRegionRedefError() { string templates1 = "" + "group super;" + NL + "a() ::= \"X<@r()>Y\"" + "@a.r() ::= \"foo\"" + NL; StringTemplateGroup group = new StringTemplateGroup( new StringReader(templates1)); string templates2 = "" + "group sub;" + NL + "@a.r() ::= \"foo\"" + NL + "@a.r() ::= \"bar\"" + NL; IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup subGroup = new StringTemplateGroup( new StringReader(templates2), errors, group); StringTemplate st = subGroup.GetInstanceOf("a"); string result = errors.ToString(); string expecting = "group sub line 3: redefinition of template region: @a.r"; Assert.AreEqual(expecting, result); }
public virtual void testGroupExtendsSuperGroup() { // this also tests the group loader IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup.RegisterGroupLoader( new CommonGroupLoader(errors, TEMPDIR) ); string superGroup = "" + "group superG;" + NL + "bold(item) ::= <<*<item>*>>;\n" + NL; WriteFile(TEMPDIR, "superG.stg", superGroup); string templates = "" + "group testG : superG;" + NL + "main(x) ::= <<$bold(x)$>>" + NL; WriteFile(TEMPDIR, "testG.stg", templates); file1 = new StreamReader(Path.Combine(TEMPDIR, "testG.stg")); StringTemplateGroup group = new StringTemplateGroup(file1, typeof(DefaultTemplateLexer), errors); StringTemplate st = group.GetInstanceOf("main"); st.SetAttribute("x", "foo"); string expecting = "*foo*"; Assert.AreEqual(expecting, st.ToString()); }
public void testEmbeddedRegionRedefError() { // cannot define an embedded template within group string templates = "" + "group test;" + NL + "a() ::= \"X<@r>dork<@end>Y\"" + "@a.r() ::= \"foo\"" + NL; IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), errors); StringTemplate st = group.GetInstanceOf("a"); st.ToString(); string result = errors.ToString(); string expecting = "group test line 2: redefinition of template region: @a.r"; Assert.AreEqual(expecting, result); }
public virtual void testCannotFindInterfaceFile() { // this also tests the group loader IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup.RegisterGroupLoader(new CommonGroupLoader(errors, TEMPDIR)); string templates = "" + "group testG implements blort;" + NL + "t() ::= <<foo>>" + NL + "bold(item) ::= <<foo>>" + NL + "duh(a,b,c) ::= <<foo>>" + NL; WriteFile(TEMPDIR, "testG.stg", templates); file1 = new StreamReader(Path.Combine(TEMPDIR, "testG.stg")); StringTemplateGroup group = new StringTemplateGroup(file1, errors); string expecting = "no such interface file 'blort.sti'"; Assert.AreEqual(expecting, errors.ToString()); }
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 virtual void testParallelAttributeIterationWithMismatchArgListSizes() { IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplate e = new StringTemplate("$names,phones,salaries:{n,p | $n$@$p$}; separator=\", \"$"); e.ErrorListener = errors; e = e.GetInstanceOf(); e.SetAttribute("names", "Ter"); e.SetAttribute("names", "Tom"); e.SetAttribute("phones", "1"); e.SetAttribute("phones", "2"); e.SetAttribute("salaries", "big"); string expecting = "Ter@1, Tom@2"; Assert.AreEqual(expecting, e.ToString()); string errorExpecting = "number of arguments [n, p] mismatch between attribute list and anonymous template in context [anonymous]"; Assert.AreEqual(errorExpecting, errors.ToString()); }
public void TestStringTypeMismatch2() { ErrorBuffer errors = new ErrorBuffer(); ErrorManager.ErrorListener = errors; ST e = new ST("<strlen(s)>"); e.Add("s", 34); e.Render(); // generate the error String errorExpecting = "context [anonymous] 1:1 function strlen expects a string not System.Int32" + newline; Assert.AreEqual(errorExpecting, errors.ToString()); }
public virtual void testParallelAttributeIterationWithMissingArgs() { IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplate e = new StringTemplate("$names,phones,salaries:{$n$@$p$}; separator=\", \"$"); e.ErrorListener = errors; e = e.GetInstanceOf(); e.SetAttribute("names", "Tom"); e.SetAttribute("phones", "2"); e.SetAttribute("salaries", "big"); e.ToString(); // generate the error string errorExpecting = "missing arguments in anonymous template in context [anonymous]"; Assert.AreEqual(errorExpecting, errors.ToString()); }
public virtual void testEmptyIteratedValueGetsSeparator() { StringTemplateGroup group = new StringTemplateGroup("test"); IStringTemplateErrorListener errors = new ErrorBuffer(); group.ErrorListener = errors; StringTemplate t = new StringTemplate(group, "$names; separator=\",\"$"); t.SetAttribute("names", "Terence"); t.SetAttribute("names", ""); t.SetAttribute("names", ""); t.SetAttribute("names", "Tom"); t.SetAttribute("names", "Frank"); t.SetAttribute("names", ""); // empty values get separator still string expecting = "Terence,,,Tom,Frank,"; string result = t.ToString(); Assert.AreEqual(expecting, result); }
public virtual void testSingleExprTemplateArgumentError() { string templates = "" + "group test;" + NL + "test(name) ::= \"<bold(name)>\"" + NL + "bold(item,ick) ::= \"*<item>*\"" + NL; IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer), errors); StringTemplate e = group.GetInstanceOf("test"); e.SetAttribute("name", "Ter"); string result = e.ToString(); string expecting = "template bold must have exactly one formal arg in template context [test <invoke bold arg context>]"; Assert.AreEqual(expecting, errors.ToString()); }
public virtual void testEmptyExprAsFirstLineGetsNoOutput() { StringTemplateGroup group = new StringTemplateGroup("test"); IStringTemplateErrorListener errors = new ErrorBuffer(); group.ErrorListener = errors; group.DefineTemplate("bold", "<b>$it$</b>"); StringTemplate t = new StringTemplate(group, "$users$\n" + "end\n"); string expecting = "end\n"; string result = t.ToString(); Assert.AreEqual(expecting, result); }
public virtual void testSizeZeroOnLineWithIndentGetsNoOutput() { StringTemplateGroup group = new StringTemplateGroup("test"); IStringTemplateErrorListener errors = new ErrorBuffer(); group.ErrorListener = errors; StringTemplate t = new StringTemplate(group, "begin\n" + " $name$\n" + " $users:{name: $it$}$\n" + " $users:{name: $it$$\\n$}$\n" + "end\n"); string expecting = "begin\nend\n"; string result = t.ToString(); Assert.AreEqual(expecting, result); }
public void testImplicitRegionRedefError() { // cannot define an implicitly-defined template more than once string templates = "" + "group test;" + NL + "a() ::= \"X<@r()>Y\"" + NL + "@a.r() ::= \"foo\"" + NL + "@a.r() ::= \"bar\"" + NL; IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), errors); StringTemplate st = group.GetInstanceOf("a"); st.ToString(); string result = errors.ToString(); string expecting = "group test line 4: redefinition of template region: @a.r"; Assert.AreEqual(expecting, result); }
public virtual void testEmptyListNoIteratorGetsNoOutput() { StringTemplateGroup group = new StringTemplateGroup("test"); IStringTemplateErrorListener errors = new ErrorBuffer(); group.ErrorListener = errors; StringTemplate t = new StringTemplate(group, "begin\n" + "$users; separator=\", \"$\n" + "end\n"); t.SetAttribute("users", new ArrayList()); string expecting = "begin\nend\n"; string result = t.ToString(); Assert.AreEqual(expecting, result); }
public virtual void testSimpleIndentOfAttributeList() { string templates = "" + "group test;" + NL + "list(names) ::= <<" + @" $names; separator=""\n""$" + NL + ">>" + NL; IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(DefaultTemplateLexer), errors); StringTemplate t = group.GetInstanceOf("list"); t.SetAttribute("names", "Terence"); t.SetAttribute("names", "Jim"); t.SetAttribute("names", "Sriram"); string expecting = "" + " Terence" + NL + " Jim" + NL + " Sriram"; Assert.AreEqual(expecting, t.ToString()); }
public virtual void testListOfEmbeddedTemplateSeesEnclosingAttributes() { string templates = "" + "group test;" + NL + "output(cond,items) ::= <<page: $items$>>" + NL + "mybody() ::= <<$font()$stuff>>" + NL + "font() ::= <<$if(cond)$this$else$that$endif$>>"; IStringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(DefaultTemplateLexer), errors); StringTemplate outputST = group.GetInstanceOf("output"); StringTemplate bodyST1 = group.GetInstanceOf("mybody"); StringTemplate bodyST2 = group.GetInstanceOf("mybody"); StringTemplate bodyST3 = group.GetInstanceOf("mybody"); outputST.SetAttribute("items", bodyST1); outputST.SetAttribute("items", bodyST2); outputST.SetAttribute("items", bodyST3); string expecting = "page: thatstuffthatstuffthatstuff"; Assert.AreEqual(expecting, outputST.ToString()); }
public virtual void testComputedPropertyName() { StringTemplateGroup group = new StringTemplateGroup("test"); IStringTemplateErrorListener errors = new ErrorBuffer(); group.ErrorListener = errors; StringTemplate t = new StringTemplate(group, "variable property $propName$=$v.(propName)$"); t.SetAttribute("v", new Decl("i", "int")); t.SetAttribute("propName", "type"); string expecting = "variable property type=int"; string result = t.ToString(); Assert.AreEqual(errors.ToString(), ""); Assert.AreEqual(expecting, result); }
public virtual void testEmptyIteratedConditionalWithElseValueGetsSeparator() { StringTemplateGroup group = new StringTemplateGroup("test"); IStringTemplateErrorListener errors = new ErrorBuffer(); group.ErrorListener = errors; StringTemplate t = new StringTemplate(group, "$users:{$if(it.ok)$$it.name$$else$$endif$}; separator=\",\"$"); t.SetAttribute("users.{name,ok}", "Terence", (object)true); t.SetAttribute("users.{name,ok}", "Tom", (object)false); t.SetAttribute("users.{name,ok}", "Frank", (object)true); t.SetAttribute("users.{name,ok}", "Johnny", (object)false); // empty conditional values go get separator string expecting = "Terence,,Frank,"; string result = t.ToString(); Assert.AreEqual(expecting, result); }