Repository navigation
Expand file tree
/
Copy pathDataMergeEngineTests.cs
More file actions
251 lines (210 loc) · 8.38 KB
/
Copy pathDataMergeEngineTests.cs
File metadata and controls
251 lines (210 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.Globalization;
using PdfEditorApp.Core.Data;
using PdfEditorApp.Core.Models;
using PdfEditorApp.Core.Models.Elements;
using PdfEditorApp.Models;
using Xunit;
namespace PdfEditorApp.Tests;
public class DataMergeEngineTests
{
private readonly IDataMergeEngine _engine = new DataMergeEngine();
[Fact]
public void EvaluateText_SimplePlaceholder_ReplacesValue()
{
var record = new Dictionary<string, string>
{
["EmployeeName"] = "Johnathan Doe",
["EmployeeId"] = "EMP-0842"
};
string template = "Employee: {{EmployeeName}} (ID: {{EmployeeId}})";
string result = _engine.EvaluateText(template, record);
Assert.Equal("Employee: Johnathan Doe (ID: EMP-0842)", result);
}
[Fact]
public void EvaluateText_CaseInsensitiveLookup_MatchesDifferentCasing()
{
var record = new Dictionary<string, string>
{
["EMPLOYEE_NAME"] = "Sophia Martinez"
};
string template = "Hello, {{EmployeeName}}!";
string result = _engine.EvaluateText(template, record, new DataMergeOptions { CaseInsensitiveLookup = true });
Assert.Equal("Hello, Sophia Martinez!", result);
}
[Fact]
public void EvaluateText_FallbackValue_UsedWhenKeyIsMissingOrEmpty()
{
var record = new Dictionary<string, string>
{
["EmployeeName"] = "David Kim"
};
string template = "Dept: {{Department ?? Engineering}}, Level: {{Level ?? L5}}";
string result = _engine.EvaluateText(template, record);
Assert.Equal("Dept: Engineering, Level: L5", result);
}
[Fact]
public void EvaluateText_CurrencyFormatting_FormatsCorrectly()
{
var record = new Dictionary<string, string>
{
["BasicSalary"] = "8500",
["InrSalary"] = "125000",
["EurSalary"] = "6200",
["UsdSalary"] = "10500"
};
string templateInr = "Salary: {{InrSalary:currency:INR}}";
string templateEur = "Salary: {{EurSalary:currency:EUR}}";
string templateUsd = "Salary: {{UsdSalary:currency:USD}}";
string resultInr = _engine.EvaluateText(templateInr, record);
string resultEur = _engine.EvaluateText(templateEur, record);
string resultUsd = _engine.EvaluateText(templateUsd, record);
Assert.Contains("₹1,25,000.00", resultInr);
Assert.Contains("€6,200.00", resultEur);
Assert.Contains("$10,500.00", resultUsd);
}
[Theory]
[InlineData("en-US")]
[InlineData("en-GB")]
[InlineData("de-DE")]
[InlineData("fr-FR")]
[InlineData("ja-JP")]
public void EvaluateText_CurrencyFormatting_IsCultureInvariantAcrossLocales(string cultureName)
{
var prevCulture = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = new CultureInfo(cultureName);
var record = new Dictionary<string, string>
{
["InrSalary"] = "125000",
["EurSalary"] = "6200"
};
string resultInr = _engine.EvaluateText("Salary: {{InrSalary:currency:INR}}", record);
string resultEur = _engine.EvaluateText("Salary: {{EurSalary:currency:EUR}}", record);
Assert.Contains("₹1,25,000.00", resultInr);
Assert.Contains("€6,200.00", resultEur);
}
finally
{
CultureInfo.CurrentCulture = prevCulture;
}
}
[Fact]
public void EvaluateText_NumericAndDateFormatting_FormatsCorrectly()
{
var record = new Dictionary<string, string>
{
["GrossAmount"] = "12345.6789",
["JoiningDate"] = "2022-04-15"
};
string templateNum = "Amount: {{GrossAmount:N2}}";
string templateDate = "Joined: {{JoiningDate:yyyy/MM/dd}}";
string resultNum = _engine.EvaluateText(templateNum, record);
string resultDate = _engine.EvaluateText(templateDate, record);
Assert.Equal("Amount: 12,345.68", resultNum);
Assert.Equal("Joined: 2022/04/15", resultDate);
}
[Fact]
public void EvaluateText_CaseTransforms_UppercaseLowercaseTitleCase()
{
var record = new Dictionary<string, string>
{
["Designation"] = "senior cloud engineer"
};
string templateUpper = "{{Designation:upper}}";
string templateTitle = "{{Designation:title}}";
Assert.Equal("SENIOR CLOUD ENGINEER", _engine.EvaluateText(templateUpper, record));
Assert.Equal("Senior Cloud Engineer", _engine.EvaluateText(templateTitle, record));
}
[Fact]
public void DetectPlaceholders_ExtractsAllTagsAcrossDocumentElements()
{
var doc = new PdfDocumentModel
{
Title = "Payslip for {{EmployeeName}}",
Author = "{{Company}}"
};
var page = new PdfPageModel
{
HeaderLeft = "Confidential - {{Department}}",
FooterRight = "Generated for {{EmployeeId}}",
Watermark = new PdfWatermarkElement { Text = "{{WatermarkStatus}}" },
Elements = new List<PdfElementBase>
{
new PdfTextElement { Text = "Gross: {{GrossSalary:C}}" },
new PdfQrCodeElement { Content = "https://verify.com?id={{EmployeeId}}" },
new PdfBarcodeElement { CodeValue = "{{BarcodeValue}}" },
new PdfTableElement
{
Headers = new List<string> { "{{Header1}}", "Amount" },
Rows = new List<List<string>>
{
new() { "Basic", "{{BasicSalary}}" }
}
}
}
};
doc.Pages.Add(page);
var detected = _engine.DetectPlaceholders(doc);
Assert.Contains("EmployeeName", detected);
Assert.Contains("Company", detected);
Assert.Contains("Department", detected);
Assert.Contains("EmployeeId", detected);
Assert.Contains("WatermarkStatus", detected);
Assert.Contains("GrossSalary", detected);
Assert.Contains("BarcodeValue", detected);
Assert.Contains("Header1", detected);
Assert.Contains("BasicSalary", detected);
}
[Fact]
public void HydrateDocument_PopulatesAllElementsInClonedDocument()
{
var doc = new PdfDocumentModel
{
Title = "Payslip - {{EmployeeName}}"
};
var page = new PdfPageModel
{
HeaderLeft = "Dept: {{Department}}",
Elements = new List<PdfElementBase>
{
new PdfTextElement { Text = "Welcome {{EmployeeName}}, Salary: {{Salary:currency:INR}}" },
new PdfQrCodeElement { Content = "https://verify.corp.com/emp/{{EmployeeId}}" },
new PdfBarcodeElement { CodeValue = "{{EmployeeId}}" },
new PdfTableElement
{
Headers = new List<string> { "Item", "Value" },
Rows = new List<List<string>>
{
new() { "Base", "{{Salary:currency:INR}}" }
}
}
}
};
doc.Pages.Add(page);
var record = new Dictionary<string, string>
{
["EmployeeName"] = "Amina Al-Mansoor",
["Department"] = "Human Resources",
["EmployeeId"] = "EMP-1315",
["Salary"] = "92000"
};
var hydrated = _engine.HydrateDocument(doc, record);
// Original template remains untouched
Assert.Equal("Payslip - {{EmployeeName}}", doc.Title);
// Hydrated clone has values replaced
Assert.Equal("Payslip - Amina Al-Mansoor", hydrated.Title);
Assert.Equal("Dept: Human Resources", hydrated.Pages[0].HeaderLeft);
var textEl = (PdfTextElement)hydrated.Pages[0].Elements[0];
Assert.Contains("Welcome Amina Al-Mansoor", textEl.Text);
Assert.Contains("₹92,000.00", textEl.Text);
var qrEl = (PdfQrCodeElement)hydrated.Pages[0].Elements[1];
Assert.Equal("https://verify.corp.com/emp/EMP-1315", qrEl.Content);
var barEl = (PdfBarcodeElement)hydrated.Pages[0].Elements[2];
Assert.Equal("EMP-1315", barEl.CodeValue);
var tableEl = (PdfTableElement)hydrated.Pages[0].Elements[3];
Assert.Equal("₹92,000.00", tableEl.Rows[0][1]);
}
}