Skip to content

Commit 3cc0932

Browse files
author
Bernhard Millauer
committed
Added unit tests for case sensitivity;
1 parent 32a93ad commit 3cc0932

5 files changed

Lines changed: 191 additions & 22 deletions

File tree

src/SmartFormat.Tests/CodeProjectExampleTests.cs

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
using NUnit.Framework;
99
using SmartFormat.Core;
1010
using SmartFormat.Core.Extensions;
11+
using SmartFormat.Core.Settings;
1112
using SmartFormat.Extensions;
12-
13+
using SmartFormat.Tests.Utilities;
1314

1415
namespace SmartFormat.Tests
1516
{
@@ -18,16 +19,18 @@ public class CodeProjectExampleTests
1819
{
1920
private Person MakeQuentin()
2021
{
21-
var p = new Person() {
22-
FullName = "Quentin Starin",
23-
Birthday = DateTime.Today.AddYears(-30),
24-
Address = new Address("101 1st Ave", "Minneapolis", States.Minnesota, "55401"),
25-
Friends = new List<Person> {
26-
new Person() { FullName = "John Smith", Birthday = new DateTime(1978, 1, 1) },
27-
new Person() { FullName = "Bob Johnson", Birthday = new DateTime(1957, 1, 1) },
28-
new Person() { FullName = "Mary Meyer", Birthday = new DateTime(1990, 1, 1) },
29-
new Person() { FullName = "Dr. Jamal Cornucopia", Birthday = new DateTime(1973, 1, 1) }
30-
}
22+
var p = new Person()
23+
{
24+
FullName = "Quentin Starin",
25+
Birthday = DateTime.Today.AddYears(-30),
26+
Address = new Address("101 1st Ave", "Minneapolis", States.Minnesota, "55401"),
27+
Friends = new List<Person>
28+
{
29+
new Person() { FullName = "John Smith", Birthday = new DateTime(1978, 1, 1) },
30+
new Person() { FullName = "Bob Johnson", Birthday = new DateTime(1957, 1, 1) },
31+
new Person() { FullName = "Mary Meyer", Birthday = new DateTime(1990, 1, 1) },
32+
new Person() { FullName = "Dr. Jamal Cornucopia", Birthday = new DateTime(1973, 1, 1) }
33+
}
3134
};
3235

3336
return p;
@@ -86,11 +89,12 @@ public void BasicConditional()
8689
[Test]
8790
public void BasicArray()
8891
{
89-
var data = new DateTime[] {
90-
new DateTime(1999, 12, 31),
91-
new DateTime(2010, 10, 10),
92-
new DateTime(3000, 1, 1),
93-
};
92+
var data = new DateTime[]
93+
{
94+
new DateTime(1999, 12, 31),
95+
new DateTime(2010, 10, 10),
96+
new DateTime(3000, 1, 1),
97+
};
9498

9599
var formatString = "All dates: {0:{:M/d/yyyy}| and }.";
96100
var expectedOutput = "All dates: 12/31/1999 and 10/10/2010 and 1/1/3000.";
@@ -128,6 +132,25 @@ public void DotNotation()
128132
}
129133

130134

135+
[Test]
136+
public void DotNotation_CaseInsensitive()
137+
{
138+
using (
139+
new SmartSettingOverride(
140+
x => x.CaseSensitivity = CaseSensitivityType.CaseInsensitiv,
141+
x => x.CaseSensitivity = CaseSensitivityType.CaseSensitiv))
142+
{
143+
var p1 = MakeQuentin();
144+
145+
var formatString = "{aDDress.ciTy}, {Address.sTate} {Address.ZiP}";
146+
var expectedOutput = "Minneapolis, Minnesota 55401";
147+
148+
string actualOutput = Smart.Format(formatString, p1);
149+
Assert.AreEqual(expectedOutput, actualOutput);
150+
}
151+
}
152+
153+
131154
[Test]
132155
public void Nesting()
133156
{
@@ -153,6 +176,24 @@ public void NestingCollection()
153176
Assert.AreEqual(expectedOutput, actualOutput);
154177
}
155178

179+
[Test]
180+
public void NestingCollection_CaseInsensitive()
181+
{
182+
using (
183+
new SmartSettingOverride(
184+
x => x.CaseSensitivity = CaseSensitivityType.CaseInsensitiv,
185+
x => x.CaseSensitivity = CaseSensitivityType.CaseSensitiv))
186+
{
187+
Size[] sizes = { new Size(1, 1), new Size(4, 3), new Size(16, 9) };
188+
189+
var formatString = "{0:({widTh} x {hEIght})| and }";
190+
var expectedOutput = "(1 x 1) and (4 x 3) and (16 x 9)";
191+
192+
string actualOutput = Smart.Format(formatString, sizes);
193+
Assert.AreEqual(expectedOutput, actualOutput);
194+
}
195+
}
196+
156197

157198
[Test]
158199
public void Scope()
@@ -343,6 +384,25 @@ public void ComplexConditionFirstMatchingCase()
343384
}
344385

345386

387+
[Test]
388+
public void ComplexConditionFirstMatchingCase_CaseInsensitive()
389+
{
390+
using (
391+
new SmartSettingOverride(
392+
x => x.CaseSensitivity = CaseSensitivityType.CaseInsensitiv,
393+
x => x.CaseSensitivity = CaseSensitivityType.CaseSensitiv))
394+
{
395+
var p1 = new Person() { Birthday = DateTime.MinValue };
396+
397+
var formatString = "{aGe::>=55?Senior Citizen|>=30?Adult|>=18?Young Adult|>12?Teenager|>2?Child|Baby}";
398+
var expectedOutput = "Senior Citizen";
399+
400+
string actualOutput = Smart.Format(formatString, p1);
401+
Assert.AreEqual(expectedOutput, actualOutput);
402+
}
403+
}
404+
405+
346406
[Test]
347407
public void ComplexConditionFallThroughCase()
348408
{

src/SmartFormat.Tests/Extensions/DictionaryFormatterTests.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
using System.Linq;
55
using System.Text;
66
using NUnit.Framework;
7+
8+
using SmartFormat.Core.Settings;
79
using SmartFormat.Extensions;
10+
using SmartFormat.Tests.Utilities;
811

912
namespace SmartFormat.Tests
1013
{
@@ -41,6 +44,7 @@ public dynamic GetDynamicArgs()
4144
dynamic d = new ExpandoObject();
4245
d.Numbers = new Dictionary<string, object> { { "One", 1 }, { "Two", 2 }, { "Three", 3 }, };
4346
d.Letters = new Dictionary<string, object> { { "A", "a" }, { "B", "b" }, { "C", "c" }, };
47+
d.Raw = new System.Collections.Hashtable { { "X", "z" } };
4448
d.Object = new { Prop1 = "a", Prop2 = "b", Prop3 = "c", };
4549

4650
return new object[] {
@@ -73,15 +77,39 @@ public void Test_Dynamic()
7377
formatter.AddExtensions(new DictionarySource(formatter));
7478

7579
var formats = new string[] {
76-
"Chained: {0.Numbers.One} {Numbers.Two} {Letters.A} {Object.Prop1} ",
77-
"Nested: {0:{Numbers:{One} {Two}} } {Letters:{A}} {Object:{Prop1}} ", // Due to double-brace escaping, the spacing in this nested format is irregular
80+
"Chained: {0.Numbers.One} {Numbers.Two} {Letters.A} {Object.Prop1} {Raw.X} ",
81+
"Nested: {0:{Numbers:{One} {Two}} } {Letters:{A}} {Object:{Prop1}} {Raw:{X}} ", // Due to double-brace escaping, the spacing in this nested format is irregular
7882
};
7983
var expected = new string[] {
80-
"Chained: 1 2 a a ",
81-
"Nested: 1 2 a a ",
84+
"Chained: 1 2 a a z ",
85+
"Nested: 1 2 a a z ",
8286
};
8387
var args = (object[])GetDynamicArgs();
8488
formatter.Test(formats, args, expected);
8589
}
90+
91+
[Test]
92+
public void Test_Dynamic_CaseInsensitive()
93+
{
94+
using (
95+
new SmartSettingOverride(
96+
x => x.CaseSensitivity = CaseSensitivityType.CaseInsensitiv,
97+
x => x.CaseSensitivity = CaseSensitivityType.CaseSensitiv))
98+
{
99+
var formatter = Smart.CreateDefaultSmartFormat();
100+
formatter.AddExtensions(new DictionarySource(formatter));
101+
102+
var formats = new string[] {
103+
"Chained: {0.Numbers.One} {Numbers.Two} {Letters.A} {Object.Prop1} {Raw.x} ",
104+
"Nested: {0:{Numbers:{One} {Two}} } {Letters:{A}} {Object:{Prop1}} {Raw:{x}} ", // Due to double-brace escaping, the spacing in this nested format is irregular
105+
};
106+
var expected = new string[] {
107+
"Chained: 1 2 a a z ",
108+
"Nested: 1 2 a a z ",
109+
};
110+
var args = (object[])GetDynamicArgs();
111+
formatter.Test(formats, args, expected);
112+
}
113+
}
86114
}
87115
}

src/SmartFormat.Tests/Extensions/ReflectionFormatterTests.cs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using System.Text;
55
using NUnit.Framework;
66

7+
using SmartFormat.Core.Settings;
8+
using SmartFormat.Tests.Utilities;
9+
710
namespace SmartFormat.Tests
811
{
912
[TestFixture]
@@ -41,6 +44,31 @@ public void Test_Properties()
4144
Smart.Default.Test(formats, args, expected);
4245
}
4346

47+
[Test]
48+
public void Test_Properties_CaseInsensitive()
49+
{
50+
using (
51+
new SmartSettingOverride(
52+
x => x.CaseSensitivity = CaseSensitivityType.CaseInsensitiv,
53+
x => x.CaseSensitivity = CaseSensitivityType.CaseSensitiv))
54+
{
55+
var formats = new string[]
56+
{
57+
"{0} {0.lenGth} {length}", "{2.YEar} {2.MoNth:00}-{2.daY:00}", "{3.Value} {3.AnoN}",
58+
"Chained: {4.fIrstName} {4.Firstname.Length} {4.Address.City} {4.aDdress.StAte} ",
59+
"Nested: {4:{FirstName:{} {Length} }{Address:{City} {StaTe} } }",
60+
// Due to double-brace escaping, the spacing in this nested format is irregular
61+
};
62+
var expected = new string[]
63+
{
64+
"Zero 4 4", "2222 02-02", "3 True", "Chained: Michael 7 Scranton Pennsylvania ",
65+
"Nested: Michael 7 Scranton Pennsylvania ",
66+
};
67+
var args = GetArgs();
68+
Smart.Default.Test(formats, args, expected);
69+
}
70+
}
71+
4472
[Test]
4573
public void Test_Methods()
4674
{
@@ -54,21 +82,51 @@ public void Test_Methods()
5482
Smart.Default.Test(formats, args, expected);
5583
}
5684

85+
[Test]
86+
public void Test_Methods_CaseInsensitive()
87+
{
88+
using (
89+
new SmartSettingOverride(
90+
x => x.CaseSensitivity = CaseSensitivityType.CaseInsensitiv,
91+
x => x.CaseSensitivity = CaseSensitivityType.CaseSensitiv))
92+
{
93+
var formats = new string[] { "{0} {0.ToLower} {toloWer} {touPPer}", };
94+
var expected = new string[] { "Zero zero zero ZERO", };
95+
var args = GetArgs();
96+
Smart.Default.Test(formats, args, expected);
97+
}
98+
}
99+
57100
[Test]
58101
public void Test_Fields()
59102
{
60103
var formats = new string[] {
61-
"{Field}",
104+
"{Field}"
62105
};
63106
var expected = new string[] {
64-
"Field",
107+
"Field"
65108
};
66109
var args = new object[] {
67110
new MiscObject(),
68111
};
69112
Smart.Default.Test(formats, args, expected);
70113
}
71114

115+
[Test]
116+
public void Test_Fields_CaseInsensitive()
117+
{
118+
using (
119+
new SmartSettingOverride(
120+
x => x.CaseSensitivity = CaseSensitivityType.CaseInsensitiv,
121+
x => x.CaseSensitivity = CaseSensitivityType.CaseSensitiv))
122+
{
123+
var formats = new string[] { "{field}" };
124+
var expected = new string[] { "Field" };
125+
var args = new object[] { new MiscObject(), };
126+
Smart.Default.Test(formats, args, expected);
127+
}
128+
}
129+
72130
public class MiscObject
73131
{
74132
public MiscObject()

src/SmartFormat.Tests/SmartFormat.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Compile Include="Properties\AssemblyInfo.cs" />
7171
<Compile Include="TestUtils\TestHelpers.cs" />
7272
<Compile Include="Extensions\TimeFormatterTests.cs" />
73+
<Compile Include="Utilities\SmartSettingOverride.cs" />
7374
</ItemGroup>
7475
<ItemGroup>
7576
<ProjectReference Include="..\SmartFormat\SmartFormat.csproj">
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
using SmartFormat.Core.Settings;
4+
5+
namespace SmartFormat.Tests.Utilities
6+
{
7+
public class SmartSettingOverride : IDisposable
8+
{
9+
private readonly Action<SmartSettings> after;
10+
11+
public SmartSettingOverride(Action<SmartSettings> before, Action<SmartSettings> after)
12+
{
13+
this.after = after;
14+
before(Smart.Settings);
15+
}
16+
17+
public void Dispose()
18+
{
19+
after(Smart.Settings);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)