-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathMultipartIdentifierTests.cs
More file actions
382 lines (349 loc) · 17 KB
/
MultipartIdentifierTests.cs
File metadata and controls
382 lines (349 loc) · 17 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Xunit;
namespace Microsoft.Data.Common.UnitTests;
public class MultipartIdentifierTests
{
/// <summary>
/// Gets a collection of test data containing various part identifier strings and their expected
/// parse results.
/// </summary>
/// <remarks>
/// The returned data includes different combinations of part identifier formats, such as
/// those with embedded whitespace, leading or trailing whitespace, and bracket characters.
/// </remarks>
/// <see cref="SinglePartIdentifierWithBracketsAndWhiteSpace_ParsesCorrectly" />
public static TheoryData<string, string[]> ValidSinglePartIdentifierVariations
{
get
{
ReadOnlySpan<string> part1Words = ["word1", "word 1"];
TheoryData<string, string[]> data = [];
HashSet<string> seen = [];
// Combination 1: embedded and non-embedded whitespace.
// Combination 2: leading and/or trailing whitespace, and no whitespace
// Combination 3: bracket characters "" or []
// Combination 4: wrapped in bracket characters, no bracket characters
// Combination 5: if wrapped in bracket characters, embedded (escaped) characters
foreach (string part1 in part1Words)
{
foreach ((string p1Combination, string p1Expected) in GeneratePartCombinations(part1))
{
// Skip duplicates — different generation paths can produce
// identical (input, expected) pairs, which xUnit rejects.
if (!seen.Add(p1Combination))
{
continue;
}
string onePartCombination = p1Combination;
string[] onePartExpected = [p1Expected];
data.Add(onePartCombination, onePartExpected);
}
}
return data;
}
}
/// <summary>
/// Gets a collection of test cases representing various formats and structures of multipart identifiers.
/// </summary>
/// <remarks>
/// This property provides examples of multipart identifiers with different numbers of
/// parts, including cases with empty segments, variations of whitespace around the separator, and bracketed components.
/// </remarks>
/// <see cref="MultipartIdentifier_ParsesCorrectly" />
public static TheoryData<string, string[]> ValidMultipartIdentifierVariations =>
new()
{
// Two parts, bracketed and unbracketed
{ "[word1].[word2]", ["word1", "word2"] },
{ "word1.word2", ["word1", "word2"] },
// Two parts, one of which is empty
{ ".word2", ["", "word2"] },
{ "word1.", ["word1", ""] },
// Two parts, with whitespace around the separator
{ "word1 .word2", ["word1", "word2"] },
{ "word1. word2", ["word1", "word2"] },
{ "word1 . word2", ["word1", "word2"] },
{ "[word1] .[word2]", ["word1", "word2"] },
{ "[word1]. [word2]", ["word1", "word2"] },
{ "[word1] . [word2]", ["word1", "word2"] },
// Three parts, one of which is empty
{ ".word2.word3", ["", "word2", "word3"] },
{ "word1..word3", ["word1", "", "word3"] },
{ "word1.word2.", ["word1", "word2", ""] },
// Four parts, one of which is empty
{ ".word2.word3.word4", ["", "word2", "word3", "word4"] },
{ "word1..word3.word4", ["word1", "", "word3", "word4"] },
{ "word1.word2..word4", ["word1", "word2", "", "word4"] },
{ "word1.word2.word3.", ["word1", "word2", "word3", ""] },
};
/// <summary>
/// Gets a collection of test cases representing various formats of invalid part identifiers.
/// </summary>
/// <remarks>
/// This property provides examples of single part identifiers with mismatched brackets, invalid bracket
/// placement, unclosed brackets or quotes, and cases with more parts than expected.
/// These cases are intended to test the parser's ability to correctly identify and reject invalid formats.
/// </remarks>
/// <see cref="InvalidSinglePartIdentifier_Throws" />
public static TheoryData<string> InvalidSinglePartIdentifierVariations =>
new()
{
// Empty string
{ "" },
// Bracket halfway through a part
{ "word1[word2" },
{ "word1]word2" },
{ "word1\"word2" },
// Invalid bracket placement (i.e. starts with a close bracket or ends with an open bracket)
{ "]word1" },
{ "word1[" },
// Unclosed brackets or quotes
{ "[word1" },
{ "\"word1" },
// Part starts with one bracket and ends with another
{ "[word1\"" },
{ "\"word1]" },
// More parts than expected, in various conditions.
// Additional part is empty
{ "word1." },
// Additional part, wrapped in brackets
{ "word1.word2" },
{ "word1.[word2]" },
{ "word1.\"word2\"" },
// Additional part, with whitespace before or after the separator
{ "word1 .word2" },
{ "word1. word2" },
// Additional part, with whitespace after the second part
{ "word1.word2 " },
{ "word1.[word2 ]" },
{ "word1.\"word2 \"" },
// Additional part, with separator after the second part
{ "word1.word2." },
{ "word1.word2.word3" },
{ "word1.[word2]." },
{ "word1.\"word2\"." },
{ "word1.[word2].word3" },
{ "word1.\"word2\".word3" },
};
/// <summary>
/// Gets a collection of test cases representing various formats and structures of invalid multipart identifiers.
/// </summary>
/// <remarks>
/// This property provides examples of multipart identifiers with trailing non-whitespace characters after bracketed
/// parts, and cases with whitespace followed by non-whitespace characters after bracketed parts.
/// These cases are intended to test the parser's ability to correctly identify and reject invalid multipart identifier formats.
/// </remarks>
/// <see cref="InvalidMultipartIdentifier_Throws" />
public static TheoryData<string, int> InvalidMultipartIdentifierVariations =>
new()
{
// Bracketed part with trailing non-whitespace characters
{ "[foo]!.[bar]", 2 },
{ "\"foo\"!.\"bar\"", 2 },
{ "[foo].[bar]!", 2 },
{ "\"foo\".\"bar\"!", 2 },
// Bracketed part with trailing whitespace followed by non-whitespace characters
{ "[foo] !.[bar]", 2 },
{ "\"foo\" !.\"bar\"", 2 },
{ "[foo]. ![bar]", 2 },
{ "\"foo\". !\"bar\"", 2 },
{ "[foo].[bar] !", 2 },
{ "\"foo\".\"bar\" !", 2 },
};
/// <summary>
/// Gets a collection of test cases representing the results of processing an identifier with fewer parts than expected.
/// </summary>
/// <see cref="SingleUnbracketedOvercount_FillsFirstElementsWithNull" />
public static TheoryData<string, string?[], int> OvercountMultipartIdentifierVariations =>
new()
{
{ "word1", [null, "word1"], 2 },
{ "word1", [null, null, "word1"], 3 },
{ "word1", [null, null, null, "word1"], 4 },
{ "word1.word2", [null, "word1", "word2"], 3 },
{ "word1.word2", [null, null, "word1", "word2"], 4 },
{ "word1.word2.word3", [null, "word1", "word2", "word3"], 4 },
};
/// <summary>
/// Verifies that one part in an identifier parses successfully when it contains various
/// combinations of brackets and whitespace, and that the expected value is returned.
/// </summary>
/// <param name="partIdentifier">The raw identifier to parse.</param>
/// <param name="expected">The expected output of parsing the identifier.</param>
[Theory]
[MemberData(nameof(ValidSinglePartIdentifierVariations))]
public void SinglePartIdentifierWithBracketsAndWhiteSpace_ParsesSuccessfully(string partIdentifier, string[] expected) =>
RunParse(partIdentifier, expected);
/// <summary>
/// Verifies that a multi-part identifier parses successfully when its parts are combinations
/// of bracketed and unbracketed identifiers, with various placements of whitespace, and that
/// the expected values are returned.
/// </summary>
/// <param name="partIdentifier">The raw identifier to parse.</param>
/// <param name="expected">The expected output of parsing the identifier.</param>
[Theory]
[MemberData(nameof(ValidMultipartIdentifierVariations))]
public void MultipartIdentifier_ParsesSuccessfully(string partIdentifier, string[] expected) =>
RunParse(partIdentifier, expected);
/// <summary>
/// Verifies that parsing one part in an identifier throws an exception when it is invalid.
/// This encompasses mismatched, misplaced or unclosed brackets and more parts than expected,
/// in various combinations with whitespace.
/// </summary>
/// <param name="partIdentifier">The raw identifier to parse.</param>
[Theory]
[MemberData(nameof(InvalidSinglePartIdentifierVariations))]
public void InvalidSinglePartIdentifier_Throws(string partIdentifier) =>
ThrowParse(partIdentifier, expectedLength: 1);
/// <summary>
/// Verifies that parsing a multi-part identifier throws an exception when it is invalid (such as
/// containing non-whitespace characters between a closing bracket and the separator, or between a
/// closing bracket and the end of the string.)
/// </summary>
/// <param name="partIdentifier">The raw identifier to parse.</param>
/// <param name="expectedLength">The expected number of components in the identifier.</param>
[Theory]
[MemberData(nameof(InvalidMultipartIdentifierVariations))]
public void InvalidMultipartIdentifier_Throws(string partIdentifier, int expectedLength) =>
ThrowParse(partIdentifier, expectedLength);
/// <summary>
/// Verifies that when a multipart identifier contains fewer parts than expected, the parser fills the
/// missing elements in the array with null values (starting from the first element) and successfully
/// parses the identifier.
/// </summary>
/// <param name="partIdentifier">The raw identifier to parse.</param>
/// <param name="expected">The expected output of parsing the identifier.</param>
/// <param name="maxCount">The number of parts which the part parsing should normally expect.</param>
[Theory]
[MemberData(nameof(OvercountMultipartIdentifierVariations))]
public void SingleUnbracketedOvercount_FillsFirstElementsWithNull(string partIdentifier, string?[] parts, int maxCount) =>
RunParse(partIdentifier, parts, maxCount);
/// <summary>
/// Verifies that multipart identifier strings containing zero-length segments are parsed into the expected
/// array of empty strings.
/// </summary>
/// <remarks>
/// This test case contrasts with <see cref="EmptyMultipartIdentifierWithThrowOnEmptyTrue_Throws"/>, where the
/// input is a completely empty string rather than a multipart identifier with empty segments.
/// </remarks>
/// <param name="partIdentifier">The raw identifier to parse.</param>
[Theory]
[InlineData("[].[].[].[]")]
[InlineData("...")]
[InlineData(".[].[].")]
[InlineData("[]...[]")]
[InlineData(" . . . ")]
[InlineData(" []. [] . [] .[] ")]
public void MultipartIdentifierOfZeroLengthParts_ParsesSuccessfully(string partIdentifier) =>
RunParse(partIdentifier, ["", "", "", ""]);
/// <summary>
/// Verifies that parsing a multipart identifier with more parts than expected throws an exception.
/// </summary>
/// <param name="maxCount">The number of parts which the part parsing should normally expect.</param>
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void MultipartIdentifierWithMorePartsThanExpected_Throws(int maxCount) =>
ThrowParse("word1.word2.word3.word4", maxCount);
/// <summary>
/// Verifies that parsing an empty multipart identifier with the throwOnEmpty flag set to false returns an array
/// of nulls with the specified number of parts (rather than throwing an exception.)
/// </summary>
/// <param name="expectedLength">The expected number of components in the identifier.</param>
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
public void EmptyMultipartIdentifierWithThrowOnEmptyFalse_ReturnsArrayOfNulls(int expectedLength) =>
RunParse("", new string?[expectedLength], expectedLength, throwOnEmpty: false);
/// <summary>
/// Verifies that parsing an empty multipart identifier with the throwOnEmpty flag set to true throws an exception.
/// </summary>
/// <remarks>
/// This test case contrasts with <see cref="MultipartIdentifierOfZeroLengthParts_ParsesSuccessfully"/>, where the
/// input is a multipart identifier with empty segments rather than a completely empty string.
/// </remarks>
/// <param name="expectedLength">The expected number of components in the identifier.</param>
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
public void EmptyMultipartIdentifierWithThrowOnEmptyTrue_Throws(int expectedLength) =>
ThrowParse("", expectedLength);
private static void RunParse(string name, string?[] expected, int maxCount = 0, bool throwOnEmpty = true)
{
if (maxCount == 0)
{
for (int index = 0; index < expected.Length; index++)
{
if (expected[index] != null)
{
maxCount += 1;
}
}
}
string?[] originalParts = MultipartIdentifier.ParseMultipartIdentifier(name, "", throwOnEmpty, maxCount);
Assert.Equal(expected.Length, originalParts.Length);
for (int index = 0; index < expected.Length; index++)
{
string? expectedPart = expected[index];
string? originalPart = originalParts[index];
Assert.Equal(expectedPart, originalPart);
}
}
private static void ThrowParse(string name, int expectedLength)
{
ArgumentException originalException = Assert.Throws<ArgumentException>(() =>
MultipartIdentifier.ParseMultipartIdentifier(name, "test", true, expectedLength)
);
Assert.NotNull(originalException);
}
private static IEnumerable<(string, string)> GeneratePartCombinations(string word)
{
Debug.Assert(word is "word1" or "word 1");
(string OpeningBracket, string ClosingBracket)[] bracketCombinations = [("[", "]"), ("\"", "\"")];
// Combinations of whitespace, contained entirely within various combinations of brackets
foreach (string wsCombination in GenerateWhitespaceCombinations(word))
{
foreach ((string openingBracket, string closingBracket) in bracketCombinations)
{
foreach ((string bracketCombination, string expectedValue) in GenerateBracketCombinations(wsCombination, openingBracket, closingBracket))
{
yield return (bracketCombination, expectedValue);
}
}
}
// Combinations of brackets, with whitespace outside the brackets
foreach ((string openingBracket, string closingBracket) in bracketCombinations)
{
foreach ((string bracketCombination, string unbracketedValue) in GenerateBracketCombinations(word, openingBracket, closingBracket))
{
foreach (string wsCombination in GenerateWhitespaceCombinations(bracketCombination))
{
yield return (wsCombination, unbracketedValue);
}
}
}
static IEnumerable<string> GenerateWhitespaceCombinations(string word)
{
yield return word;
yield return $" {word}";
yield return $"{word} ";
yield return $" {word} ";
}
static IEnumerable<(string Combination, string Expected)> GenerateBracketCombinations(string word, string openingBracket, string closingBracket)
{
yield return (word, word.Trim());
yield return (openingBracket + word + closingBracket, word);
yield return (openingBracket + word.Insert(word.Length - 3, closingBracket + closingBracket) + closingBracket, word.Insert(word.Length - 3, closingBracket));
}
}
}