From 3f0d1ca49ae07bc8fe34b90338e1766ff370fcd1 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 24 Sep 2020 08:44:29 +1000 Subject: [PATCH] better ArgumentException serialization --- src/Directory.Build.props | 2 +- ...Tests.ThrowsArgumentException.verified.txt | 5 +++++ ...owsInheritedArgumentException.verified.txt | 5 +++++ src/Verify.Tests/Tests.cs | 22 +++++++++++++++++++ .../Serialization/CustomContractResolver.cs | 10 +++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/Verify.Tests/Tests.ThrowsArgumentException.verified.txt create mode 100644 src/Verify.Tests/Tests.ThrowsInheritedArgumentException.verified.txt diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 7ad507952..106216190 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;xUnit1026 - 6.20.0 + 6.21.0 1.0.0 Json, Testing, Verify, Snapshot, Approvals Enables verification of complex models and documents. diff --git a/src/Verify.Tests/Tests.ThrowsArgumentException.verified.txt b/src/Verify.Tests/Tests.ThrowsArgumentException.verified.txt new file mode 100644 index 000000000..701ca2971 --- /dev/null +++ b/src/Verify.Tests/Tests.ThrowsArgumentException.verified.txt @@ -0,0 +1,5 @@ +{ + Type: 'ArgumentException', + Message: 'The Message', + ParamName: 'The parameter' +} \ No newline at end of file diff --git a/src/Verify.Tests/Tests.ThrowsInheritedArgumentException.verified.txt b/src/Verify.Tests/Tests.ThrowsInheritedArgumentException.verified.txt new file mode 100644 index 000000000..80aee5831 --- /dev/null +++ b/src/Verify.Tests/Tests.ThrowsInheritedArgumentException.verified.txt @@ -0,0 +1,5 @@ +{ + Type: 'ArgumentNullException', + Message: 'The Message', + ParamName: 'The parameter' +} \ No newline at end of file diff --git a/src/Verify.Tests/Tests.cs b/src/Verify.Tests/Tests.cs index 735e8cda7..aa437fa05 100644 --- a/src/Verify.Tests/Tests.cs +++ b/src/Verify.Tests/Tests.cs @@ -103,6 +103,28 @@ static void MethodThatThrows() throw new Exception("The Message"); } + [Fact] + public Task ThrowsArgumentException() + { + return Verifier.Throws(MethodThatThrowsArgumentException); + } + + static void MethodThatThrowsArgumentException() + { + throw new ArgumentException("The Message", "The parameter"); + } + + [Fact] + public Task ThrowsInheritedArgumentException() + { + return Verifier.Throws(MethodThatThrowsArgumentNullException); + } + + static void MethodThatThrowsArgumentNullException() + { + throw new ArgumentNullException("The parameter", "The Message"); + } + [Fact] public Task ThrowsAggregate() { diff --git a/src/Verify/Serialization/CustomContractResolver.cs b/src/Verify/Serialization/CustomContractResolver.cs index fb8723fc2..80c3e9385 100644 --- a/src/Verify/Serialization/CustomContractResolver.cs +++ b/src/Verify/Serialization/CustomContractResolver.cs @@ -113,6 +113,8 @@ string ResolveDictionaryKey(JsonDictionaryContract contract, string value) return value; } + FieldInfo exceptionMessageField = typeof(Exception).GetField("_message", BindingFlags.Instance | BindingFlags.NonPublic)!; + protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization serialization) { var property = base.CreateProperty(member, serialization); @@ -124,6 +126,14 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ return property; } + if (member.Name == "Message") + { + if (member.DeclaringType == typeof(ArgumentException)) + { + valueProvider = new ReflectionValueProvider(exceptionMessageField); + } + } + if (propertyType.IsException()) { property.TypeNameHandling = TypeNameHandling.All;