public virtual void testParenthesizedExpression() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>"); StringTemplate t = new StringTemplate(group, "$(first+last):bold()$"); t.setAttribute("first", "Joe"); t.setAttribute("last", "Schmoe"); //System.out.println(t); String expecting = "<b>JoeSchmoe</b>"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testApplyTemplateToSingleValuedAttributeWithDefaultAttribute() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>"); StringTemplate name = new StringTemplate(group, "$name:bold()$"); name.setAttribute("name", "Terence"); Assert.AreEqual(name.ToString(), "<b>Terence</b>"); }
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 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 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 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 testStringLiteralAsAttribute() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>"); StringTemplate name = new StringTemplate(group, "$\"Terence\":bold()$"); Assert.AreEqual(name.ToString(), "<b>Terence</b>"); }
public virtual void testCollectionAttributes() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>"); StringTemplate t = new StringTemplate(group, "$data$, $data:bold()$, " + "$list:bold():bold()$, $array$, $a2$, $a3$, $a4$"); ArrayList v = ArrayList.Synchronized(new ArrayList(10)); v.Add("1"); v.Add("2"); v.Add("3"); IList list = new ArrayList(); list.Add("a"); list.Add("b"); list.Add("c"); t.setAttribute("data", v); t.setAttribute("list", list); t.setAttribute("array", new String[]{"x", "y"}); t.setAttribute("a2", new int[]{10, 20}); t.setAttribute("a3", new float[]{1.2f, 1.3f}); t.setAttribute("a4", new double[]{8.7, 9.2}); //System.out.println(t); String expecting = "123, <b>1</b><b>2</b><b>3</b>, " + "<b><b>a</b></b><b><b>b</b></b><b><b>c</b></b>, xy, 1020, 1.21.3, 8.79.2"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testSimpleInheritance() { // make a bold template in the super group that you can inherit from sub StringTemplateGroup supergroup = new StringTemplateGroup("super"); StringTemplateGroup subgroup = new StringTemplateGroup("sub"); StringTemplate bold = supergroup.defineTemplate("bold", "<b>$it$</b>"); subgroup.setSuperGroup(supergroup); StringTemplateErrorListener errors = new ErrorBuffer(); subgroup.setErrorListener(errors); supergroup.setErrorListener(errors); StringTemplate duh = new StringTemplate(subgroup, "$name:bold()$"); duh.setAttribute("name", "Terence"); String expecting = "<b>Terence</b>"; Assert.AreEqual(duh.ToString(), expecting); }
public virtual void testStringCatenationOnSingleValuedAttribute() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate bold = group.defineTemplate("bold", "<b>$it$</b>"); StringTemplate a = new StringTemplate(group, "$name+\" Parr\":bold()$"); StringTemplate b = new StringTemplate(group, "$bold(it=name+\" Parr\")$"); a.setAttribute("name", "Terence"); b.setAttribute("name", "Terence"); String expecting = "<b>Terence Parr</b>"; Assert.AreEqual(a.ToString(), expecting); Assert.AreEqual(b.ToString(), expecting); }
public virtual void testRepeatedApplicationOfTemplateToSingleValuedAttribute() { StringTemplateGroup group = new StringTemplateGroup("dummy", "."); StringTemplate search = group.defineTemplate("bold", "<b>$it$</b>"); StringTemplate item = new StringTemplate(group, "$item:bold():bold()$"); item.setAttribute("item", "Jim"); Assert.AreEqual(item.ToString(), "<b><b>Jim</b></b>"); }
public virtual void testRepeatedApplicationOfTemplateToMultiValuedAttributeWithSeparator() { StringTemplateGroup group = new StringTemplateGroup("dummy", "."); StringTemplate search = group.defineTemplate("bold", "<b>$it$</b>"); StringTemplate item = new StringTemplate(group, "$item:bold():bold(); separator=\",\"$"); item.setAttribute("item", "Jim"); item.setAttribute("item", "Mike"); item.setAttribute("item", "Ashar"); // first application of template must yield another vector! //System.out.println("ITEM="+item); Assert.AreEqual(item.ToString(), "<b><b>Jim</b></b>,<b><b>Mike</b></b>,<b><b>Ashar</b></b>"); }
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 testChangingAttrValueRepeatedTemplateApplicationToVector() { StringTemplateGroup group = new StringTemplateGroup("dummy", "."); StringTemplate bold = group.defineTemplate("bold", "<b>$item$</b>"); StringTemplate italics = group.defineTemplate("italics", "<i>$it$</i>"); StringTemplate members = new StringTemplate(group, "$members:bold(item=it):italics(it=it)$"); members.setAttribute("members", "Jim"); members.setAttribute("members", "Mike"); members.setAttribute("members", "Ashar"); //System.out.println("members="+members); String expecting = "<i><b>Jim</b></i><i><b>Mike</b></i><i><b>Ashar</b></i>"; Assert.AreEqual(members.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 testChangingAttrValueTemplateApplicationToVector() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate italics = group.defineTemplate("italics", "<i>$x$</i>"); StringTemplate bold = group.defineTemplate("bold", "<b>$x$</b>"); StringTemplate t = new StringTemplate(group, "$names:bold(x=it)$"); t.setAttribute("names", "Terence"); t.setAttribute("names", "Tom"); //System.out.println("'"+t.toString()+"'"); String expecting = "<b>Terence</b><b>Tom</b>"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testTemplateNameExpression() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate foo = group.defineTemplate("foo", "hi there!"); StringTemplate t = new StringTemplate(group, "$(name)()$"); t.setAttribute("name", "foo"); //System.out.println(t); String expecting = "hi there!"; Assert.AreEqual(t.ToString(), expecting); }
public virtual void testComplicatedSeparatorExpr() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate bold = group.defineTemplate("bulletSeparator", "</li>$foo$<li>"); // make separator a complicated expression with args passed to included template StringTemplate t = new StringTemplate(group, "<ul>$name; separator=bulletSeparator(foo=\" \")+\" \"$</ul>"); t.setAttribute("name", "Ter"); t.setAttribute("name", "Tom"); t.setAttribute("name", "Mel"); //System.out.println(t); String expecting = "<ul>Ter</li> <li> Tom</li> <li> Mel</ul>"; Assert.AreEqual(t.ToString(), expecting); }
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 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 testApplyRepeatedAnonymousTemplateWithForeignTemplateRefToMultiValuedAttribute() { // 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, "start|$p:{$link(url=\"/member/view?ID=\"+it.ID, title=it.firstName)$ $if(it.canEdit)$canEdit$endif$}:" + "{$it$<br>\n}$|end"); duh.setAttribute("p", new Connector(this)); duh.setAttribute("p", new Connector2(this)); String newline = System.Environment.NewLine; //System.out.println(duh); String expecting = "start|<a href=\"/member/view?ID=1\"><b>Terence</b></a> <br>\n<a href=\"/member/view?ID=2\"><b>Tom</b></a> canEdit<br>\n|end"; Assert.AreEqual(duh.ToString(), expecting); }
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 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 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 testApplyTemplateNameExpression() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate bold = group.defineTemplate("foobar", "foo$attr$bar"); StringTemplate t = new StringTemplate(group, "$data:(name+\"bar\")()$"); t.setAttribute("data", "Ter"); t.setAttribute("data", "Tom"); t.setAttribute("name", "foo"); //System.out.println(t); String expecting = "fooTerbarfooTombar"; Assert.AreEqual(t.ToString(), 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 testParameterAndAttributeScoping() { StringTemplateGroup group = new StringTemplateGroup("test"); StringTemplate italics = group.defineTemplate("italics", "<i>$x$</i>"); StringTemplate bold = group.defineTemplate("bold", "<b>$x$</b>"); StringTemplate t = new StringTemplate(group, "$bold(x=italics(x=name))$"); t.setAttribute("name", "Terence"); //System.out.println(t); String expecting = "<b><i>Terence</i></b>"; Assert.AreEqual(t.ToString(), expecting); }