Skip to content

Commit 3fb4b94

Browse files
committed
.
1 parent d2e5300 commit 3fb4b94

File tree

8 files changed

+116
-33
lines changed

8 files changed

+116
-33
lines changed

docs/serializer-settings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ var settings = new JsonSerializerSettings
174174
DefaultValueHandling = DefaultValueHandling.Ignore
175175
};
176176
```
177-
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L82-L91' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultSerialization' title='Start of snippet'>anchor</a></sup>
177+
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L83-L92' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultSerialization' title='Start of snippet'>anchor</a></sup>
178178
<!-- endSnippet -->
179179

180180

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
2020, 2, 12: 2020-02-12,
3+
2020, 2, 15: 2020-02-15,
4+
2020, 3, 12: 2020-03-12,
5+
2020, 3, 15: 2020-03-15,
6+
2022, 2, 12: 2022-02-12,
7+
2022, 2, 15: 2022-02-15,
8+
2022, 3, 12: 2022-03-12,
9+
2022, 3, 15: 2022-03-15
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
2020, 2, 12: DateTime_1,
3+
2020, 2, 15: DateTime_2,
4+
2020, 3, 12: DateTime_3,
5+
2020, 3, 15: DateTime_4,
6+
2022, 2, 12: DateTime_5,
7+
2022, 2, 15: DateTime_6,
8+
2022, 3, 12: DateTime_7,
9+
2022, 3, 15: DateTime_8
10+
}

src/Verify.Xunit.Tests/VerifyCombinationsSample.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ public Task Two()
1919
a, b);
2020
}
2121

22+
[Fact]
23+
public Task WithScrubbed()
24+
{
25+
List<int> years = [2020, 2022];
26+
List<int> months = [2, 3];
27+
List<int> dates = [12, 15];
28+
return VerifyCombinations(
29+
(year, month, date) => new DateTime(year, month, date),
30+
years, months, dates);
31+
}
32+
33+
[Fact]
34+
public Task WithDontScrub()
35+
{
36+
List<int> years = [2020, 2022];
37+
List<int> months = [2, 3];
38+
List<int> dates = [12, 15];
39+
return VerifyCombinations(
40+
(year, month, date) => new DateTime(year, month, date),
41+
years, months, dates)
42+
.DontScrubDateTimes();
43+
}
44+
2245
[Fact]
2346
public Task Three()
2447
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace VerifyTests;
2+
3+
public class CombinationResult
4+
{
5+
public CombinationResult(object?[] keys, Exception exception)
6+
{
7+
Keys = keys;
8+
Exception = exception;
9+
}
10+
11+
public CombinationResult(object?[] keys, object? value)
12+
{
13+
Keys = keys;
14+
Value = value;
15+
}
16+
17+
public object?[] Keys { get; }
18+
public object? Value { get; }
19+
public Exception? Exception { get; }
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace VerifyTests;
2+
3+
public class CombinationResultsConverter :
4+
WriteOnlyJsonConverter<List<CombinationResult>>
5+
{
6+
public override void Write(VerifyJsonWriter writer, List<CombinationResult> items)
7+
{
8+
writer.WriteStartObject();
9+
foreach (var item in items)
10+
{
11+
writer.WritePropertyName(BuildKeys(item.Keys).ToString());
12+
var exception = item.Exception;
13+
if (exception == null)
14+
{
15+
writer.WriteValue(item.Value);
16+
}
17+
else
18+
{
19+
writer.WriteValue($"{exception.GetType().Name}: {exception.Message}");
20+
}
21+
}
22+
writer.WriteEndObject();
23+
}
24+
25+
static StringBuilder BuildKeys(object?[] combo)
26+
{
27+
var builder = new StringBuilder();
28+
for (var index = 0; index < combo.Length; index++)
29+
{
30+
var item = combo[index];
31+
VerifierSettings.AppendParameter(item, builder, true);
32+
if (index + 1 != combo.Length)
33+
{
34+
builder.Append(", ");
35+
}
36+
}
37+
38+
return builder;
39+
}
40+
}

src/Verify/Combinations/InnerVerifier_Combinations.cs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -161,58 +161,36 @@ public Task<VerifyResult> VerifyCombinations(
161161
return Verify(target);
162162
}
163163

164-
static Dictionary<StringBuilder, object?> GetCombinationString(
164+
static List<CombinationResult> GetCombinationString(
165165
Func<object?[], object?> processCall,
166166
List<IEnumerable<object?>> lists)
167167
{
168-
var items = new Dictionary<StringBuilder,object?>();
168+
var items = new List<CombinationResult>();
169169
var listCopy = lists.Select(_ => _.ToList()).ToList();
170170
var combinationGenerator = new CombinationGenerator(
171171
listCopy,
172172
combo =>
173173
{
174+
var keys = combo.ToArray();
174175
object? value;
175176
try
176177
{
177178
value = processCall(combo);
178179
}
179180
catch (TargetInvocationException exception)
180181
{
181-
value = ExceptionToString(exception.InnerException!);
182+
items.Add(new(keys, exception.InnerException!));
183+
return;
182184
}
183185
catch (Exception exception)
184186
{
185-
value = ExceptionToString(exception);
187+
items.Add(new(keys, exception));
188+
return;
186189
}
187-
items.Add(BuildKeys(combo), value);
190+
191+
items.Add(new(keys, value));
188192
});
189193
combinationGenerator.Run();
190194
return items;
191195
}
192-
193-
static string ExceptionToString(Exception exception) =>
194-
$"{exception.GetType().Name}: {exception.Message}";
195-
196-
static StringBuilder BuildKeys(object?[] combo)
197-
{
198-
var builder = new StringBuilder();
199-
for (var index = 0; index < combo.Length; index++)
200-
{
201-
var item = combo[index];
202-
VerifierSettings.AppendParameter(item, builder, true);
203-
if (index + 1 != combo.Length)
204-
{
205-
builder.Append(", ");
206-
}
207-
}
208-
209-
return builder;
210-
}
211-
212-
public class Item(StringBuilder keys, object? value)
213-
{
214-
public StringBuilder Keys { get; } = keys;
215-
public object? Value { get; } = value;
216-
}
217-
}
218-
196+
}

src/Verify/Serialization/SerializationSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ partial class SerializationSettings
3232
static StringDictionaryConverter stringDictionaryConverter = new();
3333
static TaskConverter taskConverter = new();
3434
static ValueTaskConverter valueTaskConverter = new();
35+
static CombinationResultsConverter combinationResultsConverter = new();
3536

3637
JsonSerializerSettings jsonSettings;
3738

@@ -122,6 +123,7 @@ JsonSerializerSettings BuildSettings()
122123
converters.Add(nameValueCollectionConverter);
123124
converters.Add(stringDictionaryConverter);
124125
converters.Add(keyValuePairConverter);
126+
converters.Add(combinationResultsConverter);
125127
foreach (var extraSetting in extraSettings)
126128
{
127129
extraSetting(settings);

0 commit comments

Comments
 (0)