Exemplo n.º 1
0
        public void It_Should_Remove_The_Upper_For_Limit()
        {
            // Arrange
            const string     templateString = "Result : {% for item in array%}<li>{{ item }}</li>{% endfor %}";
            ITemplateContext ctx            = new TemplateContext()
                                              .WithNoForLimit();
            var list = new LiquidCollection();

            var items =
                Enumerable.Range(1, 100)
                .Select(LiquidNumeric.Create)
                .Cast <ILiquidValue>().ToList();

            foreach (var item in items)
            {
                //list.Add((Option<ILiquidValue>)item);
                list.Add(Option <ILiquidValue> .Create(item));
            }

            ctx.DefineLocalVariable("array", list);
            var template = LiquidTemplate.Create(templateString);

            // Act
            String result = template.LiquidTemplate.Render(ctx).Result;

            // Assert
            Assert.Contains("<li>100</li>", result);
        }
Exemplo n.º 2
0
        public void Test_Parsing_Error()
        {
            var    parsingResult = LiquidTemplate.Create("This filter delimiter is not terminated: {{ myfilter");
            String error         = String.Join(",", parsingResult.ParsingErrors.Select(x => x.ToString()));

            Assert.That(error, Is.StringContaining("line 1:52 at <EOF>: Missing '}}'"));
        }
Exemplo n.º 3
0
        public void Test_Introductory_Example()
        {
            // create a template context that knows about the standard filters,
            // and define a string variable "myvariable"
            ITemplateContext ctx = new TemplateContext()
                                   .WithAllFilters()
                                   .DefineLocalVariable("myvariable", LiquidString.Create("Hello World"));

            // parse the template and check for errors
            var parsingResult = LiquidTemplate.Create("<div>{{myvariable}}</div>");

            if (parsingResult.HasParsingErrors)
            {
                HandleErrors(parsingResult.ParsingErrors);
                return;
            }

            // merge the variables from the context into the template and check for errors
            var renderingResult = parsingResult.LiquidTemplate.Render(ctx);

            if (renderingResult.HasParsingErrors)
            {
                HandleErrors(renderingResult.ParsingErrors);
                return;
            }
            if (renderingResult.HasRenderingErrors)
            {
                HandleErrors(renderingResult.RenderingErrors);
                return;
            }

            Assert.That(renderingResult.Result, Is.EqualTo("<div>Hello World</div>"));
        }
Exemplo n.º 4
0
        public void Test_Introductory_Example_With_Syntactic_Sugar()
        {
            // create a place to accumulate parsing and rendering errors.
            var errors = new List <LiquidError>();

            // Note that you will still get a best-guess LiquidTemplate, even if you encounter errors.
            var liquidTemplate = LiquidTemplate.Create("<div>{{myvariable}}</div>")
                                 .OnParsingError(errors.Add)
                                 .LiquidTemplate;

            // [add code here to handle the parsing errors, return]
            Assert.That(errors.Any(), Is.False);

            var ctx = new TemplateContext()
                      .WithAllFilters()
                      .DefineLocalVariable("myvariable", LiquidString.Create("Hello World"));


            // The final String output will still be available in .Result,
            // even when parsing or rendering errors are encountered.
            var result = liquidTemplate.Render(ctx)
                         .OnAnyError(errors.Add) // also available: .OnParsingError, .OnRenderingError
                         .Result;

            // [add code here to handle the parsing and rendering errors]
            Assert.That(errors.Any(), Is.False);

            Console.WriteLine(result);
            Assert.That(result, Is.EqualTo("<div>Hello World</div>"));
        }
Exemplo n.º 5
0
        public void It_Should_Iterate_Through_A_Dictionary(String expected)
        {
            // SEE: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers

            // Arrange
            TemplateContext ctx = new TemplateContext();

            ctx.DefineLocalVariable("dict", new LiquidHash
            {
                { "one", LiquidString.Create("ONE") },
                { "two", LiquidString.Create("TWO") },
                { "three", LiquidString.Create("THREE") },
                { "four", LiquidString.Create("FOUR") }
            });
            var template =
                LiquidTemplate.Create(
                    "Result : {% for item in dict %}<li>{{ item[0] }} : {{ item[1] }}</li>{% endfor %}");


            // Act
            String result = template.LiquidTemplate.Render(ctx).Result;

            // Assert
            Assert.Contains(expected, result);
        }
Exemplo n.º 6
0
        public void It_Should_Iterate_Through_A_Nested_Collection()
        {
            // Arrange
            const string templateString = "Result : {% for subarray in array1 %}"
                                          + "<tr>{% for item in subarray %}"
                                          + "<td>{{item}}</td>"
                                          + "{% endfor %}"
                                          + "</tr>{% endfor %}";
            TemplateContext ctx = new TemplateContext();
            var             liquidCollection = new LiquidCollection();

            var numericValues = Enumerable.Range(0, 3).Select(x => (ILiquidValue)LiquidNumeric.Create(x)).ToList();

            foreach (var item in numericValues)
            {
                liquidCollection.Add(Option <ILiquidValue> .Create(item));
            }
            //var array1 = Enumerable.Range(0, 3).Select(x => new LiquidCollection(array2);
            var array1 = new LiquidCollection {
                liquidCollection, liquidCollection, liquidCollection
            };

            ctx.DefineLocalVariable("array1", array1);

            var template = LiquidTemplate.Create(templateString);

            // Act
            String result = template.LiquidTemplate.Render(ctx).Result;

            // Assert
            String row = "<tr><td>0</td><td>1</td><td>2</td></tr>";

            Assert.Equal("Result : " + row + row + row, result);
        }
        public void It_Should_Not_Parse_A_Custom_BlockTag_With_No_End()
        {
            // Act
            //var templateContext = new TemplateContext().WithAllFilters().WithCustomTagBlockRenderer<WordReverserBlockTag>("echoargs");

            var templateResult = LiquidTemplate.Create("Result : {% echoargs \"hello\" 123 true %}echo{% endsomethingelse %}");

            Assert.True(templateResult.HasParsingErrors);
            Assert.Contains("There was no opening tag for the ending tag 'endsomethingelse'",
                            templateResult.ParsingErrors[0].Message);
            //Assert.That
            //var result = templateResult.LiquidTemplate.Render(new TemplateContext().WithAllFilters());

//            try
//            {
            //RenderingHelper.RenderTemplate(
            //"Result : {% echoargs \"hello\" 123 true %}echo{% endsomethingelse %}", templateContext);
            // Assert.Fail("This should have thrown an error.");
            //}
            //catch (LiquidParserException ex)
            //{
            //    var allErrors = String.Join(",", ex.LiquidErrors.Select(x => x.ToString()));
            //    Logger.Log(allErrors);
            //    Assert.That(allErrors, Does.Contain("There was no opening tag for the ending tag 'endsomethingelse'"));
            //}
        }
        private string TemplateRender(string context, object constant, string template)
        {
            string result = "";
            string liquidErrors = "";

            ITemplateContext ctx = new TemplateContext()
                .WithAllFilters()
                .DefineLocalVariable(context, constant.ToLiquid());

            var parsingResult = LiquidTemplate.Create(template);

            if (parsingResult.ParsingErrors.Count() > 0)
            {
                liquidErrors += string.Join(", ", parsingResult.ParsingErrors.Select(x => x.ToString()));
                result += liquidErrors;
            }

            var renderingResult = parsingResult.LiquidTemplate.Render(ctx);

            if (renderingResult.RenderingErrors.Count() > 0)
            {
                liquidErrors = string.Join(", ", renderingResult.RenderingErrors.Select(x => x.ToString()));
                result += liquidErrors;
            }

            result += renderingResult.Result;

            return result;
        }
Exemplo n.º 9
0
 public FluidBenchmarks()
 {
     FluidTemplate.TryParse(_source1, out _sampleTemplateFluid, out var messages);
     _sampleTemplateDotLiquid = Template.Parse(_source1);
     _sampleTemplateDotLiquid.MakeThreadSafe();
     _sampleTemplateLiquidNet = LiquidTemplate.Create(_source1);
 }
Exemplo n.º 10
0
        public void It_Should_Show_Error_On_Missing_BLocks()
        {
            var template = LiquidTemplate.Create("Result : {% test %}{% endtest %}");
            var result   = template.LiquidTemplate.Render(new TemplateContext().WithAllFilters());

            Assert.Equal("Liquid syntax error: Unknown tag 'test'", String.Join(",", result.RenderingErrors.Select(x => x.Message)));
        }
Exemplo n.º 11
0
        public void It_Should_Allow_Variables_In_Args()
        {
            // Arrange
            const string    templateString = "Result : {% tablerow i in array cols: x limit: y offset: z %}{{ i }}{% endtablerow %}";
            TemplateContext ctx            = new TemplateContext();
            var             arr            = new LiquidCollection();

            for (int i = 1; i < 10; i++)
            {
                arr.Add(LiquidNumeric.Create(i));
            }

            ctx.DefineLocalVariable("array", arr);
            ctx.DefineLocalVariable("x", LiquidNumeric.Create(2));
            ctx.DefineLocalVariable("y", LiquidNumeric.Create(3));
            ctx.DefineLocalVariable("z", LiquidNumeric.Create(1));
            var template = LiquidTemplate.Create(templateString);

            // Act
            String result = template.LiquidTemplate.Render(ctx).Result;

            Logger.Log(result);

            // Assert
            Assert.That(result, Is.StringContaining("<tr class=\"row1\">"));
            Assert.That(result, Is.StringContaining("<tr class=\"row2\">"));
            Assert.That(result, Is.Not.StringContaining("<tr class=\"row3\">"));
            Assert.That(result, Is.Not.StringContaining(">1</td>"));
            Assert.That(result, Is.StringContaining(">2</td>"));
            Assert.That(result, Is.StringContaining(">4</td>"));
            Assert.That(result, Is.Not.StringContaining(">5</td>"));
        }
Exemplo n.º 12
0
        public void It_Should_Assign_A_Cycle_To_Within_A_Macro()
        {
            // Arrange
            TemplateContext ctx = new TemplateContext();

            ctx.DefineLocalVariable("array", CreateArrayValues());

            var template = LiquidTemplate.Create("Result : {% macro thecycle %}{% cycle 'odd', 'even' %}{% endmacro %}{% for item in array %}{% thecycle %}{% endfor %}");

            // Act
//            try
//            {
            String result = template.LiquidTemplate.Render(ctx).Result;

            Logger.Log(result);

            // Assert
            Assert.Equal("Result : oddevenoddeven", result.TrimEnd());
//            }
//            catch (LiquidRendererException ex)
//            {
//                Logger.Log(ex.Message);
//                throw;
//            }
        }
        public void It_Should_Throw_An_Error_When_Comparison_Incorrect(String arg1, string op, String arg2, string expected)
        {
            // Act

            //try
            //{
            //var result = RenderingHelper.RenderTemplate("Result : {% if " + arg1 + " " + op + " " + arg2 +
            //                                            " %}TRUE{% else %}FALSE{% endif %}");
            var template = LiquidTemplate.Create("Result : {% if " + arg1 + " " + op + " " + arg2 +
                                                 " %}TRUE{% else %}FALSE{% endif %}");
            //var result = template.LiquidTemplate.Render(new TemplateContext().WithAllFilters());

            var errors = template.ParsingErrors.Select(x => x.ToString());

            Assert.Equal(1,
                         errors.Count(x => x.Contains(expected))); //"Could not find error containing '" + expected + "'";

            //L//ogger.Log(result.Result);
            //Assert.Fail("Expected an error but none was thrown: " + result);
            //}
            //catch (LiquidParserException ex)
            //{
            //    var errors = ex.LiquidErrors.Select(x => x.ToString());
            //    Assert.That(errors.Count(x => x.Contains(expected)), Is.EqualTo(1), "Could not find error containing '" + expected+"'");
            //}
            // Assert
            //Assert.Fail("Need to figure out error message here.");

            //Assert.Equal("Result : " + "ERROR", result);
        }
Exemplo n.º 14
0
 public LiquidNetBenchmarks()
 {
     _liquidNetTemplate = LiquidTemplate.Create(TextTemplate);
     _products          = Products.ToLiquid();
     _context           = new Liquid.NET.TemplateContext();
     _context.DefineLocalVariable("products", _products);
 }
Exemplo n.º 15
0
        public override string ParseAndRender()
        {
            var template = LiquidTemplate.Create(ProductTemplate);
            var context  = new Liquid.NET.TemplateContext();

            context.DefineLocalVariable("products", Products.ToLiquid());
            return(template.LiquidTemplate.Render(context).Result);
        }
Exemplo n.º 16
0
        public void It_Should_Generate_One_Error() // bug
        {
            // Arrange
            var templateResult = LiquidTemplate.Create("This tag delimiter is not terminated: {% .");

            // Assert
            Assert.Equal(1, templateResult.ParsingErrors.Count);
        }
Exemplo n.º 17
0
        public override Task <string> RenderLoremSimpleOuput()
        {
            var context = new Liquid.NET.TemplateContext();

            context.DefineLocalVariable("image", LiquidString.Create("kitten.jpg"));
            var parsingResult = LiquidTemplate.Create(_source4);

            return(Task.FromResult(parsingResult.LiquidTemplate.Render(context).Result));
        }
Exemplo n.º 18
0
        public string RenderLoreSimpleOuputLiquidNet()
        {
            var context = new Liquid.NET.TemplateContext();

            context.DefineLocalVariable("image", LiquidString.Create("kitten.jpg"));
            var parsingResult = LiquidTemplate.Create(_source4);

            return(parsingResult.LiquidTemplate.Render(context).Result);
        }
Exemplo n.º 19
0
        public string ParseAndRenderSampleLiquidNet()
        {
            var context = new Liquid.NET.TemplateContext();

            context.DefineLocalVariable("products", _products.ToLiquid());
            var parsingResult = LiquidTemplate.Create(_source1);

            return(parsingResult.LiquidTemplate.Render(context).Result);
        }
Exemplo n.º 20
0
        public void It_Should_Render_An_Error_When_Conversion_From_Object_Fails()
        {
            // Act
            var template = LiquidTemplate.Create("Result : {{ true | plus: 3 }}");
            var result   = template.LiquidTemplate.Render(new TemplateContext().WithAllFilters());

            // Assert
            Assert.Contains("Can't convert", result.Result); // note: it doesn't remove that extra space.
        }
Exemplo n.º 21
0
        public void It_Should_Show_Error_On_Missing_Tags()
        {
            var template = LiquidTemplate.Create("Result : {% test %}");
            var result   = template.LiquidTemplate.Render(new TemplateContext().WithAllFilters());

            //RenderingHelper.RenderTemplate(, onRenderingError: errors.Add);

            Assert.That(String.Join(",", result.RenderingErrors.Select(x => x.Message)), Is.EqualTo("Liquid syntax error: Unknown tag 'test'"));
        }
Exemplo n.º 22
0
        public override Task <string> ParseAndRenderSample()
        {
            var context = new Liquid.NET.TemplateContext();

            context.DefineLocalVariable("products", _products.ToLiquid());
            var parsingResult = LiquidTemplate.Create(_source1);

            return(Task.FromResult(parsingResult.LiquidTemplate.Render(context).Result));
        }
Exemplo n.º 23
0
        public void It_Should_CHeck_For_Div_By_Zero()
        {
            // Act
            //var result = RenderingHelper.RenderTemplate("Result : {{ 2  | modulo: 0 }}");
            var template = LiquidTemplate.Create("Result : {{ 2  | modulo: 0 }}");
            var result   = template.LiquidTemplate.Render(new TemplateContext().WithAllFilters());

            // Assert
            Assert.That(result.Result, Is.StringContaining("Liquid error: divided by 0"));
        }
Exemplo n.º 24
0
        public void Test_Simple_Template()
        {
            ITemplateContext ctx = new TemplateContext();

            ctx.DefineLocalVariable("myvariable", LiquidString.Create("Hello World"));

            var parsingResult = LiquidTemplate.Create("<div>{{myvariable}}</div>");

            Assert.That(parsingResult.LiquidTemplate.Render(ctx).Result, Is.EqualTo("<div>Hello World</div>"));
        }
Exemplo n.º 25
0
        public void Test_Rendering_Error()
        {
            ITemplateContext ctx   = new TemplateContext().WithAllFilters();
            var    parsingResult   = LiquidTemplate.Create("Divide by zero result in: {{ 1 | divided_by: 0}}");
            var    renderingResult = parsingResult.LiquidTemplate.Render(ctx);
            String error           = String.Join(",", renderingResult.RenderingErrors.Select(x => x.Message));

            //Console.WriteLine("The ERROR was : " + error);
            //Console.WriteLine("The RESULT was : " + renderingResult.Result);
            Assert.That(error, Is.StringContaining("Liquid error: divided by 0"));
        }
Exemplo n.º 26
0
        public void Test_Filter()
        {
            ITemplateContext ctx = new TemplateContext()
                                   .WithAllFilters()
                                   .DefineLocalVariable("resultcount", LiquidNumeric.Create(42))
                                   .DefineLocalVariable("searchterm", LiquidString.Create("MiXeDcAsE"));

            var parsingResult = LiquidTemplate.Create("{{ resultcount }} {{ resultcount | pluralize: 'item', 'items' }} were found for '{{searchterm | downcase}}'.");

            Assert.That(parsingResult.LiquidTemplate.Render(ctx).Result, Is.EqualTo("42 items were found for 'mixedcase'."));
        }
Exemplo n.º 27
0
        public void It_Should_Iterate_Over_A_Strings_Characters(String str, String expected)
        {
            //[InlineData(@"{% for char in characters %}I WILL NOT BE OUTPUT{% endfor %}", @"{""characters"":""""}", @"")]
            TemplateContext ctx      = new TemplateContext();
            var             template = LiquidTemplate.Create(@"{% for char in " + str + " %}char:{{char}}{% endfor %}");
            // Act
            String result = template.LiquidTemplate.Render(ctx).Result;

            // Assert
            Assert.Equal(expected, result);
        }
Exemplo n.º 28
0
        public void It_Should_Assign_Empty_Values()
        {
            ITemplateContext ctx = new TemplateContext().WithAllFilters();
            var template         = LiquidTemplate.Create("{% assign content_column_width = content_column_width | minus: image_column_width | minus: 10 -%}");

            // Act
            String result = template.LiquidTemplate.Render(ctx).Result;

            // Assert
            Assert.Equal("", result);
        }
Exemplo n.º 29
0
        public void It_Should_Iterate_Through_A_Generator(String generator, String expected)
        {
            // Arrange
            var template = LiquidTemplate.Create("Result : {% for item in " + generator + " %}{{ item }}{% endfor %}");

            // Act
            String result = template.LiquidTemplate.Render(new TemplateContext()).Result;

            // Assert
            Assert.Equal("Result : " + expected, result);
        }
Exemplo n.º 30
0
        public void It_Should_Remove_Nil_From_A_String()
        {
            // Arrange

            //var result = RenderingHelper.RenderTemplate("Result : {{ \"test\" | remove : x }}");
            var template = LiquidTemplate.Create("Result : {{ \"test\" | remove : x }}");
            var result   = template.LiquidTemplate.Render(new TemplateContext().WithAllFilters());

            // Assert
            Assert.Equal("Result : ERROR: Please specify a replacement string.", result.Result);
        }