Skip to content

Commit 2a709ef

Browse files
committed
chore: more real-life unit tests scenarios
1 parent 88218f7 commit 2a709ef

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using ObjectSemantics.NET.Tests.MoqModels;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using Xunit;
5+
6+
namespace ObjectSemantics.NET.Tests
7+
{
8+
public class PromotionAndMessagingTemplateTests
9+
{
10+
[Theory]
11+
[InlineData("A", "Special offer for Jane Doe")]
12+
[InlineData("B", "Do not miss out Jane Doe")]
13+
public void Should_Render_Ab_Test_Subject_Line(string campaignVariant, string expected)
14+
{
15+
OrderEmailModel order = new OrderEmailModel
16+
{
17+
Customer = new OrderCustomer
18+
{
19+
FullName = "jane doe"
20+
}
21+
};
22+
23+
var extra = new Dictionary<string, object>
24+
{
25+
["CampaignVariant"] = campaignVariant
26+
};
27+
28+
string template = "{{ #if(CampaignVariant == A) }}Special offer for {{ Customer.FullName:titlecase }}{{ #else }}Do not miss out {{ Customer.FullName:titlecase }}{{ #endif }}";
29+
string result = order.Map(template, extra);
30+
31+
Assert.Equal(expected, result);
32+
}
33+
34+
[Fact]
35+
public void Should_Use_Generic_Greeting_When_Personalized_Name_Is_Null()
36+
{
37+
OrderEmailModel order = new OrderEmailModel
38+
{
39+
Customer = new OrderCustomer
40+
{
41+
FullName = null
42+
}
43+
};
44+
45+
string template = "{{ #if(Customer.FullName == null) }}Hello Customer{{ #else }}Hello {{ Customer.FullName:titlecase }}{{ #endif }}";
46+
string result = order.Map(template);
47+
48+
Assert.Equal("Hello Customer", result);
49+
}
50+
51+
[Theory]
52+
[InlineData("EMAIL", "Compliance: Unsubscribe via https://example.com/unsub/u-100")]
53+
[InlineData("SMS", "Compliance: Reply STOP to unsubscribe")]
54+
public void Should_Render_Channel_Specific_Compliance_Block(string channel, string expected)
55+
{
56+
OrderEmailModel order = new OrderEmailModel();
57+
var extra = new Dictionary<string, object>
58+
{
59+
["Meta.Channel"] = channel,
60+
["Meta.UnsubscribeUrl"] = "https://example.com/unsub/u-100"
61+
};
62+
63+
string template = "Compliance: {{ #if(Meta.Channel == EMAIL) }}Unsubscribe via {{ Meta.UnsubscribeUrl }}{{ #else }}Reply STOP to unsubscribe{{ #endif }}";
64+
string result = order.Map(template, extra);
65+
66+
Assert.Equal(expected, result);
67+
}
68+
69+
[Fact]
70+
public void Should_Map_Bulk_Message_Batch_Deterministically()
71+
{
72+
string template = "{{ #if(CampaignVariant == A) }}[A]{{ #else }}[B]{{ #endif }} {{ #if(Customer.FullName == null) }}Hello Customer{{ #else }}Hello {{ Customer.FullName:titlecase }}{{ #endif }} - {{ OrderNo }}";
73+
74+
for (int i = 1; i <= 250; i++)
75+
{
76+
OrderEmailModel order = new OrderEmailModel
77+
{
78+
OrderNo = "ORD-" + i.ToString("000", CultureInfo.InvariantCulture),
79+
Customer = new OrderCustomer
80+
{
81+
FullName = i % 5 == 0 ? null : "customer " + i.ToString(CultureInfo.InvariantCulture)
82+
}
83+
};
84+
85+
var extra = new Dictionary<string, object>
86+
{
87+
["CampaignVariant"] = i % 2 == 0 ? "A" : "B"
88+
};
89+
90+
string result = order.Map(template, extra);
91+
string expectedPrefix = i % 2 == 0 ? "[A]" : "[B]";
92+
string expectedName = i % 5 == 0 ? "Hello Customer" : "Hello Customer " + i.ToString(CultureInfo.InvariantCulture);
93+
string expected = expectedPrefix + " " + expectedName + " - ORD-" + i.ToString("000", CultureInfo.InvariantCulture);
94+
95+
Assert.Equal(expected, result);
96+
Assert.DoesNotContain("{{", result);
97+
}
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)