Skip to content

Commit

Permalink
add void return combination support (#1335)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Oct 31, 2024
1 parent c7261b4 commit 7880260
Show file tree
Hide file tree
Showing 21 changed files with 1,058 additions and 48 deletions.
46 changes: 26 additions & 20 deletions docs/combinations.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,34 @@ public class CombinationResultsConverter :

protected virtual void WriteValue(VerifyJsonWriter writer, CombinationResult result)
{
var exception = result.Exception;
if (exception == null)
switch (result.Type)
{
if (result.Value == null)
{
writer.WriteNull();
}
else
{
writer.Serialize(result.Value);
}

return;
}
case CombinationResultType.Void:
writer.WriteValue("void");
break;
case CombinationResultType.Value:
if (result.Value == null)
{
writer.WriteNull();
}
else
{
writer.Serialize(result.Value);
}
break;
case CombinationResultType.Exception:
var exception = result.Exception;
var message = exception.Message;
if (exception is ArgumentException)
{
message = FlattenMessage(message);
}

var message = exception.Message;
if (exception is ArgumentException)
{
message = FlattenMessage(message);
writer.WriteValue($"{exception.GetType().Name}: {message}");
break;
default:
throw new ArgumentOutOfRangeException();
}

writer.WriteValue($"{exception.GetType().Name}: {message}");
}

static string FlattenMessage(string message)
Expand All @@ -310,7 +316,7 @@ public class CombinationResultsConverter :
}
}
```
<sup><a href='/src/Verify/Combinations/CombinationResultsConverter.cs#L1-L125' title='Snippet source file'>snippet source</a> | <a href='#snippet-CombinationResultsConverter.cs' title='Start of snippet'>anchor</a></sup>
<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>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget</NoWarn>
<Version>28.0.0</Version>
<Version>28.1.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
/: /,
\: \
}
20 changes: 20 additions & 0 deletions src/Verify.TUnit.Tests/CombinationTests.MixedLengths.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
A , 100, True : a100True,
A , 100, False: a100False,
A , 2, True : a2True,
A , 2, False: a2False,
A , 30, True : a30True,
A , 30, False: a30False,
bcc , 100, True : bcc100True,
bcc , 100, False: bcc100False,
bcc , 2, True : bcc2True,
bcc , 2, False: bcc2False,
bcc , 30, True : bcc30True,
bcc , 30, False: bcc30False,
sssssC, 100, True : sssssc100True,
sssssC, 100, False: sssssc100False,
sssssC, 2, True : sssssc2True,
sssssC, 2, False: sssssc2False,
sssssC, 30, True : sssssc30True,
sssssC, 30, False: sssssc30False
}
5 changes: 5 additions & 0 deletions src/Verify.TUnit.Tests/CombinationTests.One.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
A: a,
b: b,
C: c
}
20 changes: 20 additions & 0 deletions src/Verify.TUnit.Tests/CombinationTests.Three.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
A, 1, True : a1True,
A, 1, False: a1False,
A, 2, True : a2True,
A, 2, False: a2False,
A, 3, True : a3True,
A, 3, False: a3False,
b, 1, True : b1True,
b, 1, False: b1False,
b, 2, True : b2True,
b, 2, False: b2False,
b, 3, True : b3True,
b, 3, False: b3False,
C, 1, True : c1True,
C, 1, False: c1False,
C, 2, True : c2True,
C, 2, False: c2False,
C, 3, True : c3True,
C, 3, False: c3False
}
11 changes: 11 additions & 0 deletions src/Verify.TUnit.Tests/CombinationTests.Two.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
A, 1: a1,
A, 2: a2,
A, 3: a3,
b, 1: b1,
b, 2: b2,
b, 3: b3,
C, 1: c1,
C, 2: c2,
C, 3: c3
}
10 changes: 10 additions & 0 deletions src/Verify.TUnit.Tests/CombinationTests.WithDontScrub.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
2020, 2, 12: 2020-02-12,
2020, 2, 15: 2020-02-15,
2020, 3, 12: 2020-03-12,
2020, 3, 15: 2020-03-15,
2022, 2, 12: 2022-02-12,
2022, 2, 15: 2022-02-15,
2022, 3, 12: 2022-03-12,
2022, 3, 15: 2022-03-15
}
20 changes: 20 additions & 0 deletions src/Verify.TUnit.Tests/CombinationTests.WithException.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
A, 1, True : a1True,
A, 1, False: a1False,
A, 2, True : a2True,
A, 2, False: a2False,
A, 3, True : a3True,
A, 3, False: a3False,
b, 1, True : ArgumentException: B is not allowed.,
b, 1, False: ArgumentException: B is not allowed.,
b, 2, True : ArgumentException: B is not allowed.,
b, 2, False: ArgumentException: B is not allowed.,
b, 3, True : ArgumentException: B is not allowed.,
b, 3, False: ArgumentException: B is not allowed.,
C, 1, True : c1True,
C, 1, False: c1False,
C, 2, True : c2True,
C, 2, False: c2False,
C, 3, True : c3True,
C, 3, False: c3False
}
10 changes: 10 additions & 0 deletions src/Verify.TUnit.Tests/CombinationTests.WithScrubbed.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
2020, 2, 12: DateTime_1,
2020, 2, 15: DateTime_2,
2020, 3, 12: DateTime_3,
2020, 3, 15: DateTime_4,
2022, 2, 12: DateTime_5,
2022, 2, 15: DateTime_6,
2022, 3, 12: DateTime_7,
2022, 3, 15: DateTime_8
}
6 changes: 6 additions & 0 deletions src/Verify.Tests/CombinationTests.VoidTaskTest.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
1, Smith St : void,
1, Wallace St: void,
10, Smith St : void,
10, Wallace St: void
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
1, Smith St : void,
1, Wallace St: void,
10, Smith St : void,
10, Wallace St: void
}
22 changes: 22 additions & 0 deletions src/Verify.Tests/CombinationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public Task TaskTest() =>
params1,
params2);

public static Task VoidTaskMethod(int param1, string param2) =>
Task.Delay(1);

[Fact]
public Task VoidTaskTest() =>
Combination()
.Verify(
VoidTaskMethod,
params1,
params2);

public static async ValueTask<string> ValueTaskMethod(int param1, string param2)
{
await Task.Delay(1);
Expand All @@ -59,6 +70,17 @@ public Task ValueTaskTest() =>
params1,
params2);

public static async ValueTask VoidValueTaskMethod(int param1, string param2) =>
await Task.Delay(1);

[Fact]
public Task VoidValueTaskTest() =>
Combination()
.Verify(
VoidValueTaskMethod,
params1,
params2);

public static string SimpleReturnMethod(int param1, string param2) =>
$"{param1} {param2}";

Expand Down
58 changes: 52 additions & 6 deletions src/Verify/Combinations/CombinationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,65 @@

public class CombinationResult
{
public CombinationResult(IReadOnlyList<object?> keys, Exception exception)
public static CombinationResult ForException(IReadOnlyList<object?> keys, Exception exception) =>
new(keys, exception);

CombinationResult(IReadOnlyList<object?> keys, Exception exception)
{
Keys = keys;
Exception = exception;
Type = CombinationResultType.Exception;
this.exception = exception;
}

public CombinationResult(IReadOnlyList<object?> keys, object? value)
public static CombinationResult ForValue(IReadOnlyList<object?> keys, object? value) =>
new(keys, value);

CombinationResult(IReadOnlyList<object?> keys, object? value)
{
Keys = keys;
Value = value;
Type = CombinationResultType.Value;
this.value = value;
}

public static CombinationResult ForVoid(IReadOnlyList<object?> keys) =>
new(keys);

CombinationResult(IReadOnlyList<object?> keys)
{
Keys = keys;
Type = CombinationResultType.Void;
}

public CombinationResultType Type { get; }
public IReadOnlyList<object?> Keys { get; }
public object? Value { get; }
public Exception? Exception { get; }

readonly object? value;

public object? Value
{
get
{
if (Type != CombinationResultType.Value)
{
throw new($"Invalid CombinationResultType: {Type}");
}

return value;
}
}

Exception? exception;

public Exception Exception
{
get
{
if (Type != CombinationResultType.Exception)
{
throw new($"Invalid CombinationResultType: {Type}");
}

return exception!;
}
}
}
8 changes: 8 additions & 0 deletions src/Verify/Combinations/CombinationResultType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace VerifyTests;

public enum CombinationResultType
{
Void,
Value,
Exception
}
44 changes: 25 additions & 19 deletions src/Verify/Combinations/CombinationResultsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,34 @@ protected virtual string BuildPropertyName(IReadOnlyList<CombinationKey> keys)

protected virtual void WriteValue(VerifyJsonWriter writer, CombinationResult result)
{
var exception = result.Exception;
if (exception == null)
switch (result.Type)
{
if (result.Value == null)
{
writer.WriteNull();
}
else
{
writer.Serialize(result.Value);
}

return;
}
case CombinationResultType.Void:
writer.WriteValue("void");
break;
case CombinationResultType.Value:
if (result.Value == null)
{
writer.WriteNull();
}
else
{
writer.Serialize(result.Value);
}
break;
case CombinationResultType.Exception:
var exception = result.Exception;
var message = exception.Message;
if (exception is ArgumentException)
{
message = FlattenMessage(message);
}

var message = exception.Message;
if (exception is ArgumentException)
{
message = FlattenMessage(message);
writer.WriteValue($"{exception.GetType().Name}: {message}");
break;
default:
throw new ArgumentOutOfRangeException();
}

writer.WriteValue($"{exception.GetType().Name}: {message}");
}

static string FlattenMessage(string message)
Expand Down
Loading

0 comments on commit 7880260

Please sign in to comment.