Exemplo n.º 1
0
        public void TestDelimitersClause()
        {
            string templates =
                "delimiters \"$\", \"$\"" + newline +
                "method(name) ::= <<" + newline +
                "$stat(name)$" + newline +
                ">>" + newline +
                "stat(name,value=\"99\") ::= \"x=$value$; // $name$\"" + newline
            ;

            writeFile(tmpdir, "group.stg", templates);
            TemplateGroup group = new TemplateGroupFile(tmpdir + "/group.stg");
            Template      b     = group.GetInstanceOf("method");

            b.Add("name", "foo");
            string expecting = "x=99; // foo";
            string result    = b.Render();

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 2
0
        public void TestDictKeyLookupViaTemplate()
        {
            // Make sure we try rendering stuff to string if not found as regular object
            string templates =
                "typeInit ::= [\"int\":{0<w>}, \"float\":{0.0<w>}] " + newline +
                "var(type,w,name) ::= \"<type> <name> = <typeInit.(type)>;\"" + newline
            ;

            writeFile(tmpdir, "test.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg"));
            Template      st    = group.GetInstanceOf("var");

            st.Add("w", "L");
            st.Add("type", new Template("int"));
            st.Add("name", "x");
            string expecting = "int x = 0L;";
            string result    = st.Render();

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 3
0
        public void TestLineDoesNotWrapDueToLiteral()
        {
            string templates =
                "m(args,body) ::= <<[TestMethod] public voidfoo(<args; wrap=\"\\n\",separator=\", \">) throws Ick { <body> }>>" + newline;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            Template a = group.GetInstanceOf("m");

            a.Add("args",
                  new string[] { "a", "b", "c" });
            a.Add("body", "i=3;");
            // make it wrap because of ") throws Ick { " literal
            int    n         = "[TestMethod] public voidfoo(a, b, c".Length;
            string expecting =
                "[TestMethod] public voidfoo(a, b, c) throws Ick { i=3; }";

            Assert.AreEqual(expecting, a.Render(n));
        }
Exemplo n.º 4
0
        public void TestListAsTemplateArgument()
        {
            string templates =
                "test(names,phones) ::= \"<foo([names,phones])>\"" + newline +
                "foo(items) ::= \"<items:{a | *<a>*}>\"" + newline
            ;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            Template      e     = group.GetInstanceOf("test");

            e.Add("names", "Ter");
            e.Add("names", "Tom");
            e.Add("phones", "1");
            e.Add("phones", "2");
            string expecting = "*Ter**Tom**1**2*";
            string result    = e.Render();

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 5
0
        public void TestInstanceofRenderer()
        {
            string templates =
                "numberThing(x,y,z) ::= \"numbers: <x>, <y>; <z>\"\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            group.RegisterRenderer(typeof(int), new NumberRenderer());
            group.RegisterRenderer(typeof(double), new NumberRenderer());
            Template st = group.GetInstanceOf("numberThing");

            st.Add("x", -2100);
            st.Add("y", 3.14159);
            st.Add("z", "hi");
            string expecting = "numbers: -2100, 3.14159; hi";
            string result    = st.Render();

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 6
0
        public void TestLineWrapLastCharIsNewline()
        {
            string templates =
                "duh(chars) ::= <<" + newline +
                "<chars; wrap=\"\\n\"\\>" + newline +
                ">>" + newline;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            Template a = group.GetInstanceOf("duh");

            a.Add("chars", new string[] { "a", "b", "\n", "d", "e" });
            // don't do \n if it's last element anyway
            string expecting =
                "ab" + Environment.NewLine +
                "de";

            Assert.AreEqual(expecting, a.Render(3));
        }
Exemplo n.º 7
0
        public void TestLocaleWithNumberRenderer()
        {
            //string templates = "foo(x,y) ::= << <x; format=\"%,d\"> <y; format=\"%,2.3f\"> >>\n";
            string templates = "foo(x,y) ::= << <x; format=\"{0:#,#}\"> <y; format=\"{0:0.000}\"> >>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            group.RegisterRenderer(typeof(int), new NumberRenderer());
            group.RegisterRenderer(typeof(double), new NumberRenderer());
            Template st = group.GetInstanceOf("foo");

            st.Add("x", -2100);
            st.Add("y", 3.14159);
            // Polish uses ' ' (ASCII 160) for ',' and ',' for '.'
            string expecting = " -2 100 3,142 "; // Ê
            string result    = st.Render(new CultureInfo("pl"));

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 8
0
        public void TestDictionaryBehaviorEmptyList()
        {
            string templates =
                "d ::= [\n" +
                "	\"x\" : [],\n"+
                "	default : false,\n"+
                "]\n" +
                "\n" +
                "t() ::= <<\n" +
                "<d.(\"x\")><if(d.(\"x\"))>+<else>-<endif>\n" +
                ">>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group    = new TemplateGroupFile(tmpdir + Path.DirectorySeparatorChar + "t.stg");
            Template      st       = group.GetInstanceOf("t");
            string        expected = "-";
            string        result   = st.Render();

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 9
0
        public void TestStringRendererWithTemplateInclude_cap()
        {
            // must toString the t() ref before applying format
            string templates =
                "foo(x) ::= << <(t()); format=\"cap\"> >>\n" +
                "t() ::= <<ack>>\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            //Interpreter.trace = true;
            group.RegisterRenderer(typeof(string), new StringRenderer());
            Template st = group.GetInstanceOf("foo");

            st.Add("x", "hi");
            string expecting = " Ack ";
            string result    = st.Render();

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 10
0
        public void TestDefaultArgumentAsTemplate2()
        {
            string templates =
                "method(name,size) ::= <<" + newline +
                "<stat(name)>" + newline +
                ">>" + newline +
                "stat(name,value={ [<name>] }) ::= \"x=<value>; // <name>\"" + newline
            ;

            writeFile(tmpdir, "group.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "group.stg"));
            Template      b     = group.GetInstanceOf("method");

            b.Add("name", "foo");
            b.Add("size", "2");
            string expecting = "x=[foo] ; // foo"; // won't see ' ' after '=' since it's an indent not simple string
            string result    = b.Render();

            //System.err.println("result='"+result+"'");
            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 11
0
        public void TestSubtemplateAsDefaultArg()
        {
            string templates =
                "t(x,y={<x:{s|<s><s>}>}) ::= <<\n" +
                "x: <x>\n" +
                "y: <y>\n" +
                ">>" + newline
            ;

            writeFile(tmpdir, "group.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "group.stg"));
            Template      b     = group.GetInstanceOf("t");

            b.Add("x", "a");
            string expecting =
                "x: a" + newline +
                "y: aa";
            string result = b.Render();

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 12
0
        public void VisualizerTestDefaultArgumentTemplate()
        {
            string templates =
                "main() ::= <<\n" +
                "<f(p=\"x\")>*<f(p=\"y\")>\n" +
                ">>\n" +
                "\n" +
                "f(p,q={<({a<p>})>}) ::= <<\n" +
                "-<q>-\n" +
                ">>";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            group.TrackCreationEvents = true;
            Template template = group.GetInstanceOf("main");
            string   result   = template.Render();

            Assert.AreEqual("-ax-*-ay-", result);
            template.Visualize();
        }
Exemplo n.º 13
0
        public void TestImportStatementWithFile()
        {
            string dir       = tmpdir;
            string groupFile =
                "import \"" + dir + "/group2.stg\"\n" +
                "a() ::= \"g1 a\"\n" +
                "b() ::= \"<c()>\"\n";

            writeFile(dir, "group1.stg", groupFile);

            groupFile =
                "c() ::= \"g2 c\"\n";
            writeFile(dir, "group2.stg", groupFile);

            TemplateGroup group1   = new TemplateGroupFile(dir + "/group1.stg");
            Template      st       = group1.GetInstanceOf("c"); // should see c()
            string        expected = "g2 c";
            string        result   = st.Render();

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 14
0
        public void TestSimpleIndentOfAttributeList()
        {
            string templates =
                "list(names) ::= <<" +
                "  <names; separator=\"\\n\">" + newline +
                ">>" + newline;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
            Template      t     = group.GetInstanceOf("list");

            t.Add("names", "Terence");
            t.Add("names", "Jim");
            t.Add("names", "Sriram");
            string expecting =
                "  Terence" + newline +
                "  Jim" + newline +
                "  Sriram";

            Assert.AreEqual(expecting, t.Render());
        }
Exemplo n.º 15
0
        public void TestLineWrapCharAfterWrapIsNewline()
        {
            string templates =
                "duh(chars) ::= <<<chars; wrap=\"\\n\"\\>>>" + newline;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            Template a = group.GetInstanceOf("duh");

            a.Add("chars", new string[] { "a", "b", "c", "\n", "d", "e" });
            // Once we wrap, we must dump chars as we see them.  A newline right
            // after a wrap is just an "unfortunate" event.  People will expect
            // a newline if it's in the data.
            string expecting =
                "abc" + Environment.NewLine +
                "" + Environment.NewLine +
                "de";

            Assert.AreEqual(expecting, a.Render(3));
        }
Exemplo n.º 16
0
        public void TestEmbeddedSubtemplate()
        {
            // fix so we ignore inside {...}
            string dir       = tmpdir;
            string groupFile =
                "a() ::= <<\n" +
                "[\n" +
                "  <{\n" +
                "  bar\n" +
                "  }>\n" +
                "]\n" +
                ">>\n";

            writeFile(dir, "group.stg", groupFile);
            TemplateGroup group    = new TemplateGroupFile(Path.Combine(dir, "group.stg"));
            Template      st       = group.GetInstanceOf("a");
            string        expected = "[" + newline + "  bar" + newline + "]";
            string        result   = st.Render();

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 17
0
        public void TestIndentBeyondLineWidth()
        {
            string templates =
                "duh(chars) ::= <<    <chars; wrap=\"\\n\"\\>>>" + newline;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            Template a = group.GetInstanceOf("duh");

            a.Add("chars", new string[] { "a", "b", "c", "d", "e" });
            //
            string expecting =
                "    a" + Environment.NewLine +
                "    b" + Environment.NewLine +
                "    c" + Environment.NewLine +
                "    d" + Environment.NewLine +
                "    e";

            Assert.AreEqual(expecting, a.Render(2));
        }
Exemplo n.º 18
0
        public void TestUndefinedAttr()
        {
            ErrorBuffer errors = new ErrorBuffer();

            string templates =
                "t() ::= \"<u()>\"\n" +
                "u() ::= \"<x>\"\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(tmpdir + "/" + "t.stg");

            group.Listener = errors;
            group.Debug    = true;
            Template 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);
        }
Exemplo n.º 19
0
        public void TestUnknownRegionDefError()
        {
            string dir = tmpdir;
            string g   =
                "a() ::= <<\n" +
                "X<@r()>Y" +
                ">>\n" +
                "@a.q() ::= \"foo\"" + newline;
            ITemplateErrorListener errors = new ErrorBuffer();

            writeFile(dir, "g.stg", g);
            TemplateGroupFile group = new TemplateGroupFile(Path.Combine(dir, "g.stg"));

            group.Listener = errors;
            Template st = group.GetInstanceOf("a");

            st.Render();
            string result    = errors.ToString();
            string expecting = "g.stg 3:3: template a doesn't have a region called q" + newline;

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 20
0
        public void TestParallelAttributeIterationWithDifferentSizesTemplateRefInsideToo()
        {
            string templates =
                "page(names,phones,salaries) ::= " + newline +
                "	<< <names,phones,salaries:{n,p,s | <value(n)>@<value(p)>: <value(s)>}; separator=\", \"> >>"+ newline +
                "value(x) ::= \"<if(!x)>n/a<else><x><endif>\"" + newline;

            writeFile(tmpdir, "g.stg", templates);

            TemplateGroup group = new TemplateGroupFile(tmpdir + "/g.stg");
            Template      p     = group.GetInstanceOf("page");

            p.Add("names", "Ter");
            p.Add("names", "Tom");
            p.Add("names", "Sriram");
            p.Add("phones", "1");
            p.Add("phones", "2");
            p.Add("salaries", "big");
            string expecting = " Ter@1: big, Tom@2: n/a, Sriram@n/a: n/a ";

            Assert.AreEqual(expecting, p.Render());
        }
Exemplo n.º 21
0
        public void TestAccessDictionaryFromAnonymousTemplateInRegion()
        {
            string dir = tmpdir;
            string g   =
                "a() ::= <<[<@r()>]>>\n" +
                "@a.r() ::= <<\n" +
                "<[\"foo\",\"a\"]:{x|<if(values.(x))><x><endif>}>\n" +
                ">>\n" +
                "values ::= [\n" +
                "    \"a\":false,\n" +
                "    default:true\n" +
                "]\n";

            writeFile(dir, "g.stg", g);

            TemplateGroup group    = new TemplateGroupFile(Path.Combine(dir, "g.stg"));
            Template      st       = group.GetInstanceOf("a");
            string        expected = "[foo]";
            string        result   = st.Render();

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 22
0
        public void TestDefineRegionInSubgroup()
        {
            string dir = tmpdir;
            string g1  = "a() ::= <<[<@r()>]>>\n";

            writeFile(dir, "g1.stg", g1);
            string g2 = "@a.r() ::= <%\n" +
                        "	foo\n\n\n"+
                        "%>\n";

            writeFile(dir, "g2.stg", g2);

            TemplateGroup group1 = new TemplateGroupFile(Path.Combine(dir, "g1.stg"));
            TemplateGroup group2 = new TemplateGroupFile(Path.Combine(dir, "g2.stg"));

            group2.ImportTemplates(group1); // define r in g2
            Template st       = group2.GetInstanceOf("a");
            string   expected = "[foo]";
            string   result   = st.Render();

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 23
0
        public void TestUnknownNamedArg()
        {
            string dir       = tmpdir;
            string groupFile =
                "f(x,y) ::= \"<x><y>\"\n" +
                "g() ::= \"<f(x={a},z={b})>\"";

            //012345678901234567

            writeFile(dir, "group.stg", groupFile);
            TemplateGroupFile group  = new TemplateGroupFile(Path.Combine(dir, "group.stg"));
            ErrorBuffer       errors = new ErrorBuffer();

            group.Listener = errors;
            Template st = group.GetInstanceOf("g");

            st.Render();
            string expected = "context [/g] 1:1 attribute z isn't defined" + newline;
            string result   = errors.ToString();

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 24
0
        public void TestIndentedExpr()
        {
            string templates =
                "duh(chars) ::= <<" + newline +
                "    <chars; wrap=\"\\n\">" + newline +
                ">>" + newline;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            Template a = group.GetInstanceOf("duh");

            a.Add("chars", new string[] { "a", "b", "c", "d", "e" });
            //
            string expecting =
                "    ab" + Environment.NewLine +
                "    cd" + Environment.NewLine +
                "    e";

            // width=4 spaces + 2 char.
            Assert.AreEqual(expecting, a.Render(6));
        }
Exemplo n.º 25
0
        public void TestFalseCondWithFormalArgs()
        {
            // insert of indent instr was not working; ok now
            string dir       = tmpdir;
            string groupFile =
                "a(scope) ::= <<\n" +
                "foo\n" +
                "    <if(scope)>oops<endif>\n" +
                "bar\n" +
                ">>\n";

            writeFile(dir, "group.stg", groupFile);
            TemplateGroupFile group = new TemplateGroupFile(dir + "/group.stg");
            Template          st    = group.GetInstanceOf("a");

            st.impl.Dump();
            string expected = "foo" + newline +
                              "bar";
            string result = st.Render();

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 26
0
        public void TestSoleArgUsingApplySyntax()
        {
            ErrorBuffer errors = new ErrorBuffer();

            string templates =
                "t() ::= \"<{9}:u()>\"\n" +
                "u(x,y) ::= \"<x>\"\n";

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            group.Listener = errors;
            Template st       = group.GetInstanceOf("t");
            string   expected = "9";
            string   result   = st.Render();

            Assert.AreEqual(expected, result);

            expected = "context [/t] 1:5 passed 1 arg(s) to template /u with 2 declared arg(s)" + newline;
            result   = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 27
0
        public void TestDictKeyLookupAsNonToStringableObject()
        {
            // Make sure we try rendering stuff to string if not found as regular object
            string templates =
                "foo(m,k) ::= \"<m.(k)>\"" + newline
            ;

            writeFile(tmpdir, "test.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg"));
            Template      st    = group.GetInstanceOf("foo");
            IDictionary <HashableUser, string> m = new Dictionary <HashableUser, string>();

            m[new HashableUser(99, "parrt")]     = "first";
            m[new HashableUser(172036, "tombu")] = "second";
            m[new HashableUser(391, "sriram")]   = "third";
            st.Add("m", m);
            st.Add("k", new HashableUser(172036, "tombu"));
            string expecting = "second";
            string result    = st.Render();

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 28
0
        public void TestWeCanResetAdaptorCacheInvalidatedUponAdaptorReset()
        {
            string templates =
                "foo(x) ::= \"<x.id>: <x.name>\"\n";

            writeFile(tmpdir, "foo.stg", templates);
            TemplateGroup group = new TemplateGroupFile(tmpdir + "/foo.stg");

            group.RegisterModelAdaptor(typeof(User), new UserAdaptor());
            group.GetModelAdaptor(typeof(User)); // get User, SuperUser into cache
            group.GetModelAdaptor(typeof(SuperUser));

            group.RegisterModelAdaptor(typeof(User), new UserAdaptorConst());
            // cache should be reset so we see new adaptor
            Template st = group.GetInstanceOf("foo");

            st.Add("x", new User(100, "parrt"));
            string expecting = "const id value: const name value"; // sees UserAdaptorConst
            string result    = st.Render();

            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 29
0
        public void TestLineWrapWithDiffAnchor()
        {
            string templates =
                "array(values) ::= <<int[] a = { <{1,9,2,<values; wrap, separator=\",\">}; anchor> };>>" + newline;

            writeFile(tmpdir, "t.stg", templates);
            TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));

            Template a = group.GetInstanceOf("array");

            a.Add("values",
                  new int[] { 3, 9, 20, 2, 1, 4, 6, 32, 5, 6, 77, 888, 2, 1, 6, 32, 5, 6, 77,
                              4, 9, 20, 2, 1, 4, 63, 9, 20, 2, 1, 4, 6 });
            string expecting =
                "int[] a = { 1,9,2,3,9,20,2,1,4," + Environment.NewLine +
                "            6,32,5,6,77,888,2," + Environment.NewLine +
                "            1,6,32,5,6,77,4,9," + Environment.NewLine +
                "            20,2,1,4,63,9,20,2," + Environment.NewLine +
                "            1,4,6 };";

            Assert.AreEqual(expecting, a.Render(30));
        }
Exemplo n.º 30
0
        public void TestImportTemplateFileSameDir()
        {
            /*
             * dir
             *  group1.stg		(that imports c.st)
             *  c.st
             */
            string dir       = tmpdir;
            string groupFile =
                "import \"c.st\"\n" +
                "a() ::= \"g1 a\"\n" +
                "b() ::= \"<c()>\"\n";

            writeFile(dir, "group1.stg", groupFile);
            writeFile(dir, "c.st", "c() ::= \"c\"\n");

            TemplateGroup group1   = new TemplateGroupFile(Path.Combine(dir, "group1.stg"));
            Template      st       = group1.GetInstanceOf("c"); // should see c()
            string        expected = "c";
            string        result   = st.Render();

            Assert.AreEqual(expected, result);
        }