Skip to content

Commit 9c44094

Browse files
authored
Merge pull request #8 from swagfin/refactor/minor-changes-and-refactoring
Refactor/minor changes and refactoring
2 parents 736eee6 + 168c292 commit 9c44094

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

ObjectSemantics.NET.Tests/ObjectSemanticsTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,46 @@ public void Should_Map_Enumerable_Collection_In_Object()
7676
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
7777
}
7878

79+
[Fact]
80+
public void Should_Map_Multiple_Enumerable_Collection_On_Same_Template()
81+
{
82+
//Create Model
83+
Student student = new Student
84+
{
85+
StudentName = "John Doe",
86+
Invoices = new List<Invoice>
87+
{
88+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
89+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
90+
}
91+
};
92+
//Template
93+
var template = new ObjectSemanticsTemplate
94+
{
95+
FileContents = @"
96+
{{ StudentName }} Invoices
97+
LOOP #1
98+
{{ for-each-start:invoices }}
99+
<h5>{{ Id }} On Loop #1</h5>
100+
{{ for-each-end:invoices }}
101+
LOOP #2
102+
{{ for-each-start:invoices }}
103+
<h5>{{ Id }} On Loop #2</h5>
104+
{{ for-each-end:invoices }}
105+
"
106+
};
107+
string generatedTemplate = TemplateMapper.Map(student, template);
108+
string expectedResult = "\r\nJohn Doe Invoices" +
109+
"\r\nLOOP #1" +
110+
"\r\n <h5>2 On Loop #1</h5>" +
111+
"\r\n <h5>1 On Loop #1</h5>" +
112+
"\r\nLOOP #2" +
113+
"\r\n <h5>2 On Loop #2</h5>" +
114+
"\r\n <h5>1 On Loop #2</h5>" +
115+
"\r\n"; ;
116+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
117+
}
118+
79119
[Fact]
80120
public void Should_Map_Additional_Parameters()
81121
{

ObjectSemantics.NET/Algorithim/GavinsAlgorithim.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ internal static TemplatedContent GenerateTemplateFromFileContents(string fileCon
9898
string _replaceCode = string.Format("REPLACE_IF_CONDITION_{0}", Guid.NewGuid().ToString().ToUpper());
9999
string subBlock = templatedContent.Template.GetSubstringByIndexStartAndEnd(regexIfConditionMatch.Index + regexIfConditionMatch.Length, regexIfConditionMatchEnd.Index - 1);
100100
//#Replace Template Block with unique Code
101-
//templatedContent.Template = templatedContent.Template.ReplaceFirstOccurrence(subBlock, _replaceCode);
102101
templatedContent.Template = templatedContent.Template.ReplaceByIndexStartAndEnd(regexIfConditionMatch.Index, (regexIfConditionMatchEnd.Index - 1) + regexIfConditionMatchEnd.Length, _replaceCode);
103102
//#Append Condition Code
104103
templatedContent.ReplaceIfConditionCodes.Add(new ReplaceIfConditionCode
@@ -116,8 +115,8 @@ internal static TemplatedContent GenerateTemplateFromFileContents(string fileCon
116115
#endregion
117116

118117
#region Generate Obj Looop
119-
Match regexLoopMatch = Regex.Match(templatedContent.Template, "{{(.+?)for-each-start:(.+?)}}", RegexOptions.IgnoreCase);
120-
Match regexLoopEnd = Regex.Match(templatedContent.Template, "{{(.+?)for-each-end:(.+?)}}", RegexOptions.IgnoreCase);
118+
Match regexLoopMatch = Regex.Match(templatedContent.Template, "{{\\s*for-each-start:([^()]+?)\\s*}}", RegexOptions.IgnoreCase);
119+
Match regexLoopEnd = Regex.Match(templatedContent.Template, "{{\\s*for-each-end:([^()]+?)\\s*}}", RegexOptions.IgnoreCase);
121120
while (regexLoopMatch.Success && regexLoopEnd.Success)
122121
{
123122
int startAtIndex = templatedContent.Template.IndexOf(regexLoopMatch.Value); //Getting again index just incase it was replaced
@@ -128,7 +127,7 @@ internal static TemplatedContent GenerateTemplateFromFileContents(string fileCon
128127
string replaceCode = string.Format("REPLACE_LOOP_{0}", Guid.NewGuid().ToString().ToUpper());
129128
templatedContent.Template = Regex.Replace(templatedContent.Template, subBlock, replaceCode, RegexOptions.IgnoreCase);
130129
//Loop Target Object Name
131-
string targetObjName = regexLoopMatch.Value.Replace("{", string.Empty).Replace("}", string.Empty).Trim().Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
130+
string targetObjName = (regexLoopMatch.Groups.Count >= 1) ? regexLoopMatch.Groups[1].Value?.Trim()?.ToString()?.ToLower()?.Replace(" ", string.Empty) : string.Empty;
132131
//Obj
133132
ReplaceObjLoopCode replaceObjLoopCode = new ReplaceObjLoopCode { ReplaceRef = replaceCode, TargetObjectName = targetObjName };
134133
//Extra Loop Contents

0 commit comments

Comments
 (0)