-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnumerableLoopTests.cs
More file actions
159 lines (134 loc) · 4.33 KB
/
EnumerableLoopTests.cs
File metadata and controls
159 lines (134 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using ObjectSemantics.NET.Tests.MoqModels;
using System.Collections.Generic;
using Xunit;
namespace ObjectSemantics.NET.Tests
{
public class EnumerableLoopTests
{
[Fact]
public void Should_Map_Enumerable_Collection_SingleLine()
{
//Create Model
Person person = new Person
{
MyCars = new List<Car>
{
new Car { Make = "BMW", Year = 2023 },
new Car { Make = "Rolls-Royce", Year = 2020 }
}
};
//Template
string template = @"{{ #foreach(MyCars) }}I own a {{ Year }} {{ Make }}.{{ #endforeach }}";
string result = person.Map(template);
string expectedResult = $"I own a 2023 BMW.I own a 2020 Rolls-Royce.";
Assert.Equal(expectedResult, result);
}
[Fact]
public void Should_Map_Enumerable_Collection_MultiLine()
{
//Create Model
Person person = new Person
{
Name = "John Doe",
MyCars = new List<Car>
{
new Car { Make = "BMW", Year = 2023 },
new Car { Make = "Rolls-Royce", Year = 2020 }
}
};
//Template
string template = @"
{{ Name }}'s Cars
{{ #foreach(MyCars) }}
- {{ Year }} {{ Make }}
{{ #endforeach }}";
string result = person.Map(template);
string expectedResult = @"
John Doe's Cars
- 2023 BMW
- 2020 Rolls-Royce
";
Assert.Equal(expectedResult, result);
}
[Fact]
public void Should_Map_Multiple_Enumerable_Collections()
{
//Create Model
Person person = new Person
{
MyCars = new List<Car>
{
new Car { Make = "Honda" },
new Car { Make = "Toyota" }
},
MyDreamCars = new List<Car>
{
new Car { Make = "BWM" },
new Car { Make = "Rolls-Royce" }
}
};
//Template
string template = @"
My Cars
{{ #foreach(MyCars) }}
- {{ Make }}
{{ #endforeach }}
My Dream Cars
{{ #foreach(MyDreamCars) }}
* {{ Make }}
{{ #endforeach }}
";
string result = person.Map(template);
string expectedResult = @"
My Cars
- Honda
- Toyota
My Dream Cars
* BWM
* Rolls-Royce
";
Assert.Equal(expectedResult, result);
}
[Fact]
public void Should_Map_Array_Of_String()
{
//Create Model
Person person = new Person
{
MyFriends = new string[] { "Morgan", "George", "Jane" }
};
//Template
string template = @"{{ #foreach(MyFriends) }} {{ . }} {{ #endforeach }}";
string generatedTemplate = person.Map(template);
string expectedResult = " Morgan George Jane ";
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
}
[Fact]
public void Should_Map_Array_Of_String_With_Formatting()
{
//Create Model
Person person = new Person
{
MyFriends = new string[] { "Morgan", "George", "Jane" }
};
//Template
string template = @"{{ #foreach(MyFriends) }} {{ .:uppercase }} {{ #endforeach }}";
string generatedTemplate = person.Map(template);
string expectedResult = " MORGAN GEORGE JANE ";
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
}
[Theory]
[InlineData(@"{{ #foreach(MyFriends) }}[{{ .:uppercase }}]{{ #endforeach }}")]
[InlineData(@"{{# foreach(MyFriends) }}[{{ .:uppercase }}]{{# endforeach }}")]
[InlineData(@"{{ #foreach(MyFriends) }}[{{ .:uppercase }}]{{ #endforeach }}")]
public void Should_Evaluate_ForEach_Having_Spaces_Before_And_After_Parentheses(string template)
{
Person person = new Person
{
MyFriends = new string[] { "Morgan", "George" }
};
string result = person.Map(template);
Assert.Equal("[MORGAN][GEORGE]", result);
}
}
}