Skip to content

Commit 94a836f

Browse files
authored
Merge pull request #11 from swagfin/feature/if-else-improvements
Feature/if else improvements
2 parents e487f7b + 0f60bb6 commit 94a836f

File tree

6 files changed

+121
-75
lines changed

6 files changed

+121
-75
lines changed

ObjectSemantics.NET.Tests/ObjectSemanticsTests.cs

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public void Should_Act_On_IfCondition_SingleLine()
260260
//Template
261261
var template = new ObjectSemanticsTemplate
262262
{
263-
FileContents = "IsEnabled: {{ if-start:invoices(!=null) }}YES{{ if-end:invoices }}"
263+
FileContents = "IsEnabled: {{ #if(invoices!=null) }}YES{{ #endif }}"
264264
};
265265
string generatedTemplate = TemplateMapper.Map(student, template);
266266
string expectedResult = "IsEnabled: YES";
@@ -284,7 +284,7 @@ public void Should_Act_On_IfCondition_SingleLine_With_Attribute()
284284
//Template
285285
var template = new ObjectSemanticsTemplate
286286
{
287-
FileContents = "InvoicedPerson: {{ if-start:invoices(!=null) }}{{StudentName}}{{ if-end:invoices }}"
287+
FileContents = "InvoicedPerson: {{ #if(invoices!=null) }}{{StudentName}}{{ #endif }}"
288288
};
289289
string generatedTemplate = TemplateMapper.Map(student, template);
290290
string expectedResult = "InvoicedPerson: John Doe";
@@ -309,9 +309,9 @@ public void Should_Act_On_IfCondition_MultiLine()
309309
var template = new ObjectSemanticsTemplate
310310
{
311311
FileContents = @"status
312-
{{ if-start:invoices(!=null) }}
312+
{{ #if(invoices!=null) }}
313313
<h4>condition--passed</h4>
314-
{{ if-end:invoices }}"
314+
{{ #endif }}"
315315
};
316316
string generatedTemplate = TemplateMapper.Map(student, template);
317317
string expectedResult = "status\r\n\r\n<h4>condition--passed</h4>\r\n";
@@ -336,9 +336,9 @@ public void Should_Act_On_IfCondition_MultiLine_With_Attribute()
336336
var template = new ObjectSemanticsTemplate
337337
{
338338
FileContents = @"status
339-
{{ if-start:invoices(!=null) }}
339+
{{ #if(invoices!=null) }}
340340
<h4>Hi, I have invoices for {{ StudentName }} </h4>
341-
{{ if-end:invoices }}"
341+
{{ #endif }}"
342342
};
343343
string generatedTemplate = TemplateMapper.Map(student, template);
344344
string expectedResult = "status\r\n\r\n<h4>Hi, I have invoices for John Doe </h4>\r\n";
@@ -363,15 +363,15 @@ public void Should_Act_On_IfCondition_Having_Loop_As_Child()
363363
var template = new ObjectSemanticsTemplate
364364
{
365365
FileContents = @"
366-
{{ if-start:invoices(!=null) }}
366+
{{ #if (invoices != null) }}
367367
{{ StudentName }} Invoices
368368
{{ for-each-start:invoices }}
369369
<tr>
370370
<td>{{ Id }}</td>
371371
<td>{{ RefNo }}</td>
372372
</tr>
373373
{{ for-each-end:invoices }}
374-
{{ if-end:invoices }}"
374+
{{ #endif }}"
375375
};
376376
string generatedTemplate = TemplateMapper.Map(student, template);
377377
string expectedResult = "\r\n\r\nJohn Doe Invoices" +
@@ -390,7 +390,7 @@ public void Should_Act_On_IfCondition_Having_Loop_As_Child()
390390
//#Match =, !=, >, >=, <, and <=.
391391

392392
[Theory]
393-
[InlineData("=", 5000)]
393+
[InlineData("==", 5000)]
394394
[InlineData("!=", 0)]
395395
[InlineData(">", 2000)]
396396
[InlineData("<=", 5001)]
@@ -402,7 +402,7 @@ public void Should_Act_On_IfCondition_Equality_Checks(string condition, double a
402402
//Template
403403
var template = new ObjectSemanticsTemplate
404404
{
405-
FileContents = string.Format("{2} if-start:balance({0}{1}) {3} {0} passed {2} if-end:balance {3}", condition, amount, "{{", "}}")
405+
FileContents = string.Format("{2} #if(balance{0}{1}) {3} {0} passed {2} #endif {3}", condition, amount, "{{", "}}")
406406
};
407407

408408
string expectedResult = string.Format(" {0} passed ", condition);
@@ -412,8 +412,6 @@ public void Should_Act_On_IfCondition_Equality_Checks(string condition, double a
412412

413413

414414

415-
416-
417415
[Fact]
418416
public void Should_Act_On_IfCondition_IEnumerable_Tests_Equall()
419417
{
@@ -430,7 +428,7 @@ public void Should_Act_On_IfCondition_IEnumerable_Tests_Equall()
430428
//Template
431429
var template = new ObjectSemanticsTemplate
432430
{
433-
FileContents = "{{ if-start:invoices(=2) }} 2 records {{ if-end:invoices }}"
431+
FileContents = "{{ #if(invoices==2) }} 2 records {{ #endif }}"
434432
};
435433
string generatedTemplate = TemplateMapper.Map(student, template);
436434
string expectedResult = " 2 records ";
@@ -454,7 +452,7 @@ public void Should_Act_On_IfCondition_IEnumerable_Tests_NotEqualNull()
454452
//Template
455453
var template = new ObjectSemanticsTemplate
456454
{
457-
FileContents = "{{ if-start:invoices(!=null) }} is not NULL {{ if-end:invoices }}"
455+
FileContents = "{{ #if(invoices!=null) }} is not NULL {{ #endif }}"
458456
};
459457
string generatedTemplate = TemplateMapper.Map(student, template);
460458
string expectedResult = " is not NULL ";
@@ -473,7 +471,7 @@ public void Should_Act_On_IfCondition_IEnumerable_Tests_NULL_Object_Behaviour()
473471
//Template
474472
var template = new ObjectSemanticsTemplate
475473
{
476-
FileContents = "{{ if-start:invoices(=null) }} is NULL {{ if-end:invoices }}"
474+
FileContents = "{{ #if(invoices==null) }} is NULL {{ #endif }}"
477475
};
478476
string generatedTemplate = TemplateMapper.Map(student, template);
479477
string expectedResult = " is NULL ";
@@ -491,7 +489,7 @@ public void Should_Act_On_IfCondition_IEnumerable_Tests_Count_NULL_Object_Behavi
491489
//Template
492490
var template = new ObjectSemanticsTemplate
493491
{
494-
FileContents = "{{ if-start:invoices(=0) }} no records {{ if-end:invoices }}"
492+
FileContents = "{{ #if(invoices==0) }} no records {{ #endif }}"
495493
};
496494
string generatedTemplate = TemplateMapper.Map(student, template);
497495
string expectedResult = " no records ";
@@ -510,7 +508,7 @@ public void Should_Act_On_IfCondition_IEnumerable_Tests_Count_Object_Behaviour()
510508
//Template
511509
var template = new ObjectSemanticsTemplate
512510
{
513-
FileContents = "{{ if-start:invoices(=null) }} no records {{ if-end:invoices }}"
511+
FileContents = "{{ #if(invoices==null) }} no records {{ #endif }}"
514512
};
515513
string generatedTemplate = TemplateMapper.Map(student, template);
516514
string expectedResult = " no records ";
@@ -529,7 +527,7 @@ public void Should_Act_On_IfCondition_Having_ElseIf_Inline(double amount)
529527
//Template
530528
var template = new ObjectSemanticsTemplate
531529
{
532-
FileContents = "{{ if-start:Balance(=5000) }} --ok-passed-- {{ else-if }} --error-failed-- {{ if-end:Balance }}"
530+
FileContents = "{{ #if(Balance==5000) }} --ok-passed-- {{ #else }} --error-failed-- {{ #endif }}"
533531
};
534532
string generatedTemplate = TemplateMapper.Map(student, template);
535533
string expectedResult = (amount == 5000) ? " --ok-passed-- " : " --error-failed-- ";
@@ -547,11 +545,11 @@ public void Should_Act_On_IfCondition_Having_ElseIf_MultiLine(double amount)
547545
var template = new ObjectSemanticsTemplate
548546
{
549547
FileContents = @"
550-
{{ if-start:Balance(=5000) }}
548+
{{ #if(Balance==5000) }}
551549
--ok-passed--
552-
{{ else-if }}
550+
{{ #else }}
553551
--error-failed--
554-
{{ if-end:Balance }}"
552+
{{ #endif }}"
555553
};
556554
string generatedTemplate = TemplateMapper.Map(student, template);
557555
string expectedResult = (amount == 5000) ? "\r\n\r\n--ok-passed--\r\n" : "\r\n\r\n--error-failed--\r\n";
@@ -570,11 +568,11 @@ public void Should_Act_On_IfCondition_Having_ElseIf_MultiLine_String_EquallsNull
570568
var template = new ObjectSemanticsTemplate
571569
{
572570
FileContents = @"
573-
{{ if-start:StudentName(=NULL) }}
571+
{{ #if(StudentName==NULL) }}
574572
--ok-passed--
575-
{{ else-if }}
573+
{{ #else }}
576574
--error-failed--
577-
{{ if-end:Balance }}"
575+
{{ #endif }}"
578576
};
579577
string generatedTemplate = TemplateMapper.Map(student, template);
580578
string expectedResult = string.IsNullOrEmpty(studentName) ? "\r\n\r\n--ok-passed--\r\n" : "\r\n\r\n--error-failed--\r\n";
@@ -593,19 +591,18 @@ public void Should_Act_On_IfCondition_Having_ElseIf_MultiLine_String_NotEquallsN
593591
var template = new ObjectSemanticsTemplate
594592
{
595593
FileContents = @"
596-
{{ if-start:StudentName(!=NULL) }}
594+
{{ #if(StudentName!=NULL) }}
597595
--ok-passed--
598-
{{ else-if }}
596+
{{ #else }}
599597
--error-failed--
600-
{{ if-end:Balance }}"
598+
{{ #endif }}"
601599
};
602600
string generatedTemplate = TemplateMapper.Map(student, template);
603601
string expectedResult = (!string.IsNullOrEmpty(studentName)) ? "\r\n\r\n--ok-passed--\r\n" : "\r\n\r\n--error-failed--\r\n";
604602
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
605603
}
606604

607605

608-
609606
[Theory]
610607
[InlineData(null)]
611608
[InlineData("John Doe")]
@@ -618,11 +615,11 @@ public void Should_Act_On_IfCondition_Having_ElseIf_MultiLine_String_Equalls(str
618615
var template = new ObjectSemanticsTemplate
619616
{
620617
FileContents = @"
621-
{{ if-start:StudentName(=John Doe) }}
618+
{{ #if(StudentName==John Doe) }}
622619
--ok-passed--
623-
{{ else-if }}
620+
{{ #else }}
624621
--error-failed--
625-
{{ if-end:Balance }}"
622+
{{ #endif }}"
626623
};
627624
string generatedTemplate = TemplateMapper.Map(student, template);
628625
string expectedResult = (studentName == "John Doe") ? "\r\n\r\n--ok-passed--\r\n" : "\r\n\r\n--error-failed--\r\n";
@@ -638,7 +635,7 @@ public void Should_Act_On_IfCondition_Simple_Property_String_Equality()
638635
//Template
639636
var template = new ObjectSemanticsTemplate
640637
{
641-
FileContents = "{{ if-start:studentName(=John Doe) }} YES, i am John Doe {{ if-end:studentName }}"
638+
FileContents = "{{ #if(studentName==John Doe) }} YES, i am John Doe {{ #endif }}"
642639
};
643640
string generatedTemplate = TemplateMapper.Map(student, template);
644641
string expectedResult = " YES, i am John Doe ";
@@ -667,16 +664,16 @@ public void Should_Act_On_IfCondition_Having_ElseIf_Having_A_LoopBlock(bool popu
667664
var template = new ObjectSemanticsTemplate
668665
{
669666
FileContents = @"
670-
{{ if-start:invoices(=null) }}
667+
{{ #if(invoices==null) }}
671668
-- no invoices found --
672-
{{ else-if }}
669+
{{ #else }}
673670
{{ for-each-start:invoices }}
674671
<tr>
675672
<td>{{ Id }}</td>
676673
<td>{{ RefNo }}</td>
677674
</tr>
678675
{{ for-each-end:invoices }}
679-
{{ if-end:invoices }}"
676+
{{ #endif }}"
680677
};
681678
string generatedTemplate = TemplateMapper.Map(student, template);
682679
string expectedResult = (populateInvoices) ? "\r\n" +
@@ -692,6 +689,58 @@ public void Should_Act_On_IfCondition_Having_ElseIf_Having_A_LoopBlock(bool popu
692689
: "\r\n\r\n-- no invoices found --\r\n";
693690
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
694691
}
692+
693+
694+
695+
696+
[Fact]
697+
public void Should_Act_On_IfCondition_Having_Multiple_IF_Condition_Blocks_SingleLine()
698+
{
699+
//Create Model
700+
Student student = new Student { StudentName = "John Doe", Balance = 2000 };
701+
//Template
702+
var template = new ObjectSemanticsTemplate
703+
{
704+
FileContents = "{{ #if(studentName==John Doe) }} YES, i am John Doe {{ #endif }} | {{ #if(Balance==2000) }} YES, my balance is 2000 {{ #endif }}"
705+
};
706+
string generatedTemplate = TemplateMapper.Map(student, template);
707+
string expectedResult = " YES, i am John Doe | YES, my balance is 2000 ";
708+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
709+
}
710+
711+
[Theory]
712+
[InlineData(null)]
713+
[InlineData("John Doe")]
714+
[InlineData("")]
715+
public void Should_Act_On_IfCondition_Having_Multiple_IF_Condition_Blocks_MultiLine(string studentName)
716+
{
717+
//Create Model
718+
Student student = new Student { StudentName = studentName };
719+
//Template
720+
var template = new ObjectSemanticsTemplate
721+
{
722+
FileContents = @"
723+
#Test 1
724+
{{ #if(StudentName!=NULL) }}
725+
--ok-passed--
726+
{{ #else }}
727+
--error-failed--
728+
{{ #endif }}
729+
#Test 2
730+
{{ #if(StudentName==John Doe) }}
731+
--I am, John Doe--
732+
{{ #else }}
733+
--I am NOT--
734+
{{ #endif }}"
735+
736+
};
737+
string generatedTemplate = TemplateMapper.Map(student, template);
738+
string expectedResult = (studentName == "John Doe")
739+
?
740+
"\r\n#Test 1\r\n\r\n--ok-passed--\r\n\r\n#Test 2\r\n\r\n--I am, John Doe--\r\n"
741+
: "\r\n#Test 1\r\n\r\n--error-failed--\r\n\r\n#Test 2\r\n\r\n--I am NOT--\r\n";
742+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
743+
}
695744
#endregion
696745
}
697746
}

ObjectSemantics.NET/Algorithim/GavinsAlgorithim.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ public static class GavinsAlgorithim
1616
List<ExtractedObjProperty> objProperties = GetObjectProperties(record, parameterKeyValues);
1717

1818
#region Replace If Conditions
19-
foreach (ReplaceIfConditionCode ifCondition in clonedTemplate.ReplaceIfConditionCodes)
19+
foreach (ReplaceIfOperationCode ifCondition in clonedTemplate.ReplaceIfConditionCodes)
2020
{
2121
ExtractedObjProperty property = objProperties.FirstOrDefault(x => x.Name.ToUpper().Equals(ifCondition.IfPropertyName.ToUpper()));
2222
if (property != null)
2323
{
24-
if (property.IsPropertyValueConditionPassed(ifCondition.IfConditionValue, ifCondition.IfConditionType))
24+
if (property.IsPropertyValueConditionPassed(ifCondition.IfOperationValue, ifCondition.IfOperationType))
2525
{
2626
//Condition Passed
27-
TemplatedContent templatedIfContent = GenerateTemplateFromFileContents(ifCondition.IfConditionTrueTemplate, options);
27+
TemplatedContent templatedIfContent = GenerateTemplateFromFileContents(ifCondition.IfOperationTrueTemplate, 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))
31+
else if (!string.IsNullOrEmpty(ifCondition.IfOperationFalseTemplate))
3232
{
3333
//If Else Condition Block
34-
TemplatedContent templatedIfContent = GenerateTemplateFromFileContents(ifCondition.IfConditionFalseTemplate, options);
34+
TemplatedContent templatedIfContent = GenerateTemplateFromFileContents(ifCondition.IfOperationFalseTemplate, options);
3535
string templatedIfElseContentMapped = GenerateFromTemplate(record, templatedIfContent, parameterKeyValues, options);
3636
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, ifCondition.ReplaceRef, templatedIfElseContentMapped);
3737
}
@@ -98,29 +98,26 @@ internal static TemplatedContent GenerateTemplateFromFileContents(string fileCon
9898
{
9999
TemplatedContent templatedContent = new TemplatedContent { Template = fileContent };
100100
#region If Condition
101-
Match regexIfConditionMatch = Regex.Match(templatedContent.Template, @"{{\s*if-start:\s*([^()]+?)\(\s*(!?=|[<>]=?)\s*([^()]+?)\s*\)\s*}}", RegexOptions.IgnoreCase); //#Match =, !=, >, >=, <, and <=.
102-
Match regexIfConditionMatchEnd = Regex.Match(templatedContent.Template, @"{{\s*if-end:([^()]+?)\s*}}", RegexOptions.IgnoreCase);
103-
while (regexIfConditionMatch.Success && regexIfConditionMatchEnd.Success)
101+
//Match =, !=, >, >=, <, and <=.
102+
//Without ElseIf-> {{\s*#if\s*\(\s*(\w+)\s*([!=<>]=?|<|>)\s*([\w\s.-]+)\s*\)\s*}}([\s\S]*?){{\s*#endif\s*}}
103+
string _matchWithElseIf = @"{{\s*#if\s*\(\s*(?<param>\w+)\s*(?<operator>==|!=|>=|<=|>|<)\s*(?<value>[^)]+)\s*\)\s*}}(?<code>[\s\S]*?)(?:{{\s*#else\s*}}(?<else>[\s\S]*?))?{{\s*#endif\s*}}";
104+
while (Regex.IsMatch(templatedContent.Template, _matchWithElseIf))
104105
{
105-
string _replaceCode = string.Format("REPLACE_IF_CONDITION_{0}", Guid.NewGuid().ToString().ToUpper());
106-
string subBlock = templatedContent.Template.GetSubstringByIndexStartAndEnd(regexIfConditionMatch.Index + regexIfConditionMatch.Length, regexIfConditionMatchEnd.Index - 1);
107-
//#Replace Template Block with unique Code
108-
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);
111-
//#Append Condition Code
112-
templatedContent.ReplaceIfConditionCodes.Add(new ReplaceIfConditionCode
106+
templatedContent.Template = Regex.Replace(templatedContent.Template, _matchWithElseIf, match =>
113107
{
114-
ReplaceRef = _replaceCode,
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
108+
string _replaceCode = string.Format("R_IF_BLOCK_{0}", Guid.NewGuid().ToString().ToUpper());
109+
templatedContent.ReplaceIfConditionCodes.Add(new ReplaceIfOperationCode
110+
{
111+
ReplaceRef = _replaceCode,
112+
IfPropertyName = match.Groups[1].Value?.ToString().Trim().ToLower().Replace(" ", string.Empty),
113+
IfOperationType = match.Groups[2].Value?.ToString().Trim().ToLower().Replace(" ", string.Empty),
114+
IfOperationValue = match.Groups[3].Value?.ToString().Trim(),
115+
IfOperationTrueTemplate = match.Groups[4].Value?.ToString(),
116+
IfOperationFalseTemplate = (match.Groups.Count >= 6) ? match.Groups[5].Value?.ToString() : string.Empty
117+
});
118+
// Return Replacement
119+
return _replaceCode;
120120
});
121-
//Move Next (Both)
122-
regexIfConditionMatch = regexIfConditionMatch.NextMatch();
123-
regexIfConditionMatchEnd = regexIfConditionMatchEnd.NextMatch();
124121
}
125122
#endregion
126123

ObjectSemantics.NET/Algorithim/ReplaceIfConditionCode.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class ReplaceIfOperationCode
2+
{
3+
public string IfPropertyName { get; set; }
4+
public string IfOperationType { get; set; }
5+
public string IfOperationValue { get; set; }
6+
public string ReplaceRef { get; set; }
7+
public string IfOperationTrueTemplate { get; set; } = string.Empty;
8+
public string IfOperationFalseTemplate { get; set; } = string.Empty;
9+
}
10+

0 commit comments

Comments
 (0)