Esempio n. 1
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>"));
        }
Esempio n. 2
0
        public void It_Should_Set_A_Value()
        {
            var dict = new LiquidHash();

            dict["key"] = new Some <ILiquidValue>(LiquidString.Create("test"));
            Assert.Equal(LiquidString.Create("test"), dict["key"].Value);
        }
Esempio n. 3
0
        public void It_Should_Add_A_Value()
        {
            var dict = new LiquidHash();

            dict.Add("key", LiquidString.Create("test"));
            Assert.Equal(LiquidString.Create("test"), dict["key"].Value);
        }
Esempio n. 4
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>"));
        }
Esempio n. 5
0
 public static Option <ILiquidValue> Transform(JValue obj)
 {
     if (obj.Type.Equals(JTokenType.Integer))
     {
         return(LiquidNumeric.Create(obj.ToObject <int>()));
     }
     else if (obj.Type.Equals(JTokenType.Float))
     {
         return(LiquidNumeric.Create(obj.ToObject <decimal>()));
     }
     else if (obj.Type.Equals(JTokenType.String))
     {
         return(LiquidString.Create(obj.ToObject <String>()));
     }
     else if (obj.Type.Equals(JTokenType.Boolean))
     {
         return(new LiquidBoolean(obj.ToObject <bool>()));
     }
     else if (obj.Type.Equals(JTokenType.Null))
     {
         //throw new ApplicationException("NULL Not implemented yet");
         //return null; // TODO: Change this to an option
         return(Option <ILiquidValue> .None());
     }
     else
     {
         throw new Exception("Don't know how to transform a " + obj.GetType() + ".");
     }
 }
Esempio n. 6
0
        public LiquidString Render(
            RenderingVisitor renderingVisitor,
            MacroBlockTag macroBlocktag,
            ITemplateContext templateContext,
            IList <Option <ILiquidValue> > args)
        {
            var macroScope = new SymbolTable();

            var i = 0;

            foreach (var varName in macroBlocktag.Args.Take(args.Count))
            {
                macroScope.DefineLocalVariable(varName, args[i]);
                i++;
            }
            templateContext.SymbolTableStack.Push(macroScope);

            String hiddenText = "";

            renderingVisitor.PushTextAccumulator(str => hiddenText += str);
            renderingVisitor.StartWalking(macroBlocktag.LiquidBlock);
            renderingVisitor.PopTextAccumulator();

            templateContext.SymbolTableStack.Pop();

            return(LiquidString.Create(hiddenText));
        }
            public LiquidString Render(
                RenderingVisitor renderingVisitor,
                ITemplateContext templateContext,
                TreeNode <IASTNode> liquidBlock,
                IList <Option <ILiquidValue> > args)
            {
                var localBlockScope = new SymbolTable();

                templateContext.SymbolTableStack.Push(localBlockScope);
                LiquidString result;

                try
                {
                    // normally you would need to verify that arg0 and arg1 exists and are the correct value types.
                    String varname = args[0].Value.ToString();

                    var iterableFactory = new ArrayValueCreator(
                        new TreeNode <LiquidExpression>(
                            new LiquidExpression {
                        Expression = args[1].Value
                    }));

                    var iterable = iterableFactory.Eval(templateContext).ToList();

                    result = IterateBlock(renderingVisitor, varname, templateContext, iterable, liquidBlock);
                }
                finally
                {
                    templateContext.SymbolTableStack.Pop();
                }
                return(LiquidString.Create("START CUSTOM FOR LOOP" + result.StringVal + "END CUSTOM FOR LOOP"));
            }
Esempio n. 8
0
        public void It_Should_Render_An_Array_Element_From_A_Crazy_Chain_of_Nested_Indexes()
        {
            // Arrange
            TemplateContext templateContext = new TemplateContext();
            var             arrayofnums     = new LiquidCollection {
                LiquidNumeric.Create(0), LiquidNumeric.Create(1)
            };
            var array1 = new LiquidCollection {
                arrayofnums
            };
            var array2 = new LiquidCollection {
                array1
            };
            var arrayofstr = new LiquidCollection {
                LiquidString.Create("aaa"), LiquidString.Create("bbb")
            };

            //templateContext.Define("arrayofnums", new LiquidNumeric(1));
            templateContext.DefineLocalVariable("arrayofnums", arrayofnums);
            templateContext.DefineLocalVariable("array1", array1);
            templateContext.DefineLocalVariable("array2", array2);
            templateContext.DefineLocalVariable("arrayofstr", arrayofstr);

            // Act
            String result = GenerateAndRender("Result : {{ arrayofstr[array2[0][0][arrayofnums[arrayofnums[1]]]] }}", templateContext);

            // Assert
            Assert.Equal("Result : bbb", result);
        }
            private static LiquidString Reverse(string result)
            {
                var words = result.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                return(LiquidString.Create(String.Join(" ",
                                                       words.Select(x => new String(x.Reverse().ToArray())))));
            }
Esempio n. 10
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);
        }
Esempio n. 11
0
        public SolidityStruct(Entity entity)
        {
            En = entity;

            structName = LiquidString.Create(entity.Name);
            body       = new List <SolidityComponent>();
        }
Esempio n. 12
0
        public void It_Should_Compare_Against_Null()
        {
            // Arrange
            var str = LiquidString.Create("hello");

            // Act
            Assert.That(new EasyValueComparer().Equals(null, str), Is.False);
        }
Esempio n. 13
0
 LiquidString InheritanceToLiquid()
 {
     if (inheritance.Count == 0)
     {
         return(LiquidString.Create(""));
     }
     return(LiquidString.Create(string.Join(", ", inheritance)));
 }
Esempio n. 14
0
        public void It_Should_Retrieve_Values()
        {
            var dict = new LiquidHash {
                { "key", LiquidString.Create("test") }
            };

            Assert.Equal(1, dict.Values.Count);
        }
Esempio n. 15
0
        public void It_Should_Not_Allow_Null_Creation()
        {
            // Arrange
            var stringSymbol = LiquidString.Create(null);

            // Assert
            Assert.That(stringSymbol, Is.Null);
        }
Esempio n. 16
0
        public void It_Should_Compare_Two_Identical_Values()
        {
            // Arrange
            var str = LiquidString.Create("hello");

            // Act
            Assert.That(new EasyValueComparer().Equals(str, str), Is.True);
        }
Esempio 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));
        }
Esempio n. 18
0
 private LiquidCollection CreateArrayValues()
 {
     return(new LiquidCollection {
         LiquidString.Create("a string"),
         LiquidNumeric.Create(123),
         LiquidNumeric.Create(456m),
         new LiquidBoolean(false)
     });
 }
Esempio n. 19
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);
        }
Esempio n. 20
0
        public override string ToString(int indent = 0)
        {
            ITemplateContext ctx = new TemplateContext();

            ctx.DefineLocalVariable("indent", CreateIndent(indent)).
            DefineLocalVariable("name", LiquidString.Create(structName)).
            DefineLocalVariable("body", BodyToLiquid(indent));
            return(template.Render(ctx).Result);
        }
Esempio n. 21
0
        public void It_Should_Store_The_Value()
        {
            // Arrange
            var stringSymbol = LiquidString.Create("String Test");
            var result       = stringSymbol.Value;

            // Assert
            Assert.That(result, Is.EqualTo("String Test"));
        }
Esempio n. 22
0
        public void It_Should_Find_An_Element_By_Value()
        {
            var liquidCollection = new LiquidCollection {
                LiquidString.Create("test")
            };
            var index = liquidCollection.IndexOf(LiquidString.Create("test"));

            Assert.That(index, Is.EqualTo(0));
        }
Esempio n. 23
0
        public void It_Should_Remove_An_Element_At_An_Index_In_An_Array()
        {
            var liquidCollection = new LiquidCollection {
                LiquidString.Create("test")
            };

            liquidCollection.RemoveAt(0);
            Assert.That(liquidCollection.Count, Is.EqualTo(0));
        }
Esempio n. 24
0
        public void It_Should_Clear_An_Array()
        {
            var liquidCollection = new LiquidCollection {
                LiquidString.Create("test")
            };

            liquidCollection.Clear();
            Assert.That(liquidCollection.Count, Is.EqualTo(0));
        }
Esempio n. 25
0
        public void It_Should_Set_An_Element()
        {
            var liquidCollection = new LiquidCollection {
                LiquidString.Create("test")
            };

            liquidCollection[0] = LiquidString.Create("test 1");
            Assert.That(liquidCollection.IndexOf(LiquidString.Create("test 1")), Is.EqualTo(0));
        }
Esempio n. 26
0
        public void It_Should_Not_Cast_Array_To_Numeric()
        {
            // Arrange
            var result = ValueCaster.Cast <LiquidCollection, LiquidNumeric>(new LiquidCollection {
                LiquidString.Create("test")
            });

            Assert.False(result.IsSuccess);
        }
Esempio n. 27
0
        private Option <ILiquidValue> FromPrimitive(Object obj)
        {
            if (obj is bool)
            {
                return(new LiquidBoolean((bool)obj));
            }

            if (IsInt32Like(obj))
            {
                var val = System.Convert.ToInt32(obj);
                return(LiquidNumeric.Create(val));
            }

            if (IsLong(obj))
            {
                var val = System.Convert.ToInt64(obj);
                return(LiquidNumeric.Create(val));
            }

            if (obj is DateTime)
            {
                var val = System.Convert.ToDateTime(obj);
                return(new LiquidDate(val));
            }

            if (IsDecimalLike(obj))
            {
                var val = System.Convert.ToDecimal(obj);
                return(LiquidNumeric.Create(val));
            }

            if (obj is BigInteger)
            {
                var val = (BigInteger)obj;
                return(LiquidNumeric.Create(val));
            }

            var str = obj as String;

            if (str != null)
            {
                return(LiquidString.Create(str));
            }

            if (IsList(obj))
            {
                var val = obj as IList;
                return(CreateCollection(val));
            }

            if (IsDictionary(obj))
            {
                var val = obj as IDictionary;
                return(CreateHash(val));
            }
            return(null);
        }
Esempio n. 28
0
 public static LiquidHash CreateDictionary(int id, string field1, string field2)
 {
     return(new LiquidHash
     {
         { "id", LiquidNumeric.Create(id) },
         { "field1", LiquidString.Create(field1) },
         { "field2", LiquidString.Create(field2) },
     });
 }
Esempio n. 29
0
        public void It_Should_Convert_Guid_To_LiquidString()
        {
            var guid = Guid.NewGuid();
            var val  = _converter.Convert(guid);

            Assert.True(val.HasValue);
            Assert.IsType <LiquidString>(val.Value);
            Assert.Equal(LiquidString.Create(guid.ToString("D")), val.Value);
        }
Esempio n. 30
0
        public void It_Should_Convert_Enum_To_LiquidString()
        {
            var testEnum = TestEnum.One;
            var val      = _converter.Convert(testEnum);

            Assert.True(val.HasValue);
            Assert.IsType <LiquidString>(val.Value);
            Assert.Equal(LiquidString.Create(Enum.GetName(testEnum.GetType(), testEnum)), val.Value);
        }