Repository navigation
Expand file tree
/
Copy pathCollectionHumanizeTests.cs
More file actions
160 lines (132 loc) · 5.22 KB
/
Copy pathCollectionHumanizeTests.cs
File metadata and controls
160 lines (132 loc) · 5.22 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
public class SomeClass
{
public string? SomeString;
public int SomeInt;
public override string ToString() =>
"ToString";
}
[UseCulture("en")]
public class CollectionHumanizeTests
{
static readonly object?[] NullObjects = [null, null];
static readonly int[] CollectionPair = [1, 2];
static readonly int[] CollectionTriple = [1, 2, 3];
static readonly string[] ThreeStrings = ["A", "B", "C"];
static readonly int?[] NullableInts = [1, null, 3];
static readonly string[] StringsWithEmptyItem = ["A", " ", "C"];
static readonly string[] StringsWithWhitespace = ["A", " B ", "C"];
[Fact]
public void HumanizeReturnsOnlyNameWhenCollectionContainsOneItem()
{
var collection = new List<string> { "A String" };
Assert.Equal("A String", collection.Humanize());
}
[Fact]
public void HumanizeUsesSeparatorWhenMoreThanOneItemIsInCollection()
{
var collection = new List<string>
{
"A String",
"Another String",
};
Assert.Equal("A String or Another String", collection.Humanize("or"));
}
[Fact]
public void HumanizeDefaultsSeparatorToAnd()
{
var collection = new List<string>
{
"A String",
"Another String",
};
Assert.Equal("A String and Another String", collection.Humanize());
}
[Fact]
public void HumanizeUsesSpecifiedCulture() =>
Assert.Equal("A, B and C", ThreeStrings.Humanize(new CultureInfo("en-GB")));
[Theory]
[MemberData(
nameof(Humanizer.Tests.Localisation.LocaleCoverageData.CollectionFormatterExpectationTheoryData),
MemberType = typeof(Humanizer.Tests.Localisation.LocaleCoverageData))]
public void HumanizeUsesSpecifiedCultureForEverySupportedLocale(string localeName, string expectedTwo, string expectedThree)
{
var culture = new CultureInfo(localeName);
Assert.Equal(expectedTwo, CollectionPair.Humanize(culture));
Assert.Equal(expectedThree, CollectionTriple.Humanize(culture));
}
[Fact]
public void HumanizeThrowsWhenCultureIsNull()
{
var exception = Assert.Throws<ArgumentNullException>(() => ThreeStrings.Humanize((CultureInfo)null!));
Assert.Equal("culture", exception.ParamName);
}
[Fact]
public void HumanizeUsesOxfordComma()
{
var collection = new List<string>
{
"A String",
"Another String",
"A Third String",
};
Assert.Equal("A String, Another String, or A Third String", collection.Humanize("or"));
}
readonly List<SomeClass> testCollection =
[
new() { SomeInt = 1, SomeString = "One" },
new() { SomeInt = 2, SomeString = "Two" },
new() { SomeInt = 3, SomeString = "Three" }
];
[Fact]
public void HumanizeDefaultsToToString() =>
Assert.Equal("ToString, ToString, or ToString", testCollection.Humanize("or"));
[Fact]
public void HumanizeUsesStringDisplayFormatter()
{
var humanized = testCollection.Humanize(sc => $"SomeObject #{sc.SomeInt} - {sc.SomeString}");
Assert.Equal("SomeObject #1 - One, SomeObject #2 - Two, and SomeObject #3 - Three", humanized);
}
[Fact]
public void HumanizeUsesObjectDisplayFormatter()
{
var humanized = testCollection.Humanize(sc => sc.SomeInt);
Assert.Equal("1, 2, and 3", humanized);
}
[Fact]
public void HumanizeUsesStringDisplayFormatterWhenSeparatorIsProvided()
{
var humanized = testCollection.Humanize(sc => $"SomeObject #{sc.SomeInt} - {sc.SomeString}", "or");
Assert.Equal("SomeObject #1 - One, SomeObject #2 - Two, or SomeObject #3 - Three", humanized);
}
[Fact]
public void HumanizeUsesObjectDisplayFormatterWhenSeparatorIsProvided()
{
var humanized = testCollection.Humanize(sc => sc.SomeInt, "or");
Assert.Equal("1, 2, or 3", humanized);
}
[Fact]
public void HumanizeHandlesNullItemsWithoutAnException() =>
Assert.Null(Record.Exception(() => NullObjects.Humanize()));
[Fact]
public void HumanizeHandlesNullStringDisplayFormatterReturnsWithoutAnException() =>
Assert.Null(Record.Exception(() => ThreeStrings.Humanize(_ => null!)));
[Fact]
public void HumanizeHandlesNullObjectDisplayFormatterReturnsWithoutAnException() =>
Assert.Null(Record.Exception(() => ThreeStrings.Humanize(_ => (object)null!)));
[Fact]
public void HumanizeRunsStringDisplayFormatterOnNulls() =>
Assert.Equal("1, (null), and 3", NullableInts.Humanize(_ => _?.ToString() ?? "(null)"));
[Fact]
public void HumanizeRunsObjectDisplayFormatterOnNulls() =>
Assert.Equal("1, 2, and 3", NullableInts.Humanize(_ => _ ?? 2));
[Fact]
public void HumanizeRemovesEmptyItemsByDefault() =>
Assert.Equal("A and C", StringsWithEmptyItem.Humanize(DummyFormatter));
[Fact]
public void HumanizeTrimsItemsByDefault() =>
Assert.Equal("A, B, and C", StringsWithWhitespace.Humanize(DummyFormatter));
/// <summary>
/// Use the dummy formatter to ensure tests are testing formatter output rather than input
/// </summary>
static readonly Func<string, string> DummyFormatter = input => input;
}