|
| 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 LoopFunctionTests |
| 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 = TemplateMapper.Map(student, template); |
| 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 | + Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void Should_Map_Enumerable_Collection_SingleLine_Test() |
| 62 | + { |
| 63 | + //Create Model |
| 64 | + Student student = new Student |
| 65 | + { |
| 66 | + StudentName = "John Doe", |
| 67 | + Invoices = new List<Invoice> |
| 68 | + { |
| 69 | + new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) }, |
| 70 | + new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) } |
| 71 | + } |
| 72 | + }; |
| 73 | + //Template |
| 74 | + var template = new ObjectSemanticsTemplate |
| 75 | + { |
| 76 | + FileContents = @"{{ #foreach(invoices) }} [{{RefNo}}] {{ #endforeach }}" |
| 77 | + }; |
| 78 | + string generatedTemplate = TemplateMapper.Map(student, template); |
| 79 | + string expectedResult = " [INV_002] [INV_001] "; |
| 80 | + Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void Should_Map_Multiple_Same_Property_Enumerable_Collection_On_Same_Template() |
| 86 | + { |
| 87 | + //Create Model |
| 88 | + Student student = new Student |
| 89 | + { |
| 90 | + StudentName = "John Doe", |
| 91 | + Invoices = new List<Invoice> |
| 92 | + { |
| 93 | + new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) }, |
| 94 | + new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) } |
| 95 | + } |
| 96 | + }; |
| 97 | + //Template |
| 98 | + var template = new ObjectSemanticsTemplate |
| 99 | + { |
| 100 | + FileContents = @" |
| 101 | +{{ StudentName }} Invoices |
| 102 | +LOOP #1 |
| 103 | +{{ #foreach(invoices) }} |
| 104 | + <h5>{{ Id }} On Loop #1</h5> |
| 105 | +{{ #endforeach }} |
| 106 | +LOOP #2 |
| 107 | +{{ #foreach(invoices) }} |
| 108 | + <h5>{{ Id }} On Loop #2</h5> |
| 109 | +{{ #endforeach }}" |
| 110 | + }; |
| 111 | + string generatedTemplate = TemplateMapper.Map(student, template); |
| 112 | + string expectedResult = @" |
| 113 | +John Doe Invoices |
| 114 | +LOOP #1 |
| 115 | +
|
| 116 | + <h5>2 On Loop #1</h5> |
| 117 | +
|
| 118 | + <h5>1 On Loop #1</h5> |
| 119 | +LOOP #2 |
| 120 | +
|
| 121 | + <h5>2 On Loop #2</h5> |
| 122 | +
|
| 123 | + <h5>1 On Loop #2</h5>"; |
| 124 | + Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void Should_Map_Multiple_Different_Property_Enumerable_Collection_On_Same_Template() |
| 130 | + { |
| 131 | + //Create Model |
| 132 | + Student student = new Student |
| 133 | + { |
| 134 | + StudentName = "John Doe", |
| 135 | + Invoices = new List<Invoice> |
| 136 | + { |
| 137 | + new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) }, |
| 138 | + new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) } |
| 139 | + }, |
| 140 | + StudentClockInDetails = new List<StudentClockInDetail> |
| 141 | + { |
| 142 | + new StudentClockInDetail{ LastClockedInDate = new DateTime(2024, 04, 01), LastClockedInPoints = 10 }, |
| 143 | + new StudentClockInDetail{ LastClockedInDate = new DateTime(2024, 04, 02), LastClockedInPoints = 30 } |
| 144 | + } |
| 145 | + }; |
| 146 | + //Template |
| 147 | + var template = new ObjectSemanticsTemplate |
| 148 | + { |
| 149 | + FileContents = @" |
| 150 | +{{ StudentName }} Invoices |
| 151 | +LOOP #1 |
| 152 | +{{ #foreach(invoices) }} |
| 153 | + <h5>{{ Id }} On Loop #1</h5> |
| 154 | +{{ #endforeach }} |
| 155 | +LOOP #2 |
| 156 | +{{ #foreach(studentClockInDetails) }} |
| 157 | + <h5>Got {{ LastClockedInPoints }} for {{ LastClockedInDate:yyyy-MM-dd }}</h5> |
| 158 | +{{ #endforeach }}" |
| 159 | + }; |
| 160 | + string generatedTemplate = TemplateMapper.Map(student, template); |
| 161 | + string expectedResult = @" |
| 162 | +John Doe Invoices |
| 163 | +LOOP #1 |
| 164 | +
|
| 165 | + <h5>2 On Loop #1</h5> |
| 166 | +
|
| 167 | + <h5>1 On Loop #1</h5> |
| 168 | +LOOP #2 |
| 169 | +
|
| 170 | + <h5>Got 10 for 2024-04-01</h5> |
| 171 | +
|
| 172 | + <h5>Got 30 for 2024-04-02</h5>"; |
| 173 | + |
| 174 | + Assert.Equal(expectedResult, generatedTemplate, false, true, true); |
| 175 | + } |
| 176 | + |
| 177 | + } |
| 178 | +} |
0 commit comments