Skip to content

Commit f823f3a

Browse files
author
Mike Bridge
committed
simple object => liquidvalue translator
1 parent 82d0e2b commit f823f3a

File tree

4 files changed

+427
-0
lines changed

4 files changed

+427
-0
lines changed

Liquid.NET.Tests/Liquid.NET.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
<Compile Include="TemplateContextTests.cs" />
182182
<Compile Include="Utils\EitherTests.cs" />
183183
<Compile Include="Utils\OptionTests.cs" />
184+
<Compile Include="Utils\LiquidValueConverterTests.cs" />
184185
<Compile Include="Utils\RegistryTests.cs" />
185186
<Compile Include="Utils\StrFTimeTests.cs" />
186187
<Compile Include="Utils\TryTests.cs" />
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Numerics;
4+
using Liquid.NET.Constants;
5+
using Liquid.NET.Utils;
6+
using NUnit.Framework;
7+
8+
namespace Liquid.NET.Tests.Utils
9+
{
10+
[TestFixture]
11+
public class LiquidValueConverterTests
12+
{
13+
LiquidValueConverter _converter;
14+
15+
[SetUp]
16+
public void SetUp()
17+
{
18+
_converter = new LiquidValueConverter();
19+
}
20+
21+
[Test]
22+
public void It_Should_Convert_Null_To_None()
23+
{
24+
// Act
25+
var none = _converter.Convert(null);
26+
27+
// Assert
28+
Assert.That(none, Is.EqualTo(Option<ILiquidValue>.None()));
29+
}
30+
31+
[Test]
32+
public void It_Should_Convert_String_To_LiquidString()
33+
{
34+
// Act
35+
var testString = "Test STring";
36+
var val = _converter.Convert(testString);
37+
38+
// Assert
39+
Assert.That(val.HasValue, Is.True);
40+
Assert.That(val.Value, Is.TypeOf<LiquidString>());
41+
Assert.That(val.Value, Is.EqualTo(LiquidString.Create(testString)));
42+
}
43+
44+
[Test]
45+
[TestCase(123, typeof(IntLiquidNumeric), 123)]
46+
[TestCase(123U, typeof(IntLiquidNumeric), 123)]
47+
[TestCase(123L, typeof(LongLiquidNumeric), 123)]
48+
[TestCase(123UL, typeof(LongLiquidNumeric), 123)]
49+
public void It_Should_Convert_Int_To_LiquidNumeric_Int(object origValue, Type type, object expected)
50+
{
51+
// Act
52+
var val = _converter.Convert(origValue);
53+
54+
// Assert
55+
Assert.That(val.HasValue, Is.True);
56+
Assert.That(val.Value.GetType().IsAssignableFrom(type), Is.True);
57+
Assert.That(val.Value.Value, Is.EqualTo(expected));
58+
}
59+
60+
[Test]
61+
public void It_Should_Convert_To_Decimal()
62+
{
63+
var val = _converter.Convert(123.2m);
64+
Assert.That(val.HasValue, Is.True);
65+
Assert.That(val.Value, Is.TypeOf<DecimalLiquidNumeric>());
66+
}
67+
68+
[Test]
69+
public void It_Should_Convert_Double_To_Decimal()
70+
{
71+
var val = _converter.Convert(123.2d);
72+
Assert.That(val.HasValue, Is.True);
73+
Assert.That(val.Value, Is.TypeOf<DecimalLiquidNumeric>());
74+
Assert.That(val.Value.Value, Is.EqualTo(123.2m));
75+
}
76+
77+
[Test]
78+
public void It_Should_Convert_Float_To_Decimal()
79+
{
80+
var val = _converter.Convert(123.2f);
81+
Assert.That(val.HasValue, Is.True);
82+
Assert.That(val.Value, Is.TypeOf<DecimalLiquidNumeric>());
83+
Assert.That(val.Value.Value, Is.EqualTo(123.2m));
84+
}
85+
86+
[Test]
87+
public void It_Should_Covert_BigInteger()
88+
{
89+
var val = _converter.Convert(new BigInteger(999));
90+
Assert.That(val.HasValue, Is.True);
91+
Assert.That(val.Value, Is.TypeOf<BigIntegerLiquidNumeric>());
92+
Assert.That(val.Value.Value, Is.EqualTo(new BigInteger(999)));
93+
}
94+
95+
[Test]
96+
public void It_Should_Covert_A_Boolean()
97+
{
98+
var val = _converter.Convert(true);
99+
Assert.That(val.HasValue, Is.True);
100+
Assert.That(val.Value, Is.TypeOf<LiquidBoolean>());
101+
Assert.That(val.Value.Value, Is.True);
102+
}
103+
104+
105+
[Test]
106+
public void It_Should_Covert_A_DateTime()
107+
{
108+
var dateTime = new DateTime(2015,11,10,15,34,10);
109+
var val = _converter.Convert(dateTime);
110+
Assert.That(val.HasValue, Is.True);
111+
Assert.That(val.Value, Is.TypeOf<LiquidDate>());
112+
Assert.That(val.Value, Is.EqualTo(new LiquidDate(dateTime)));
113+
}
114+
115+
[Test]
116+
public void It_Should_Convert_Generic_List_To_Collection()
117+
{
118+
var list = new List<Object>();
119+
var val = _converter.Convert(list);
120+
Assert.That(val.HasValue, Is.True);
121+
Assert.That(val.Value, Is.TypeOf<LiquidCollection>());
122+
}
123+
124+
[Test]
125+
public void It_Should_Convert_IDictionary_To_Hash()
126+
{
127+
var dict = new Dictionary<String, Object>();
128+
var val = _converter.Convert(dict);
129+
Assert.That(val.HasValue, Is.True);
130+
Assert.That(val.Value, Is.TypeOf<LiquidHash>());
131+
}
132+
133+
[Test]
134+
public void It_Should_Convert_IDictionary_Values_To_Hash_When_String_Key()
135+
{
136+
var dict = new Dictionary<String, Object>{{"test", 1}, {"test2", "TEST"}};
137+
var val = _converter.Convert(dict);
138+
Assert.That(val.HasValue, Is.True);
139+
var hash = (LiquidHash)val.Value;
140+
Assert.That(hash.ContainsKey("test"), Is.True);
141+
Assert.That(hash.ContainsKey("test2"), Is.True);
142+
Assert.That(hash["test"].Value, Is.EqualTo(LiquidNumeric.Create(1)));
143+
Assert.That(hash["test2"].Value, Is.EqualTo(LiquidString.Create("TEST")));
144+
}
145+
146+
[Test]
147+
public void It_Should_Convert_IDictionary_Values_To_Hash_Via_Key_ToString_WHen_Conflicting()
148+
{
149+
var dict = new Dictionary<Object, Object> { { "3", "three" }, {3, "THREE" } };
150+
var val = _converter.Convert(dict);
151+
Assert.That(val.HasValue, Is.True);
152+
var hash = (LiquidHash)val.Value;
153+
Assert.That(hash.Keys.Count, Is.EqualTo(1));
154+
Assert.That(hash.ContainsKey("3"), Is.True);
155+
Assert.That(hash["3"].Value, Is.EqualTo(LiquidString.Create("THREE")));
156+
}
157+
158+
[Test]
159+
public void It_Should_Convert_IList_Values()
160+
{
161+
var arr = new List<Object> {"Test", 123, null };
162+
var val = _converter.Convert(arr);
163+
Assert.That(val.HasValue, Is.True);
164+
var coll = (LiquidCollection) val.Value;
165+
Assert.That(coll.Count, Is.EqualTo(3));
166+
Assert.That(coll[0].Value, Is.EqualTo(LiquidString.Create("Test")));
167+
Assert.That(coll[1].Value, Is.EqualTo(LiquidNumeric.Create(123)));
168+
Assert.That(coll[2].HasValue, Is.False);
169+
}
170+
171+
[Test]
172+
public void It_Should_Convert_An_Object()
173+
{
174+
// Act
175+
var testClass = new TestClass{FieLd1="aaa", Field1 = "bbb", IntField1=3, ObjField1="my obj"};
176+
var result = _converter.Convert(testClass);
177+
178+
Assert.That(result.HasValue);
179+
var objAsHash = (LiquidHash) result.Value;
180+
181+
// Assert
182+
Assert.That(objAsHash.ContainsKey("field1"));
183+
Assert.That(objAsHash.Keys.Count, Is.EqualTo(3));
184+
Assert.That(objAsHash["field1"].Value, Is.EqualTo(LiquidString.Create(testClass.FieLd1)));
185+
Assert.That(objAsHash["intfield1"].Value, Is.EqualTo(LiquidNumeric.Create(testClass.IntField1)));
186+
Assert.That(objAsHash["objfield1"].Value, Is.EqualTo(LiquidString.Create((String) testClass.ObjField1)));
187+
}
188+
189+
[Test]
190+
public void It_Should_Convert_A_Nested_Object()
191+
{
192+
// Act
193+
var nestedClass = new TestClass { IntField1= 33};
194+
var testClass = new TestClass { FieLd1 = "aaa", Field1 = "bbb", IntField1 = 3, ObjField1 = nestedClass };
195+
196+
var result = _converter.Convert(testClass);
197+
198+
Assert.That(result.HasValue);
199+
var objAsHash = (LiquidHash)result.Value;
200+
201+
// Assert
202+
Assert.That(objAsHash["objfield1"].Value, Is.InstanceOf<LiquidHash>());
203+
var foundNestedObject = (LiquidHash) objAsHash["objfield1"].Value;
204+
Assert.That(foundNestedObject.ContainsKey("intfield1"));
205+
Assert.That(foundNestedObject.ContainsKey("intfield1"));
206+
207+
}
208+
209+
[Test]
210+
public void It_Should_Convert_An_Object_With_Null_Field()
211+
{
212+
// Act
213+
var testClass = new TestClass { FieLd1 = "aaa", Field1 = "bbb", IntField1 = 3, ObjField1 = null };
214+
215+
var result = _converter.Convert(testClass);
216+
217+
Assert.That(result.HasValue);
218+
var objAsHash = (LiquidHash)result.Value;
219+
220+
// Assert
221+
Assert.That(objAsHash["objfield1"].HasValue, Is.False);
222+
223+
}
224+
225+
public class TestClass
226+
{
227+
public String Field1 {get; set;}
228+
public String FieLd1 {get; set;} // different capitalization
229+
public int IntField1 {get; set;}
230+
public Object ObjField1 {get; set;}
231+
232+
}
233+
234+
235+
236+
}
237+
}

Liquid.NET/Liquid.NET.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
<Compile Include="src\Utils\LiquidExpressionResult.cs" />
219219
<Compile Include="src\Utils\LiquidVersionAttribute.cs" />
220220
<Compile Include="src\Utils\Option.cs" />
221+
<Compile Include="src\Utils\LiquidValueConverter.cs" />
221222
<Compile Include="src\Utils\Registry.cs" />
222223
<Compile Include="src\Utils\StrFTime.cs" />
223224
<Compile Include="src\Utils\TreeNode.cs" />

0 commit comments

Comments
 (0)