Skip to content

Commit 736eee6

Browse files
authored
Merge pull request #7 from swagfin/feature/if-confition-statement
Feature/if confition statement
2 parents ddd0c9b + 0ac0c1b commit 736eee6

File tree

10 files changed

+475
-39
lines changed

10 files changed

+475
-39
lines changed

ObjectSemantics.NET.Tests/ObjectSemanticsTests.cs

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace ObjectSemantics.NET.Tests
88
public class ObjectSemanticsTests
99
{
1010

11+
#region Basic Functions Tests
12+
13+
1114
[Fact]
1215
public void Should_Map_Object_To_Template_From_TemplateObject()
1316
{
@@ -136,6 +139,10 @@ public void Should_Ignore_Whitespaces_Inside_CurlyBrackets()
136139
Assert.Equal(expectedString, generatedTemplate, false, true, true);
137140
}
138141

142+
#endregion
143+
144+
145+
#region Text and Data Formatting Tests
139146
[Fact]
140147
public void Should_Accept_String_To_String_Formatting()
141148
{
@@ -191,5 +198,298 @@ public void Should_Accept_DateTime_To_String_Formatting()
191198
Assert.Equal(expectedString, generatedTemplate, false, true, true);
192199
}
193200

201+
202+
#endregion
203+
204+
205+
#region IF Conditional Tests
206+
207+
[Fact]
208+
public void Should_Act_On_IfCondition_SingleLine()
209+
{
210+
//Create Model
211+
Student student = new Student
212+
{
213+
StudentName = "John Doe",
214+
Invoices = new List<Invoice>
215+
{
216+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
217+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
218+
}
219+
};
220+
//Template
221+
var template = new ObjectSemanticsTemplate
222+
{
223+
FileContents = "IsEnabled: {{ if-start:invoices(!=null) }}YES{{ if-end:invoices }}"
224+
};
225+
string generatedTemplate = TemplateMapper.Map(student, template);
226+
string expectedResult = "IsEnabled: YES";
227+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
228+
}
229+
230+
231+
[Fact]
232+
public void Should_Act_On_IfCondition_SingleLine_With_Attribute()
233+
{
234+
//Create Model
235+
Student student = new Student
236+
{
237+
StudentName = "John Doe",
238+
Invoices = new List<Invoice>
239+
{
240+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
241+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
242+
}
243+
};
244+
//Template
245+
var template = new ObjectSemanticsTemplate
246+
{
247+
FileContents = "InvoicedPerson: {{ if-start:invoices(!=null) }}{{StudentName}}{{ if-end:invoices }}"
248+
};
249+
string generatedTemplate = TemplateMapper.Map(student, template);
250+
string expectedResult = "InvoicedPerson: John Doe";
251+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
252+
}
253+
254+
255+
[Fact]
256+
public void Should_Act_On_IfCondition_MultiLine()
257+
{
258+
//Create Model
259+
Student student = new Student
260+
{
261+
StudentName = "John Doe",
262+
Invoices = new List<Invoice>
263+
{
264+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
265+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
266+
}
267+
};
268+
//Template
269+
var template = new ObjectSemanticsTemplate
270+
{
271+
FileContents = @"status
272+
{{ if-start:invoices(!=null) }}
273+
<h4>condition--passed</h4>
274+
{{ if-end:invoices }}"
275+
};
276+
string generatedTemplate = TemplateMapper.Map(student, template);
277+
string expectedResult = "status\r\n\r\n<h4>condition--passed</h4>\r\n";
278+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
279+
}
280+
281+
282+
[Fact]
283+
public void Should_Act_On_IfCondition_MultiLine_With_Attribute()
284+
{
285+
//Create Model
286+
Student student = new Student
287+
{
288+
StudentName = "John Doe",
289+
Invoices = new List<Invoice>
290+
{
291+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
292+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
293+
}
294+
};
295+
//Template
296+
var template = new ObjectSemanticsTemplate
297+
{
298+
FileContents = @"status
299+
{{ if-start:invoices(!=null) }}
300+
<h4>Hi, I have invoices for {{ StudentName }} </h4>
301+
{{ if-end:invoices }}"
302+
};
303+
string generatedTemplate = TemplateMapper.Map(student, template);
304+
string expectedResult = "status\r\n\r\n<h4>Hi, I have invoices for John Doe </h4>\r\n";
305+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
306+
}
307+
308+
309+
[Fact]
310+
public void Should_Act_On_IfCondition_Having_Loop_As_Child()
311+
{
312+
//Create Model
313+
Student student = new Student
314+
{
315+
StudentName = "John Doe",
316+
Invoices = new List<Invoice>
317+
{
318+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
319+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
320+
}
321+
};
322+
//Template
323+
var template = new ObjectSemanticsTemplate
324+
{
325+
FileContents = @"
326+
{{ if-start:invoices(!=null) }}
327+
{{ StudentName }} Invoices
328+
{{ for-each-start:invoices }}
329+
<tr>
330+
<td>{{ Id }}</td>
331+
<td>{{ RefNo }}</td>
332+
</tr>
333+
{{ for-each-end:invoices }}
334+
{{ if-end:invoices }}"
335+
};
336+
string generatedTemplate = TemplateMapper.Map(student, template);
337+
string expectedResult = "\r\n\r\nJohn Doe Invoices" +
338+
"\r\n<tr>" +
339+
"\r\n <td>2</td>" +
340+
"\r\n <td>INV_002</td>" +
341+
"\r\n</tr>" +
342+
"\r\n<tr>" +
343+
"\r\n <td>1</td>" +
344+
"\r\n <td>INV_001</td>" +
345+
"\r\n</tr>\r\n"; ;
346+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
347+
}
348+
349+
350+
//#Match =, !=, >, >=, <, and <=.
351+
352+
[Theory]
353+
[InlineData("=", 5000)]
354+
[InlineData("!=", 0)]
355+
[InlineData(">", 2000)]
356+
[InlineData("<=", 5001)]
357+
[InlineData("<", 5001)]
358+
public void Should_Act_On_IfCondition_Equality_Checks(string condition, double amount)
359+
{
360+
//Create Model
361+
Student student = new Student { Balance = 5000 };
362+
//Template
363+
var template = new ObjectSemanticsTemplate
364+
{
365+
FileContents = string.Format("{2} if-start:balance({0}{1}) {3} {0} passed {2} if-end:balance {3}", condition, amount, "{{", "}}")
366+
};
367+
368+
string expectedResult = string.Format(" {0} passed ", condition);
369+
string generatedTemplate = TemplateMapper.Map(student, template);
370+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
371+
}
372+
373+
374+
[Fact]
375+
public void Should_Act_On_IfCondition_Simple_Property_String_Equality()
376+
{
377+
//Create Model
378+
Student student = new Student { StudentName = "John Doe" };
379+
//Template
380+
var template = new ObjectSemanticsTemplate
381+
{
382+
FileContents = "{{ if-start:studentName(=John Doe) }} YES, i am John Doe {{ if-end:studentName }}"
383+
};
384+
string generatedTemplate = TemplateMapper.Map(student, template);
385+
string expectedResult = " YES, i am John Doe ";
386+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
387+
}
388+
389+
390+
[Fact]
391+
public void Should_Act_On_IfCondition_IEnumerable_Tests_Equall()
392+
{
393+
//Create Model
394+
Student student = new Student
395+
{
396+
StudentName = "John Doe",
397+
Invoices = new List<Invoice>
398+
{
399+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
400+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
401+
}
402+
};
403+
//Template
404+
var template = new ObjectSemanticsTemplate
405+
{
406+
FileContents = "{{ if-start:invoices(=2) }} 2 records {{ if-end:invoices }}"
407+
};
408+
string generatedTemplate = TemplateMapper.Map(student, template);
409+
string expectedResult = " 2 records ";
410+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
411+
}
412+
413+
414+
[Fact]
415+
public void Should_Act_On_IfCondition_IEnumerable_Tests_NotEqualNull()
416+
{
417+
//Create Model
418+
Student student = new Student
419+
{
420+
StudentName = "John Doe",
421+
Invoices = new List<Invoice>
422+
{
423+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
424+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
425+
}
426+
};
427+
//Template
428+
var template = new ObjectSemanticsTemplate
429+
{
430+
FileContents = "{{ if-start:invoices(!=null) }} is not NULL {{ if-end:invoices }}"
431+
};
432+
string generatedTemplate = TemplateMapper.Map(student, template);
433+
string expectedResult = " is not NULL ";
434+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
435+
}
436+
437+
[Fact]
438+
public void Should_Act_On_IfCondition_IEnumerable_Tests_NULL_Object_Behaviour()
439+
{
440+
//Create Model
441+
Student student = new Student
442+
{
443+
StudentName = "John Doe",
444+
Invoices = null
445+
};
446+
//Template
447+
var template = new ObjectSemanticsTemplate
448+
{
449+
FileContents = "{{ if-start:invoices(=null) }} is NULL {{ if-end:invoices }}"
450+
};
451+
string generatedTemplate = TemplateMapper.Map(student, template);
452+
string expectedResult = " is NULL ";
453+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
454+
}
455+
[Fact]
456+
public void Should_Act_On_IfCondition_IEnumerable_Tests_Count_NULL_Object_Behaviour()
457+
{
458+
//Create Model
459+
Student student = new Student
460+
{
461+
StudentName = "John Doe",
462+
Invoices = null
463+
};
464+
//Template
465+
var template = new ObjectSemanticsTemplate
466+
{
467+
FileContents = "{{ if-start:invoices(=0) }} no records {{ if-end:invoices }}"
468+
};
469+
string generatedTemplate = TemplateMapper.Map(student, template);
470+
string expectedResult = " no records ";
471+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
472+
}
473+
474+
[Fact]
475+
public void Should_Act_On_IfCondition_IEnumerable_Tests_Count_Object_Behaviour()
476+
{
477+
//Create Model
478+
Student student = new Student
479+
{
480+
StudentName = "John Doe",
481+
Invoices = new List<Invoice>()
482+
};
483+
//Template
484+
var template = new ObjectSemanticsTemplate
485+
{
486+
FileContents = "{{ if-start:invoices(=null) }} no records {{ if-end:invoices }}"
487+
};
488+
string generatedTemplate = TemplateMapper.Map(student, template);
489+
string expectedResult = " no records ";
490+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
491+
}
492+
493+
#endregion
194494
}
195495
}

0 commit comments

Comments
 (0)