|
1 | | -//using ObjectSemantics.NET.Tests.MoqModels; |
2 | | -//using System; |
3 | | -//using System.Collections.Generic; |
4 | | -//using Xunit; |
5 | | - |
6 | | -//namespace ObjectSemantics.NET.Tests |
7 | | -//{ |
8 | | -// public class EnumerableLoopTests |
9 | | -// { |
10 | | - |
11 | | -// [Fact] |
12 | | -// public void Should_Map_Enumerable_Collection_In_Object() |
13 | | -// { |
14 | | -// //Create Model |
15 | | -// Student student = new Student |
16 | | -// { |
17 | | -// StudentName = "John Doe", |
18 | | -// Invoices = new List<Invoice> |
19 | | -// { |
20 | | -// new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) }, |
21 | | -// new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) } |
22 | | -// } |
23 | | -// }; |
24 | | -// //Template |
25 | | -// var template = new ObjectSemanticsTemplate |
26 | | -// { |
27 | | -// FileContents = @"{{ StudentName }} Invoices |
28 | | -//{{ #foreach(invoices) }} |
29 | | -//<tr> |
30 | | -// <td>{{ Id }}</td> |
31 | | -// <td>{{ RefNo }}</td> |
32 | | -// <td>{{ Narration }}</td> |
33 | | -// <td>{{ Amount:N0 }}</td> |
34 | | -// <td>{{ InvoiceDate:yyyy-MM-dd }}</td> |
35 | | -//</tr> |
36 | | -//{{ #endforeach }}" |
37 | | -// }; |
38 | | -// string generatedTemplate = template.Map(student); |
39 | | -// string expectedResult = @"John Doe Invoices |
40 | | - |
41 | | -//<tr> |
42 | | -// <td>2</td> |
43 | | -// <td>INV_002</td> |
44 | | -// <td>Grade II Fees Invoice</td> |
45 | | -// <td>2,000</td> |
46 | | -// <td>2023-04-01</td> |
47 | | -//</tr> |
48 | | - |
49 | | -//<tr> |
50 | | -// <td>1</td> |
51 | | -// <td>INV_001</td> |
52 | | -// <td>Grade I Fees Invoice</td> |
53 | | -// <td>320</td> |
54 | | -// <td>2022-08-01</td> |
55 | | -//</tr> |
56 | | -//"; |
57 | | -// Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
58 | | -// } |
59 | | - |
60 | | - |
61 | | -// [Fact] |
62 | | -// public void Should_Map_Enumerable_Collection_SingleLine_Test() |
63 | | -// { |
64 | | -// //Create Model |
65 | | -// Student student = new Student |
66 | | -// { |
67 | | -// StudentName = "John Doe", |
68 | | -// Invoices = new List<Invoice> |
69 | | -// { |
70 | | -// new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) }, |
71 | | -// new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) } |
72 | | -// } |
73 | | -// }; |
74 | | -// //Template |
75 | | -// var template = new ObjectSemanticsTemplate |
76 | | -// { |
77 | | -// FileContents = @"{{ #foreach(invoices) }} [{{RefNo}}] {{ #endforeach }}" |
78 | | -// }; |
79 | | -// string generatedTemplate = template.Map(student); |
80 | | -// string expectedResult = " [INV_002] [INV_001] "; |
81 | | -// Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
82 | | -// } |
83 | | - |
84 | | - |
85 | | -// [Fact] |
86 | | -// public void Should_Map_Multiple_Same_Property_Enumerable_Collection_On_Same_Template() |
87 | | -// { |
88 | | -// //Create Model |
89 | | -// Student student = new Student |
90 | | -// { |
91 | | -// StudentName = "John Doe", |
92 | | -// Invoices = new List<Invoice> |
93 | | -// { |
94 | | -// new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) }, |
95 | | -// new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) } |
96 | | -// } |
97 | | -// }; |
98 | | -// //Template |
99 | | -// var template = new ObjectSemanticsTemplate |
100 | | -// { |
101 | | -// FileContents = @" |
102 | | -//{{ StudentName }} Invoices |
103 | | -//LOOP #1 |
104 | | -//{{ #foreach(invoices) }} |
105 | | -// <h5>{{ Id }} On Loop #1</h5> |
106 | | -//{{ #endforeach }} |
107 | | -//LOOP #2 |
108 | | -//{{ #foreach(invoices) }} |
109 | | -// <h5>{{ Id }} On Loop #2</h5> |
110 | | -//{{ #endforeach }}" |
111 | | -// }; |
112 | | -// string generatedTemplate = template.Map(student); |
113 | | -// string expectedResult = @" |
114 | | -//John Doe Invoices |
115 | | -//LOOP #1 |
116 | | - |
117 | | -// <h5>2 On Loop #1</h5> |
118 | | - |
119 | | -// <h5>1 On Loop #1</h5> |
120 | | - |
121 | | -//LOOP #2 |
122 | | - |
123 | | -// <h5>2 On Loop #2</h5> |
124 | | - |
125 | | -// <h5>1 On Loop #2</h5> |
126 | | -//"; |
127 | | -// Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
128 | | -// } |
129 | | - |
130 | | - |
131 | | -// [Fact] |
132 | | -// public void Should_Map_Multiple_Different_Property_Enumerable_Collection_On_Same_Template() |
133 | | -// { |
134 | | -// //Create Model |
135 | | -// Student student = new Student |
136 | | -// { |
137 | | -// StudentName = "John Doe", |
138 | | -// Invoices = new List<Invoice> |
139 | | -// { |
140 | | -// new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) }, |
141 | | -// new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) } |
142 | | -// }, |
143 | | -// StudentClockInDetails = new List<StudentClockInDetail> |
144 | | -// { |
145 | | -// new StudentClockInDetail{ LastClockedInDate = new DateTime(2024, 04, 01), LastClockedInPoints = 10 }, |
146 | | -// new StudentClockInDetail{ LastClockedInDate = new DateTime(2024, 04, 02), LastClockedInPoints = 30 } |
147 | | -// } |
148 | | -// }; |
149 | | -// //Template |
150 | | -// var template = new ObjectSemanticsTemplate |
151 | | -// { |
152 | | -// FileContents = @" |
153 | | -//{{ StudentName }} Invoices |
154 | | -//LOOP #1 |
155 | | -//{{ #foreach(invoices) }} |
156 | | -// <h5>{{ Id }} On Loop #1</h5> |
157 | | -//{{ #endforeach }} |
158 | | -//LOOP #2 |
159 | | -//{{ #foreach(studentClockInDetails) }} |
160 | | -// <h5>Got {{ LastClockedInPoints }} for {{ LastClockedInDate:yyyy-MM-dd }}</h5> |
161 | | -//{{ #endforeach }}" |
162 | | -// }; |
163 | | -// string generatedTemplate = template.Map(student); |
164 | | -// string expectedResult = @" |
165 | | -//John Doe Invoices |
166 | | -//LOOP #1 |
167 | | - |
168 | | -// <h5>2 On Loop #1</h5> |
169 | | - |
170 | | -// <h5>1 On Loop #1</h5> |
171 | | - |
172 | | -//LOOP #2 |
173 | | - |
174 | | -// <h5>Got 10 for 2024-04-01</h5> |
175 | | - |
176 | | -// <h5>Got 30 for 2024-04-02</h5> |
177 | | -//"; |
178 | | - |
179 | | -// Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
180 | | -// } |
181 | | - |
182 | | -// [Fact] |
183 | | -// public void Should_Map_Array_Of_String_With_Formatting() |
184 | | -// { |
185 | | -// //Create Model |
186 | | -// Student student = new Student |
187 | | -// { |
188 | | -// ArrayOfString = new string[] |
189 | | -// { |
190 | | -// "String 001", |
191 | | -// "String 002" |
192 | | -// } |
193 | | -// }; |
194 | | -// //Template |
195 | | -// var template = new ObjectSemanticsTemplate |
196 | | -// { |
197 | | -// FileContents = @"{{ #foreach(ArrayOfString) }} {{ . }} | {{ .:uppercase }} {{ #endforeach }}" |
198 | | -// }; |
199 | | -// string generatedTemplate = template.Map(student); |
200 | | -// string expectedResult = " String 001 | STRING 001 String 002 | STRING 002 "; |
201 | | -// Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
202 | | -// } |
203 | | - |
204 | | - |
205 | | -// [Fact] |
206 | | -// public void Should_Map_Array_Of_Double_With_Formatting_Test() |
207 | | -// { |
208 | | -// //Create Model |
209 | | -// Student student = new Student |
210 | | -// { |
211 | | -// ArrayOfDouble = new double[] |
212 | | -// { |
213 | | -// 1000.15, |
214 | | -// 2000.22 |
215 | | -// } |
216 | | -// }; |
217 | | -// //Template |
218 | | -// var template = new ObjectSemanticsTemplate |
219 | | -// { |
220 | | -// FileContents = @"{{ #foreach(ArrayOfDouble) }} {{ . }} | {{ .:N0 }} {{ #endforeach }}" |
221 | | -// }; |
222 | | -// string generatedTemplate = template.Map(student); |
223 | | -// string expectedResult = " 1000.15 | 1,000 2000.22 | 2,000 "; |
224 | | -// Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
225 | | -// } |
226 | | -// } |
227 | | -//} |
| 1 | +using ObjectSemantics.NET.Tests.MoqModels; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace ObjectSemantics.NET.Tests |
| 7 | +{ |
| 8 | + public class EnumerableLoopTests |
| 9 | + { |
| 10 | + |
| 11 | + [Fact] |
| 12 | + public void Should_Map_Enumerable_Collection_SingleLine() |
| 13 | + { |
| 14 | + //Create Model |
| 15 | + Person person = new Person |
| 16 | + { |
| 17 | + MyCars = new List<Car> |
| 18 | + { |
| 19 | + new Car { Make = "BMW", Year = 2023 }, |
| 20 | + new Car { Make = "Rolls-Royce", Year = 2020 } |
| 21 | + } |
| 22 | + }; |
| 23 | + //Template |
| 24 | + string template = @"{{ #foreach(MyCars) }}I own a {{ Year }} {{ Make }}.{{ #endforeach }}"; |
| 25 | + |
| 26 | + string result = person.Map(template); |
| 27 | + |
| 28 | + string expectedResult = $"I own a {person.MyCars[0].Year} {person.MyCars[0].Make}.I own a {person.MyCars[1].Year} {person.MyCars[1].Make}."; |
| 29 | + Assert.Equal(expectedResult, result); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void Should_Map_Enumerable_Collection_MultiLine() |
| 34 | + { |
| 35 | + //Create Model |
| 36 | + Person person = new Person |
| 37 | + { |
| 38 | + Name = "John Doe", |
| 39 | + MyCars = new List<Car> |
| 40 | + { |
| 41 | + new Car { Make = "BMW", Year = 2023 }, |
| 42 | + new Car { Make = "Rolls-Royce", Year = 2020 } |
| 43 | + } |
| 44 | + }; |
| 45 | + //Template |
| 46 | + string template = @" |
| 47 | +{{ Name }}'s Cars |
| 48 | +{{ #foreach(MyCars) }} |
| 49 | + - {{ Year }} {{ Make }} |
| 50 | +{{ #endforeach }}"; |
| 51 | + |
| 52 | + string result = person.Map(template); |
| 53 | + |
| 54 | + string expectedResult = @$" |
| 55 | +{person.Name}'s Cars |
| 56 | + - {person.MyCars[0].Year} {person.MyCars[0].Make} |
| 57 | + - {person.MyCars[1].Year} {person.MyCars[1].Make} |
| 58 | +"; |
| 59 | + Assert.Equal(expectedResult, result); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void Should_Map_Multiple_Enumerable_Collections() |
| 66 | + { |
| 67 | + //Create Model |
| 68 | + Person person = new Person |
| 69 | + { |
| 70 | + MyCars = new List<Car> |
| 71 | + { |
| 72 | + new Car { Make = "Honda" }, |
| 73 | + new Car { Make = "Toyota" } |
| 74 | + }, |
| 75 | + MyDreamCars = new List<Car> |
| 76 | + { |
| 77 | + new Car { Make = "BWM" }, |
| 78 | + new Car { Make = "Rolls-Royce" } |
| 79 | + } |
| 80 | + }; |
| 81 | + //Template |
| 82 | + string template = @" |
| 83 | +My Cars |
| 84 | +{{ #foreach(MyCars) }} |
| 85 | + - {{ Make }} |
| 86 | +{{ #endforeach }} |
| 87 | +My Dream Cars |
| 88 | +{{ #foreach(MyDreamCars) }} |
| 89 | + * {{ Make }} |
| 90 | +{{ #endforeach }} |
| 91 | +"; |
| 92 | + |
| 93 | + string result = person.Map(template); |
| 94 | + |
| 95 | + string expectedResult = @$" |
| 96 | +My Cars |
| 97 | + - {person.MyCars[0].Make} |
| 98 | + - {person.MyCars[1].Make} |
| 99 | +My Dream Cars |
| 100 | + - {person.MyDreamCars[0].Make} |
| 101 | + - {person.MyDreamCars[1].Make} |
| 102 | +"; |
| 103 | + Assert.Equal(expectedResult, result); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void Should_Map_Array_Of_String() |
| 108 | + { |
| 109 | + //Create Model |
| 110 | + Person person = new Person |
| 111 | + { |
| 112 | + MyFriends = new string[] { "Morgan", "George", "Jane" } |
| 113 | + }; |
| 114 | + //Template |
| 115 | + string template = @"{{ #foreach(MyFriends) }} {{ . }} {{ #endforeach }}"; |
| 116 | + |
| 117 | + string generatedTemplate = person.Map(template); |
| 118 | + string expectedResult = string.Join(string.Empty, person.MyFriends); |
| 119 | + Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public void Should_Map_Array_Of_String_With_Formatting() |
| 124 | + { |
| 125 | + //Create Model |
| 126 | + Person person = new Person |
| 127 | + { |
| 128 | + MyFriends = new string[] { "Morgan", "George", "Jane" } |
| 129 | + }; |
| 130 | + //Template |
| 131 | + string template = @"{{ #foreach(MyFriends) }} {{ .:uppercase }} {{ #endforeach }}"; |
| 132 | + |
| 133 | + string generatedTemplate = person.Map(template); |
| 134 | + string expectedResult = string.Join(string.Empty, person.MyFriends.Select(x => x.ToUpper())); |
| 135 | + Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments