Skip to content

Commit 4b99cdd

Browse files
authored
Merge pull request #14 from swagfin/refactoring/minor-refactoring
hotfix: Fixed Nullable Types loosing string formatting
2 parents 0f62731 + c9e5f32 commit 4b99cdd

File tree

5 files changed

+76
-17
lines changed

5 files changed

+76
-17
lines changed

ObjectSemantics.NET.Tests/MoqModels/Student.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ internal class Student
1111
public DateTime RegDate { get; set; } = DateTime.Now;
1212
public List<Invoice> Invoices { get; set; } = new List<Invoice>();
1313
}
14+
class StudentClockInDetail
15+
{
16+
public DateTime? LastClockedInDate { get; set; } = null;
17+
public long? LastClockedInPoints { get; set; } = null;
18+
}
1419
}

ObjectSemantics.NET.Tests/ObjectSemanticsTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,5 +832,65 @@ public void Should_Escape_Xml_Char_In_LOOPS_If_Option_Is_Enabled()
832832
#endregion
833833

834834

835+
#region Nullable Property Tests
836+
[Fact]
837+
public void Should_Map_Nullable_DateTime_Property_Given_NULL()
838+
{
839+
//Create Model
840+
StudentClockInDetail clockInDetails = new StudentClockInDetail { LastClockedInDate = null };
841+
var template = new ObjectSemanticsTemplate
842+
{
843+
FileContents = @"Last Clocked In: {{ LastClockedInDate:yyyy-MM-dd }}"
844+
};
845+
string generatedTemplate = TemplateMapper.Map(clockInDetails, template);
846+
string expectedString = "Last Clocked In: ";
847+
Assert.Equal(expectedString, generatedTemplate, false, true, true);
848+
}
849+
850+
[Fact]
851+
public void Should_Map_Nullable_DateTime_Property_Given_A_Value()
852+
{
853+
//Create Model
854+
StudentClockInDetail clockInDetails = new StudentClockInDetail { LastClockedInDate = DateTime.Now };
855+
var template = new ObjectSemanticsTemplate
856+
{
857+
FileContents = @"Last Clocked In: {{ LastClockedInDate:yyyy-MM-dd }}"
858+
};
859+
string generatedTemplate = TemplateMapper.Map(clockInDetails, template);
860+
string expectedString = $"Last Clocked In: {DateTime.Now:yyyy-MM-dd}";
861+
Assert.Equal(expectedString, generatedTemplate, false, true, true);
862+
}
863+
864+
[Fact]
865+
public void Should_Map_Nullable_Number_Property_Given_NULL()
866+
{
867+
//Create Model
868+
StudentClockInDetail clockInDetails = new StudentClockInDetail { LastClockedInPoints = null };
869+
var template = new ObjectSemanticsTemplate
870+
{
871+
FileContents = @"Last Clocked In Points: {{ LastClockedInPoints:N2 }}"
872+
};
873+
string generatedTemplate = TemplateMapper.Map(clockInDetails, template);
874+
string expectedString = "Last Clocked In Points: ";
875+
Assert.Equal(expectedString, generatedTemplate, false, true, true);
876+
}
877+
878+
[Theory]
879+
[InlineData(null)]
880+
[InlineData(2500)]
881+
[InlineData(200)]
882+
public void Should_Map_Nullable_Number_Property_Given_A_Value(long? number)
883+
{
884+
//Create Model
885+
StudentClockInDetail clockInDetails = new StudentClockInDetail { LastClockedInPoints = number };
886+
var template = new ObjectSemanticsTemplate
887+
{
888+
FileContents = @"Last Clocked In Points: {{ LastClockedInPoints:N2 }}"
889+
};
890+
string generatedTemplate = TemplateMapper.Map(clockInDetails, template);
891+
string expectedString = $"Last Clocked In Points: {number:N2}";
892+
Assert.Equal(expectedString, generatedTemplate, false, true, true);
893+
}
894+
#endregion
835895
}
836896
}

ObjectSemantics.NET/Extensions/ExtractedObjPropertyExtensions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ public static string GetPropertyDisplayString(this ExtractedObjProperty p, strin
1111
{
1212
string formattedPropertyString = GetAppliedPropertyFormatting(p, stringFormatting);
1313
//Apply Options to Property value string
14-
if (templateMapperOptions==null) return formattedPropertyString;
14+
if (templateMapperOptions == null) return formattedPropertyString;
1515
if (templateMapperOptions.XmlCharEscaping)
1616
formattedPropertyString = SecurityElement.Escape(formattedPropertyString);
1717
return formattedPropertyString;
1818
}
1919
private static string GetAppliedPropertyFormatting(this ExtractedObjProperty p, string customFormattingValue)
2020
{
21-
if (string.IsNullOrWhiteSpace(customFormattingValue))
21+
if (string.IsNullOrWhiteSpace(customFormattingValue) || p.OriginalValue == null)
2222
return p.StringFormatted;
23-
if (p.Type.Equals(typeof(int)))
23+
if (p.Type.Equals(typeof(int)) || p.Type.Equals(typeof(int?)))
2424
return int.Parse(p.StringFormatted).ToString(customFormattingValue);
25-
else if (p.Type.Equals(typeof(double)))
25+
else if (p.Type.Equals(typeof(double)) || p.Type.Equals(typeof(double?)))
2626
return double.Parse(p.StringFormatted).ToString(customFormattingValue);
27-
else if (p.Type.Equals(typeof(long)))
27+
else if (p.Type.Equals(typeof(long)) || p.Type.Equals(typeof(long?)))
2828
return long.Parse(p.StringFormatted).ToString(customFormattingValue);
29-
else if (p.Type.Equals(typeof(float)))
29+
else if (p.Type.Equals(typeof(float)) || p.Type.Equals(typeof(float?)))
3030
return float.Parse(p.StringFormatted).ToString(customFormattingValue);
31-
else if (p.Type.Equals(typeof(decimal)))
31+
else if (p.Type.Equals(typeof(decimal)) || p.Type.Equals(typeof(decimal?)))
3232
return decimal.Parse(p.StringFormatted).ToString(customFormattingValue);
33-
else if (p.Type.Equals(typeof(DateTime)))
33+
else if (p.Type.Equals(typeof(DateTime)) || p.Type.Equals(typeof(DateTime?)))
3434
return DateTime.Parse(p.StringFormatted).ToString(customFormattingValue);
3535
//Custom Formats
3636
else if (customFormattingValue.ToLower().Equals("uppercase"))

ObjectSemantics.NET/ObjectSemantics.NET.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
Added support for if condition statements with ElseIf
1717
Single Line ForLoop
1818
[BREAKING CHANGE]: forloop statement changes</PackageReleaseNotes>
19-
<AssemblyVersion>6.0.1.1</AssemblyVersion>
20-
<FileVersion>6.0.1.1</FileVersion>
21-
<Version>6.0.1</Version>
19+
<AssemblyVersion>6.0.2.2</AssemblyVersion>
20+
<FileVersion>6.0.2.2</FileVersion>
21+
<Version>6.0.2</Version>
2222
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
2323
<ApplicationIcon></ApplicationIcon>
2424
<PackageReadmeFile>README.md</PackageReadmeFile>

ObjectSemantics.NET/TemplateMapperOptions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ public class TemplateMapperOptions
44
{
55
/// <summary>
66
/// This will apply XML Character Escape on invalid characters in a Property Value String with their valid XML Equivalent
7-
/// Example of Characters;
8-
/// " &quot;
9-
/// ' &apos;
10-
/// < &lt;
11-
/// > &gt;
12-
/// & &amp;
137
/// </summary>
148
public bool XmlCharEscaping { get; set; } = false;
159
}

0 commit comments

Comments
 (0)