From 5a7c03ffaa1a4209742364f4281bb4dc6030d9ef Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 29 Apr 2022 12:55:21 +1000 Subject: [PATCH] Un-wrap TargetInvocationException (#514) --- docs/serializer-settings.md | 32 +++++++++---------- readme.md | 2 +- src/Directory.Build.props | 2 +- ...stedTargetInvocationException.verified.txt | 7 ++++ ...sts.TargetInvocationException.verified.txt | 5 +++ .../Serialization/SerializationTests.cs | 30 +++++++++++++++++ .../TargetInvocationExceptionConverter.cs | 6 ++++ .../Serialization/SerializationSettings.cs | 2 ++ 8 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 src/Verify.Tests/Serialization/SerializationTests.NestedTargetInvocationException.verified.txt create mode 100644 src/Verify.Tests/Serialization/SerializationTests.TargetInvocationException.verified.txt create mode 100644 src/Verify/Serialization/Converters/TargetInvocationExceptionConverter.cs diff --git a/docs/serializer-settings.md b/docs/serializer-settings.md index d0474a9ee8..4f82b257c3 100644 --- a/docs/serializer-settings.md +++ b/docs/serializer-settings.md @@ -84,7 +84,7 @@ var settings = new JsonSerializerSettings Culture = CultureInfo.InvariantCulture }; ``` -snippet source | anchor +snippet source | anchor @@ -620,7 +620,7 @@ public Task ScopedSerializerFluent() .AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -754,7 +754,7 @@ public Task IgnoreTypeFluent() }); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -765,7 +765,7 @@ Or globally: VerifierSettings.ModifySerialization( _ => _.IgnoreMembersWithType()); ``` -snippet source | anchor +snippet source | anchor Result: @@ -841,7 +841,7 @@ public Task AddIgnoreInstanceFluent() }); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -852,7 +852,7 @@ Or globally: VerifierSettings.ModifySerialization( _ => _.IgnoreInstance(x => x.Property == "Ignore")); ``` -snippet source | anchor +snippet source | anchor Result: @@ -895,7 +895,7 @@ public Task WithObsoleteProp() return Verify(target); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -943,7 +943,7 @@ public Task WithObsoletePropIncludedFluent() .ModifySerialization(_ => _.IncludeObsoletes()); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -953,7 +953,7 @@ Or globally: ```cs VerifierSettings.ModifySerialization(_ => _.IncludeObsoletes()); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1018,7 +1018,7 @@ public Task IgnoreMemberByExpressionFluent() }); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -1035,7 +1035,7 @@ VerifierSettings.ModifySerialization(_ => _.IgnoreMember(x => x.PropertyThatThrows); }); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1113,7 +1113,7 @@ public Task IgnoreMemberByNameFluent() }); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1136,7 +1136,7 @@ VerifierSettings.ModifySerialization(_ => _.IgnoreMember(_ => _.PropertyThatThrows); }); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1183,7 +1183,7 @@ public Task CustomExceptionPropFluent() .ModifySerialization(_ => _.IgnoreMembersThatThrow()); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1193,7 +1193,7 @@ Or globally: ```cs VerifierSettings.ModifySerialization(_ => _.IgnoreMembersThatThrow()); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1401,7 +1401,7 @@ public Task MemberConverterByExpression() return Verify(input); } ``` -snippet source | anchor +snippet source | anchor diff --git a/readme.md b/readme.md index 86c9168efa..c6802ff10b 100644 --- a/readme.md +++ b/readme.md @@ -309,7 +309,7 @@ public Task VerifyJsonJToken() return VerifyJson(target); } ``` -snippet source | anchor +snippet source | anchor Results in: diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 153b9ddb7a..034a53fe13 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;xUnit1026;xUnit1013;msb3277;CS0436 - 16.6.0 + 16.7.0 enable 10 true diff --git a/src/Verify.Tests/Serialization/SerializationTests.NestedTargetInvocationException.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.NestedTargetInvocationException.verified.txt new file mode 100644 index 0000000000..1ed3361c8a --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.NestedTargetInvocationException.verified.txt @@ -0,0 +1,7 @@ +{ + exception: { + Type: Exception, + Message: the message, + StackTrace: at SerializationTests.MethodThatThrows() + } +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.TargetInvocationException.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.TargetInvocationException.verified.txt new file mode 100644 index 0000000000..62e3a87bd7 --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.TargetInvocationException.verified.txt @@ -0,0 +1,5 @@ +{ + Type: Exception, + Message: the message, + StackTrace: at SerializationTests.MethodThatThrows() +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.cs b/src/Verify.Tests/Serialization/SerializationTests.cs index 13a04c214b..aceda0047e 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.cs +++ b/src/Verify.Tests/Serialization/SerializationTests.cs @@ -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()); + }); + } + + [Fact] + public Task NestedTargetInvocationException() + { + var member = GetType().GetMethod("MethodThatThrows")!; + TargetInvocationException? exception = null; + try + { + member.Invoke(null, Array.Empty()); + } + catch (TargetInvocationException e) + { + exception = e; + } + + return Verify(new {exception}); + } + + public static void MethodThatThrows() => + throw new ("the message"); + void AddIgnoreInstanceGlobal() { #region AddIgnoreInstanceGlobal diff --git a/src/Verify/Serialization/Converters/TargetInvocationExceptionConverter.cs b/src/Verify/Serialization/Converters/TargetInvocationExceptionConverter.cs new file mode 100644 index 0000000000..d24ed0d5bb --- /dev/null +++ b/src/Verify/Serialization/Converters/TargetInvocationExceptionConverter.cs @@ -0,0 +1,6 @@ +class TargetInvocationExceptionConverter : + WriteOnlyJsonConverter +{ + public override void Write(VerifyJsonWriter writer, TargetInvocationException exception) => + writer.Serialize(exception.InnerException!); +} \ No newline at end of file diff --git a/src/Verify/Serialization/SerializationSettings.cs b/src/Verify/Serialization/SerializationSettings.cs index c4267ee863..557a605c0f 100644 --- a/src/Verify/Serialization/SerializationSettings.cs +++ b/src/Verify/Serialization/SerializationSettings.cs @@ -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(); @@ -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);