/
Formatting_arguments_collection_specs.cs
338 lines (295 loc) · 8.18 KB
/
Formatting_arguments_collection_specs.cs
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
namespace Formatting_arguments_collection_specs;
public class Can_be_constructed
{
[Test]
public void based_on_parent_collection()
{
var parent = new FormattingArgumentsCollection(TestCultures.nl)
{
{ typeof(Date), "yyyy-MM" },
};
var collection = new FormattingArgumentsCollection(TestCultures.nl_BE, parent);
collection.Should().ContainSingle();
}
}
public class Formats
{
[Test]
public void indexes_up_to_1_000_000()
{
var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE"));
var args = new object[1_000_001];
args[1_000_000] = "Test";
collection.Format("Begin {1000000} End", args).Should().Be("Begin Test End");
}
[Test]
public void with_allign_left()
{
var collection = new FormattingArgumentsCollection();
collection.Format("{0,-4}", "a").Should().Be("a ");
}
[Test]
public void with_allign_right()
{
var collection = new FormattingArgumentsCollection();
collection.Format("{0,3}", "a").Should().Be(" a");
}
[Test]
public void null_arguments()
{
var collection = new FormattingArgumentsCollection();
var args = new object?[] { null };
collection.Format("Value: '{0}'", args).Should().Be("Value: ''");
}
[TestCase("{{", "{")]
[TestCase("}}", "}")]
public void with_escaped_placehodlers(string format, string formatted)
{
var collection = new FormattingArgumentsCollection();
collection.Format(format).Should().Be(formatted);
}
[Test]
public void applying_specified_formats()
{
using (CultureInfoScope.NewInvariant())
{
var collection = new FormattingArgumentsCollection(new CultureInfo("nl-BE"))
{
{ typeof(Date), "yyyy-MM-dd HH:mm" },
{ typeof(decimal), "0.000" }
};
var formatted = collection.Format("{0:000.00} - {1} * {1:dd-MM-yyyy} - {2} - {3} - {4}", 3, new Date(2014, 10, 8), 666, 0.8m, 0.9);
formatted.Should().Be("003,00 - 2014-10-08 00:00 * 08-10-2014 - 666 - 0,800 - 0,9");
}
}
[Test]
public void applying_custom_format_provider()
{
using (CultureInfoScope.NewInvariant())
{
var collection = new FormattingArgumentsCollection(FormatProvider.CustomFormatter)
{
{ typeof(Date), "yyyy-MM-dd HH:mm" },
{ typeof(decimal), "0.000" }
};
var formattted = collection.Format("{0:yyyy-MM-dd} * {0}", new Date(2014, 10, 8));
formattted.Should().Be("Unit Test Formatter, value: '2014-10-08', format: 'yyyy-MM-dd' * Unit Test Formatter, value: '10/08/2014', format: ''");
}
}
}
public class Does_not_support_formats
{
private static readonly FormattingArgumentsCollection Collection = new(TestCultures.en_GB);
[Test]
public void with_length_not_being_a_number()
=> "{0,a}".Invoking(format => Collection.Format(format)).Should().Throw<FormatException>();
[Test]
public void with_index_not_being_a_number()
=> "{x}".Invoking(format => Collection.Format(format)).Should().Throw<FormatException>();
[Test]
public void with_index_not_being_negative()
=> "{-1}".Invoking(format => Collection.Format(format)).Should().Throw<FormatException>();
[TestCase("{")]
[TestCase("{0")]
[TestCase("}")]
public void with_mallformed_placeholders(string format)
=> format.Invoking(f => Collection.Format(f)).Should().Throw<FormatException>();
[Test]
public void indexes_beging_out_of_range()
=> "{0}{1}".Invoking(format => Collection.Format(format, 1))
.Should()
.Throw<FormatException>()
.WithMessage("Index (zero based) must be greater than or equal to zero and less than the size of the argument list.");
}
public class Stringifies
{
[Test]
public void IFormattable_null_as_null()
{
IFormattable? obj = null;
var collection = new FormattingArgumentsCollection();
collection.ToString(obj).Should().BeNull();
}
[Test]
public void object_null_as_null()
{
var collection = new FormattingArgumentsCollection();
collection.ToString((object?)null).Should().BeNull();
}
[Test]
public void object_as_ToString_value()
{
var collection = new FormattingArgumentsCollection();
collection.ToString(typeof(int)).Should().Be("System.Int32");
}
[Test]
public void appying_peferred_formats()
{
var collection = new FormattingArgumentsCollection
{
{ typeof(int), "000" }
};
collection.ToString((object)7).Should().Be("007");
}
}
public class Can_be_modified
{
public class Adding
{
[Test]
public void format()
{
var collection = new FormattingArgumentsCollection();
collection.Should().BeEmpty();
collection.Add(typeof(int), "Int32Format");
collection.Should().ContainSingle();
}
[Test]
public void IFormatProvider()
{
var collection = new FormattingArgumentsCollection();
collection.Should().BeEmpty();
collection.Add(typeof(int), new CultureInfo("nl-NL"));
collection.Should().ContainSingle();
}
[Test]
public void format_with_IFormatProvider()
{
var collection = new FormattingArgumentsCollection();
collection.Should().BeEmpty();
collection.Add(typeof(int), "Int32Format", new CultureInfo("nl-NL"));
collection.Should().ContainSingle();
}
}
public class Setting
{
[Test]
public void format()
{
var collection = new FormattingArgumentsCollection();
collection.Should().BeEmpty();
collection.Set(typeof(int), "Int32Format");
collection.Should().ContainSingle();
}
[Test]
public void IFormatProvider()
{
var collection = new FormattingArgumentsCollection();
collection.Should().BeEmpty();
collection.Set(typeof(int), new CultureInfo("nl-NL"));
collection.Should().ContainSingle();
}
[Test]
public void format_with_IFormatProvider()
{
var collection = new FormattingArgumentsCollection();
collection.Should().BeEmpty();
collection.Set(typeof(int), "Int32Format", new CultureInfo("nl-NL"));
collection.Should().ContainSingle();
}
[Test]
public void same_key_twice()
{
var collection = new FormattingArgumentsCollection();
collection.Set(typeof(int), "New");
collection.Set(typeof(int), "Update");
collection.Should().ContainSingle();
}
}
public class Removing
{
[Test]
public void non_cotained_key()
{
var collection = new FormattingArgumentsCollection();
collection.Remove(typeof(int)).Should().BeFalse();
}
[Test]
public void contained_key()
{
var collection = new FormattingArgumentsCollection
{
{ typeof(int), "Int32Format" },
{ typeof(long), "Int64Format" },
};
collection.Remove(typeof(int)).Should().BeTrue();
collection.Should().ContainSingle();
}
}
[Test]
public void Clear_CollectionWithTwoItems_0Items()
{
var collection = new FormattingArgumentsCollection
{
{ typeof(int), "Int32Format" },
{ typeof(Date), "Date" }
};
collection.Clear();
var act = collection.Count;
var exp = 0;
act.Should().Be(exp);
}
}
public class Can_be_queried
{
[Test]
public void on_types()
{
var collection = new FormattingArgumentsCollection
{
{ typeof(int), "Int32Format" },
{ typeof(Date), "Date" }
};
var act = collection.Types;
var exp = new[] { typeof(int), typeof(Date) };
act.Should().BeEquivalentTo(exp);
}
[Test]
public void on_types_contained()
{
var collection = new FormattingArgumentsCollection
{
{ typeof(int), "00" }
};
collection.Contains(typeof(int)).Should().BeTrue();
}
[Test]
public void on_types_not_contained()
{
var collection = new FormattingArgumentsCollection();
collection.Contains(typeof(int)).Should().BeFalse();
}
[Test]
public void via_enumerator()
{
var collection = new FormattingArgumentsCollection
{
{ typeof(int), "Int32Format" },
{ typeof(Date), "Date" }
};
var ienumerable = collection as IEnumerable<KeyValuePair<Type, FormattingArguments>>;
ienumerable.Should().HaveCount(2);
}
}
public class Does_not_support
{
private static readonly FormattingArgumentsCollection Collection = new(TestCultures.en_GB);
[Test]
public void types_who_are_not_formattable()
=> Collection.Invoking(c => c.Add(typeof(Type), ""))
.Should()
.Throw<ArgumentException>()
.WithMessage("The argument must implement System.IFormattable.*");
[Test]
public void same_key_twice()
{
var collection = new FormattingArgumentsCollection
{
{ typeof(int), "New" }
};
collection.Invoking(c => c.Add(typeof(int), "Update"))
.Should()
.Throw<ArgumentException>()
.WithMessage("An item with the same key has already been added.*");
}
}