Skip to content

Commit eec91e6

Browse files
committed
chore: added encoding string formattings
ToMD5 ToBase64 FromBase64
1 parent 05354c2 commit eec91e6

8 files changed

Lines changed: 570 additions & 949 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using ObjectSemantics.NET.Tests.MoqModels;
2+
using System.Collections.Generic;
3+
using Xunit;
4+
5+
namespace ObjectSemantics.NET.Tests
6+
{
7+
public class BasicMappingTests
8+
{
9+
[Fact]
10+
public void Should_Map_Object_To_Template_From_TemplateObject()
11+
{
12+
//Create Model
13+
Student student = new Student
14+
{
15+
StudentName = "George Waynne",
16+
Balance = 2510
17+
};
18+
var template = new ObjectSemanticsTemplate
19+
{
20+
FileContents = @"My Name is: {{ StudentName }}"
21+
};
22+
string generatedTemplate = TemplateMapper.Map(student, template);
23+
string expectedString = "My Name is: George Waynne";
24+
Assert.Equal(expectedString, generatedTemplate, false, true, true);
25+
}
26+
27+
28+
[Fact]
29+
public void Should_Map_Additional_Parameters()
30+
{
31+
//Create Model
32+
Student student = new Student
33+
{
34+
StudentName = "George Waynne"
35+
};
36+
//Template
37+
var template = new ObjectSemanticsTemplate
38+
{
39+
FileContents = @"My Name is: {{ StudentName }} | CompanyName: {{ CompanyName }} | CompanyEmail: {{ CompanyEmail }} | Employees: {{ Employees }}"
40+
};
41+
//Additional Parameters
42+
List<ObjectSemanticsKeyValue> additionalParams = new List<ObjectSemanticsKeyValue>
43+
{
44+
new ObjectSemanticsKeyValue{ Key ="CompanyName", Value= "TEST INC." },
45+
new ObjectSemanticsKeyValue{ Key ="CompanyEmail", Value= "test.inc@test.com" },
46+
new ObjectSemanticsKeyValue{ Key ="Employees", Value= 1289 },
47+
};
48+
string generatedTemplate = TemplateMapper.Map(student, template, additionalParams);
49+
string expectedString = "My Name is: George Waynne | CompanyName: TEST INC. | CompanyEmail: test.inc@test.com | Employees: 1289";
50+
Assert.Equal(expectedString, generatedTemplate, false, true, true);
51+
}
52+
53+
54+
[Fact]
55+
public void Should_Return_Unknown_Properties_If_Not_Found_In_Object()
56+
{
57+
//Create Model
58+
Student student = new Student
59+
{
60+
StudentName = "George Waynne"
61+
};
62+
//Template
63+
var template = new ObjectSemanticsTemplate
64+
{
65+
FileContents = @"Unknown Object example: {{StudentIdentityCardXyx}}"
66+
};
67+
string generatedTemplate = TemplateMapper.Map(student, template);
68+
string expectedString = "Unknown Object example: {{ StudentIdentityCardXyx }}";
69+
Assert.Equal(expectedString, generatedTemplate, false, true, true);
70+
}
71+
72+
73+
[Fact]
74+
public void Should_Ignore_Whitespaces_Inside_CurlyBrackets()
75+
{
76+
//Create Model
77+
Student student = new Student
78+
{
79+
StudentName = "George Waynne"
80+
};
81+
//Template
82+
var template = new ObjectSemanticsTemplate
83+
{
84+
FileContents = @"StudentName is: {{ StudentName }}"
85+
};
86+
string generatedTemplate = TemplateMapper.Map(student, template);
87+
string expectedString = "StudentName is: George Waynne";
88+
Assert.Equal(expectedString, generatedTemplate, false, true, true);
89+
}
90+
91+
}
92+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using ObjectSemantics.NET.Tests.MoqModels;
2+
using System.Collections.Generic;
3+
using Xunit;
4+
5+
namespace ObjectSemantics.NET.Tests
6+
{
7+
public class CharacterEscapingTests
8+
{
9+
10+
[Fact]
11+
public void Should_Escape_Xml_Char_If_Option_Is_Enabled()
12+
{
13+
//Create Model
14+
Student student = new Student { StudentName = "I've got \"special\" < & also >" };
15+
var template = new ObjectSemanticsTemplate
16+
{
17+
FileContents = @"{{ StudentName }}"
18+
};
19+
string generatedTemplate = TemplateMapper.Map(student, template, null, new TemplateMapperOptions
20+
{
21+
XmlCharEscaping = true
22+
});
23+
string expectedString = "I&apos;ve got &quot;special&quot; &lt; &amp; also &gt;";
24+
Assert.Equal(expectedString, generatedTemplate, false, true, true);
25+
}
26+
27+
[Fact]
28+
public void Should_Escape_Xml_Char_In_LOOPS_If_Option_Is_Enabled()
29+
{
30+
//Create Model
31+
Student student = new Student
32+
{
33+
Invoices = new List<Invoice>
34+
{
35+
new Invoice{ Narration="I've got \"special\""},
36+
new Invoice{ Narration="I've got < & also >"}
37+
}
38+
};
39+
//Template
40+
var template = new ObjectSemanticsTemplate
41+
{
42+
FileContents = @"{{ #foreach(invoices) }} [{{ Narration }}] {{ #endforeach }}"
43+
};
44+
string generatedTemplate = TemplateMapper.Map(student, template, null, new TemplateMapperOptions
45+
{
46+
XmlCharEscaping = true
47+
});
48+
string expectedResult = @" [I&apos;ve got &quot;special&quot;] [I&apos;ve got &lt; &amp; also &gt;] ";
49+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
50+
}
51+
}
52+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
using ObjectSemantics.NET.Tests.MoqModels;
2+
using System;
3+
using System.Collections.Generic;
4+
using Xunit;
5+
6+
namespace ObjectSemantics.NET.Tests
7+
{
8+
public class LoopFunctionTests
9+
{
10+
11+
[Fact]
12+
public void Should_Map_Enumerable_Collection_In_Object()
13+
{
14+
//Create Model
15+
Student student = new Student
16+
{
17+
StudentName = "John Doe",
18+
Invoices = new List<Invoice>
19+
{
20+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
21+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
22+
}
23+
};
24+
//Template
25+
var template = new ObjectSemanticsTemplate
26+
{
27+
FileContents = @"{{ StudentName }} Invoices
28+
{{ #foreach(invoices) }}
29+
<tr>
30+
<td>{{ Id }}</td>
31+
<td>{{ RefNo }}</td>
32+
<td>{{ Narration }}</td>
33+
<td>{{ Amount:N0 }}</td>
34+
<td>{{ InvoiceDate:yyyy-MM-dd }}</td>
35+
</tr>
36+
{{ #endforeach }}"
37+
};
38+
string generatedTemplate = TemplateMapper.Map(student, template);
39+
string expectedResult = @"John Doe Invoices
40+
41+
<tr>
42+
<td>2</td>
43+
<td>INV_002</td>
44+
<td>Grade II Fees Invoice</td>
45+
<td>2,000</td>
46+
<td>2023-04-01</td>
47+
</tr>
48+
49+
<tr>
50+
<td>1</td>
51+
<td>INV_001</td>
52+
<td>Grade I Fees Invoice</td>
53+
<td>320</td>
54+
<td>2022-08-01</td>
55+
</tr>";
56+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
57+
}
58+
59+
60+
[Fact]
61+
public void Should_Map_Enumerable_Collection_SingleLine_Test()
62+
{
63+
//Create Model
64+
Student student = new Student
65+
{
66+
StudentName = "John Doe",
67+
Invoices = new List<Invoice>
68+
{
69+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
70+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
71+
}
72+
};
73+
//Template
74+
var template = new ObjectSemanticsTemplate
75+
{
76+
FileContents = @"{{ #foreach(invoices) }} [{{RefNo}}] {{ #endforeach }}"
77+
};
78+
string generatedTemplate = TemplateMapper.Map(student, template);
79+
string expectedResult = " [INV_002] [INV_001] ";
80+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
81+
}
82+
83+
84+
[Fact]
85+
public void Should_Map_Multiple_Same_Property_Enumerable_Collection_On_Same_Template()
86+
{
87+
//Create Model
88+
Student student = new Student
89+
{
90+
StudentName = "John Doe",
91+
Invoices = new List<Invoice>
92+
{
93+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
94+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
95+
}
96+
};
97+
//Template
98+
var template = new ObjectSemanticsTemplate
99+
{
100+
FileContents = @"
101+
{{ StudentName }} Invoices
102+
LOOP #1
103+
{{ #foreach(invoices) }}
104+
<h5>{{ Id }} On Loop #1</h5>
105+
{{ #endforeach }}
106+
LOOP #2
107+
{{ #foreach(invoices) }}
108+
<h5>{{ Id }} On Loop #2</h5>
109+
{{ #endforeach }}"
110+
};
111+
string generatedTemplate = TemplateMapper.Map(student, template);
112+
string expectedResult = @"
113+
John Doe Invoices
114+
LOOP #1
115+
116+
<h5>2 On Loop #1</h5>
117+
118+
<h5>1 On Loop #1</h5>
119+
LOOP #2
120+
121+
<h5>2 On Loop #2</h5>
122+
123+
<h5>1 On Loop #2</h5>";
124+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
125+
}
126+
127+
128+
[Fact]
129+
public void Should_Map_Multiple_Different_Property_Enumerable_Collection_On_Same_Template()
130+
{
131+
//Create Model
132+
Student student = new Student
133+
{
134+
StudentName = "John Doe",
135+
Invoices = new List<Invoice>
136+
{
137+
new Invoice{ Id=2, RefNo="INV_002",Narration="Grade II Fees Invoice", Amount=2000, InvoiceDate= new DateTime(2023, 04, 01) },
138+
new Invoice{ Id=1, RefNo="INV_001",Narration="Grade I Fees Invoice", Amount=320, InvoiceDate= new DateTime(2022, 08, 01) }
139+
},
140+
StudentClockInDetails = new List<StudentClockInDetail>
141+
{
142+
new StudentClockInDetail{ LastClockedInDate = new DateTime(2024, 04, 01), LastClockedInPoints = 10 },
143+
new StudentClockInDetail{ LastClockedInDate = new DateTime(2024, 04, 02), LastClockedInPoints = 30 }
144+
}
145+
};
146+
//Template
147+
var template = new ObjectSemanticsTemplate
148+
{
149+
FileContents = @"
150+
{{ StudentName }} Invoices
151+
LOOP #1
152+
{{ #foreach(invoices) }}
153+
<h5>{{ Id }} On Loop #1</h5>
154+
{{ #endforeach }}
155+
LOOP #2
156+
{{ #foreach(studentClockInDetails) }}
157+
<h5>Got {{ LastClockedInPoints }} for {{ LastClockedInDate:yyyy-MM-dd }}</h5>
158+
{{ #endforeach }}"
159+
};
160+
string generatedTemplate = TemplateMapper.Map(student, template);
161+
string expectedResult = @"
162+
John Doe Invoices
163+
LOOP #1
164+
165+
<h5>2 On Loop #1</h5>
166+
167+
<h5>1 On Loop #1</h5>
168+
LOOP #2
169+
170+
<h5>Got 10 for 2024-04-01</h5>
171+
172+
<h5>Got 30 for 2024-04-02</h5>";
173+
174+
Assert.Equal(expectedResult, generatedTemplate, false, true, true);
175+
}
176+
177+
}
178+
}

0 commit comments

Comments
 (0)