|
| 1 | +using ObjectSemantics.NET.Tests.MoqModels; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace ObjectSemantics.NET.Tests |
| 6 | +{ |
| 7 | + public class IfConditionTests |
| 8 | + { |
| 9 | + [Theory] |
| 10 | + [InlineData(1, "Valid")] |
| 11 | + [InlineData(0, "Invalid")] |
| 12 | + public void Should_Render_If_Block_When_Condition_Is_True(int id, string expected) |
| 13 | + { |
| 14 | + var model = new Invoice { Id = id }; |
| 15 | + |
| 16 | + var template = new ObjectSemanticsTemplate |
| 17 | + { |
| 18 | + FileContents = @"{{ #if(Id == 1) }}Valid{{ #else }}Invalid{{ #endif }}" |
| 19 | + }; |
| 20 | + |
| 21 | + string result = template.Map(model); |
| 22 | + Assert.Equal(expected, result); |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + [Theory] |
| 27 | + [InlineData(18, "Minor")] |
| 28 | + [InlineData(21, "Adult")] |
| 29 | + [InlineData(5, "Minor")] |
| 30 | + public void Should_Handle_LessThan_Or_Equal(int age, string expected) |
| 31 | + { |
| 32 | + var model = new Student { Age = age }; |
| 33 | + |
| 34 | + var template = new ObjectSemanticsTemplate |
| 35 | + { |
| 36 | + FileContents = @"{{ #if(Age <= 18) }}Minor{{ #else }}Adult{{ #endif }}" |
| 37 | + }; |
| 38 | + |
| 39 | + var result = template.Map(model); |
| 40 | + Assert.Equal(expected, result); |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + [Theory] |
| 45 | + [InlineData(1, "1")] |
| 46 | + [InlineData(0, "Error")] |
| 47 | + [InlineData(-1, "Error")] |
| 48 | + [InlineData(5, "5")] |
| 49 | + [InlineData(+2, "2")] |
| 50 | + public void Should_Handle_Whitespace_And_Case_Insensitive_Condition(int id, string expected) |
| 51 | + { |
| 52 | + var model = new Invoice { Id = id }; |
| 53 | + |
| 54 | + var template = new ObjectSemanticsTemplate |
| 55 | + { |
| 56 | + FileContents = @"{{ #if( id > 0 ) }}{{id}}{{ #else }}Error{{ #endif }}" |
| 57 | + }; |
| 58 | + |
| 59 | + var result = template.Map(model); |
| 60 | + Assert.Equal(expected, result); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void Should_Render_If_Block_Without_Else_When_True() |
| 66 | + { |
| 67 | + var model = new Student { IsActive = true }; |
| 68 | + |
| 69 | + var template = new ObjectSemanticsTemplate |
| 70 | + { |
| 71 | + FileContents = @"Student: John {{ #if(IsActive == true) }}[Is Active]{{ #endif }}" |
| 72 | + }; |
| 73 | + |
| 74 | + var result = template.Map(model); |
| 75 | + Assert.Equal("Student: John [Is Active]", result); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void Should_Evaluate_If_Enumerable_Count() |
| 80 | + { |
| 81 | + var model = new Student |
| 82 | + { |
| 83 | + Invoices = new List<Invoice> |
| 84 | + { |
| 85 | + new Invoice{ Id = 2, RefNo = "INV_002" }, |
| 86 | + new Invoice{ Id = 1, RefNo = "INV_001" } |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + var template = new ObjectSemanticsTemplate |
| 91 | + { |
| 92 | + FileContents = @"{{ #if(Invoices == 2) }}Matched{{ #else }}Not Matched{{ #endif }}" |
| 93 | + }; |
| 94 | + |
| 95 | + var result = template.Map(model); |
| 96 | + Assert.Equal("Matched", result); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void Should_Evaluate_Empty_Enumerable_As_Zero() |
| 101 | + { |
| 102 | + var model = new Student |
| 103 | + { |
| 104 | + Invoices = new List<Invoice>() |
| 105 | + }; |
| 106 | + |
| 107 | + var template = new ObjectSemanticsTemplate |
| 108 | + { |
| 109 | + FileContents = @"{{ #if(Invoices == 0) }}No invoices available{{ #else }}Invoices Found{{ #endif }}" |
| 110 | + }; |
| 111 | + |
| 112 | + var result = template.Map(model); |
| 113 | + Assert.Equal("No invoices available", result); |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | +} |
0 commit comments