Skip to content

Commit 800d7cc

Browse files
committed
hotfix: string comparison checks
1 parent cf33ce1 commit 800d7cc

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

ObjectSemantics.NET.Tests/ObjectSemanticsTests.cs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -411,20 +411,7 @@ public void Should_Act_On_IfCondition_Equality_Checks(string condition, double a
411411
}
412412

413413

414-
[Fact]
415-
public void Should_Act_On_IfCondition_Simple_Property_String_Equality()
416-
{
417-
//Create Model
418-
Student student = new Student { StudentName = "John Doe" };
419-
//Template
420-
var template = new ObjectSemanticsTemplate
421-
{
422-
FileContents = "{{ if-start:studentName(=John Doe) }} YES, i am John Doe {{ if-end:studentName }}"
423-
};
424-
string generatedTemplate = TemplateMapper.Map(student, template);
425-
string expectedResult = " YES, i am John Doe ";
426-
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
427-
}
414+
428415

429416

430417
[Fact]
@@ -571,6 +558,44 @@ public void Should_Act_On_IfCondition_Having_ElseIf_MultiLine(double amount)
571558
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
572559
}
573560

561+
[Theory]
562+
[InlineData(null)]
563+
[InlineData("John Doe")]
564+
[InlineData("")]
565+
public void Should_Act_On_IfCondition_Having_ElseIf_MultiLine_StringTest(string studentName)
566+
{
567+
//Create Model
568+
Student student = new Student { StudentName = studentName };
569+
//Template
570+
var template = new ObjectSemanticsTemplate
571+
{
572+
FileContents = @"
573+
{{ if-start:StudentName(=NULL) }}
574+
--is null--
575+
{{ else-if }}
576+
--not-null--
577+
{{ if-end:Balance }}"
578+
};
579+
string generatedTemplate = TemplateMapper.Map(student, template);
580+
string expectedResult = (string.IsNullOrEmpty(studentName)) ? "\r\n\r\n--is null--\r\n" : "\r\n\r\n--not-null--\r\n";
581+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
582+
}
583+
584+
585+
[Fact]
586+
public void Should_Act_On_IfCondition_Simple_Property_String_Equality()
587+
{
588+
//Create Model
589+
Student student = new Student { StudentName = "John Doe" };
590+
//Template
591+
var template = new ObjectSemanticsTemplate
592+
{
593+
FileContents = "{{ if-start:studentName(=John Doe) }} YES, i am John Doe {{ if-end:studentName }}"
594+
};
595+
string generatedTemplate = TemplateMapper.Map(student, template);
596+
string expectedResult = " YES, i am John Doe ";
597+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
598+
}
574599

575600

576601
[Theory]

ObjectSemantics.NET/Extensions/ExtractedObjPropertyExtensions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ public static class ExtractedObjPropertyExtensions
77
{
88
private static T GetConvertibleValue<T>(string value) where T : IConvertible
99
{
10-
return (value == "null" || string.IsNullOrEmpty(value)) ? default : (T)Convert.ChangeType(value, typeof(T));
10+
return (string.IsNullOrEmpty(value) || value?.ToLower()?.Trim() == "null") ? default : (T)Convert.ChangeType(value, typeof(T));
1111
}
1212
public static bool IsPropertyValueConditionPassed(this ExtractedObjProperty property, string valueComparer, string criteria)
1313
{
1414
try
1515
{
1616
if (property == null) return false;
1717
else if (property.Type == typeof(string))
18-
return string.Compare(property.OriginalValue.ToString()?.Trim(), GetConvertibleValue<string>(valueComparer)?.Trim(), true) == 0;
18+
{
19+
string v1 = property.OriginalValue?.ToString()?.Trim().ToLower() ?? string.Empty;
20+
string v2 = GetConvertibleValue<string>(valueComparer)?.Trim().ToLower() ?? string.Empty;
21+
return string.Compare(v1, v2, true) == 0;
22+
}
1923
else if (property.Type == typeof(int) || property.Type == typeof(double) || property.Type == typeof(long) || property.Type == typeof(float) || property.Type == typeof(decimal))
2024
{
21-
double v1 = Convert.ToDouble(property.OriginalValue);
25+
double v1 = Convert.ToDouble(property.OriginalValue ?? "0");
2226
double v2 = Convert.ToDouble(GetConvertibleValue<double>(valueComparer));
2327
switch (criteria)
2428
{
@@ -63,7 +67,6 @@ public static bool IsPropertyValueConditionPassed(this ExtractedObjProperty prop
6367
}
6468
else
6569
return false;
66-
6770
}
6871
catch { return false; }
6972
}

ObjectSemantics.NET/ObjectSemantics.NET.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
<RepositoryType>Github</RepositoryType>
1515
<PackageReleaseNotes>v.5.1.3
1616
Added support for if condition statements with ElseIf</PackageReleaseNotes>
17-
<AssemblyVersion>5.1.3.1</AssemblyVersion>
18-
<FileVersion>5.1.3.1</FileVersion>
19-
<Version>5.1.3</Version>
17+
<AssemblyVersion>5.1.4.1</AssemblyVersion>
18+
<FileVersion>5.1.4.1</FileVersion>
19+
<Version>5.1.4</Version>
2020
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
2121
<ApplicationIcon></ApplicationIcon>
2222
<PackageReadmeFile>README.md</PackageReadmeFile>

0 commit comments

Comments
 (0)