Ejemplo n.º 1
0
        public void Cache()
        {
            var person = new PersonBuilder(this.Session).WithFirstName("John").Build();

            var stringTemplate = new StringTemplateBuilder(this.Session).WithBody(
            @"main(this) ::= <<
            Hello $this.FirstName$!
            >>").Build();

            for (var i = 0; i < NrOfRuns; i++)
            {
                var result = stringTemplate.Apply(new Dictionary<string, object> { { "this", person } });
                Assert.AreEqual("Hello John!", result);
            }

            stringTemplate.Body =
            @"main(this) ::= <<
            Hi $this.FirstName$!
            >>";

            this.Session.Derive();

            for (var i = 0; i < NrOfRuns; i++)
            {
                var result = stringTemplate.Apply(new Dictionary<string, object> { { "this", person } });
                Assert.AreEqual("Hi John!", result);
            }
        }
Ejemplo n.º 2
0
        public void TestTemplateBuilder_ReturnValidTemplate()
        {
            IStringTemplateBuilder builder  = new StringTemplateBuilder();
            IStringTemplate        template = builder
                                              .SetFormat(templateData)
                                              .AddParameter(templateVarName)
                                              .Build();

            IStringTemplateProcessor processor = new StringTemplateProcessor();
            var a = processor.Process(template, templateVarValue);

            var argB = template.CreateArgument(0, templateVarValue);
            var b    = processor.Process(template, argB);

            var argC = template.CreateArgument(templateVarName, templateVarValue);
            var c    = processor.Process(template, argC);

            var argD = template.CreateArgument(template.Parameters[0], templateVarValue);
            var d    = processor.Process(template, argD);

            Assert.AreEqual(expected_result, a);
            Assert.AreEqual(expected_result, b);
            Assert.AreEqual(expected_result, c);
            Assert.AreEqual(expected_result, d);
        }
Ejemplo n.º 3
0
        public void ErrorInTemplate()
        {
            var stringTemplate = new StringTemplateBuilder(this.Session).WithBody(
            @"main(this) ::= <<
            Hello $this.FirstName$!
            >>").Build();

            for (var i = 0; i < NrOfRuns; i++)
            {
                var result = stringTemplate.Apply(new Dictionary<string, object> { { "this", null } });
                Assert.AreEqual("Hello John!", result);
            }
        }
Ejemplo n.º 4
0
        public void WithEscapes()
        {
            var john = new PersonBuilder(this.Session).WithFirstName("John").Build();
            var jane = new PersonBuilder(this.Session).WithFirstName("Jane").Build();

            var objects = new Dictionary<string, IObject> { { "john", john }, { "jane", jane } };

            var stringTemplate =
                new StringTemplateBuilder(this.Session).WithBody(
                    @"main(this) ::= <<
            Hello $this.john.FirstName$ \$\\ $this.jane.FirstName$!
            >>").Build();

            for (var i = 0; i < NrOfRuns; i++)
            {
                var result = stringTemplate.Apply(new Dictionary<string, object> { { "this", objects } });
                Assert.AreEqual(@"Hello John $\ Jane!", result);
            }
        }
Ejemplo n.º 5
0
        public void InvalidGroup()
        {
            var stringTemplate = new StringTemplateBuilder(this.Session).WithBody(
            @"Invalid!!!").Build();

            for (var i = 0; i < NrOfRuns; i++)
            {
                stringTemplate.Apply(new Dictionary<string, object> { { "this", string.Empty } });
            }
        }