From 2705fa4b3870c6ea2f80ac14691cc0ebf6278425 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Wed, 26 Jun 2024 19:26:02 +0100 Subject: [PATCH] Fix failing tests and refine a number of exception message assertions. (#104034) --- .../src/System/Text/Json/Nodes/JsonObject.cs | 10 +++++++++ .../CollectionTests.Generic.Read.cs | 4 ++-- .../CollectionTests.KeyValuePair.cs | 8 +++---- .../ConstructorTests.AttributePresence.cs | 6 ++--- .../tests/Common/ExtensionDataTests.cs | 4 ++-- .../JsonCreationHandlingTests.Object.cs | 2 +- .../tests/Common/NumberHandlingTests.cs | 20 ++++++++--------- .../JsonNode/JsonObjectTests.cs | 12 +++++----- .../CustomConverterTests.BadConverters.cs | 6 ++--- .../Serialization/ExceptionTests.cs | 2 +- .../Serialization/InvalidTypeTests.cs | 8 +++---- .../Serialization/OptionsTests.cs | 6 ++--- .../Serialization/ReadValueTests.cs | 14 ++++++------ .../Serialization/Stream.ReadTests.cs | 4 ++-- .../Serialization/WriteValueTests.cs | 22 +++++++++---------- 15 files changed, 69 insertions(+), 59 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs index 667a7626b120b..20cdc076aac64 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Nodes/JsonObject.cs @@ -204,6 +204,11 @@ internal override bool DeepEqualsCore(JsonNode node) internal JsonNode? GetItem(string propertyName) { + if (propertyName is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); + } + if (TryGetPropertyValue(propertyName, out JsonNode? value)) { return value; @@ -236,6 +241,11 @@ internal override void GetPath(ref ValueStringBuilder path, JsonNode? child) internal void SetItem(string propertyName, JsonNode? value) { + if (propertyName is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(propertyName)); + } + OrderedDictionary dict = Dictionary; if (dict.TryGetValue(propertyName, out JsonNode? replacedValue)) diff --git a/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Generic.Read.cs b/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Generic.Read.cs index a1b9b76f003ce..d02db100547db 100644 --- a/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Generic.Read.cs +++ b/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Generic.Read.cs @@ -1255,7 +1255,7 @@ public ICollection AllowedGrantTypes public async Task CustomInterfacesNotSupported_Enumerables(Type type) { NotSupportedException ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper("[]", type)); - Assert.Contains(type.ToString(), ex.ToString()); + Assert.Contains(type.ToString(), ex.Message); } [Theory] @@ -1263,7 +1263,7 @@ public async Task CustomInterfacesNotSupported_Enumerables(Type type) public async Task CustomInterfacesNotSupported_Dictionaries(Type type) { NotSupportedException ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper("{}", type)); - Assert.Contains(type.ToString(), ex.ToString()); + Assert.Contains(type.ToString(), ex.Message); } public static IEnumerable CustomInterfaces_Enumerables() diff --git a/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.KeyValuePair.cs b/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.KeyValuePair.cs index b0bcefc6934c1..1b0d5c25e5783 100644 --- a/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.KeyValuePair.cs +++ b/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.KeyValuePair.cs @@ -445,11 +445,11 @@ public async Task InvalidJsonFail(string json) public async Task JsonPathIsAccurate(string json, string expectedPath) { JsonException ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper>(json)); - Assert.Contains(expectedPath, ex.ToString()); + Assert.Contains(expectedPath, ex.Message); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper>(json)); - Assert.Contains(expectedPath, ex.ToString()); + Assert.Contains(expectedPath, ex.Message); } [Theory] @@ -459,7 +459,7 @@ public async Task JsonPathIsAccurate_CaseInsensitive(string json, string expecte { var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; JsonException ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper>(json, options)); - Assert.Contains(expectedPath, ex.ToString()); + Assert.Contains(expectedPath, ex.Message); } [Theory] @@ -469,7 +469,7 @@ public async Task JsonPathIsAccurate_PropertyNamingPolicy(string json, string ex { var options = new JsonSerializerOptions { PropertyNamingPolicy = new LeadingUnderscorePolicy() }; JsonException ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper>(json, options)); - Assert.Contains(expectedPath, ex.ToString()); + Assert.Contains(expectedPath, ex.Message); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.AttributePresence.cs b/src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.AttributePresence.cs index f54692a5c59a4..156cee58843af 100644 --- a/src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.AttributePresence.cs +++ b/src/libraries/System.Text.Json/tests/Common/ConstructorTests/ConstructorTests.AttributePresence.cs @@ -18,7 +18,7 @@ public abstract partial class ConstructorTests public async Task NonPublicCtors_NotSupported(Type type) { NotSupportedException ex = await Assert.ThrowsAsync(() => Serializer.DeserializeWrapper("{}", type)); - Assert.Contains("JsonConstructorAttribute", ex.ToString()); + Assert.Contains("JsonConstructorAttribute", ex.Message); } [Theory] @@ -35,7 +35,7 @@ public async Task NonPublicCtors_WithJsonConstructorAttribute_WorksAsExpected(Ty else { NotSupportedException ex = await Assert.ThrowsAsync(() => Serializer.DeserializeWrapper("{}", type)); - Assert.Contains("JsonConstructorAttribute", ex.ToString()); + Assert.Contains("JsonConstructorAttribute", ex.Message); } } @@ -53,7 +53,7 @@ public async Task NonPublicParameterlessCtors_WithJsonConstructorAttribute_Works else { NotSupportedException ex = await Assert.ThrowsAsync(() => Serializer.DeserializeWrapper("{}", type)); - Assert.Contains("JsonConstructorAttribute", ex.ToString()); + Assert.Contains("JsonConstructorAttribute", ex.Message); } } diff --git a/src/libraries/System.Text.Json/tests/Common/ExtensionDataTests.cs b/src/libraries/System.Text.Json/tests/Common/ExtensionDataTests.cs index ab1960772ecc3..bad434157ee29 100644 --- a/src/libraries/System.Text.Json/tests/Common/ExtensionDataTests.cs +++ b/src/libraries/System.Text.Json/tests/Common/ExtensionDataTests.cs @@ -527,7 +527,7 @@ public async Task ExtensionProperty_IgnoresCustomSerializerWithOptions_JsonObjec InvalidOperationException ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper>(@"{""TestKey"":""TestValue""}", options)); - Assert.Contains("JsonObject", ex.ToString()); + Assert.Contains("JsonObject", ex.Message); } [Theory] @@ -1303,7 +1303,7 @@ public async Task CustomJsonObjectConverterInExtensionProperty() InvalidOperationException ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper(Json, options)); - Assert.Contains("JsonObject", ex.ToString()); + Assert.Contains("JsonObject", ex.Message); } public class JsonObjectConverter : JsonConverter diff --git a/src/libraries/System.Text.Json/tests/Common/JsonCreationHandlingTests.Object.cs b/src/libraries/System.Text.Json/tests/Common/JsonCreationHandlingTests.Object.cs index 5f50962441b3f..5aef2578ec949 100644 --- a/src/libraries/System.Text.Json/tests/Common/JsonCreationHandlingTests.Object.cs +++ b/src/libraries/System.Text.Json/tests/Common/JsonCreationHandlingTests.Object.cs @@ -1145,7 +1145,7 @@ public void JsonObjectCreationHandlingAttribute_InvalidConstructorArgument_Throw ArgumentOutOfRangeException ex = Assert.Throws( () => new JsonObjectCreationHandlingAttribute(handling)); - Assert.Contains("handling", ex.ToString()); + Assert.Contains("handling", ex.Message); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/Common/NumberHandlingTests.cs b/src/libraries/System.Text.Json/tests/Common/NumberHandlingTests.cs index 833b01114dc68..67ff84c87f461 100644 --- a/src/libraries/System.Text.Json/tests/Common/NumberHandlingTests.cs +++ b/src/libraries/System.Text.Json/tests/Common/NumberHandlingTests.cs @@ -1734,13 +1734,13 @@ public static void JsonNumberHandling_ArgOutOfRangeFail() // Global options ArgumentOutOfRangeException ex = Assert.Throws( () => new JsonSerializerOptions { NumberHandling = (JsonNumberHandling)(-1) }); - Assert.Contains("value", ex.ToString()); + Assert.Contains("value", ex.Message); Assert.Throws( () => new JsonSerializerOptions { NumberHandling = (JsonNumberHandling)(8) }); ex = Assert.Throws( () => new JsonNumberHandlingAttribute((JsonNumberHandling)(-1))); - Assert.Contains("handling", ex.ToString()); + Assert.Contains("handling", ex.Message); Assert.Throws( () => new JsonNumberHandlingAttribute((JsonNumberHandling)(8))); } @@ -1798,24 +1798,24 @@ public async Task InternalCollectionConverter_CustomNumberConverter_OnProperty() { // Invalid to set number handling for number collection property when number is handled with custom converter. var ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper("")); - Assert.Contains(nameof(ClassWithListPropAndAttribute_ConverterOnProp), ex.ToString()); - Assert.Contains("IntProp", ex.ToString()); + Assert.Contains(nameof(ClassWithListPropAndAttribute_ConverterOnProp), ex.Message); + Assert.Contains("IntProp", ex.Message); ex = await Assert.ThrowsAsync(async () => await Serializer.SerializeWrapper(new ClassWithListPropAndAttribute_ConverterOnProp())); - Assert.Contains(nameof(ClassWithListPropAndAttribute_ConverterOnProp), ex.ToString()); - Assert.Contains("IntProp", ex.ToString()); + Assert.Contains(nameof(ClassWithListPropAndAttribute_ConverterOnProp), ex.Message); + Assert.Contains("IntProp", ex.Message); #if !BUILDING_SOURCE_GENERATOR_TESTS // Source-gen isn't currently validating that the converter on the test prop // is invalid so JsonException is being thrown instead due to invalid JSON. // [ActiveIssue("https://github.com/dotnet/runtime/issues/73714"] ex = await Assert.ThrowsAsync(async () => await Serializer.DeserializeWrapper("")); - Assert.Contains(nameof(ClassWithDictPropAndAttribute_ConverterOnProp), ex.ToString()); - Assert.Contains("IntProp", ex.ToString()); + Assert.Contains(nameof(ClassWithDictPropAndAttribute_ConverterOnProp), ex.Message); + Assert.Contains("IntProp", ex.Message); ex = await Assert.ThrowsAsync(async () => await Serializer.SerializeWrapper(new ClassWithDictPropAndAttribute_ConverterOnProp())); - Assert.Contains(nameof(ClassWithDictPropAndAttribute_ConverterOnProp), ex.ToString()); - Assert.Contains("IntProp", ex.ToString()); + Assert.Contains(nameof(ClassWithDictPropAndAttribute_ConverterOnProp), ex.Message); + Assert.Contains("IntProp", ex.Message); #endif } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonNode/JsonObjectTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonNode/JsonObjectTests.cs index c6df4e23b7b6c..4cba2e27aa07b 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonNode/JsonObjectTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonNode/JsonObjectTests.cs @@ -105,20 +105,20 @@ public static void NullPropertyNameFail() var jObject = new JsonObject(); ex = Assert.Throws(() => jObject.Add(null, 42)); - Assert.Contains("propertyName", ex.ToString()); + Assert.Contains("propertyName", ex.Message); ex = Assert.Throws(() => jObject[null] = 42); - Assert.Contains("propertyName", ex.ToString()); + Assert.Contains("propertyName", ex.Message); ex = Assert.Throws(() => jObject.ContainsKey(null)); - Assert.Contains("propertyName", ex.ToString()); + Assert.Contains("propertyName", ex.Message); ex = Assert.Throws(() => jObject.Remove(null)); - Assert.Contains("propertyName", ex.ToString()); + Assert.Contains("propertyName", ex.Message); var iDictionary = (IDictionary)jObject; ex = Assert.Throws(() => iDictionary.TryGetValue(null, out JsonNode _)); - Assert.Contains("propertyName", ex.ToString()); + Assert.Contains("propertyName", ex.Message); } [Fact] @@ -565,7 +565,7 @@ public static void ReAddSameNode_Throws() var jObject = new JsonObject(); jObject.Add("Prop", jValue); ArgumentException ex = Assert.Throws(() => jObject.Add("Prop", jValue)); - Assert.Contains("Prop", ex.ToString()); + Assert.Contains("Prop", ex.Message); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.BadConverters.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.BadConverters.cs index 045dd0ef01c8d..c46e841616bbe 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.BadConverters.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CustomConverterTests/CustomConverterTests.BadConverters.cs @@ -304,7 +304,7 @@ public static void ConverterReadTooLittle() } catch (JsonException ex) { - Assert.Contains("$.Level2.Level3s[0]", ex.ToString()); + Assert.Contains("$.Level2.Level3s[0]", ex.Message); Assert.Equal("$.Level2.Level3s[0]", ex.Path); } } @@ -324,7 +324,7 @@ public static void ConverterReadTooMuch() } catch (JsonException ex) { - Assert.Contains("$.Level2.Level3s[0]", ex.ToString()); + Assert.Contains("$.Level2.Level3s[0]", ex.Message); Assert.Equal("$.Level2.Level3s[0]", ex.Path); } } @@ -356,7 +356,7 @@ public static void ConverterWroteTooMuch() } catch (JsonException ex) { - Assert.Contains("$.Level2.Level3s", ex.ToString()); + Assert.Contains("$.Level2.Level3s", ex.Message); Assert.Equal("$.Level2.Level3s", ex.Path); } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ExceptionTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ExceptionTests.cs index 4e4c90d3d1962..b09f2c26d207d 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ExceptionTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ExceptionTests.cs @@ -522,7 +522,7 @@ public static void TypeWithBadCtorNoProps(Type type) // Each constructor parameter must bind to an object property or field. InvalidOperationException ex = Assert.Throws(() => JsonSerializer.Deserialize("{}", type)); - Assert.Contains(type.FullName, ex.ToString()); + Assert.Contains(type.FullName, ex.Message); } [Theory] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/InvalidTypeTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/InvalidTypeTests.cs index d4ea4d2f594c1..244a7229cd7fd 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/InvalidTypeTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/InvalidTypeTests.cs @@ -58,7 +58,7 @@ public InvalidTypeTests(JsonSerializerWrapper serializer) public void DeserializeInvalidType(Type type) { InvalidOperationException ex = Assert.Throws(() => JsonSerializer.Deserialize("", type)); - Assert.Contains(type.ToString(), ex.ToString()); + Assert.Contains(type.ToString(), ex.Message); } [Theory] @@ -107,7 +107,7 @@ public async Task SerializeOpenGeneric(Type type) public async Task SerializeInvalidTypes_NullValue(Type type) { InvalidOperationException ex = await Assert.ThrowsAsync(() => Serializer.SerializeWrapper(null, type)); - Assert.Contains(type.ToString(), ex.ToString()); + Assert.Contains(type.ToString(), ex.Message); } [Fact] @@ -117,7 +117,7 @@ public async Task SerializeOpenGeneric_NullableOfT() object obj = Activator.CreateInstance(openNullableType.MakeGenericType(typeof(int))); InvalidOperationException ex = await Assert.ThrowsAsync(() => Serializer.SerializeWrapper(obj, openNullableType)); - Assert.Contains(openNullableType.ToString(), ex.ToString()); + Assert.Contains(openNullableType.ToString(), ex.Message); } private class Test { } @@ -191,7 +191,7 @@ public void ArraySegmentTest() Assert.Equal(@"{""ArraySegment"":[1]}", serialized); NotSupportedException ex = Assert.Throws(() => JsonSerializer.Deserialize(serialized)); - Assert.Contains(typeof(ArraySegment).ToString(), ex.ToString()); + Assert.Contains(typeof(ArraySegment).ToString(), ex.Message); } private class ClassWithArraySegment diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs index b6f91aa6e6e20..6f24d795af148 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs @@ -1174,10 +1174,10 @@ static void RunTest(int maxDepth, int effectiveMaxDepth) string effectiveMaxDepthAsStr = effectiveMaxDepth.ToString(); JsonException ex = Assert.Throws(() => JsonSerializer.Serialize(myList, options)); - Assert.Contains(effectiveMaxDepthAsStr, ex.ToString()); + Assert.Contains(effectiveMaxDepthAsStr, ex.Message); ex = Assert.Throws(() => JsonSerializer.Serialize(myList, newOptions)); - Assert.Contains(effectiveMaxDepthAsStr, ex.ToString()); + Assert.Contains(effectiveMaxDepthAsStr, ex.Message); } // Zero max depth @@ -1219,7 +1219,7 @@ public static void CopyConstructor_CopiesJsonSerializerContext() public static void CopyConstructor_NullInput() { ArgumentNullException ex = Assert.Throws(() => new JsonSerializerOptions(null)); - Assert.Contains("options", ex.ToString()); + Assert.Contains("options", ex.Message); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ReadValueTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ReadValueTests.cs index 58506b2fdc6ca..db896fa636782 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ReadValueTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ReadValueTests.cs @@ -18,29 +18,29 @@ public static void NullReturnTypeThrows() JsonSerializer.Deserialize(ref reader, returnType: null); }); - Assert.Contains("returnType", ex.ToString()); + Assert.Contains("returnType", ex.Message); ex = Assert.Throws(() => JsonSerializer.Deserialize("", returnType: null)); - Assert.Contains("returnType", ex.ToString()); + Assert.Contains("returnType", ex.Message); ex = Assert.Throws(() => JsonSerializer.Deserialize(new char[] { '1' }, returnType: null)); - Assert.Contains("returnType", ex.ToString()); + Assert.Contains("returnType", ex.Message); ex = Assert.Throws(() => JsonSerializer.Deserialize(new byte[] { 1 }, returnType: null)); - Assert.Contains("returnType", ex.ToString()); + Assert.Contains("returnType", ex.Message); ex = Assert.Throws(() => JsonSerializer.DeserializeAsync(new MemoryStream(), returnType: null)); - Assert.Contains("returnType", ex.ToString()); + Assert.Contains("returnType", ex.Message); } [Fact] public static void NullJsonThrows() { ArgumentNullException ex = Assert.Throws(() => JsonSerializer.Deserialize(json: (string)null, returnType: typeof(string))); - Assert.Contains("json", ex.ToString()); + Assert.Contains("json", ex.Message); ex = Assert.Throws(() => JsonSerializer.DeserializeAsync(utf8Json: null, returnType: null)); - Assert.Contains("utf8Json", ex.ToString()); + Assert.Contains("utf8Json", ex.Message); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Stream.ReadTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Stream.ReadTests.cs index 09e257490da6b..f689cf296ead1 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Stream.ReadTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Stream.ReadTests.cs @@ -285,7 +285,7 @@ public async Task ReadPrimitiveWithWhitespaceAndThenInvalid(int bufferSize) Assert.Equal(16387, stream.Position); // We should get an exception like: '!' is invalid after a single JSON value. - Assert.Contains("!", ex.ToString()); + Assert.Contains("!", ex.Message); } } @@ -306,7 +306,7 @@ public async Task ReadObjectWithWhitespaceAndThenInvalid(int bufferSize) Assert.Equal(16387, stream.Position); // We should get an exception like: '!' is invalid after a single JSON value. - Assert.Contains("!", ex.ToString()); + Assert.Contains("!", ex.Message); } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/WriteValueTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/WriteValueTests.cs index 62360109ab73b..4ef060d165828 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/WriteValueTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/WriteValueTests.cs @@ -15,10 +15,10 @@ public static void NullWriterThrows() ArgumentNullException ex; ex = Assert.Throws(() => JsonSerializer.Serialize(writer: null, 1)); - Assert.Contains("writer", ex.ToString()); + Assert.Contains("writer", ex.Message); ex = Assert.Throws(() => JsonSerializer.Serialize(writer: null, 1, typeof(int))); - Assert.Contains("writer", ex.ToString()); + Assert.Contains("writer", ex.Message); } [Fact] @@ -28,19 +28,19 @@ public async static void NullInputTypeThrows() Utf8JsonWriter writer = new Utf8JsonWriter(new MemoryStream()); ex = Assert.Throws(() => JsonSerializer.Serialize(writer: writer, value: null, inputType: null)); - Assert.Contains("inputType", ex.ToString()); + Assert.Contains("inputType", ex.Message); ex = Assert.Throws(() => JsonSerializer.Serialize(writer, value: null, inputType: null)); - Assert.Contains("inputType", ex.ToString()); + Assert.Contains("inputType", ex.Message); ex = Assert.Throws(() => JsonSerializer.Serialize(1, inputType: null)); - Assert.Contains("inputType", ex.ToString()); + Assert.Contains("inputType", ex.Message); ex = Assert.Throws(() => JsonSerializer.SerializeToUtf8Bytes(null, inputType: null)); - Assert.Contains("inputType", ex.ToString()); + Assert.Contains("inputType", ex.Message); ex = await Assert.ThrowsAsync(async () => await JsonSerializer.SerializeAsync(new MemoryStream(), null, inputType: null)); - Assert.Contains("inputType", ex.ToString()); + Assert.Contains("inputType", ex.Message); } [Fact] @@ -50,16 +50,16 @@ public async static void NullValueWithValueTypeThrows() Utf8JsonWriter writer = new Utf8JsonWriter(new MemoryStream()); ex = Assert.Throws(() => JsonSerializer.Serialize(writer: writer, value: null, inputType: typeof(int))); - Assert.Contains(typeof(int).ToString(), ex.ToString()); + Assert.Contains(typeof(int).ToString(), ex.Message); ex = Assert.Throws(() => JsonSerializer.Serialize(value: null, inputType: typeof(int))); - Assert.Contains(typeof(int).ToString(), ex.ToString()); + Assert.Contains(typeof(int).ToString(), ex.Message); ex = Assert.Throws(() => JsonSerializer.SerializeToUtf8Bytes(value: null, inputType: typeof(int))); - Assert.Contains(typeof(int).ToString(), ex.ToString()); + Assert.Contains(typeof(int).ToString(), ex.Message); ex = await Assert.ThrowsAsync(async () => await JsonSerializer.SerializeAsync(new MemoryStream(), value: null, inputType: typeof(int))); - Assert.Contains(typeof(int).ToString(), ex.ToString()); + Assert.Contains(typeof(int).ToString(), ex.Message); } [Fact]