Skip to content

Commit cf33ce1

Browse files
authored
Merge pull request #9 from swagfin/feature/if-else-statement
added else if condition
2 parents 9c44094 + 846129b commit cf33ce1

File tree

4 files changed

+111
-11
lines changed

4 files changed

+111
-11
lines changed

ObjectSemantics.NET.Tests/ObjectSemanticsTests.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,95 @@ public void Should_Act_On_IfCondition_IEnumerable_Tests_Count_Object_Behaviour()
530530
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
531531
}
532532

533+
534+
535+
[Theory]
536+
[InlineData(5000)]
537+
[InlineData(2000)]
538+
public void Should_Act_On_IfCondition_Having_ElseIf_Inline(double amount)
539+
{
540+
//Create Model
541+
Student student = new Student { Balance = amount };
542+
//Template
543+
var template = new ObjectSemanticsTemplate
544+
{
545+
FileContents = "{{ if-start:Balance(=5000) }} --ok-passed-- {{ else-if }} --error-failed-- {{ if-end:Balance }}"
546+
};
547+
string generatedTemplate = TemplateMapper.Map(student, template);
548+
string expectedResult = (amount == 5000) ? " --ok-passed-- " : " --error-failed-- ";
549+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
550+
}
551+
552+
[Theory]
553+
[InlineData(5000)]
554+
[InlineData(2000)]
555+
public void Should_Act_On_IfCondition_Having_ElseIf_MultiLine(double amount)
556+
{
557+
//Create Model
558+
Student student = new Student { Balance = amount };
559+
//Template
560+
var template = new ObjectSemanticsTemplate
561+
{
562+
FileContents = @"
563+
{{ if-start:Balance(=5000) }}
564+
--ok-passed--
565+
{{ else-if }}
566+
--error-failed--
567+
{{ if-end:Balance }}"
568+
};
569+
string generatedTemplate = TemplateMapper.Map(student, template);
570+
string expectedResult = (amount == 5000) ? "\r\n\r\n--ok-passed--\r\n" : "\r\n\r\n--error-failed--\r\n";
571+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
572+
}
573+
574+
575+
576+
[Theory]
577+
[InlineData(true)]
578+
[InlineData(false)]
579+
public void Should_Act_On_IfCondition_Having_ElseIf_Having_A_LoopBlock(bool populateInvoices)
580+
{
581+
//Create Model
582+
Student student = new Student
583+
{
584+
StudentName = "John Doe",
585+
Invoices = (populateInvoices)
586+
? new List<Invoice>
587+
{
588+
new Invoice { Id = 2, RefNo = "INV_002", Narration = "Grade II Fees Invoice", Amount = 2000, InvoiceDate = new DateTime(2023, 04, 01) },
589+
new Invoice { Id = 1, RefNo = "INV_001", Narration = "Grade I Fees Invoice", Amount = 320, InvoiceDate = new DateTime(2022, 08, 01) }
590+
}
591+
: null
592+
};
593+
//Template
594+
var template = new ObjectSemanticsTemplate
595+
{
596+
FileContents = @"
597+
{{ if-start:invoices(=null) }}
598+
-- no invoices found --
599+
{{ else-if }}
600+
{{ for-each-start:invoices }}
601+
<tr>
602+
<td>{{ Id }}</td>
603+
<td>{{ RefNo }}</td>
604+
</tr>
605+
{{ for-each-end:invoices }}
606+
{{ if-end:invoices }}"
607+
};
608+
string generatedTemplate = TemplateMapper.Map(student, template);
609+
string expectedResult = (populateInvoices) ? "\r\n" +
610+
"\r\n<tr>" +
611+
"\r\n <td>2</td>" +
612+
"\r\n <td>INV_002</td>" +
613+
"\r\n</tr>" +
614+
"\r\n<tr>" +
615+
"\r\n <td>1</td>" +
616+
"\r\n <td>INV_001</td>" +
617+
"\r\n</tr>\r\n"
618+
619+
: "\r\n\r\n-- no invoices found --\r\n";
620+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
621+
}
533622
#endregion
534623
}
535624
}

ObjectSemantics.NET/Algorithim/GavinsAlgorithim.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ public static class GavinsAlgorithim
2424
if (property.IsPropertyValueConditionPassed(ifCondition.IfConditionValue, ifCondition.IfConditionType))
2525
{
2626
//Condition Passed
27-
TemplatedContent templatedIfContent = GenerateTemplateFromFileContents(ifCondition.IfConditionTemplate, options);
27+
TemplatedContent templatedIfContent = GenerateTemplateFromFileContents(ifCondition.IfConditionTrueTemplate, options);
2828
string templatedIfContentMapped = GenerateFromTemplate(record, templatedIfContent, parameterKeyValues, options);
2929
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, ifCondition.ReplaceRef, templatedIfContentMapped);
3030
}
31+
else if (!string.IsNullOrEmpty(ifCondition.IfConditionFalseTemplate))
32+
{
33+
//If Else Condition Block
34+
TemplatedContent templatedIfContent = GenerateTemplateFromFileContents(ifCondition.IfConditionFalseTemplate, options);
35+
string templatedIfElseContentMapped = GenerateFromTemplate(record, templatedIfContent, parameterKeyValues, options);
36+
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, ifCondition.ReplaceRef, templatedIfElseContentMapped);
37+
}
3138
else
3239
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, ifCondition.ReplaceRef, string.Empty);
3340
}
@@ -99,14 +106,17 @@ internal static TemplatedContent GenerateTemplateFromFileContents(string fileCon
99106
string subBlock = templatedContent.Template.GetSubstringByIndexStartAndEnd(regexIfConditionMatch.Index + regexIfConditionMatch.Length, regexIfConditionMatchEnd.Index - 1);
100107
//#Replace Template Block with unique Code
101108
templatedContent.Template = templatedContent.Template.ReplaceByIndexStartAndEnd(regexIfConditionMatch.Index, (regexIfConditionMatchEnd.Index - 1) + regexIfConditionMatchEnd.Length, _replaceCode);
109+
//Determine if subBlock has Else Condition
110+
string[] elseIfSplits = Regex.Split(subBlock, @"{{\s*else-if\s*}}", RegexOptions.IgnoreCase);
102111
//#Append Condition Code
103112
templatedContent.ReplaceIfConditionCodes.Add(new ReplaceIfConditionCode
104113
{
105114
ReplaceRef = _replaceCode,
106-
IfPropertyName = (regexIfConditionMatch.Groups.Count >= 1) ? regexIfConditionMatch.Groups[1].Value?.Trim()?.ToString()?.ToLower()?.Replace(" ", string.Empty) : "unspecified",
107-
IfConditionType = (regexIfConditionMatch.Groups.Count >= 2) ? regexIfConditionMatch.Groups[2].Value?.Trim()?.ToString()?.ToLower()?.Replace(" ", string.Empty) : "unspecified",
108-
IfConditionValue = (regexIfConditionMatch.Groups.Count >= 3) ? regexIfConditionMatch.Groups[3].Value?.Trim()?.ToString()?.ToLower() : "unspecified",
109-
IfConditionTemplate = subBlock
115+
IfPropertyName = (regexIfConditionMatch.Groups.Count >= 1) ? regexIfConditionMatch.Groups[1].Value?.ToString().Trim().ToLower()?.Replace(" ", string.Empty) : "unspecified",
116+
IfConditionType = (regexIfConditionMatch.Groups.Count >= 2) ? regexIfConditionMatch.Groups[2].Value?.ToString().Trim().ToLower()?.Replace(" ", string.Empty) : "unspecified",
117+
IfConditionValue = (regexIfConditionMatch.Groups.Count >= 3) ? regexIfConditionMatch.Groups[3].Value?.ToString()?.Trim().ToLower() : "unspecified",
118+
IfConditionTrueTemplate = (elseIfSplits?.Length >= 2) ? elseIfSplits[0] : subBlock,
119+
IfConditionFalseTemplate = (elseIfSplits?.Length >= 2) ? elseIfSplits[1] : string.Empty
110120
});
111121
//Move Next (Both)
112122
regexIfConditionMatch = regexIfConditionMatch.NextMatch();

ObjectSemantics.NET/Algorithim/ReplaceIfConditionCode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
public string IfConditionType { get; set; }
55
public string IfConditionValue { get; set; }
66
public string ReplaceRef { get; set; }
7-
public string IfConditionTemplate { get; set; }
7+
public string IfConditionTrueTemplate { get; set; } = string.Empty;
8+
public string IfConditionFalseTemplate { get; set; } = string.Empty;
89
}
910

ObjectSemantics.NET/ObjectSemantics.NET.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
<PackageIconUrl />
1313
<RepositoryUrl>https://github.com/swagfin/ObjectSemantics.NET</RepositoryUrl>
1414
<RepositoryType>Github</RepositoryType>
15-
<PackageReleaseNotes>v.5.1.2
16-
Added support for if condition statements</PackageReleaseNotes>
17-
<AssemblyVersion>5.1.2.1</AssemblyVersion>
18-
<FileVersion>5.1.2.1</FileVersion>
19-
<Version>5.1.2</Version>
15+
<PackageReleaseNotes>v.5.1.3
16+
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>
2020
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
2121
<ApplicationIcon></ApplicationIcon>
2222
<PackageReadmeFile>README.md</PackageReadmeFile>

0 commit comments

Comments
 (0)