Skip to content

Commit ec3ef7a

Browse files
authored
Merge pull request #12 from swagfin/feature/for-loop-improvements
Feature/for loop improvements
2 parents 72d81e5 + fd8255f commit ec3ef7a

File tree

7 files changed

+205
-173
lines changed

7 files changed

+205
-173
lines changed

ObjectSemantics.NET.Tests/ObjectSemanticsTests.cs

Lines changed: 97 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,62 @@ public void Should_Map_Enumerable_Collection_In_Object()
4747
var template = new ObjectSemanticsTemplate
4848
{
4949
FileContents = @"{{ StudentName }} Invoices
50-
{{ for-each-start:invoices }}
50+
{{ #foreach(invoices) }}
5151
<tr>
5252
<td>{{ Id }}</td>
5353
<td>{{ RefNo }}</td>
5454
<td>{{ Narration }}</td>
5555
<td>{{ Amount:N0 }}</td>
5656
<td>{{ InvoiceDate:yyyy-MM-dd }}</td>
5757
</tr>
58-
{{ for-each-end:invoices }}"
58+
{{ #endforeach }}"
5959
};
6060
string generatedTemplate = TemplateMapper.Map(student, template);
61-
string expectedResult = "John Doe Invoices" +
62-
"\r\n<tr>" +
63-
"\r\n <td>2</td>" +
64-
"\r\n <td>INV_002</td>" +
65-
"\r\n <td>Grade II Fees Invoice</td>" +
66-
"\r\n <td>2,000</td>" +
67-
"\r\n <td>2023-04-01</td>" +
68-
"\r\n</tr>" +
69-
"\r\n<tr>" +
70-
"\r\n <td>1</td>" +
71-
"\r\n <td>INV_001</td>" +
72-
"\r\n <td>Grade I Fees Invoice</td>" +
73-
"\r\n <td>320</td>" +
74-
"\r\n <td>2022-08-01</td>" +
75-
"\r\n</tr>";
61+
string expectedResult = @"John Doe Invoices
62+
63+
<tr>
64+
<td>2</td>
65+
<td>INV_002</td>
66+
<td>Grade II Fees Invoice</td>
67+
<td>2,000</td>
68+
<td>2023-04-01</td>
69+
</tr>
70+
71+
<tr>
72+
<td>1</td>
73+
<td>INV_001</td>
74+
<td>Grade I Fees Invoice</td>
75+
<td>320</td>
76+
<td>2022-08-01</td>
77+
</tr>";
78+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
79+
}
80+
81+
82+
[Fact]
83+
public void Should_Map_Enumerable_Collection_SingleLine_Test()
84+
{
85+
//Create Model
86+
Student student = new Student
87+
{
88+
StudentName = "John Doe",
89+
Invoices = new List<Invoice>
90+
{
91+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
92+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
93+
}
94+
};
95+
//Template
96+
var template = new ObjectSemanticsTemplate
97+
{
98+
FileContents = @"{{ #foreach(invoices) }} [{{RefNo}}] {{ #endforeach }}"
99+
};
100+
string generatedTemplate = TemplateMapper.Map(student, template);
101+
string expectedResult = " [INV_002] [INV_001] ";
76102
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
77103
}
78104

105+
79106
[Fact]
80107
public void Should_Map_Multiple_Enumerable_Collection_On_Same_Template()
81108
{
@@ -95,24 +122,27 @@ public void Should_Map_Multiple_Enumerable_Collection_On_Same_Template()
95122
FileContents = @"
96123
{{ StudentName }} Invoices
97124
LOOP #1
98-
{{ for-each-start:invoices }}
125+
{{ #foreach(invoices) }}
99126
<h5>{{ Id }} On Loop #1</h5>
100-
{{ for-each-end:invoices }}
127+
{{ #endforeach }}
101128
LOOP #2
102-
{{ for-each-start:invoices }}
129+
{{ #foreach(invoices) }}
103130
<h5>{{ Id }} On Loop #2</h5>
104-
{{ for-each-end:invoices }}
105-
"
131+
{{ #endforeach }}"
106132
};
107133
string generatedTemplate = TemplateMapper.Map(student, template);
108-
string expectedResult = "\r\nJohn Doe Invoices" +
109-
"\r\nLOOP #1" +
110-
"\r\n <h5>2 On Loop #1</h5>" +
111-
"\r\n <h5>1 On Loop #1</h5>" +
112-
"\r\nLOOP #2" +
113-
"\r\n <h5>2 On Loop #2</h5>" +
114-
"\r\n <h5>1 On Loop #2</h5>" +
115-
"\r\n"; ;
134+
string expectedResult = @"
135+
John Doe Invoices
136+
LOOP #1
137+
138+
<h5>2 On Loop #1</h5>
139+
140+
<h5>1 On Loop #1</h5>
141+
LOOP #2
142+
143+
<h5>2 On Loop #2</h5>
144+
145+
<h5>1 On Loop #2</h5>";
116146
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
117147
}
118148

@@ -156,7 +186,7 @@ public void Should_Return_Unknown_Properties_If_Not_Found_In_Object()
156186
FileContents = @"Unknown Object example: {{StudentIdentityCardXyx}}"
157187
};
158188
string generatedTemplate = TemplateMapper.Map(student, template);
159-
string expectedString = "Unknown Object example: {{StudentIdentityCardXyx}}";
189+
string expectedString = "Unknown Object example: {{ StudentIdentityCardXyx }}";
160190
Assert.Equal(expectedString, generatedTemplate, false, true, true);
161191
}
162192

@@ -365,24 +395,29 @@ public void Should_Act_On_IfCondition_Having_Loop_As_Child()
365395
FileContents = @"
366396
{{ #if (invoices != null) }}
367397
{{ StudentName }} Invoices
368-
{{ for-each-start:invoices }}
398+
{{ #foreach(invoices) }}
369399
<tr>
370400
<td>{{ Id }}</td>
371401
<td>{{ RefNo }}</td>
372402
</tr>
373-
{{ for-each-end:invoices }}
403+
{{ #endforeach }}
374404
{{ #endif }}"
375405
};
376406
string generatedTemplate = TemplateMapper.Map(student, template);
377-
string expectedResult = "\r\n\r\nJohn Doe Invoices" +
378-
"\r\n<tr>" +
379-
"\r\n <td>2</td>" +
380-
"\r\n <td>INV_002</td>" +
381-
"\r\n</tr>" +
382-
"\r\n<tr>" +
383-
"\r\n <td>1</td>" +
384-
"\r\n <td>INV_001</td>" +
385-
"\r\n</tr>\r\n"; ;
407+
string expectedResult = @"
408+
409+
John Doe Invoices
410+
411+
<tr>
412+
<td>2</td>
413+
<td>INV_002</td>
414+
</tr>
415+
416+
<tr>
417+
<td>1</td>
418+
<td>INV_001</td>
419+
</tr>
420+
";
386421
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
387422
}
388423

@@ -667,26 +702,33 @@ public void Should_Act_On_IfCondition_Having_ElseIf_Having_A_LoopBlock(bool popu
667702
{{ #if(invoices==null) }}
668703
-- no invoices found --
669704
{{ #else }}
670-
{{ for-each-start:invoices }}
705+
{{ #foreach(invoices) }}
671706
<tr>
672707
<td>{{ Id }}</td>
673708
<td>{{ RefNo }}</td>
674709
</tr>
675-
{{ for-each-end:invoices }}
710+
{{ #endforeach }}
676711
{{ #endif }}"
677712
};
678713
string generatedTemplate = TemplateMapper.Map(student, template);
679-
string expectedResult = (populateInvoices) ? "\r\n" +
680-
"\r\n<tr>" +
681-
"\r\n <td>2</td>" +
682-
"\r\n <td>INV_002</td>" +
683-
"\r\n</tr>" +
684-
"\r\n<tr>" +
685-
"\r\n <td>1</td>" +
686-
"\r\n <td>INV_001</td>" +
687-
"\r\n</tr>\r\n"
688-
689-
: "\r\n\r\n-- no invoices found --\r\n";
714+
string expectedResult = (populateInvoices) ? @"
715+
716+
717+
<tr>
718+
<td>2</td>
719+
<td>INV_002</td>
720+
</tr>
721+
722+
<tr>
723+
<td>1</td>
724+
<td>INV_001</td>
725+
</tr>
726+
"
727+
728+
: @"
729+
730+
-- no invoices found --
731+
";
690732
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
691733
}
692734

0 commit comments

Comments
 (0)