Пример #1
0
 public TestEvalSpeed()
 {
     group = new StringTemplateGroup(new System.IO.StringReader(gString));
     /*
     // warm up the on-the-fly compiler
     for (int i=1; i<=2*N; i++) {
     testBaselineLiteral();
     testSingleLocalAttributeReference();
     testApplyTemplateToList();
     }
     */
 }
 public virtual void testAlternatingTemplateApplication()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     StringTemplate listItem = group.defineTemplate("listItem", "<li>$it$</li>");
     StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
     StringTemplate italics = group.defineTemplate("italics", "<i>$it$</i>");
     StringTemplate item = new StringTemplate(group, "$item:bold(),italics():listItem()$");
     item.setAttribute("item", "Jim");
     item.setAttribute("item", "Mike");
     item.setAttribute("item", "Ashar");
     //System.out.println("ITEM="+item);
     String expecting = "<li><b>Jim</b></li><li><i>Mike</i></li><li><b>Ashar</b></li>";
     Assert.AreEqual(item.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 testNestedAnonymousTemplates()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     String newline = System.Environment.NewLine;
     StringTemplate t = new StringTemplate(group, "$A:{" + newline + "<i>$it:{" + newline + "<b>$it$</b>" + newline + "}$</i>" + newline + "}$");
     t.setAttribute("A", "parrt");
     String expecting = newline + "<i>" + newline + "<b>parrt</b>" + newline + "</i>" + newline;
     Assert.AreEqual(t.ToString(), expecting);
 }
 public virtual void testEscapes()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
     String newline = System.Environment.NewLine;
     group.defineTemplate("foo", "$x$ && $it$");
     StringTemplate t = new StringTemplate(group, "$A:foo(x=\"dog\\\"\\\"\")$");
     StringTemplate u = new StringTemplate(group, "$A:foo(x=\"dog\\\"g\")$");
     StringTemplate v = new StringTemplate(group, "$A:{$it:foo(x=\"\\{dog\\}\\\"\")$ is cool}$");
     t.setAttribute("A", "ick");
     u.setAttribute("A", "ick");
     v.setAttribute("A", "ick");
     //System.out.println("t is '"+t.toString()+"'");
     //System.out.println("u is '"+u.toString()+"'");
     //System.out.println("v is '"+v.toString()+"'");
     String expecting = "dog\"\" && ick";
     Assert.AreEqual(t.ToString(), expecting);
     expecting = "dog\"g && ick";
     Assert.AreEqual(u.ToString(), expecting);
     expecting = "{dog}\" && ick is cool";
     Assert.AreEqual(v.ToString(), expecting);
 }
 public virtual void testMultipleAdditions()
 {
     // 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", ".");
     group.defineTemplate("link", "<a href=\"$url$\"><b>$title$</b></a>");
     StringTemplate duh = new StringTemplate(group, "$link(url=\"/member/view?ID=\"+ID+\"&x=y\"+foo, title=\"the title\")$");
     duh.setAttribute("ID", "3321");
     duh.setAttribute("foo", "fubar");
     String expecting = "<a href=\"/member/view?ID=3321&x=yfubar\"><b>the title</b></a>";
     Assert.AreEqual(duh.ToString(), expecting);
 }
        public virtual void testMultiValuedAttributeWithSeparator()
        {
            StringTemplate query;

            // if column can be multi-valued, specify a separator
            StringTemplateGroup group = new StringTemplateGroup("dummy", ".", typeof(AngleBracketTemplateLexer));
            query = new StringTemplate(group, "SELECT <distinct> <column; separator=\", \"> FROM <table>;");
            query.setAttribute("column", "name");
            query.setAttribute("column", "email");
            query.setAttribute("table", "User");
            // uncomment next line to make "DISTINCT" appear in output
            // query.setAttribute("distince", "DISTINCT");
            // System.out.println(query);
            Assert.AreEqual(query.ToString(), "SELECT  name, email FROM User;");
        }
 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 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 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 testIFBoolean()
        {
            StringTemplateGroup group = new StringTemplateGroup("dummy", ".");
            StringTemplate t = new StringTemplate(group, "$if(b)$x$endif$ $if(!b)$y$endif$");
            t.setAttribute("b", true);
            Assert.AreEqual(t.ToString(), "x ");

            t = t.getInstanceOf();
            t.setAttribute("b", false);
            Assert.AreEqual(t.ToString(), " y");
        }
 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 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 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 testExprInParens()
 {
     // 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", ".");
     StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>");
     StringTemplate duh = new StringTemplate(group, "$(\"blort: \"+(list)):bold()$");
     duh.setAttribute("list", "a");
     duh.setAttribute("list", "b");
     duh.setAttribute("list", "c");
     // System.out.println(duh);
     String expecting = "<b>blort: abc</b>";
     Assert.AreEqual(duh.ToString(), expecting);
 }
 public virtual void testExpressionAsRHSOfAssignment()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplate hostname = group.defineTemplate("hostname", "$machine$.jguru.com");
     StringTemplate bold = group.defineTemplate("bold", "<b>$x$</b>");
     StringTemplate t = new StringTemplate(group, "$bold(x=hostname(machine=\"www\"))$");
     String expecting = "<b>www.jguru.com</b>";
     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 testIFTemplate()
 {
     StringTemplateGroup group = new StringTemplateGroup("dummy", ".", typeof(AngleBracketTemplateLexer));
     StringTemplate t = new StringTemplate(group, "SELECT <column> FROM PERSON " + "<if(cond)>WHERE ID=<id><endif>;");
     t.setAttribute("column", "name");
     t.setAttribute("cond", "true");
     t.setAttribute("id", "231");
     Assert.AreEqual(t.ToString(), "SELECT name FROM PERSON WHERE ID=231;");
 }
 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 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 testMissingEndDelimiter()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplateErrorListener errors = new ErrorBuffer();
     group.setErrorListener(errors);
     StringTemplate t = new StringTemplate(group, "stuff $a then more junk etc...");
     String expectingError = "problem parsing template 'anonymous': line 1:31: expecting '$', found '<EOF>'";
     //System.out.println("error: '"+errors+"'");
     //System.out.println("expecting: '"+expectingError+"'");
     Assert.AreEqual(errors.ToString(), expectingError);
 }
 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 testMultiLevelInheritance()
 {
     // must loop up two levels to find bold()
     StringTemplateGroup rootgroup = new StringTemplateGroup("root");
     StringTemplateGroup level1 = new StringTemplateGroup("level1");
     StringTemplateGroup level2 = new StringTemplateGroup("level2");
     rootgroup.defineTemplate("bold", "<b>$it$</b>");
     level1.setSuperGroup(rootgroup);
     level2.setSuperGroup(level1);
     StringTemplateErrorListener errors = new ErrorBuffer();
     rootgroup.setErrorListener(errors);
     level1.setErrorListener(errors);
     level2.setErrorListener(errors);
     StringTemplate duh = new StringTemplate(level2, "$name:bold()$");
     duh.setAttribute("name", "Terence");
     String expecting = "<b>Terence</b>";
     Assert.AreEqual(duh.ToString(), 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 testMultiValuedAttributeWithAnonymousTemplateUsingIndexVariableI()
 {
     StringTemplateGroup tgroup = new StringTemplateGroup("dummy", ".");
     StringTemplate t = new StringTemplate(tgroup, " List:" + this.newline + "  " + this.newline + "foo" + this.newline + this.newline + "$names:{<br>$i$. $it$" + this.newline + "}$");
     t.setAttribute("names", "Terence");
     t.setAttribute("names", "Jim");
     t.setAttribute("names", "Sriram");
     String newline = System.Environment.NewLine;
     //System.out.println(t);
     String expecting = " List:" + newline + "  " + newline + "foo" + newline + newline + "<br>1. Terence" + newline + "<br>2. Jim" + newline + "<br>3. Sriram" + newline;
     Assert.AreEqual(t.ToString(), expecting);
 }
 public virtual void testEmptyIteratedConditionalWithElseValueGetsSeparator()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplateErrorListener errors = new ErrorBuffer();
     group.setErrorListener(errors);
     StringTemplate t = new StringTemplate(group, "$users:{$if(it.ok)$$it.name$$else$$endif$}; separator=\",\"$");
     t.setAttribute("users.{name,ok}", "Terence", true);
     t.setAttribute("users.{name,ok}", "Tom", false);
     t.setAttribute("users.{name,ok}", "Frank", true);
     t.setAttribute("users.{name,ok}", "Johnny", false);
     // empty conditional values get no separator
     String expecting = "Terence,,Frank,"; // haven't solved the last empty value problem yet
     String result = t.ToString();
     Assert.AreEqual(result, expecting);
 }
 public virtual void testAlternativeWriter()
 {
     System.Text.StringBuilder buf = new System.Text.StringBuilder();
     StringTemplateWriter w = new AnonymousClassStringTemplateWriter(buf, this);
     StringTemplateGroup group = new StringTemplateGroup("test");
     group.defineTemplate("bold", "<b>$x$</b>");
     StringTemplate name = new StringTemplate(group, "$name:bold(x=name)$");
     name.setAttribute("name", "Terence");
     name.write(w);
     Assert.AreEqual(buf.ToString(), "<b>Terence</b>");
 }
 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 testEmbeddedMultiLineIF()
        {
            StringTemplateGroup group = new StringTemplateGroup("test");
            StringTemplate main = new StringTemplate(group, "$sub$");
            StringTemplate sub = new StringTemplate(group, "begin" + newline + "$if(foo)$" + newline + "$foo$" + newline + "$else$" + newline + "blort" + newline + "$endif$" + newline);
            sub.setAttribute("foo", "stuff");
            main.setAttribute("sub", sub);
            String expecting = "begin" + newline + "stuff";
            Assert.AreEqual(main.ToString(), expecting);

            main = new StringTemplate(group, "$sub$");
            sub = sub.getInstanceOf();
            main.setAttribute("sub", sub);
            expecting = "begin" + newline + "blort";
            Assert.AreEqual(main.ToString(), expecting);
        }
 public virtual void testEmptyIteratedValueGetsSeparator()
 {
     StringTemplateGroup group = new StringTemplateGroup("test");
     StringTemplateErrorListener errors = new ErrorBuffer();
     group.setErrorListener(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(result, expecting);
 }