Skip to content

Commit

Permalink
Better task serialization (VerifyTests#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Apr 25, 2022
1 parent 2a3b735 commit 61083a0
Show file tree
Hide file tree
Showing 14 changed files with 401 additions and 46 deletions.
4 changes: 2 additions & 2 deletions docs/named-tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Given a method that returns a named tuple:
static (bool Member1, string Member2, string Member3) MethodWithNamedTuple() =>
(true, "A", "B");
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1051-L1056' title='Snippet source file'>snippet source</a> | <a href='#snippet-methodwithnamedtuple' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1095-L1100' title='Snippet source file'>snippet source</a> | <a href='#snippet-methodwithnamedtuple' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Can be verified:
Expand All @@ -29,7 +29,7 @@ Can be verified:
```cs
await VerifyTuple(() => MethodWithNamedTuple());
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1044-L1048' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifytuple' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1088-L1092' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifytuple' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Resulting in:
Expand Down
10 changes: 5 additions & 5 deletions docs/scrubbers.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ For example remove lines containing `text`:
```cs
verifySettings.ScrubLines(line => line.Contains("text"));
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L906-L910' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublines' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L950-L954' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublines' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -71,7 +71,7 @@ For example remove lines containing `text1` or `text2`
```cs
verifySettings.ScrubLinesContaining("text1", "text2");
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L912-L916' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublinescontaining' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L956-L960' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublinescontaining' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Case insensitive by default (StringComparison.OrdinalIgnoreCase).
Expand All @@ -83,7 +83,7 @@ Case insensitive by default (StringComparison.OrdinalIgnoreCase).
```cs
verifySettings.ScrubLinesContaining(StringComparison.Ordinal, "text1", "text2");
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L918-L922' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublinescontainingordinal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L962-L966' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublinescontainingordinal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -98,7 +98,7 @@ For example converts lines to upper case:
```cs
verifySettings.ScrubLinesWithReplace(line => line.ToUpper());
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L924-L928' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublineswithreplace' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L968-L972' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrublineswithreplace' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -111,7 +111,7 @@ Replaces `Environment.MachineName` with `TheMachineName`.
```cs
verifySettings.ScrubMachineName();
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L930-L934' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrubmachinename' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L974-L978' title='Snippet source file'>snippet source</a> | <a href='#snippet-scrubmachinename' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
74 changes: 37 additions & 37 deletions docs/serializer-settings.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public Task VerifyJsonJToken()
return VerifyJson(target);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1933-L1965' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyjson' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1977-L2009' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyjson' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Results in:
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;msb3277;CS0436</NoWarn>
<Version>16.5.4</Version>
<Version>16.6.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10</LangVersion>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
finished: {
Status: RanToCompletion
},
running: {
Status: WaitingForActivation
},
withResult: {
Status: RanToCompletion,
Result: Value
},
withException: {
Status: Faulted,
Exception: {
Type: AggregateException,
InnerExceptions: [
{
Type: Exception,
Message: the exception
}
],
Message: One or more errors occurred. (the exception)
}
},
withExceptionAndResult: {
Status: Faulted,
Exception: {
Type: AggregateException,
InnerExceptions: [
{
Type: Exception,
Message: the exception
}
],
Message: One or more errors occurred. (the exception)
}
},
canceledAndResult: {
Status: Canceled
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
finished: {
Status: RanToCompletion
},
running: {
Status: WaitingForActivation
},
withResult: {
Status: RanToCompletion,
Result: Value
},
withException: {
Status: Faulted,
Exception: {
Type: AggregateException,
InnerExceptions: [
{
Type: Exception,
Message: the exception
}
],
Message: One or more errors occurred. (the exception)
}
},
withExceptionAndResult: {
Status: Faulted,
Exception: {
Type: AggregateException,
InnerExceptions: [
{
Type: Exception,
Message: the exception
}
],
Message: One or more errors occurred. (the exception)
}
},
canceledAndResult: {
Status: Canceled
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
finished: {
Status: RanToCompletion
},
running: {
Status: WaitingForActivation
},
withResult: {
Status: RanToCompletion,
Result: Value
},
withException: {
Status: Faulted,
Exception: {
Type: AggregateException,
InnerExceptions: [
{
Type: Exception,
Message: the exception
}
],
Message: One or more errors occurred.
}
},
withExceptionAndResult: {
Status: Faulted,
Exception: {
Type: AggregateException,
InnerExceptions: [
{
Type: Exception,
Message: the exception
}
],
Message: One or more errors occurred.
}
},
canceledAndResult: {
Status: Canceled
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
finished: {
Status: RanToCompletion
},
running: {
Status: WaitingForActivation
},
withResult: {
Status: RanToCompletion,
Result: Value
},
withException: {
Status: Faulted,
Exception: {
Type: AggregateException,
InnerExceptions: [
{
Type: Exception,
Message: the exception
}
],
Message: One or more errors occurred.
}
},
withExceptionAndResult: {
Status: Faulted,
Exception: {
Type: AggregateException,
InnerExceptions: [
{
Type: Exception,
Message: the exception
}
],
Message: One or more errors occurred.
}
},
canceledAndResult: {
Status: Canceled
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
withResult: {
IsCanceled: false,
IsCompleted: true,
IsFaulted: false,
Result: Value
},
withException: {
IsCanceled: false,
IsCompleted: true,
IsFaulted: true
},
withExceptionAndResult: {
IsCanceled: false,
IsCompleted: true,
IsFaulted: true
},
canceledAndResult: {
Status: Canceled
}
}
44 changes: 44 additions & 0 deletions src/Verify.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,50 @@ public static void Initialize()
VerifierSettings.AddExtraDatetimeOffsetFormat("F");
}

[Fact]
public async Task Tasks()
{
var withResult = Task.FromResult("Value");
var withException = Task.FromException(new("the exception"));
var withExceptionAndResult = Task.FromException<string>(new("the exception"));
var genericCompletionSource = new TaskCompletionSource<int>();
genericCompletionSource.TrySetCanceled();
var canceledAndResult = genericCompletionSource.Task;
var finished = Task.Delay(0);
var running = Task.Delay(10000);
await Verify(
new
{
finished,
running,
withResult,
withException,
withExceptionAndResult,
canceledAndResult
}).UniqueForRuntime();
}

#if NET5_0_OR_GREATER || net48
[Fact]
public async Task ValueTasks()
{
var withResult = ValueTask.FromResult("Value");
var withException = ValueTask.FromException(new("the exception"));
var withExceptionAndResult = ValueTask.FromException<string>(new("the exception"));
var genericCompletionSource = new TaskCompletionSource<int>();
genericCompletionSource.TrySetCanceled();
var canceledAndResult = genericCompletionSource.Task;
await Verify(
new
{
withResult,
withException,
withExceptionAndResult,
canceledAndResult
}).UniqueForRuntime();
}
#endif

[Fact]
public Task PathInfos() =>
Verify(
Expand Down
46 changes: 46 additions & 0 deletions src/Verify/Serialization/Converters/TaskConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class TaskConverter :
WriteOnlyJsonConverter<Task>
{
public override void Write(VerifyJsonWriter writer, Task task)
{
writer.WriteStartObject();
writer.WriteProperty(task, task.Status, "Status");

if (task.CreationOptions != TaskCreationOptions.None)
{
writer.WriteProperty(task, task.CreationOptions, "CreationOptions");
}

writer.WriteProperty(task, task.Exception, "Exception");
WriteResult(writer, task);

writer.WriteEndObject();
}

static void WriteResult(VerifyJsonWriter writer, Task task)
{
if (!task.IsCompleted ||
task.IsCanceled ||
task.IsFaulted)
{
return;
}

var type = task.GetType();

if (!type.IsGenericType)
{
return;
}

var name = type.GenericTypeArguments.First().Name;
if (name == "VoidTaskResult")
{
return;
}

var resultProperty = type.GetProperty("Result")!;
var result = resultProperty.GetValue(task);
writer.WriteProperty(task, result, "Result");
}
}
Loading

0 comments on commit 61083a0

Please sign in to comment.