Skip to content

Commit

Permalink
Un-wrap TargetInvocationException (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Apr 29, 2022
1 parent e3aa65b commit 5a7c03f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 18 deletions.
32 changes: 16 additions & 16 deletions docs/serializer-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var settings = new JsonSerializerSettings
Culture = CultureInfo.InvariantCulture
};
```
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L94-L104' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultserialization' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify/Serialization/SerializationSettings.cs#L95-L105' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultserialization' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -620,7 +620,7 @@ public Task ScopedSerializerFluent()
.AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2230-L2257' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2260-L2287' title='Snippet source file'>snippet source</a> | <a href='#snippet-scopedserializer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -754,7 +754,7 @@ public Task IgnoreTypeFluent()
});
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1461-L1572' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretype' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1491-L1602' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretype' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -765,7 +765,7 @@ Or globally:
VerifierSettings.ModifySerialization(
_ => _.IgnoreMembersWithType<ToIgnore>());
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1453-L1458' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretypeglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1483-L1488' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoretypeglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -841,7 +841,7 @@ public Task AddIgnoreInstanceFluent()
});
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1394-L1438' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1424-L1468' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -852,7 +852,7 @@ Or globally:
VerifierSettings.ModifySerialization(
_ => _.IgnoreInstance<Instance>(x => x.Property == "Ignore"));
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1386-L1391' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstanceglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1416-L1421' title='Snippet source file'>snippet source</a> | <a href='#snippet-addignoreinstanceglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -895,7 +895,7 @@ public Task WithObsoleteProp()
return Verify(target);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2194-L2214' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2224-L2244' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -943,7 +943,7 @@ public Task WithObsoletePropIncludedFluent()
.ModifySerialization(_ => _.IncludeObsoletes());
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2165-L2192' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2195-L2222' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -953,7 +953,7 @@ Or globally:
```cs
VerifierSettings.ModifySerialization(_ => _.IncludeObsoletes());
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2158-L2162' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincludedglobally' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2188-L2192' title='Snippet source file'>snippet source</a> | <a href='#snippet-withobsoletepropincludedglobally' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -1018,7 +1018,7 @@ public Task IgnoreMemberByExpressionFluent()
});
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1759-L1802' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1789-L1832' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally
Expand All @@ -1035,7 +1035,7 @@ VerifierSettings.ModifySerialization(_ =>
_.IgnoreMember<IgnoreExplicitTarget>(x => x.PropertyThatThrows);
});
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1745-L1756' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpressionglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1775-L1786' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyexpressionglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -1113,7 +1113,7 @@ public Task IgnoreMemberByNameFluent()
});
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1858-L1915' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1888-L1945' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -1136,7 +1136,7 @@ VerifierSettings.ModifySerialization(_ =>
_.IgnoreMember<IgnoreExplicitTarget>(_ => _.PropertyThatThrows);
});
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1838-L1855' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbynameglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1868-L1885' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignorememberbynameglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -1183,7 +1183,7 @@ public Task CustomExceptionPropFluent()
.ModifySerialization(_ => _.IgnoreMembersThatThrow<CustomException>());
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2053-L2072' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2083-L2102' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or globally:
Expand All @@ -1193,7 +1193,7 @@ Or globally:
```cs
VerifierSettings.ModifySerialization(_ => _.IgnoreMembersThatThrow<CustomException>());
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2046-L2050' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L2076-L2080' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:
Expand Down Expand Up @@ -1401,7 +1401,7 @@ public Task MemberConverterByExpression()
return Verify(input);
}
```
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1804-L1828' title='Snippet source file'>snippet source</a> | <a href='#snippet-memberconverter' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Serialization/SerializationTests.cs#L1834-L1858' title='Snippet source file'>snippet source</a> | <a href='#snippet-memberconverter' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
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#L1977-L2009' 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#L2007-L2039' 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.6.0</Version>
<Version>16.7.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,7 @@
{
exception: {
Type: Exception,
Message: the message,
StackTrace: at SerializationTests.MethodThatThrows()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
Type: Exception,
Message: the message,
StackTrace: at SerializationTests.MethodThatThrows()
}
30 changes: 30 additions & 0 deletions src/Verify.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,36 @@ class WithNotImplementedException
public Guid NotImplementedExceptionProperty => throw new NotImplementedException();
}

[Fact]
public Task TargetInvocationException()
{
var member = GetType().GetMethod("MethodThatThrows")!;
return Throws(() =>
{
member.Invoke(null, Array.Empty<object>());
});
}

[Fact]
public Task NestedTargetInvocationException()
{
var member = GetType().GetMethod("MethodThatThrows")!;
TargetInvocationException? exception = null;
try
{
member.Invoke(null, Array.Empty<object>());
}
catch (TargetInvocationException e)
{
exception = e;
}

return Verify(new {exception});
}

public static void MethodThatThrows() =>
throw new ("the message");

void AddIgnoreInstanceGlobal()
{
#region AddIgnoreInstanceGlobal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TargetInvocationExceptionConverter :
WriteOnlyJsonConverter<TargetInvocationException>
{
public override void Write(VerifyJsonWriter writer, TargetInvocationException exception) =>
writer.Serialize(exception.InnerException!);
}
2 changes: 2 additions & 0 deletions src/Verify/Serialization/SerializationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public partial class SerializationSettings
static DirectoryInfoConverter directoryInfoConverter = new();
static StringEnumConverter stringEnumConverter = new();
static DelegateConverter delegateConverter = new();
static TargetInvocationExceptionConverter targetInvocationExceptionConverter = new();
static ExpressionConverter expressionConverter = new();
static TypeJsonConverter typeJsonConverter = new();
static MethodInfoConverter methodInfoConverter = new();
Expand Down Expand Up @@ -118,6 +119,7 @@ JsonSerializerSettings BuildSettings()
converters.Add(stringEnumConverter);
converters.Add(expressionConverter);
converters.Add(delegateConverter);
converters.Add(targetInvocationExceptionConverter);
converters.Add(versionConverter);
converters.Add(typeJsonConverter);
converters.Add(methodInfoConverter);
Expand Down

0 comments on commit 5a7c03f

Please sign in to comment.