public void It_Should_Cast_Null_To_None() { var result = ValueCaster.Cast <LiquidString, LiquidNumeric>(null); Assert.True(result.IsSuccess); Assert.False(result.SuccessResult.HasValue); }
public void It_Should_Cast_Numeric_To_Numeric() { var result = ValueCaster.Cast <LiquidNumeric, LiquidNumeric>(LiquidNumeric.Create(1)); Assert.True(result.IsSuccess); Assert.True(result.SuccessResult.HasValue); }
public void It_Should_Convert_Num() { var result = ValueCaster.Cast <LiquidNumeric, LiquidString>(LiquidNumeric.Create(1)); Assert.That(result.IsSuccess, Is.True); Assert.That(result.SuccessResult.HasValue, Is.True); }
public void It_Should_Convert_A_Null_Date_To_0L() { var result = ValueCaster.Cast <LiquidDate, LiquidNumeric>(new LiquidDate(null)); Assert.True(result.IsSuccess); Assert.True(result.SuccessResult.HasValue); // ReSharper disable once PossibleInvalidOperationException Assert.Equal(0L, result.SuccessValue <LiquidNumeric>().LongValue); }
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); }
public void It_Should_Convert_An_Array() { var result = ValueCaster.Cast <LiquidCollection, LiquidString>(new LiquidCollection { LiquidString.Create("test") }); Assert.True(result.IsSuccess); Assert.True(result.SuccessResult.HasValue); Assert.Equal("test", result.SuccessValue <LiquidString>().StringVal); }
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 void It_Should_Convert_A_Date_To_A_Numeric() { var date = new DateTime(2015, 10, 29, 10, 11, 12); var result = ValueCaster.Cast <LiquidDate, LiquidNumeric>(new LiquidDate(date)); Assert.That(result.IsSuccess, Is.True); Assert.That(result.SuccessResult.HasValue, Is.True); // ReSharper disable once PossibleInvalidOperationException Assert.That(result.SuccessValue <LiquidNumeric>().LongValue, Is.EqualTo(date.Ticks)); }
public void It_Should_Return_The_Same_Object_If_Src_And_Dest_Are_Strings() { // Arrange var original = LiquidString.Create("Test"); // Act var result = ValueCaster.Cast <LiquidString, LiquidString>(original).SuccessValue <LiquidString>(); // Assert Assert.Equal(original, result); }
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_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_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_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); }
private void RenderFromLiquidExpression(IncludeTag includeTag, ITemplateContext templateContext, String virtualFileName, Option <ILiquidValue> forExpressionOption, LiquidAST snippetAst) { ValueCaster.Cast <ILiquidValue, LiquidCollection>(forExpressionOption.Value) .WhenError(AddRenderingErrorToResult) .WhenSuccess(result => { foreach (Option <ILiquidValue> val in (LiquidCollection)result.Value) { var val1 = val; RenderBlock(includeTag, templateContext, snippetAst, localBlockScope => localBlockScope.DefineLocalVariable(virtualFileName, val1)); } }); }
public override LiquidExpressionResult Apply(ITemplateContext ctx, TSource liquidExpression) { //Console.WriteLine("Casting from " + typeof(TSource) + " to " + typeof(TResult)); var result = ValueCaster.Cast <TSource, TResult>(liquidExpression); if (result.IsError) { return(result); } else { return(LiquidExpressionResult.Success(result.SuccessResult)); } //Console.WriteLine("RESULT IS " + result); }
public void It_Should_Format_An_Array_By_Concatenating_Each_Elements_STring_Value() { // Arrange var num = new LiquidCollection { LiquidString.Create("a"), LiquidString.Create("b"), LiquidString.Create("c") }; // Act var result1 = ValueCaster.Cast <LiquidCollection, LiquidString>(num); var stringliteral = result1.SuccessValue <LiquidString>() .StringVal; // Assert Assert.Equal("abc", stringliteral); }
public void It_Should_Handle_Casting_A_Null_Value() { // Arrange var original = LiquidString.Create(null); // Act var result = ValueCaster.Cast <LiquidString, LiquidNumeric>(original); Assert.True(result.IsSuccess); // Assert //Assert.Equal(0, result.SuccessValue<LiquidNumeric>().IntValue); Assert.True(result.IsSuccess); Assert.False(result.SuccessOption <ILiquidValue>().HasValue); }
public void It_Should_Cast_A_String_To_An_Array_Of_One_String() { // Arrange var str = LiquidString.Create("Hello"); // Act var arrayResult = ValueCaster.Cast <LiquidString, LiquidCollection>(str); Assert.False(arrayResult.IsError); // Assert var arrValue = arrayResult.SuccessValue <LiquidCollection>(); Assert.Single(arrValue); Assert.Equal("Hello", arrValue[0].Value.ToString()); }
public void It_Should_Cast_A_String_To_An_Array_Of_One_String() { // Arrange var str = LiquidString.Create("Hello"); // Act var arrayResult = ValueCaster.Cast <LiquidString, LiquidCollection>(str); Assert.That(arrayResult.IsError, Is.False); // Assert var arrValue = arrayResult.SuccessValue <LiquidCollection>(); Assert.That(arrValue.Count, Is.EqualTo(1)); Assert.That(arrValue[0].Value, Is.EqualTo("Hello")); }
public static LiquidBoolean Compare(ILiquidValue x, ILiquidValue y, Func <decimal, decimal, bool> func) { var numericValueResult1 = ValueCaster.Cast <ILiquidValue, LiquidNumeric>(x); var numericValueResult2 = ValueCaster.Cast <ILiquidValue, LiquidNumeric>(y); if (numericValueResult2.IsError || numericValueResult1.IsError) { return(new LiquidBoolean(false)); } var decimalValue1 = numericValueResult1.SuccessValue <LiquidNumeric>().DecimalValue; var decimalValue2 = numericValueResult2.SuccessValue <LiquidNumeric>().DecimalValue; return(new LiquidBoolean(func(decimalValue1, decimalValue2))); }
// SEE: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers public void It_Should_Cast_KV_Pairs_In_A_Dictionary_To_An_Array_Of_Arrays_with_Two_Elements() { // Arrange var dictValue = new LiquidHash { { "one", LiquidString.Create("ONE") }, { "two", LiquidString.Create("TWO") }, { "three", LiquidString.Create("THREE") }, { "four", LiquidString.Create("FOUR") } }; // Act var result = ValueCaster.Cast <LiquidHash, LiquidCollection>(dictValue).SuccessValue <LiquidCollection>(); // Assert Assert.Equal(4, result.Count); }
public IEnumerable <ILiquidValue> Eval(ITemplateContext templateContext) { var expressionConstant = LiquidExpressionEvaluator.Eval(_liquidCollectionExpression, templateContext); if (expressionConstant.IsError || !expressionConstant.SuccessResult.HasValue) { return(new List <ILiquidValue>()); } var castResult = ValueCaster.Cast <ILiquidValue, LiquidCollection>(expressionConstant.SuccessResult.Value); if (castResult.IsError) { // ?? return(new List <ILiquidValue>()); } else { return(castResult.SuccessValue <LiquidCollection>().Select(x => x.HasValue? x.Value : null)); } }
private LiquidNumeric ValueAsNumeric(TreeNode <LiquidExpression> expr, ITemplateContext templateContext) { var liquidExpressionResult = LiquidExpressionEvaluator.Eval(expr, templateContext); if (liquidExpressionResult.IsError) { return(LiquidNumeric.Create(0)); } var valueAsNumeric = liquidExpressionResult.SuccessResult.Value as LiquidNumeric; if (valueAsNumeric == null) { var castedValue = ValueCaster.Cast <ILiquidValue, LiquidNumeric>(liquidExpressionResult.SuccessResult.Value); return(liquidExpressionResult.IsSuccess && liquidExpressionResult.SuccessResult.HasValue ? castedValue.SuccessValue <LiquidNumeric>() : LiquidNumeric.Create(0)); } else { return(valueAsNumeric); } }
public void It_Should_Not_Cast_Dict_To_Numeric() { var result = ValueCaster.Cast <LiquidHash, LiquidNumeric>(new LiquidHash()); Assert.False(result.IsSuccess); }