|
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 | | -//} |
| 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(40, "Adult")] |
| 11 | + [InlineData(18, "Adult")] |
| 12 | + [InlineData(0, "Minor")] |
| 13 | + [InlineData(-7, "Minor")] |
| 14 | + public void Should_Render_If_GreaterOrEqual_Condition_Block(int age, string expected) |
| 15 | + { |
| 16 | + Person person = new Person |
| 17 | + { |
| 18 | + Age = age |
| 19 | + }; |
| 20 | + |
| 21 | + string template = @"{{ #if(Age >= 18) }}Adult{{ #else }}Minor{{ #endif }}"; |
| 22 | + |
| 23 | + string result = person.Map(template); |
| 24 | + Assert.Equal(expected, result); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + [Theory] |
| 29 | + [InlineData(40, "Adult 40")] |
| 30 | + [InlineData(18, "Adult 18")] |
| 31 | + [InlineData(0, "Minor 0")] |
| 32 | + [InlineData(-7, "Minor -7")] |
| 33 | + public void Should_Resolve_Variables_Inside_If_Condition(int age, string expected) |
| 34 | + { |
| 35 | + Person person = new Person |
| 36 | + { |
| 37 | + Age = age |
| 38 | + }; |
| 39 | + |
| 40 | + string template = @"{{ #if( Age > 17 ) }}Adult {{Age}}{{ #else }}Minor {{Age}}{{ #endif }}"; |
| 41 | + |
| 42 | + var result = person.Map(template); |
| 43 | + Assert.Equal(expected, result); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + [Theory] |
| 48 | + [InlineData(40, "")] |
| 49 | + [InlineData(18, "")] |
| 50 | + [InlineData(0, "NoDrinkingBeer")] |
| 51 | + [InlineData(-7, "NoDrinkingBeer")] |
| 52 | + public void Should_Render_If_Block_Without_Else_When_True(int age, string expected) |
| 53 | + { |
| 54 | + Person person = new Person |
| 55 | + { |
| 56 | + Age = age |
| 57 | + }; |
| 58 | + |
| 59 | + string template = @"{{ #if(Age < 18) }}NoDrinkingBeer{{ #endif }}"; |
| 60 | + |
| 61 | + string result = person.Map(template); |
| 62 | + Assert.Equal(expected, result); |
| 63 | + } |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public void Should_Evaluate_Enumerable_Count_Inside_If_Block() |
| 67 | + { |
| 68 | + |
| 69 | + Person person = new Person |
| 70 | + { |
| 71 | + MyCars = new List<Car> |
| 72 | + { |
| 73 | + new Car { Make = "BMW" }, |
| 74 | + new Car { Make = "Rolls-Royce" } |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + string template = @"{{ #if(MyCars == 2) }}Has 2 Cars{{ #endif }}"; |
| 79 | + |
| 80 | + string result = person.Map(template); |
| 81 | + Assert.Equal("Has 2 Cars", result); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void Should_Evaluate_Empty_Enumerable_As_Zero() |
| 86 | + { |
| 87 | + Person person = new Person |
| 88 | + { |
| 89 | + MyCars = null |
| 90 | + }; |
| 91 | + |
| 92 | + string template = @"{{ #if(MyCars == 0) }}Zero Cars{{ #endif }}"; |
| 93 | + |
| 94 | + string result = person.Map(template); |
| 95 | + Assert.Equal("Zero Cars", result); |
| 96 | + } |
| 97 | + |
| 98 | + [Theory] |
| 99 | + [InlineData("John DoeX2", "Yes")] |
| 100 | + [InlineData("John Doe", "No")] |
| 101 | + [InlineData("Doe", "No")] |
| 102 | + [InlineData("John ", "No")] |
| 103 | + public void Should_Evaluate_String_Conditions_If_Blocks(string name, string expected) |
| 104 | + { |
| 105 | + Person person = new Person |
| 106 | + { |
| 107 | + Name = name |
| 108 | + }; |
| 109 | + |
| 110 | + string template = @"{{ #if(Name == John DoeX2) }}Yes{{ #else }}No{{ #endif }}"; |
| 111 | + |
| 112 | + string result = person.Map(template); |
| 113 | + Assert.Equal(expected, result); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments