Skip to content

Commit ff6e52a

Browse files
committed
added unit tests for Model feature
1 parent a92a828 commit ff6e52a

2 files changed

Lines changed: 139 additions & 9 deletions

File tree

src/SolidifyProject.Engine.Infrastructure/Models/PageModel.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ public sealed class PageModel : TextContentModel
4444

4545
public dynamic Model { get; set; }
4646

47-
public override void Parse()
47+
public PageModel()
4848
{
4949
Custom = new ExpandoObject();
5050
Model = new ExpandoObject();
51-
51+
}
52+
53+
public override void Parse()
54+
{
5255
var lines = ContentRaw.Split(END_OF_LINE, StringSplitOptions.None);
5356

5457
var attributeLines = lines
@@ -62,12 +65,11 @@ public override void Parse()
6265

6366
var contentLines = lines.SkipWhile(x => !SEPARATOR.Equals(x)).Skip(1);
6467
ParseContent(contentLines);
65-
6668
}
6769

6870
public void MapDataToModel(ExpandoObject data)
6971
{
70-
mapDataToPageModel(Model, data);
72+
MapDataToPageModel(Model, data);
7173
}
7274

7375
private void ParseAttributeLine(string line)
@@ -177,14 +179,14 @@ private void ParseContent(IEnumerable<string> lines)
177179
Content = string.Join("\r\n", lines);
178180
}
179181

180-
private void mapDataToPageModel(ExpandoObject model, ExpandoObject data)
182+
private void MapDataToPageModel(ExpandoObject model, ExpandoObject data)
181183
{
182184
IDictionary<string, object> modelDict = model;
183185
foreach (var keyValuePair in model)
184186
{
185187
if (keyValuePair.Value is ExpandoObject expObject)
186188
{
187-
mapDataToPageModel(expObject, data);
189+
MapDataToPageModel(expObject, data);
188190
}
189191
else
190192
{

src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/PageModelTest.cs

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using NUnit.Framework;
1+
using System.Collections.Generic;
2+
using System.Dynamic;
3+
using NUnit.Framework;
24
using SolidifyProject.Engine.Infrastructure.Models;
35

46
namespace SolidifyProject.Engine.Test.Unit.Infrastructure.Models
@@ -49,8 +51,8 @@ public void ParseCustomAttributesTest()
4951
};
5052

5153
var actualModel = new PageModel { ContentRaw = @"
52-
custom.image: logo.png
53-
custom.description: this is description
54+
Custom.image: logo.png
55+
Custom.description: this is description
5456
custom.root.SomeThing: bla-bla-bla
5557
custom.l1.l2.l3.l4: hello
5658
---
@@ -63,5 +65,131 @@ public void ParseCustomAttributesTest()
6365
Assert.AreEqual(expectedModel.Custom.root.SomeThing, actualModel.Custom.root.SomeThing);
6466
Assert.AreEqual(expectedModel.Custom.l1.l2.l3.l4, actualModel.Custom.l1.l2.l3.l4);
6567
}
68+
69+
[Test]
70+
public void ParseModelSimpleTest()
71+
{
72+
ICollection<KeyValuePair<string, object>> data = new ExpandoObject();
73+
data.Add(new KeyValuePair<string, object>("goods", new List<string>
74+
{
75+
"apple",
76+
"peach",
77+
"mango"
78+
}));
79+
data.Add(new KeyValuePair<string, object>("banner", new
80+
{
81+
url = "http://mydomain.com/banner.png"
82+
}));
83+
data.Add(new KeyValuePair<string, object>("other", new { }));
84+
85+
var actualModel = new PageModel { ContentRaw = @"
86+
Model: Data
87+
---
88+
"};
89+
90+
actualModel.Parse();
91+
actualModel.MapDataToModel((ExpandoObject)data);
92+
93+
Assert.AreEqual(((dynamic)data).goods.Count, actualModel.Model.goods.Count);
94+
Assert.AreEqual(((dynamic)data).goods[0], actualModel.Model.goods[0]);
95+
Assert.AreEqual(((dynamic)data).goods[1], actualModel.Model.goods[1]);
96+
Assert.AreEqual(((dynamic)data).goods[2], actualModel.Model.goods[2]);
97+
98+
Assert.AreEqual(((dynamic)data).banner.url, actualModel.Model.banner.url);
99+
}
100+
101+
[Test]
102+
public void ParseModelSingleItemTest()
103+
{
104+
ICollection<KeyValuePair<string, object>> data = new ExpandoObject();
105+
data.Add(new KeyValuePair<string, object>("goods", new List<string>
106+
{
107+
"apple",
108+
"peach",
109+
"mango"
110+
}));
111+
data.Add(new KeyValuePair<string, object>("banner", new
112+
{
113+
url = "http://mydomain.com/banner.png"
114+
}));
115+
data.Add(new KeyValuePair<string, object>("other", new { }));
116+
117+
var actualModel = new PageModel { ContentRaw = @"
118+
Model.goods: Data.goods
119+
---
120+
"};
121+
122+
actualModel.Parse();
123+
actualModel.MapDataToModel((ExpandoObject)data);
124+
125+
Assert.AreEqual(((dynamic)data).goods.Count, actualModel.Model.goods.Count);
126+
Assert.AreEqual(((dynamic)data).goods[0], actualModel.Model.goods[0]);
127+
Assert.AreEqual(((dynamic)data).goods[1], actualModel.Model.goods[1]);
128+
Assert.AreEqual(((dynamic)data).goods[2], actualModel.Model.goods[2]);
129+
}
130+
131+
[Test]
132+
public void ParseModelMultipleItemsTest()
133+
{
134+
ICollection<KeyValuePair<string, object>> data = new ExpandoObject();
135+
data.Add(new KeyValuePair<string, object>("goods", new List<string>
136+
{
137+
"apple",
138+
"peach",
139+
"mango"
140+
}));
141+
data.Add(new KeyValuePair<string, object>("banner", new
142+
{
143+
url = "http://mydomain.com/banner.png"
144+
}));
145+
data.Add(new KeyValuePair<string, object>("other", new { }));
146+
147+
var actualModel = new PageModel { ContentRaw = @"
148+
Model.goods: Data.goods
149+
Model.bannerUrl: Data.banner.url
150+
---
151+
"};
152+
153+
actualModel.Parse();
154+
actualModel.MapDataToModel((ExpandoObject)data);
155+
156+
Assert.AreEqual(((dynamic)data).goods.Count, actualModel.Model.goods.Count);
157+
Assert.AreEqual(((dynamic)data).goods[0], actualModel.Model.goods[0]);
158+
Assert.AreEqual(((dynamic)data).goods[1], actualModel.Model.goods[1]);
159+
Assert.AreEqual(((dynamic)data).goods[2], actualModel.Model.goods[2]);
160+
Assert.AreEqual(((dynamic)data).banner.url, actualModel.Model.bannerUrl);
161+
}
162+
163+
[Test]
164+
public void ParseModelComplexMultipleItemsTest()
165+
{
166+
ICollection<KeyValuePair<string, object>> data = new ExpandoObject();
167+
data.Add(new KeyValuePair<string, object>("goods", new List<string>
168+
{
169+
"apple",
170+
"peach",
171+
"mango"
172+
}));
173+
data.Add(new KeyValuePair<string, object>("banner", new
174+
{
175+
url = "http://mydomain.com/banner.png"
176+
}));
177+
data.Add(new KeyValuePair<string, object>("other", new { }));
178+
179+
var actualModel = new PageModel { ContentRaw = @"
180+
Model.category.goods: Data.goods
181+
Model.category.bannerUrl: Data.banner.url
182+
---
183+
"};
184+
185+
actualModel.Parse();
186+
actualModel.MapDataToModel((ExpandoObject)data);
187+
188+
Assert.AreEqual(((dynamic)data).goods.Count, actualModel.Model.category.goods.Count);
189+
Assert.AreEqual(((dynamic)data).goods[0], actualModel.Model.category.goods[0]);
190+
Assert.AreEqual(((dynamic)data).goods[1], actualModel.Model.category.goods[1]);
191+
Assert.AreEqual(((dynamic)data).goods[2], actualModel.Model.category.goods[2]);
192+
Assert.AreEqual(((dynamic)data).banner.url, actualModel.Model.bannerUrl);
193+
}
66194
}
67195
}

0 commit comments

Comments
 (0)