Skip to content

Commit 693b3da

Browse files
committed
chore: additional unit tests and refactor
1 parent 402a8e0 commit 693b3da

5 files changed

Lines changed: 69 additions & 17 deletions

File tree

ObjectSemantics.NET.Tests/EnumerableLoopTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,20 @@ public void Should_Map_Array_Of_String_With_Formatting()
140140
string expectedResult = " MORGAN GEORGE JANE ";
141141
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
142142
}
143+
144+
[Theory]
145+
[InlineData(@"{{ #foreach(MyFriends) }}[{{ .:uppercase }}]{{ #endforeach }}")]
146+
[InlineData(@"{{# foreach(MyFriends) }}[{{ .:uppercase }}]{{# endforeach }}")]
147+
[InlineData(@"{{ #foreach(MyFriends) }}[{{ .:uppercase }}]{{ #endforeach }}")]
148+
public void Should_Evaluate_ForEach_Having_Spaces_Before_And_After_Parentheses(string template)
149+
{
150+
Person person = new Person
151+
{
152+
MyFriends = new string[] { "Morgan", "George" }
153+
};
154+
155+
string result = person.Map(template);
156+
Assert.Equal("[MORGAN][GEORGE]", result);
157+
}
143158
}
144159
}

ObjectSemantics.NET.Tests/IfConditionTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,21 @@ public void Should_Render_Multiple_If_Condition_Statements(int age, string expec
130130
string result = person.Map(template);
131131
Assert.Equal(expected, result);
132132
}
133+
134+
135+
[Theory]
136+
[InlineData(@"{{#if(MyCars==0)}}Zero Cars{{#else}}Hmmm!{{#endif}}")]
137+
[InlineData(@"{{# if (MyCars==0)}}Zero Cars{{ # else }}Hmmm!{{ # endif}}")]
138+
[InlineData(@"{{ #if(MyCars==0) }}Zero Cars{{ #endif }}")]
139+
public void Should_Evaluate_If_Having_Spaces_Before_And_After_Parentheses(string template)
140+
{
141+
Person person = new Person
142+
{
143+
MyCars = null
144+
};
145+
146+
string result = person.Map(template);
147+
Assert.Equal("Zero Cars", result);
148+
}
133149
}
134150
}

ObjectSemantics.NET/Engine/EngineAlgorithim.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ internal static class EngineAlgorithim
1515
{
1616
private static readonly ConcurrentDictionary<Type, PropertyInfo[]> PropertyCache = new ConcurrentDictionary<Type, PropertyInfo[]>();
1717

18-
private static readonly Regex IfConditionRegex = new Regex(@"{{\s*#if\s*\(\s*(?<param>\w+)\s*(?<operator>==|!=|>=|<=|>|<)\s*(?<value>[^)]+)\s*\)\s*}}(?<code>[\s\S]*?)(?:{{\s*#else\s*}}(?<else>[\s\S]*?))?{{\s*#endif\s*}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
19-
18+
private static readonly Regex IfConditionRegex = new Regex(@"{{\s*#\s*if\s*\(\s*(?<param>\w+)\s*(?<operator>==|!=|>=|<=|>|<)\s*(?<value>[^)]+?)\s*\)\s*}}(?<code>[\s\S]*?)(?:{{\s*#\s*else\s*}}(?<else>[\s\S]*?))?{{\s*#\s*endif\s*}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2019
private static readonly Regex LoopBlockRegex = new Regex(@"{{\s*#\s*foreach\s*\(\s*(\w+)\s*\)\s*\}\}([\s\S]*?)\{\{\s*#\s*endforeach\s*}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2120
private static readonly Regex DirectParamRegex = new Regex(@"{{(.+?)}}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2221

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Text;
2+
3+
namespace ObjectSemantics.NET.Engine.Extensions
4+
{
5+
public static class StringBuilderExtensions
6+
{
7+
public static StringBuilder ReplaceFirstOccurrence(this StringBuilder sb, string search, string replace)
8+
{
9+
if (sb == null || string.IsNullOrEmpty(search))
10+
return sb;
11+
12+
int len = sb.Length - search.Length + 1;
13+
for (int i = 0; i < len; i++)
14+
{
15+
bool match = true;
16+
for (int j = 0; j < search.Length; j++)
17+
{
18+
if (sb[i + j] != search[j])
19+
{
20+
match = false;
21+
break;
22+
}
23+
}
24+
25+
if (match)
26+
{
27+
sb.Remove(i, search.Length);
28+
sb.Insert(i, replace);
29+
break;
30+
}
31+
}
32+
33+
return sb;
34+
}
35+
36+
}
37+
}

ObjectSemantics.NET/Engine/Extensions/StringExtensions.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,6 @@ namespace ObjectSemantics.NET.Engine.Extensions
55
{
66
internal static class StringExtensions
77
{
8-
public static StringBuilder ReplaceFirstOccurrence(this StringBuilder sb, string search, string replace)
9-
{
10-
if (sb == null || string.IsNullOrEmpty(search))
11-
return sb;
12-
13-
int pos = sb.ToString().IndexOf(search, StringComparison.Ordinal);
14-
if (pos < 0)
15-
return sb;
16-
17-
sb.Remove(pos, search.Length);
18-
sb.Insert(pos, replace);
19-
20-
return sb;
21-
}
22-
238
public static string ReplaceFirstOccurrence(this string text, string search, string replace)
249
{
2510
int pos = text.IndexOf(search);

0 commit comments

Comments
 (0)