Exemplo n.º 1
0
 public void TestDictDefaultIsDefaultString()
 {
     string templates =
             "map ::= [default: \"default\"] " + newline +
             "t() ::= << <map.(\"1\")> >>" + newline
             ;
     WriteFile(tmpdir, "test.stg", templates);
     TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "test.stg"));
     Template st = group.GetInstanceOf("t");
     string expecting = " default ";
     string result = st.Render();
     Assert.AreEqual(expecting, result);
 }
Exemplo n.º 2
0
        public void TestDefineRegionInSameGroup()
        {
            string dir = GetRandomDir();
            string g = "a() ::= <<[<@r()>]>>\n" +
                       "@a.r() ::= <<foo>>\n";
            WriteFile(dir, "g.stg", g);

            TemplateGroup group = new TemplateGroupFile(dir + "/g.stg");
            Template st = group.GetInstanceOf("a");
            string expected = "[foo]";
            string result = st.Render();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
 public void TestDictDefaultValue()
 {
     string templates =
             "typeInit ::= [\"int\":\"0\", default:\"null\"] " + newline +
             "var(type,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("type", "UserRecord");
     st.Add("name", "x");
     string expecting = "UserRecord x = null;";
     string result = st.Render();
     Assert.AreEqual(expecting, result);
 }
Exemplo n.º 5
0
 public void TestDictDefaultStringAsKey()
 {
     string templates =
             "typeInit ::= [\"default\":\"foo\"] " + newline +
             "var(type,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("type", "default");
     st.Add("name", "x");
     string expecting = "default x = foo;";
     string result = st.Render();
     Assert.AreEqual(expecting, result);
 }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public void TestNumberRendererWithPrintfFormat()
        {
            string templates =
                    "foo(x,y) ::= << <x; format=\"F0\"> <y; format=\"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);
            string expecting = " -2100 3.142 ";
            string result = st.Render();
            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 8
0
        public void TestDefineRegionInSubgroup()
        {
            string dir = GetRandomDir();
            string g1 = "a() ::= <<[<@r()>]>>\n";
            WriteFile(dir, "g1.stg", g1);
            string g2 = "@a.r() ::= <<foo>>\n";
            WriteFile(dir, "g2.stg", g2);

            TemplateGroup group1 = new TemplateGroupFile(dir + "/g1.stg");
            TemplateGroup group2 = new TemplateGroupFile(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.º 9
0
        public void TestAttribute()
        {
            String templates =
                "t(x) ::= << <x> >>" + newline;

            WriteFile(tmpdir, "t.stg", templates);
            STGroup group = new STGroupFile(tmpdir + "/" + "t.stg");
            group.Debug = true;
            DebugTemplate template = (DebugTemplate)group.GetInstanceOf("t");
            IList<InterpEvent> events = template.GetEvents();
            String expected =
                "[EvalExprEvent{self=t(),start=0,stop=-1,expr=<x>}," +
                " EvalExprEvent{self=t(),start=0,stop=0,expr= }," +
                " EvalTemplateEvent{self=t(),start=0,stop=0}]";
            String result = ((IList)events).ToElementString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 10
0
        public void TestLocaleWithNumberRenderer()
        {
            string templates =
                    "foo(x,y) ::= << <x; format=\"N0\"> <y; format=\"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 ' ' for ',' and ',' for '.'
            string expecting = " -2 100 3,142 ";
            string result = st.Render(CultureInfo.GetCultureInfo("pl-PL"));
            Assert.AreEqual(expecting, result);
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        public void TestPassUnknownAttr()
        {
            String templates =
                "t() ::= \"<u(x={Ter})>\"\n" +
                "u(y) ::= <<hi <x>!>>\n";
            ErrorBuffer errors = new ErrorBuffer();
            ErrorManager.ErrorListener = errors;
            WriteFile(tmpdir, "t.stg", templates);
            STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg"));
            ST st = group.GetInstanceOf("t");
            String expected = "hi Ter!";
            String result = st.Render();
            Assert.AreEqual(expected, result);

            // check error now
            expected = "context [t u] can't set attribute x; template u has no such attribute" + newline;
            result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 14
0
        public void TestMissingSuperTemplate()
        {
            ErrorBuffer errors = new ErrorBuffer();
            ErrorManager.ErrorListener = errors;

            String templates =
                "t() ::= \"<super.t()>\"" + newline;

            WriteFile(tmpdir, "t.stg", templates);
            STGroup group = new STGroupFile(Path.Combine(tmpdir, "t.stg"));
            String templates2 =
                "u() ::= \"blech\"" + newline;

            WriteFile(tmpdir, "t2.stg", templates2);
            STGroup group2 = new STGroupFile(Path.Combine(tmpdir, "t2.stg"));
            group.ImportTemplates(group2);
            ST 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);
        }
Exemplo n.º 15
0
        public void TestDefineRegionInSubgroup2()
        {
            string dir = GetRandomDir();
            string g1 = "a() ::= <<[<@r()>]>>\n";
            WriteFile(dir, "g1.stg", g1);
            string g2 = "@a.r() ::= <<foo>>>\n";
            WriteFile(dir, "g2.stg", g2);

            TemplateGroup group1 = new TemplateGroupFile(dir + "/g1.stg");
            TemplateGroup group2 = new TemplateGroupFile(dir + "/g2.stg");
            group1.ImportTemplates(group2); // opposite of previous; g1 imports g2
            Template st = group1.GetInstanceOf("a");
            string expected = "[]"; // @a.r implicitly defined in g1; can't see g2's
            string result = st.Render();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 16
0
 public void TestReUseOfCat()
 {
     string templates =
         "a(mine,yours) ::= \"<b([mine,yours])>\"" + newline +
         "b(x) ::= \"<x>, <x>\"" + newline
         ;
     WriteFile(tmpdir, "t.stg", templates);
     TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
     Template e = group.GetInstanceOf("a");
     var mine = new ArrayList();
     mine.Add("Ter");
     mine.Add("Tom");
     e.Add("mine", mine);
     var yours = new ArrayList();
     yours.Add("Foo");
     e.Add("yours", yours);
     string expecting = "TerTomFoo, TerTomFoo";
     Assert.AreEqual(expecting, e.Render());
 }
Exemplo n.º 17
0
        public void TestNewlineInString()
        {
            String templates =
                "foo() ::= \"\nfoo\"\n";
            WriteFile(tmpdir, "t.stg", templates);

            STGroup group = null;
            var errors = new ErrorBuffer();
            group = new STGroupFile(tmpdir + "/" + "t.stg");
            ErrorManager.ErrorListener = errors;
            group.Load(); // force load
            String expected = "t.stg 1:11: \\n in string" + newline;
            String result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 18
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=\"n/a\") ::= \"<x>\"" + newline;
            WriteFile(tmpdir, "g.stg", templates);

            STGroup group = new STGroupFile(tmpdir + "/g.stg");
            ST 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.º 19
0
 public void TestRegion()
 {
     string dir = GetRandomDir();
     string groupFile =
         "a() ::= <<\n" +
         "[<@r()>]\n" +
         ">>\n";
     WriteFile(dir, "group.stg", groupFile);
     TemplateGroup group = new TemplateGroupFile(dir + "/group.stg");
     Template st = group.GetInstanceOf("a");
     string expected = "[]" + newline;
     string result = st.Render();
     Assert.AreEqual(expected, result);
 }
Exemplo n.º 20
0
        public void TestMap2()
        {
            String templates =
                "d ::= [\"k\":]\n";
            WriteFile(tmpdir, "t.stg", templates);

            STGroup group = null;
            ErrorBuffer errors = new ErrorBuffer();
            group = new STGroupFile(tmpdir + "/" + "t.stg");
            ErrorManager.ErrorListener = errors;
            group.Load(); // force load
            String expected = "t.stg 1:11: missing value for key at ']'" + newline;
            String result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 21
0
        public void TestTemplateCall()
        {
            String templates =
                "t(x) ::= <<[<u()>]>>\n" +
                "u() ::= << <x> >>\n";

            WriteFile(tmpdir, "t.stg", templates);
            STGroup group = new STGroupFile(tmpdir + "/" + "t.stg");
            group.Debug = true;
            DebugTemplate template = (DebugTemplate)group.GetInstanceOf("t");
            IList<InterpEvent> events = template.GetEvents();
            String expected =
                "[EvalExprEvent{self=t(),start=0,stop=0,expr=[}," +
                " EvalExprEvent{self=u(),start=1,stop=0,expr=<x>}," +
                " EvalExprEvent{self=u(),start=1,stop=1,expr= }," +
                " EvalTemplateEvent{self=u(),start=1,stop=1}," +
                " EvalExprEvent{self=t(),start=1,stop=1,expr=<u()>}," +
                " EvalExprEvent{self=t(),start=2,stop=2,expr=]}," +
                " EvalTemplateEvent{self=t(),start=0,stop=2}]";
            String result = ((IList)events).ToElementString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 22
0
        public void TestMap3()
        {
            String templates =
                "d ::= [\"k\":{dfkj}}]\n"; // extra }
            WriteFile(tmpdir, "t.stg", templates);

            STGroup group = null;
            ErrorBuffer errors = new ErrorBuffer();
            group = new STGroupFile(tmpdir + "/" + "t.stg");
            ErrorManager.ErrorListener = errors;
            group.Load(); // force load
            String expected = "t.stg 1:17: invalid character '}'" + newline;
            String result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 23
0
        public void TestUnterminatedString()
        {
            String templates =
                "f() ::= \""; // extra }
            WriteFile(tmpdir, "t.stg", templates);

            STGroup group = null;
            ErrorBuffer errors = new ErrorBuffer();
            group = new STGroupFile(tmpdir + "/" + "t.stg");
            ErrorManager.ErrorListener = errors;
            group.Load(); // force load
            String expected =
                "t.stg 1:9: unterminated string" + newline +
                "t.stg 1:9: missing template at '<EOF>'" + newline;
            String result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 24
0
        public void TestParen2()
        {
            String templates =
                "foo) ::= << >>\n" +
                "bar() ::= <<bar>>\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:0: garbled template definition starting at 'foo'" + newline;
            String result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 25
0
        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);
        }
Exemplo n.º 26
0
        public void TestErrorWithinTemplate()
        {
            String templates =
                "foo(a) ::= \"<a b>\"\n";
            WriteFile(tmpdir, "t.stg", templates);

            STGroup group = null;
            ErrorBuffer errors = new ErrorBuffer();
            group = new STGroupFile(Path.Combine(tmpdir, "t.stg"));
            ErrorManager.ErrorListener = errors;
            group.Load(); // force load
            String expected = "1:15: 'b' came as a complete surprise to me" + newline;
            String result = errors.ToString();
            Assert.AreEqual(expected, result);
        }
Exemplo n.º 27
0
        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;
            }
        }
Exemplo n.º 28
0
 public void TestReUseOfRestResult()
 {
     string templates =
         "a(names) ::= \"<b(rest(names))>\"" + newline +
         "b(x) ::= \"<x>, <x>\"" + newline
         ;
     WriteFile(tmpdir, "t.stg", templates);
     TemplateGroup group = new TemplateGroupFile(Path.Combine(tmpdir, "t.stg"));
     Template e = group.GetInstanceOf("a");
     var names = new ArrayList();
     names.Add("Ter");
     names.Add("Tom");
     e.Add("names", names);
     string expecting = "Tom, Tom";
     Assert.AreEqual(expecting, e.Render());
 }
Exemplo n.º 29
0
        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);
        }
Exemplo n.º 30
0
        public void TestMissingTemplate()
        {
            String templates =
                "foo() ::= \n";
            WriteFile(tmpdir, "t.stg", templates);

            STGroup group = null;
            ITemplateErrorListener errors = new ErrorBuffer();
            group = new STGroupFile(Path.Combine(tmpdir, "t.stg"));
            ErrorManager.ErrorListener = errors;
            group.Load(); // force load
            String expected = "t.stg 2:0: missing template at '<EOF>'" + newline;
            String result = errors.ToString();
            Assert.AreEqual(expected, result);
        }