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);
        }
Exemple #2
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) },
     });
 }
        public void NumericValues_Should_Equal(decimal decvar1, decimal decvar2, bool expected)
        {
            // Arrange
            var var1 = LiquidNumeric.Create(decvar1);
            var var2 = LiquidNumeric.Create(decvar2);

            // Assert
            Assert.Equal(expected, var1.Equals(var2));
        }
Exemple #4
0
 public LiquidCollection CreateArray()
 {
     return(new LiquidCollection {
         LiquidString.Create("a string"),
         LiquidNumeric.Create(123),
         LiquidNumeric.Create(456m),
         new LiquidBoolean(false)
     });
 }
 public LiquidHash CreateDictionary()
 {
     return(new LiquidHash
     {
         { "one", LiquidString.Create("1") },
         { "two", LiquidNumeric.Create(2) },
         { "three", LiquidString.Create("Three") },
         { "four", new LiquidBoolean(true) }
     });
 }
        public void It_Should_Convert_A_Numeric_To_A_Date()
        {
            var date   = new DateTime(2015, 10, 29, 10, 11, 12);
            var result = ValueCaster.Cast <LiquidNumeric, LiquidDate>(LiquidNumeric.Create(date.Ticks));

            Assert.True(result.IsSuccess);
            Assert.True(result.SuccessResult.HasValue);
            // ReSharper disable once PossibleInvalidOperationException
            Assert.Equal(date, result.SuccessValue <LiquidDate>().DateTimeValue.Value);
        }
 public LiquidCollection CreateArray()
 {
     // Arrange
     return(new LiquidCollection {
         LiquidString.Create("1"),
         LiquidNumeric.Create(2),
         LiquidString.Create("Three"),
         new LiquidBoolean(true)
     });;
 }
Exemple #8
0
        public override LiquidExpressionResult ApplyTo(ITemplateContext ctx, LiquidCollection liquidArrayExpression)
        {
            if (liquidArrayExpression == null || liquidArrayExpression.Value == null)
            {
                return(LiquidExpressionResult.Error("Array is nil"));
            }
            var positionFilter = new PositionFilter(LiquidNumeric.Create(liquidArrayExpression.Count - 1));

            return(positionFilter.ApplyTo(ctx, liquidArrayExpression));
        }
Exemple #9
0
        public void It_Should_Return_Error_When_Not_Filter_Type()
        {
            // Arrange
            var filter = FilterFactory.InstantiateFilter("mockfilter", typeof(String), new List <Option <ILiquidValue> > {
                LiquidNumeric.Create(123)
            });

            // Assert
            Assert.True(filter.IsFailure);
        }
        public void It_Should_Return_Error_When_Null_Type()
        {
            // Arrange
            var filter = FilterFactory.InstantiateFilter("mockfilter", null, new List <Option <ILiquidValue> > {
                LiquidNumeric.Create(123)
            });

            // Assert
            Assert.That(filter.IsFailure, Is.True);
        }
Exemple #11
0
        public override LiquidExpressionResult ApplyTo(ITemplateContext ctx, LiquidString liquidLiquidStringExpression)
        {
            if (liquidLiquidStringExpression == null || String.IsNullOrEmpty(liquidLiquidStringExpression.StringVal))
            {
                return(LiquidExpressionResult.Error("String is nil"));
            }
            var positionFilter = new PositionFilter(LiquidNumeric.Create(liquidLiquidStringExpression.StringVal.Length - 1));

            return(positionFilter.ApplyTo(ctx, liquidLiquidStringExpression));
        }
        public void It_Should_Return_Error_When_Multiple_Constructors()
        {
            // Arrange
            var filter = FilterFactory.InstantiateFilter("mockfilter", typeof(TwoConstructorFilter), new List <Option <ILiquidValue> > {
                LiquidNumeric.Create(123)
            });

            // Assert
            Assert.That(filter.IsFailure, Is.True);
        }
 private LiquidCollection CreateArrayValues()
 {
     return(new LiquidCollection
     {
         LiquidNumeric.Create(1),
         LiquidNumeric.Create(2),
         LiquidNumeric.Create(3),
         LiquidNumeric.Create(4)
     });
 }
        public void It_Should_Cast_An_Int_To_A_String_Like_Ruby_Liquid(int input, String expected)
        {
            // Arrange
            var castFilter = new CastFilter <LiquidNumeric, LiquidString>();

            // Act
            var result = castFilter.Apply(new TemplateContext(), LiquidNumeric.Create(input)).SuccessValue <LiquidString>();

            // Assert
            Assert.That(result.StringVal, Is.EqualTo(expected));
        }
Exemple #15
0
        public void It_Should_Pluralize_A_Decimal_Number(decimal input, String expected)
        {
            // Arrange
            TemplateContext ctx = new TemplateContext();

            ctx.DefineLocalVariable("input", LiquidNumeric.Create(input));
            var result = RenderingHelper.RenderTemplate("Result : {{ input | pluralize: 'thing', 'things' }}", ctx);

            // Assert
            Assert.That(result, Is.EqualTo("Result : " + expected));
        }
        public void It_Should_Cast_A_Decimal_To_A_String_Like_Ruby_Liquid(decimal input, String expected)
        {
            // Arrange
            var castFilter = new CastFilter <LiquidNumeric, LiquidString>();

            // Act
            var result = castFilter.Apply(new TemplateContext(), LiquidNumeric.Create(input)).SuccessValue <LiquidString>();

            // Assert
            Assert.Equal(expected, result.StringVal);
        }
Exemple #17
0
        public void It_Should_Pluralize_An_Integerr(int input, String expected)
        {
            // Arrange
            TemplateContext ctx = new TemplateContext();

            ctx.DefineLocalVariable("input", LiquidNumeric.Create(input));
            var result = RenderingHelper.RenderTemplate("Result : {{ input | pluralize: 'thing', 'things' }}", ctx);

            // Assert
            Assert.Equal("Result : " + expected, result);
        }
        public void It_Should_Generate_Some_Values(int start, int end, int[] expected)
        {
            // Arrange
            var generatorValue = new LiquidRange(LiquidNumeric.Create(start), LiquidNumeric.Create(end));

            // Act
            var result = generatorValue.AsEnumerable();

            // Assert
            Assert.Equal(expected.ToList(), result.Select(x => Convert.ToInt32(x.Value)));
        }
Exemple #19
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'."));
        }
Exemple #20
0
        public void It_Should_Evaluate_To_Itself()
        {
            // Arrange
            var number = LiquidNumeric.Create(123m);

            // Act
            var result = number.Eval(new TemplateContext(), new List <Option <ILiquidValue> >()).SuccessValue <LiquidNumeric>();

            // Assert
            Assert.Equal(123m, result.Value);
        }
Exemple #21
0
        public void It_Should_Cast_Numeric_Args_To_A_String()
        {
            // Arrange
            var filter = FilterFactory.InstantiateFilter("mockfilter", typeof(MockStringToStringFilter), new List <Option <ILiquidValue> > {
                LiquidNumeric.Create(123)
            });

            // Act

            Assert.Equal("123", ((MockStringToStringFilter)filter.Value).LiquidStringArg1.Value);
        }
Exemple #22
0
        public void A_Generator_Value_Should_Return_The_Size()
        {
            // Arrange
            var strVal = new LiquidRange(LiquidNumeric.Create(3), LiquidNumeric.Create(10));
            var filter = new SizeFilter();

            // Act
            var result = filter.Apply(new TemplateContext(), strVal).SuccessValue <LiquidNumeric>();

            // Assert
            Assert.Equal(8, result.Value);
        }
        public void It_Should_Parse_A_Regular_Variable()
        {
            String input    = @"{{ v | plus: v }}";
            var    template = LiquidTemplate.Create(input);

            // Act
            String result = template.LiquidTemplate.Render(new TemplateContext()
                                                           .DefineLocalVariable("v", LiquidNumeric.Create(3))
                                                           .WithAllFilters()).Result;

            // Assert
            Assert.Equal("6", result.Trim());
        }
        public void It_Should_Return_The_Same_Object_If_Src_And_Dest_Are_Arrays()
        {
            // Arrange
            var original = new LiquidCollection {
                LiquidNumeric.Create(123.4m), LiquidNumeric.Create(5)
            };

            // Act
            var result = ValueCaster.Cast <LiquidCollection, LiquidCollection>(original).SuccessValue <LiquidCollection>();

            // Assert
            Assert.Equal(original, result);
        }
Exemple #25
0
        public void It_Should_Dereference_A_LiquidHash()
        {
            // Arrange
            var dictValue = new LiquidHash
            {
                { "string1", LiquidString.Create("a string") },
                { "string2", LiquidNumeric.Create(123) },
                { "string3", LiquidNumeric.Create(456m) }
            };

            // Assert
            Assert.Equal("a string", dictValue.ValueAt("string1").Value.ToString());
        }
        public void It_Can_Cast_With_Generics()
        {
            // Arrange
            var num = LiquidNumeric.Create(123.45m);

            // Act
            var stringliteral = ValueCaster.Cast <LiquidNumeric, LiquidString>(num)
                                .SuccessValue <LiquidString>()
                                .StringVal;

            // Assert
            Assert.Equal("123.45", stringliteral);
        }
        public void It_Should_Cast_A_Number_To_A_String()
        {
            // Arrange
            var num = LiquidNumeric.Create(123.45m);

            // Act
            var stringliteral = ValueCaster.Cast <LiquidNumeric, LiquidString>(num)
                                .SuccessValue <LiquidString>()
                                .StringVal;

            // Assert
            Assert.Equal("123.45", stringliteral);
        }
        public void It_Should_Return_The_Same_Object_If_Dest_Is_An_ExpressionConstant()
        {
            // Arrange
            var original = new LiquidCollection {
                LiquidNumeric.Create(123.4m), LiquidNumeric.Create(5)
            };

            // Act
            var result = ValueCaster.Cast <LiquidCollection, LiquidValue>(original).SuccessValue <LiquidCollection>();

            // Assert
            Assert.That(result, Is.EqualTo(original));
        }
        public void It_Should_Generate_Some_Values_Descending()
        {
            // Arrange
            var generatorValue = new LiquidRange(LiquidNumeric.Create(5), LiquidNumeric.Create(2));

            // Act
            var result = generatorValue.AsEnumerable();

            // Assert
            Assert.Equal(new List <int> {
                5, 4, 3, 2
            }, result.Select(x => Convert.ToInt32(x.Value)));
        }
Exemple #30
0
 public static LiquidExpressionResult GetReturnValue(decimal result, LiquidNumeric val1, LiquidNumeric val2)
 {
     if (val1.IsInt && val2.IsInt)
     {
         //var int32 = Convert.ToInt32(val);
         var int32 = (int)System.Math.Floor(result); // ruby liquid seems to round down.
         return(LiquidExpressionResult.Success(LiquidNumeric.Create(int32)));
     }
     else
     {
         return(LiquidExpressionResult.Success(LiquidNumeric.Create(result)));
     }
 }