Skip to content

Commit

Permalink
chore: added support for single arrays Loop Support
Browse files Browse the repository at this point in the history
  • Loading branch information
swagfin committed May 19, 2024
1 parent e44da4e commit af510d2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace ObjectSemantics.NET.Tests
{
public class LoopFunctionTests
public class EnumerableLoopTests
{

[Fact]
Expand Down Expand Up @@ -174,5 +174,49 @@ John Doe Invoices
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
}

[Fact]
public void Should_Map_Array_Of_String_With_Formatting()
{
//Create Model
Student student = new Student
{
ArrayOfString = new string[]
{
"String 001",
"String 002"
}
};
//Template
var template = new ObjectSemanticsTemplate
{
FileContents = @"{{ #foreach(ArrayOfString) }} {{ . }} | {{ .:uppercase }} {{ #endforeach }}"
};
string generatedTemplate = template.Map(student);
string expectedResult = " String 001 | STRING 001 String 002 | STRING 002 ";
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
}


[Fact]
public void Should_Map_Array_Of_Double_With_Formatting_Test()
{
//Create Model
Student student = new Student
{
ArrayOfDouble = new double[]
{
1000.15,
2000.22
}
};
//Template
var template = new ObjectSemanticsTemplate
{
FileContents = @"{{ #foreach(ArrayOfDouble) }} {{ . }} | {{ .:N0 }} {{ #endforeach }}"
};
string generatedTemplate = template.Map(student);
string expectedResult = " 1000.15 | 1,000 2000.22 | 2,000 ";
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
}
}
}
2 changes: 2 additions & 0 deletions ObjectSemantics.NET.Tests/MoqModels/Student.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal class Student
public double Balance { get; set; }
public DateTime RegDate { get; set; } = DateTime.Now;
public List<Invoice> Invoices { get; set; } = new List<Invoice>();
public string[] ArrayOfString { get; set; } = new string[] { };
public double[] ArrayOfDouble { get; set; } = new double[] { };
public List<StudentClockInDetail> StudentClockInDetails { get; set; } = new List<StudentClockInDetail>();
}
class StudentClockInDetail
Expand Down
12 changes: 11 additions & 1 deletion ObjectSemantics.NET/Algorithim/GavinsAlgorithim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public static class GavinsAlgorithim
ExtractedObjProperty objProperty = rowRecordValues.FirstOrDefault(x => x.Name.ToUpper().Equals(objLoopCode.TargetPropertyName.ToUpper()));
if (objProperty != null)
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objProperty.GetPropertyDisplayString(objLoopCode.FormattingCommand, options));
else if (objLoopCode.TargetPropertyName.Equals("."))
{
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, new ExtractedObjProperty
{
Name = objLoopCode.TargetPropertyName,
Type = loopRow.GetType(),
OriginalValue = loopRow
}.GetPropertyDisplayString(objLoopCode.FormattingCommand, options));
}
else
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objLoopCode.ReplaceCommand);
}
Expand Down Expand Up @@ -213,7 +222,8 @@ private static List<ExtractedObjProperty> GetObjPropertiesFromUnknown(object val
return list;
Type myType = value.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
//loop (Skip properties that require index parameters (like Chars in string))
foreach (PropertyInfo prop in props.Where(x => x.GetIndexParameters().Length == 0))
{
try
{
Expand Down
20 changes: 9 additions & 11 deletions ObjectSemantics.NET/ObjectSemantics.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@
<PackageIconUrl />
<RepositoryUrl>https://github.com/swagfin/ObjectSemantics.NET</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>
v.6.0.3
1. Added encoding string formattings
ToMD5
ToBase64
FromBase64
2. added extention to allow mapping directly from Template
</PackageReleaseNotes>
<AssemblyVersion>6.0.3</AssemblyVersion>
<FileVersion>6.0.3</FileVersion>
<Version>6.0.3</Version>
<PackageReleaseNotes>. Added support for single arrays Loop Support
. Added encoding string formattings
ToMD5
ToBase64
FromBase64
. Added template extension method to allow mapping directly from Template</PackageReleaseNotes>
<AssemblyVersion>6.0.4</AssemblyVersion>
<FileVersion>6.0.4</FileVersion>
<Version>6.0.4</Version>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<ApplicationIcon></ApplicationIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down

0 comments on commit af510d2

Please sign in to comment.