Skip to content

Commit acb926c

Browse files
committed
chore: if condition block tests
1 parent 37ebab7 commit acb926c

8 files changed

Lines changed: 127 additions & 171 deletions

File tree

ObjectSemantics.NET.Tests/CognitiveMapTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void Additional_Headers_Should_Also_Be_Mapped()
4444
//additional params (outside the class)
4545
Dictionary<string, object> additionalParams = new Dictionary<string, object>
4646
{
47-
{ "Occupation","Developer"},
47+
{ "Occupation", "Developer"},
4848
{ "DateOfBirth", new DateTime(1995, 01, 01) }
4949
};
5050

Lines changed: 116 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,116 @@
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+
}

ObjectSemantics.NET.Tests/MoqModels/Invoice.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
namespace ObjectSemantics.NET.Tests.MoqModels
1+
using System.Collections.Generic;
2+
3+
namespace ObjectSemantics.NET.Tests.MoqModels
24
{
35
public class Person
46
{
57
public string Name { get; set; }
68
public int Age { get; set; }
9+
public List<Car> MyCars { get; set; } = new List<Car>();
710
}
811
}

ObjectSemantics.NET.Tests/MoqModels/Student.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

ObjectSemantics.NET/Engine/EngineAlgorithim.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ internal static class EngineAlgorithim
7878

7979
foreach (ReplaceCode objLoopCode in objLoop.ReplaceObjCodes)
8080
{
81-
if (objLoopCode.TargetPropertyName == ".")
81+
string propName = objLoopCode.GetTargetPropertyName();
82+
if (propName == ".")
8283
{
8384
ExtractedObjProperty tempProp = new ExtractedObjProperty
8485
{
@@ -90,7 +91,7 @@ internal static class EngineAlgorithim
9091
}
9192
else
9293
{
93-
if (rowMap.TryGetValue(objLoopCode.TargetPropertyName, out ExtractedObjProperty p))
94+
if (rowMap.TryGetValue(propName, out ExtractedObjProperty p))
9495
activeRow.Replace(objLoopCode.ReplaceRef, p.GetPropertyDisplayString(objLoopCode.GetFormattingCommand(), options));
9596
else
9697
activeRow.Replace(objLoopCode.ReplaceRef, objLoopCode.ReplaceCommand);
@@ -106,7 +107,7 @@ internal static class EngineAlgorithim
106107
// ---- Direct Replacements ----
107108
foreach (ReplaceCode replaceCode in template.ReplaceCodes)
108109
{
109-
if (propMap.TryGetValue(replaceCode.TargetPropertyName, out ExtractedObjProperty property))
110+
if (propMap.TryGetValue(replaceCode.GetTargetPropertyName(), out ExtractedObjProperty property))
110111
result.Replace(replaceCode.ReplaceRef,
111112
property.GetPropertyDisplayString(replaceCode.GetFormattingCommand(), options));
112113
else

ObjectSemantics.NET/Engine/Models/ReplaceCode.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,5 @@ internal class ReplaceCode
44
{
55
public string ReplaceRef { get; set; }
66
public string ReplaceCommand { get; set; }
7-
public string TargetPropertyName
8-
{
9-
get
10-
{
11-
if (string.IsNullOrEmpty(ReplaceCommand))
12-
return string.Empty;
13-
int colonIndex = ReplaceCommand.IndexOf(':');
14-
return colonIndex > 0 ? ReplaceCommand.Substring(0, colonIndex).Trim() : ReplaceCommand.Trim();
15-
}
16-
}
177
}
188
}

ObjectSemantics.NET/TemplateMapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class TemplateMapper
1616
/// <param name="additionalKeyValues"></param>
1717
/// <param name="options"></param>
1818
/// <returns></returns>
19-
public static string Map<T>(this string template, T record, Dictionary<string, object> additionalKeyValues = null, TemplateMapperOptions options = null) where T : new()
19+
public static string Map<T>(this string template, T record, Dictionary<string, object> additionalKeyValues = null, TemplateMapperOptions options = null) where T : class, new()
2020
{
2121
return Map(record, template, additionalKeyValues, options);
2222
}
@@ -31,7 +31,7 @@ public static class TemplateMapper
3131
/// <param name="options"></param>
3232
/// <returns></returns>
3333
/// <exception cref="Exception"></exception>
34-
public static string Map<T>(this T record, string template, Dictionary<string, object> additionalKeyValues = null, TemplateMapperOptions options = null) where T : new()
34+
public static string Map<T>(this T record, string template, Dictionary<string, object> additionalKeyValues = null, TemplateMapperOptions options = null) where T : class, new()
3535
{
3636
if (record == null) return string.Empty;
3737
if (template == null) throw new Exception("Template Object can't be NULL");

0 commit comments

Comments
 (0)