Skip to content

Commit df7fd34

Browse files
authored
Add column headers to combination results (#1428)
1 parent b96af87 commit df7fd34

File tree

46 files changed

+1125
-286
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1125
-286
lines changed

docs/combinations.md

Lines changed: 176 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ To change this file edit the source file and then run MarkdownSnippets.
99

1010
Combinations allows all combinations of the given input lists to be executed, and the results all written to a single file.
1111

12+
1213
## Example
1314

15+
1416
### Method being tested
1517

1618
<!-- snippet: CombinationTargetMethod -->
1719
<a id='snippet-CombinationTargetMethod'></a>
1820
```cs
19-
public static string BuildAddress(int streetNumber, string street, string city)
21+
public static string BuildAddress(int number, string street, string city)
2022
{
2123
ArgumentException.ThrowIfNullOrWhiteSpace(street);
2224
ArgumentException.ThrowIfNullOrWhiteSpace(city);
23-
ArgumentOutOfRangeException.ThrowIfLessThan(streetNumber, 1);
25+
ArgumentOutOfRangeException.ThrowIfLessThan(number, 1);
2426

25-
return $"{streetNumber} {street}, {city}";
27+
return $"{number} {street}, {city}";
2628
}
2729
```
2830
<sup><a href='/src/Verify.Tests/CombinationSample.cs#L5-L16' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationTargetMethod' title='Start of snippet'>anchor</a></sup>
@@ -37,15 +39,15 @@ public static string BuildAddress(int streetNumber, string street, string city)
3739
[Fact]
3840
public Task BuildAddressTest()
3941
{
40-
int[] streetNumbers = [1, 10];
41-
string[] streets = ["Smith St", "Wallace St"];
42-
string[] cities = ["Sydney", "Chicago"];
42+
int[] number = [1, 10];
43+
string[] street = ["Smith St", "Wallace St"];
44+
string[] city = ["Sydney", "Chicago"];
4345
return Combination()
4446
.Verify(
4547
BuildAddress,
46-
streetNumbers,
47-
streets,
48-
cities);
48+
number,
49+
street,
50+
city);
4951
}
5052
```
5153
<sup><a href='/src/Verify.Tests/CombinationSample.cs#L18-L34' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationSample' title='Start of snippet'>anchor</a></sup>
@@ -94,19 +96,19 @@ To enable exception capture use `captureExceptions = true`:
9496
[Fact]
9597
public Task BuildAddressExceptionsTest()
9698
{
97-
int[] streetNumbers = [-1, 0, 10];
98-
string[] streets = ["", " ", "Valid St"];
99-
string[] cities = [null!, "Valid City"];
99+
int[] number = [-1, 0, 10];
100+
string[] street = ["", " ", "Valid St"];
101+
string[] city = [null!, "Valid City"];
100102
return Combination(captureExceptions: true)
101103
.Verify(
102104
BuildAddress,
103-
streetNumbers,
104-
streets,
105-
cities
105+
number,
106+
street,
107+
city
106108
);
107109
}
108110
```
109-
<sup><a href='/src/Verify.Tests/CombinationSample.cs#L54-L71' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationSample_CaptureExceptions' title='Start of snippet'>anchor</a></sup>
111+
<sup><a href='/src/Verify.Tests/CombinationSample.cs#L92-L109' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationSample_CaptureExceptions' title='Start of snippet'>anchor</a></sup>
110112
<!-- endSnippet -->
111113

112114

@@ -121,13 +123,13 @@ public Task BuildAddressExceptionsTest()
121123
-1, , null : ArgumentException: The value cannot be an empty string or composed entirely of whitespace. (Parameter 'street').,
122124
-1, , Valid City: ArgumentException: The value cannot be an empty string or composed entirely of whitespace. (Parameter 'street').,
123125
-1, Valid St, null : ArgumentNullException: Value cannot be null. (Parameter 'city').,
124-
-1, Valid St, Valid City: ArgumentOutOfRangeException: streetNumber ('-1') must be greater than or equal to '1'. (Parameter 'streetNumber'). Actual value was -1.,
126+
-1, Valid St, Valid City: ArgumentOutOfRangeException: number ('-1') must be greater than or equal to '1'. (Parameter 'number'). Actual value was -1.,
125127
0, , null : ArgumentException: The value cannot be an empty string or composed entirely of whitespace. (Parameter 'street').,
126128
0, , Valid City: ArgumentException: The value cannot be an empty string or composed entirely of whitespace. (Parameter 'street').,
127129
0, , null : ArgumentException: The value cannot be an empty string or composed entirely of whitespace. (Parameter 'street').,
128130
0, , Valid City: ArgumentException: The value cannot be an empty string or composed entirely of whitespace. (Parameter 'street').,
129131
0, Valid St, null : ArgumentNullException: Value cannot be null. (Parameter 'city').,
130-
0, Valid St, Valid City: ArgumentOutOfRangeException: streetNumber ('0') must be greater than or equal to '1'. (Parameter 'streetNumber'). Actual value was 0.,
132+
0, Valid St, Valid City: ArgumentOutOfRangeException: number ('0') must be greater than or equal to '1'. (Parameter 'number'). Actual value was 0.,
131133
10, , null : ArgumentException: The value cannot be an empty string or composed entirely of whitespace. (Parameter 'street').,
132134
10, , Valid City: ArgumentException: The value cannot be an empty string or composed entirely of whitespace. (Parameter 'street').,
133135
10, , null : ArgumentException: The value cannot be an empty string or composed entirely of whitespace. (Parameter 'street').,
@@ -148,7 +150,7 @@ Exception capture can be enabled globally:
148150
<a id='snippet-GlobalCaptureExceptions'></a>
149151
```cs
150152
[ModuleInitializer]
151-
public static void Initialize() =>
153+
public static void EnableCaptureExceptions() =>
152154
CombinationSettings.CaptureExceptions();
153155
```
154156
<sup><a href='/src/StaticSettingsTests/CombinationTests.cs#L3-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-GlobalCaptureExceptions' title='Start of snippet'>anchor</a></sup>
@@ -162,15 +164,15 @@ If exception capture has been enabled globally, it can be disable at the method
162164
[Fact]
163165
public Task BuildAddressExceptionsDisabledTest()
164166
{
165-
int[] streetNumbers = [1, 10];
166-
string[] streets = ["Smith St", "Wallace St"];
167-
string[] cities = ["Sydney", "Chicago"];
167+
int[] number = [1, 10];
168+
string[] street = ["Smith St", "Wallace St"];
169+
string[] city = ["Sydney", "Chicago"];
168170
return Combination(captureExceptions: false)
169171
.Verify(
170172
BuildAddress,
171-
streetNumbers,
172-
streets,
173-
cities);
173+
number,
174+
street,
175+
city);
174176
}
175177
```
176178
<sup><a href='/src/StaticSettingsTests/CombinationTests.cs#L177-L193' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationSample_CaptureExceptionsFalse' title='Start of snippet'>anchor</a></sup>
@@ -201,7 +203,16 @@ public class CombinationResultsConverter :
201203

202204
var keysLength = items[0].Keys.Count;
203205

204-
var maxKeyLengths = new int[keysLength];
206+
int[] maxKeyLengths;
207+
if (results.Columns == null)
208+
{
209+
maxKeyLengths = new int[keysLength];
210+
}
211+
else
212+
{
213+
maxKeyLengths = results.Columns.Select(_=>_.Length).ToArray();
214+
}
215+
205216
var keyValues = new string[items.Count, keysLength];
206217

207218
for (var itemIndex = 0; itemIndex < items.Count; itemIndex++)
@@ -220,6 +231,9 @@ public class CombinationResultsConverter :
220231
}
221232
}
222233

234+
WriteColumns(writer, results, maxKeyLengths);
235+
236+
// keys is reused
223237
var keys = new CombinationKey[keysLength];
224238
for (var itemIndex = 0; itemIndex < items.Count; itemIndex++)
225239
{
@@ -240,6 +254,29 @@ public class CombinationResultsConverter :
240254
writer.WriteEndObject();
241255
}
242256

257+
static void WriteColumns(VerifyJsonWriter writer, CombinationResults results, int[] maxKeyLengths)
258+
{
259+
if (results.Columns == null)
260+
{
261+
return;
262+
}
263+
264+
var builder = new StringBuilder();
265+
for (var index = 0; index < results.Columns.Count; index++)
266+
{
267+
var column = results.Columns[index];
268+
var maxLength = maxKeyLengths[index];
269+
var padding = maxLength - column.Length;
270+
builder.Append(column);
271+
builder.Append(' ', padding);
272+
builder.Append(", ");
273+
}
274+
builder.Length -= 2;
275+
276+
writer.WritePropertyName(builder.ToString());
277+
writer.WriteValue("Result");
278+
}
279+
243280
protected virtual string BuildPropertyName(IReadOnlyList<CombinationKey> keys)
244281
{
245282
var builder = new StringBuilder();
@@ -316,7 +353,7 @@ public class CombinationResultsConverter :
316353
}
317354
}
318355
```
319-
<sup><a href='/src/Verify/Combinations/CombinationResultsConverter.cs#L1-L131' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationResultsConverter.cs' title='Start of snippet'>anchor</a></sup>
356+
<sup><a href='/src/Verify/Combinations/CombinationResultsConverter.cs#L1-L166' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationResultsConverter.cs' title='Start of snippet'>anchor</a></sup>
320357
<!-- endSnippet -->
321358

322359

@@ -370,6 +407,7 @@ public static void Init() =>
370407
<a id='snippet-CombinationTests.Combination_CustomSerialization.verified.txt'></a>
371408
```txt
372409
{
410+
streetNumbers, streets , cities : Result,
373411
1, Smith St, Sydney: 1 Smith St, Sydney,
374412
1, Smith St, Chicago: 1 Smith St, Chicago,
375413
1, Wallace St, Sydney: 1 Wallace St, Sydney,
@@ -380,5 +418,115 @@ public static void Init() =>
380418
10, Wallace St, Chicago: 10 Wallace St, Chicago
381419
}
382420
```
383-
<sup><a href='/src/StaticSettingsTests/CombinationTests.Combination_CustomSerialization.verified.txt#L1-L10' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationTests.Combination_CustomSerialization.verified.txt' title='Start of snippet'>anchor</a></sup>
421+
<sup><a href='/src/StaticSettingsTests/CombinationTests.Combination_CustomSerialization.verified.txt#L1-L11' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationTests.Combination_CustomSerialization.verified.txt' title='Start of snippet'>anchor</a></sup>
422+
<!-- endSnippet -->
423+
424+
425+
## Header
426+
427+
By default no column headers are included. To include a header pass through `header: true`
428+
429+
<!-- snippet: CombinationSampleWithHeader -->
430+
<a id='snippet-CombinationSampleWithHeader'></a>
431+
```cs
432+
[Fact]
433+
public Task BuildAddressWithHeaderTest()
434+
{
435+
int[] number = [1, 10];
436+
string[] street = ["Smith St", "Wallace St"];
437+
string[] city = ["Sydney", "Chicago"];
438+
return Combination(header: true)
439+
.Verify(
440+
BuildAddress,
441+
number,
442+
street,
443+
city);
444+
}
445+
```
446+
<sup><a href='/src/Verify.Tests/CombinationSample.cs#L36-L52' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationSampleWithHeader' title='Start of snippet'>anchor</a></sup>
447+
<!-- endSnippet -->
448+
449+
The variable names of the inputted collections will be used.
450+
451+
Result:
452+
453+
<!-- snippet: CombinationSample.BuildAddressWithHeaderTest.verified.txt -->
454+
<a id='snippet-CombinationSample.BuildAddressWithHeaderTest.verified.txt'></a>
455+
```txt
456+
{
457+
number, street , city : Result,
458+
1, Smith St , Sydney : 1 Smith St, Sydney,
459+
1, Smith St , Chicago: 1 Smith St, Chicago,
460+
1, Wallace St, Sydney : 1 Wallace St, Sydney,
461+
1, Wallace St, Chicago: 1 Wallace St, Chicago,
462+
10, Smith St , Sydney : 10 Smith St, Sydney,
463+
10, Smith St , Chicago: 10 Smith St, Chicago,
464+
10, Wallace St, Sydney : 10 Wallace St, Sydney,
465+
10, Wallace St, Chicago: 10 Wallace St, Chicago
466+
}
467+
```
468+
<sup><a href='/src/Verify.Tests/CombinationSample.BuildAddressWithHeaderTest.verified.txt#L1-L11' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationSample.BuildAddressWithHeaderTest.verified.txt' title='Start of snippet'>anchor</a></sup>
469+
<!-- endSnippet -->
470+
471+
472+
### Override
473+
474+
Header names can be overridden:
475+
476+
<!-- snippet: CombinationSampleWithHeaderOverrides -->
477+
<a id='snippet-CombinationSampleWithHeaderOverrides'></a>
478+
```cs
479+
[Fact]
480+
public Task BuildAddressWithHeaderOverridesTest()
481+
{
482+
int[] number = [1, 10];
483+
string[] street = ["Smith St", "Wallace St"];
484+
string[] city = ["Sydney", "Chicago"];
485+
return Combination(header: true)
486+
.Verify(
487+
BuildAddress,
488+
number,
489+
street,
490+
city,
491+
"Number",
492+
"Street",
493+
"City");
494+
}
495+
```
496+
<sup><a href='/src/Verify.Tests/CombinationSample.cs#L53-L72' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationSampleWithHeaderOverrides' title='Start of snippet'>anchor</a></sup>
497+
<!-- endSnippet -->
498+
499+
Result:
500+
501+
<!-- snippet: CombinationSample.BuildAddressWithHeaderOverridesTest.verified.txt -->
502+
<a id='snippet-CombinationSample.BuildAddressWithHeaderOverridesTest.verified.txt'></a>
503+
```txt
504+
{
505+
Number, Street , City : Result,
506+
1, Smith St , Sydney : 1 Smith St, Sydney,
507+
1, Smith St , Chicago: 1 Smith St, Chicago,
508+
1, Wallace St, Sydney : 1 Wallace St, Sydney,
509+
1, Wallace St, Chicago: 1 Wallace St, Chicago,
510+
10, Smith St , Sydney : 10 Smith St, Sydney,
511+
10, Smith St , Chicago: 10 Smith St, Chicago,
512+
10, Wallace St, Sydney : 10 Wallace St, Sydney,
513+
10, Wallace St, Chicago: 10 Wallace St, Chicago
514+
}
515+
```
516+
<sup><a href='/src/Verify.Tests/CombinationSample.BuildAddressWithHeaderOverridesTest.verified.txt#L1-L11' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationSample.BuildAddressWithHeaderOverridesTest.verified.txt' title='Start of snippet'>anchor</a></sup>
517+
<!-- endSnippet -->
518+
519+
520+
### Global
521+
522+
Headers can be enabled globally:
523+
524+
<!-- snippet: GlobalCombinationHeader -->
525+
<a id='snippet-GlobalCombinationHeader'></a>
526+
```cs
527+
[ModuleInitializer]
528+
public static void EnableIncludeHeaders() =>
529+
CombinationSettings.IncludeHeaders();
530+
```
531+
<sup><a href='/src/StaticSettingsTests/CombinationTests.cs#L234-L240' title='Snippet source file'>snippet source</a> | <a href='#snippet-GlobalCombinationHeader' title='Start of snippet'>anchor</a></sup>
384532
<!-- endSnippet -->

docs/mdsource/combinations.source.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
Combinations allows all combinations of the given input lists to be executed, and the results all written to a single file.
44

5+
56
## Example
67

8+
79
### Method being tested
810

911
snippet: CombinationTargetMethod
@@ -86,4 +88,35 @@ snippet: CombinationSample_CustomSerializationModuleInitializer
8688

8789
#### Result
8890

89-
snippet: CombinationTests.Combination_CustomSerialization.verified.txt
91+
snippet: CombinationTests.Combination_CustomSerialization.verified.txt
92+
93+
94+
## Header
95+
96+
By default no column headers are included. To include a header pass through `header: true`
97+
98+
snippet: CombinationSampleWithHeader
99+
100+
The variable names of the inputted collections will be used.
101+
102+
Result:
103+
104+
snippet: CombinationSample.BuildAddressWithHeaderTest.verified.txt
105+
106+
107+
### Override
108+
109+
Header names can be overridden:
110+
111+
snippet: CombinationSampleWithHeaderOverrides
112+
113+
Result:
114+
115+
snippet: CombinationSample.BuildAddressWithHeaderOverridesTest.verified.txt
116+
117+
118+
### Global
119+
120+
Headers can be enabled globally:
121+
122+
snippet: GlobalCombinationHeader

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33
<PropertyGroup>
4-
<Version>28.16.0</Version>
4+
<Version>29.0.0-beta.1</Version>
55
<NoWarn>CA1822;CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget;xUnit1051</NoWarn>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<LangVersion>preview</LangVersion>

src/StaticSettingsTests/CombinationTests.BuildAddressExceptionsDisabledTest.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
number, street , city : Result,
23
1, Smith St, Sydney: 1 Smith St, Sydney,
34
1, Smith St, Chicago: 1 Smith St, Chicago,
45
1, Wallace St, Sydney: 1 Wallace St, Sydney,

src/StaticSettingsTests/CombinationTests.CallbacksTest.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
params1, params2: Result,
23
1, value1: 1 value1,
34
1, value2: 1 value2,
45
10, value1: 10 value1,

src/StaticSettingsTests/CombinationTests.Combination_CustomSerialization.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
streetNumbers, streets , cities : Result,
23
1, Smith St, Sydney: 1 Smith St, Sydney,
34
1, Smith St, Chicago: 1 Smith St, Chicago,
45
1, Wallace St, Sydney: 1 Wallace St, Sydney,

src/StaticSettingsTests/CombinationTests.Defaults.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
list: Result,
23
A: a,
34
b: b,
45
C: c

0 commit comments

Comments
 (0)