public virtual void testListOfEmbeddedTemplateSeesEnclosingAttributes() { String templates = "group test;" + newline + "output(cond,items) ::= <<page: $items$>>" + newline + "mybody() ::= <<$font()$stuff>>" + newline + "font() ::= <<$if(cond)$this$else$that$endif$>>"; StringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), 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(outputST.ToString(), expecting); }
public virtual void testNullIndirectTemplateApplication() { String templates = "group dork;" + newline + "" + newline + "test(names) ::= <<" + "<names:(ind)()>" + newline + ">>" + newline + "ind() ::= \"[<it>]\"" + newline; ; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer)); StringTemplate f = group.getInstanceOf("test"); f.setAttribute("names", "me"); f.setAttribute("names", "you"); String expecting = ""; Assert.AreEqual(f.ToString(), expecting); }
public virtual void testIndentOfMultipleBlankLines() { String templates = "group test;" + newline + "list(names) ::= <<" + " $names$" + newline + ">>" + newline; StringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), errors); StringTemplate t = group.getInstanceOf("list"); t.setAttribute("names", "Terence\n\nis a maniac"); String expecting = " Terence\n\n is a maniac"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testInheritArgumentFromRecursiveTemplateApplication() { // do not inherit attributes through formal args String templates = "group test;" + newline + "block(stats) ::= \"<stats>\"" + "ifstat(stats) ::= \"IF true then <stats>\"" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer)); StringTemplate b = group.getInstanceOf("block"); b.setAttribute("stats", group.getInstanceOf("ifstat")); b.setAttribute("stats", group.getInstanceOf("ifstat")); String expecting = "IF true then IF true then "; String result = b.ToString(); //System.err.println("result='"+result+"'"); Assert.AreEqual(result, expecting); }
public virtual void testGroupFileFormat() { String templates = "group test;" + newline + "t() ::= \"literal template\"" + newline + "bold(item) ::= \"<b>$item$</b>\"" + newline + "duh() ::= <<" + newline + "xx" + newline + ">>" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates)); String expecting = "group test;\nduh() ::= <<xx>>\nbold(item) ::= <<<b>$item$</b>>>\nt() ::= <<literal template>>\n"; Assert.AreEqual(group.ToString(), expecting); StringTemplate a = group.getInstanceOf("t"); expecting = "literal template"; Assert.AreEqual(a.ToString(), expecting); StringTemplate b = group.getInstanceOf("bold"); b.setAttribute("item", "dork"); expecting = "<b>dork</b>"; Assert.AreEqual(b.ToString(), expecting); }
public virtual void testIndentBetweenLeftJustifiedLiterals() { String templates = "group test;" + newline + "list(names) ::= <<" + "Before:" + newline + " $names; separator=\"\\n\"$" + newline + "after" + newline + ">>" + newline; StringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), errors); StringTemplate t = group.getInstanceOf("list"); t.setAttribute("names", "Terence"); t.setAttribute("names", "Jim"); t.setAttribute("names", "Sriram"); String expecting = "Before:" + newline + " Terence\n Jim\n Sriram" + newline + "after"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testTemplateParameterDecls() { String templates = "group test;" + newline + "t() ::= \"no args but ref $foo$\"" + newline + "t2(item) ::= \"decl but not used is ok\"" + newline + "t3(a,b,c,d) ::= <<$a$ $d$>>" + newline + "t4(a,b,c,d) ::= <<$a$ $b$ $c$ $d$>>" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates)); // check setting unknown arg in empty formal list StringTemplate a = group.getInstanceOf("t"); String error = null; try { a.setAttribute("foo", "x"); // want ArgumentOutOfRangeException } catch (System.ArgumentOutOfRangeException e) { error = e.Message; } String expecting = "no such attribute: foo in template t or in enclosing template\r\nParameter name: name"; Assert.AreEqual(error, expecting); // check setting known arg a = group.getInstanceOf("t2"); a.setAttribute("item", "x"); // shouldn't get exception // check setting unknown arg in nonempty list of formal args a = group.getInstanceOf("t3"); a.setAttribute("b", "x"); }
public virtual void testFormalArgumentAssignment() { String templates = "group test;" + newline + "page() ::= <<$body(font=\"Times\")$>>" + newline + "body(font) ::= \"<font face=$font$>my body</font>\"" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates)); StringTemplate t = group.getInstanceOf("page"); String expecting = "<font face=Times>my body</font>"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testTemplateAlias() { String templates = "group test;" + newline + "page(name) ::= \"name is <name>\"" + "other ::= page" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer)); StringTemplate b = group.getInstanceOf("other"); // alias for page b.setAttribute("name", "Ter"); String expecting = "name is Ter"; String result = b.ToString(); Assert.AreEqual(result, expecting); }
public virtual void testTemplateGetPropertyGetsAttribute() { // This test will cause infinite loop if missing attribute no // properly caught in getAttribute String templates = "group test;" + newline + "Cfile(funcs) ::= <<" + newline + "#include \\<stdio.h>" + newline + "<funcs:{public void <it.name>(<it.args>);}; separator=\"\\n\">" + newline + "<funcs; separator=\"\\n\">" + newline + ">>" + newline + "func(name,args,body) ::= <<" + newline + "public void <name>(<args>) {<body>}" + newline + ">>" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer)); StringTemplate b = group.getInstanceOf("Cfile"); StringTemplate f1 = group.getInstanceOf("func"); StringTemplate f2 = group.getInstanceOf("func"); f1.setAttribute("name", "f"); f1.setAttribute("args", ""); f1.setAttribute("body", "i=1;"); f2.setAttribute("name", "g"); f2.setAttribute("args", "int arg"); f2.setAttribute("body", "y=1;"); b.setAttribute("funcs", f1); b.setAttribute("funcs", f2); String expecting = "#include <stdio.h>" + newline + "public void f();\npublic void g(int arg);" + newline + "public void f() {i=1;}\npublic void g(int arg) {y=1;}"; Assert.AreEqual(b.ToString(), expecting); }
public virtual void testSuperTemplateRef() { // you can refer to a template defined in a super group via super.t() StringTemplateGroup group = new StringTemplateGroup("super"); StringTemplateGroup subGroup = new StringTemplateGroup("sub"); subGroup.setSuperGroup(group); group.defineTemplate("page", "$font()$:text"); group.defineTemplate("font", "Helvetica"); subGroup.defineTemplate("font", "$super.font()$ and Times"); StringTemplate st = subGroup.getInstanceOf("page"); String expecting = "Helvetica and Times:text"; Assert.AreEqual(st.ToString(), expecting); }
public virtual void testAngleBracketsWithGroupFile() { String templates = "group test;" + newline + "a(s) ::= \"<s:{case <i> : <it> break;}>\"" + newline + "b(t) ::= \"<t; separator=\\\",\\\">\"" + newline + "c(t) ::= << <t; separator=\",\"> >>" + newline; // mainly testing to ensure we don't get parse errors of above StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer), null); StringTemplate t = group.getInstanceOf("a"); t.setAttribute("s", "Test"); String expecting = "case 1 : Test break;"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testRecursion() { StringTemplateGroup group = new StringTemplateGroup("dummy", ".", typeof(AngleBracketTemplateLexer)); group.defineTemplate("tree", "<if(it.firstChild)>" + "( <it.text> <it.children:tree(); separator=\" \"> )" + "<else>" + "<it.text>" + "<endif>"); StringTemplate tree = group.getInstanceOf("tree"); // build ( a b (c d) e ) Tree root = new Tree("a"); root.addChild(new Tree("b")); Tree subtree = new Tree("c"); subtree.addChild(new Tree("d")); root.addChild(subtree); root.addChild(new Tree("e")); tree.setAttribute("it", root); String expecting = "( a b ( c d ) e )"; Assert.AreEqual(tree.ToString(), expecting); }
public virtual void testDeliberateRecursiveTemplateApplication() { // This test will cause infinite loop. block contains a stat which // contains the same block. Must be in lintMode to detect String templates = "group test;" + newline + "block(stats) ::= \"<stats>\"" + "ifstat(stats) ::= \"IF true then <stats>\"" + newline; StringTemplate.setLintMode(true); StringTemplate.resetTemplateCounter(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer)); StringTemplate b = group.getInstanceOf("block"); StringTemplate ifstat = group.getInstanceOf("ifstat"); b.setAttribute("stats", ifstat); // block has if stat ifstat.setAttribute("stats", b); // but make "if" contain block String expecting = "IF true then "; String expectingError = "infinite recursion to <ifstat([stats])@4> referenced in <block([attributes, stats])@3>; stack trace:\n<ifstat([stats])@4>, attributes=[stats=<block()@3>]>\n<block([attributes, stats])@3>, attributes=[attributes, stats=<ifstat()@4>], references=[stats]>\n<ifstat([stats])@4> (start of recursive cycle)\n..."; // note that attributes attribute doesn't show up in ifstat() because // recursion detection traps the problem before it writes out the // infinitely-recursive template; I set the attributes attribute right // before I render. String errors = ""; try { String result = b.ToString(); } catch (System.SystemException ise) { errors = ise.Message; } //System.err.println("errors="+errors+"'"); //System.err.println("expecting="+expectingError+"'"); StringTemplate.setLintMode(false); Assert.AreEqual(errors, expectingError); }
public virtual void testTemplatePolymorphism() { StringTemplateGroup group = new StringTemplateGroup("super"); StringTemplateGroup subGroup = new StringTemplateGroup("sub"); subGroup.setSuperGroup(group); // bold is defined in both super and sub // if you create an instance of page via the subgroup, // then bold() should evaluate to the subgroup not the super // even though page is defined in the super. Just like polymorphism. group.defineTemplate("bold", "<b>$it$</b>"); group.defineTemplate("page", "$name:bold()$"); subGroup.defineTemplate("bold", "<strong>$it$</strong>"); StringTemplate st = subGroup.getInstanceOf("page"); st.setAttribute("name", "Ter"); String expecting = "<strong>Ter</strong>"; Assert.AreEqual(st.ToString(), expecting); }
public virtual void testFindTemplateInCLASSPATH() { // Look for templates in CLASSPATH as resources StringTemplateGroup mgroup = new StringTemplateGroup("method stuff", typeof(AngleBracketTemplateLexer)); StringTemplate m = mgroup.getInstanceOf("test/method"); // "method.st" references body() so "body.st" will be loaded too m.setAttribute("visibility", "public"); m.setAttribute("name", "foobar"); m.setAttribute("returnType", "void"); m.setAttribute("statements", "i=1;"); // body inherits these from method m.setAttribute("statements", "x=i;"); String newline = System.Environment.NewLine; String expecting = "public void foobar() {" + newline + "\t// start of a body" + newline + "\ti=1;\n\tx=i;" + newline + "\t// end of a body" + newline + "}"; //System.out.println(m); Assert.AreEqual(m.ToString(), expecting); }
public virtual void testUndefinedArgumentAssignmentInApply() { String templates = "group test;" + newline + "page(name,x) ::= <<$name:bold(font=x)$>>" + newline + "bold() ::= \"<font face=$font$><b>$it$</b></font>\"" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates)); StringTemplate t = group.getInstanceOf("page"); t.setAttribute("x", "Times"); t.setAttribute("name", "Ter"); String error = ""; try { t.ToString(); } catch (System.ArgumentOutOfRangeException iae) { error = iae.Message; } String expecting = "no such attribute: font in template bold or in enclosing template\r\nParameter name: name"; Assert.AreEqual(error, expecting); }
public virtual void testFormalArgumentAssignmentInApply() { String templates = "group test;" + newline + "page(name) ::= <<$name:bold(font=\"Times\")$>>" + newline + "bold(font) ::= \"<font face=$font$><b>$it$</b></font>\"" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates)); StringTemplate t = group.getInstanceOf("page"); t.setAttribute("name", "Ter"); String expecting = "<font face=Times><b>Ter</b></font>"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testUndefinedDefaultAttributeReference() { String templates = "group test;" + newline + "page() ::= <<$bold()$>>" + newline + "bold() ::= \"$it$\"" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates)); StringTemplate t = group.getInstanceOf("page"); String error = ""; try { t.ToString(); } catch (System.ArgumentOutOfRangeException nse) { error = nse.Message; } String expecting = "no such attribute: it in template bold\r\nParameter name: attribute"; Assert.AreEqual(error, expecting); }
public virtual void testImmediateTemplateAsAttributeLoop() { // even though block has a stats value that refers to itself, // there is no recursion because each instance of block hides // the stats value from above since it's a formal arg. String templates = "group test;" + newline + "block(stats) ::= \"{<stats>}\""; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer)); StringTemplate b = group.getInstanceOf("block"); b.setAttribute("stats", group.getInstanceOf("block")); String expecting = "{{}}"; String result = b.ToString(); //System.err.println(result); Assert.AreEqual(result, expecting); }
public virtual void testWhiteSpaceAtEndOfTemplate() { StringTemplateGroup group = new StringTemplateGroup("group"); StringTemplate pageST = group.getInstanceOf("test/page"); StringTemplate listST = group.getInstanceOf("test/users.list"); // users.list references row.st which has a single blank line at the end. // I.e., there are 2 \n in a row at the end // ST should eat all whitespace at end listST.setAttribute("users", new Connector(this)); listST.setAttribute("users", new Connector2(this)); pageST.setAttribute("title", "some title"); pageST.setAttribute("body", listST); String expecting = "some title" + newline + "Terence [email protected] [email protected]"; String result = pageST.ToString(); //System.out.println("'"+result+"'"); Assert.AreEqual(result, expecting); }
public virtual void testIndentOfMultilineAttributes() { String templates = "group test;" + newline + "list(names) ::= <<" + " $names; separator=\"\n\"$" + newline + ">>" + newline; StringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), errors); StringTemplate t = group.getInstanceOf("list"); t.setAttribute("names", "Terence\nis\na\nmaniac"); t.setAttribute("names", "Jim"); t.setAttribute("names", "Sriram\nis\ncool"); String expecting = " Terence\n is\n a\n maniac\n Jim\n Sriram\n is\n cool"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testApplyingTemplateFromDiskWithPrecompiledIF() { String newline = System.Environment.NewLine; // write the template files first to /tmp DirectoryInfo tmpDir = new DirectoryInfo("tmp"); if (tmpDir.Exists) { tmpDir.Delete(true); } tmpDir.Create(); StreamWriter fw = new StreamWriter("tmp/page.st", false, System.Text.Encoding.Default); fw.Write("<html><head>" + newline); //fw.write(" <title>PeerScope: $title$</title>"+newline); fw.Write("</head>" + newline); fw.Write("<body>" + newline); fw.Write("$if(member)$User: $member:terse()$$endif$" + newline); fw.Write("</body>" + newline); fw.Write("</head>" + newline); fw.Close(); fw = new StreamWriter("tmp/terse.st", false, System.Text.Encoding.Default); fw.Write("$it.firstName$ $it.lastName$ (<tt>$it.email$</tt>)" + newline); fw.Close(); // specify a template to apply to an attribute // Use a template group so we can specify the start/stop chars StringTemplateGroup group = new StringTemplateGroup("dummy", "tmp"); StringTemplate a = group.getInstanceOf("page"); a.setAttribute("member", new Connector(this)); String expecting = "<html><head>" + newline + "</head>" + newline + "<body>" + newline + "User: Terence Parr (<tt>[email protected]</tt>)" + newline + "</body>" + newline + "</head>"; //System.out.println("'"+a+"'"); Assert.AreEqual(a.ToString(), expecting); }
public virtual void testIndirectTemplateApplication() { String templates = "group dork;" + newline + "" + newline + "test(name) ::= <<" + "<(name)()>" + newline + ">>" + newline + "first() ::= \"the first\"" + newline + "second() ::= \"the second\"" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer)); StringTemplate f = group.getInstanceOf("test"); f.setAttribute("name", "first"); String expecting = "the first"; Assert.AreEqual(f.ToString(), expecting); }
public virtual void testApplySuperTemplateRef() { StringTemplateGroup group = new StringTemplateGroup("super"); StringTemplateGroup subGroup = new StringTemplateGroup("sub"); subGroup.setSuperGroup(group); group.defineTemplate("bold", "<b>$it$</b>"); subGroup.defineTemplate("bold", "<strong>$it$</strong>"); subGroup.defineTemplate("page", "$name:super.bold()$"); StringTemplate st = subGroup.getInstanceOf("page"); st.setAttribute("name", "Ter"); String expecting = "<b>Ter</b>"; Assert.AreEqual(st.ToString(), expecting); }
public virtual void testLazyEvalOfSuperInApplySuperTemplateRef() { StringTemplateGroup group = new StringTemplateGroup("super"); StringTemplateGroup subGroup = new StringTemplateGroup("sub"); subGroup.setSuperGroup(group); group.defineTemplate("bold", "<b>$it$</b>"); subGroup.defineTemplate("bold", "<strong>$it$</strong>"); // this is the same as testApplySuperTemplateRef() test // 'cept notice that here the supergroup defines page // As long as you create the instance via the subgroup, // "super." will evaluate lazily (i.e., not statically // during template compilation) to the templates // getGroup().superGroup value. If I create instance // of page in group not subGroup, however, I will get // an error as superGroup is null for group "group". group.defineTemplate("page", "$name:super.bold()$"); StringTemplate st = subGroup.getInstanceOf("page"); st.setAttribute("name", "Ter"); String expecting = "<b>Ter</b>"; Assert.AreEqual(st.ToString(), expecting); }
public virtual void testComplicatedIndirectTemplateApplication() { String templates = "group Java;" + newline + "" + newline + "file(variables,methods) ::= <<" + "<variables:{<it.decl:(it.format)()>}; separator=\"\\n\">" + newline + "<methods>" + newline + ">>" + newline + "intdecl() ::= \"int <it.name> = 0;\"" + newline + "intarray() ::= \"int[] <it.name> = null;\"" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), typeof(AngleBracketTemplateLexer)); StringTemplate f = group.getInstanceOf("file"); f.setAttribute("variables.{decl,format}", new Decl("i", "int"), "intdecl"); f.setAttribute("variables.{decl,format}", new Decl("a", "int-array"), "intarray"); //System.out.println("f='"+f+"'"); String expecting = "int i = 0;\nint[] a = null;" + newline; Assert.AreEqual(f.ToString(), expecting); }
public virtual void testMissingInheritedAttribute() { String templates = "group test;" + newline + "page(title,font) ::= <<" + newline + "<html>" + newline + "<body>" + newline + "$title$<br>" + newline + "$body()$" + newline + "</body>" + newline + "</html>" + newline + ">>" + newline + "body() ::= \"<font face=$font$>my body</font>\"" + newline; StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates)); StringTemplate t = group.getInstanceOf("page"); t.setAttribute("title", "my title"); t.setAttribute("font", "Helvetica"); // body() will see it t.ToString(); // should be no problem }
public virtual void testNestedIndent() { String templates = "group test;" + newline + "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; StringTemplateErrorListener errors = new ErrorBuffer(); StringTemplateGroup group = new StringTemplateGroup(new StringReader(templates), 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() {" + newline + "\tx=0;\n\tif (x>0) {" + newline + "\t y=x+y;\n\t z=4;" + newline + "\t}" + newline + "}"; Assert.AreEqual(t.ToString(), expecting); }