Skip to content

Commit 91eda7b

Browse files
authored
Merge pull request #17 from swagfin/feature/bool-if-condition-and-if-tests
chore: support for bool condition inside if block
2 parents 14aa3b8 + cc8680d commit 91eda7b

File tree

7 files changed

+189
-5
lines changed

7 files changed

+189
-5
lines changed

.github/workflows/release-nuget-pkg.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
uses: actions/checkout@v4
1414
- name: Setup .NET SDK
1515
uses: actions/setup-dotnet@v1
16+
with:
17+
dotnet-version: '8.0.x'
1618
- name: Build
1719
run: dotnet build ObjectSemantics.NET/ObjectSemantics.NET.csproj -c Release
1820
- name: Test
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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(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+
}

ObjectSemantics.NET.Tests/MoqModels/Student.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ internal class Student
88
public Guid Id { get; set; } = Guid.NewGuid();
99
public string StudentName { get; set; }
1010
public double Balance { get; set; }
11+
public int Age { get; set; }
1112
public DateTime RegDate { get; set; } = DateTime.Now;
1213
public List<Invoice> Invoices { get; set; } = new List<Invoice>();
1314
public string[] ArrayOfString { get; set; } = new string[] { };
1415
public double[] ArrayOfDouble { get; set; } = new double[] { };
16+
public bool IsActive { get; set; }
1517
public List<StudentClockInDetail> StudentClockInDetails { get; set; } = new List<StudentClockInDetail>();
1618
}
1719
class StudentClockInDetail

ObjectSemantics.NET.Tests/StringFormattingTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,31 @@ public void Should_Accept_String_To_UpperCase_or_LowerCase_Formatting()
2424
Assert.Equal(expectedString, generatedTemplate, false, true, true);
2525
}
2626

27+
[Theory]
28+
[InlineData("john doe", "John Doe")]
29+
[InlineData("JANE DOE", "Jane Doe")]
30+
[InlineData("aLiCe joHNsOn", "Alice Johnson")]
31+
[InlineData("", "")]
32+
[InlineData(null, "")]
33+
public void Should_Convert_StudentName_To_TitleCase(string studentName, string expectedTitleCase)
34+
{
35+
// Create Model
36+
Student student = new Student
37+
{
38+
StudentName = studentName
39+
};
40+
41+
// Template
42+
var template = new ObjectSemanticsTemplate
43+
{
44+
FileContents = @"{{ StudentName:titlecase }}"
45+
};
46+
47+
string generatedTemplate = template.Map(student);
48+
49+
Assert.Equal(expectedTitleCase, generatedTemplate, ignoreCase: false, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true);
50+
}
51+
2752

2853
[Fact]
2954
public void Should_Accept_Number_To_String_Formatting()
@@ -116,5 +141,29 @@ public void Should_Accept_String_From_BASE64_Formatting()
116141
string expectedString = "Original String: Sm9obiBET0U= | From BASE64 String: John DOE";
117142
Assert.Equal(expectedString, generatedTemplate, false, true, true);
118143
}
144+
145+
[Theory]
146+
[InlineData("Alice Johnson", "13")]
147+
[InlineData("Bob", "3")]
148+
[InlineData("", "0")]
149+
[InlineData(null, "")]
150+
public void Should_Return_Correct_Length_Of_StudentName(string studentName, string expectedLength)
151+
{
152+
// Create Model
153+
Student student = new Student
154+
{
155+
StudentName = studentName
156+
};
157+
158+
// Template
159+
var template = new ObjectSemanticsTemplate
160+
{
161+
FileContents = @"{{ StudentName:length }}"
162+
};
163+
164+
string generatedTemplate = template.Map(student);
165+
166+
Assert.Equal(expectedLength, generatedTemplate, ignoreCase: false, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true);
167+
}
119168
}
120169
}

ObjectSemantics.NET/Algorithim/GavinsAlgorithim.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,4 @@ private static List<ExtractedObjProperty> GetObjPropertiesFromUnknown(object val
238238
}
239239
return list;
240240
}
241-
242-
243241
}

ObjectSemantics.NET/Extensions/ExtractedObjPropertyExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Security;
56

@@ -43,6 +44,10 @@ private static string GetAppliedPropertyFormatting(this ExtractedObjProperty p,
4344
return p.StringFormatted.ToBase64String();
4445
else if (customFormattingValue.ToLower().Equals("frombase64"))
4546
return p.StringFormatted.FromBase64String();
47+
else if (customFormattingValue.ToLower().Equals("length"))
48+
return p.StringFormatted?.Length.ToString();
49+
else if (customFormattingValue.ToLower().Equals("titlecase"))
50+
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(p.StringFormatted?.ToLower() ?? string.Empty);
4651
else
4752
return p.StringFormatted;
4853
}
@@ -98,6 +103,17 @@ public static bool IsPropertyValueConditionPassed(this ExtractedObjProperty prop
98103
default: return false;
99104
}
100105
}
106+
else if (property.Type == typeof(bool))
107+
{
108+
bool v1 = Convert.ToBoolean(property.OriginalValue);
109+
bool v2 = Convert.ToBoolean(GetConvertibleValue<bool>(valueComparer));
110+
switch (criteria)
111+
{
112+
case "==": return v1 == v2;
113+
case "!=": return v1 != v2;
114+
default: return false;
115+
}
116+
}
101117
else if (property.IsEnumerableObject)
102118
{
103119
int v1 = (property.OriginalValue == null) ? 0 : ((IEnumerable<object>)property.OriginalValue).Count();

ObjectSemantics.NET/ObjectSemantics.NET.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
ToBase64
1616
FromBase64
1717
. Added template extension method to allow mapping directly from Template</PackageReleaseNotes>
18-
<AssemblyVersion>6.0.4</AssemblyVersion>
19-
<FileVersion>6.0.4</FileVersion>
20-
<Version>6.0.4</Version>
18+
<AssemblyVersion>6.0.5</AssemblyVersion>
19+
<FileVersion>6.0.5</FileVersion>
20+
<Version>6.0.5</Version>
2121
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
2222
<ApplicationIcon></ApplicationIcon>
2323
<PackageReadmeFile>README.md</PackageReadmeFile>

0 commit comments

Comments
 (0)