Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing tests and refine a number of exception message assertions. #104034

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, JsonNode?> dict = Dictionary;

if (dict.TryGetValue(propertyName, out JsonNode? replacedValue))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1255,15 +1255,15 @@ public ICollection<string> AllowedGrantTypes
public async Task CustomInterfacesNotSupported_Enumerables(Type type)
{
NotSupportedException ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.DeserializeWrapper("[]", type));
Assert.Contains(type.ToString(), ex.ToString());
Assert.Contains(type.ToString(), ex.Message);
}

[Theory]
[MemberData(nameof(CustomInterfaces_Dictionaries))]
public async Task CustomInterfacesNotSupported_Dictionaries(Type type)
{
NotSupportedException ex = await Assert.ThrowsAsync<NotSupportedException>(async () => await Serializer.DeserializeWrapper("{}", type));
Assert.Contains(type.ToString(), ex.ToString());
Assert.Contains(type.ToString(), ex.Message);
}

public static IEnumerable<object[]> CustomInterfaces_Enumerables()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,11 @@ public async Task InvalidJsonFail(string json)
public async Task JsonPathIsAccurate(string json, string expectedPath)
{
JsonException ex = await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<KeyValuePair<int, int>>(json));
Assert.Contains(expectedPath, ex.ToString());
Assert.Contains(expectedPath, ex.Message);

var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
ex = await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<KeyValuePair<int, int>>(json));
Assert.Contains(expectedPath, ex.ToString());
Assert.Contains(expectedPath, ex.Message);
}

[Theory]
Expand All @@ -459,7 +459,7 @@ public async Task JsonPathIsAccurate_CaseInsensitive(string json, string expecte
{
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
JsonException ex = await Assert.ThrowsAsync<JsonException>(async () => await Serializer.DeserializeWrapper<KeyValuePair<int, int>>(json, options));
Assert.Contains(expectedPath, ex.ToString());
Assert.Contains(expectedPath, ex.Message);
}

[Theory]
Expand All @@ -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<JsonException>(async () => await Serializer.DeserializeWrapper<KeyValuePair<int, int>>(json, options));
Assert.Contains(expectedPath, ex.ToString());
Assert.Contains(expectedPath, ex.Message);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract partial class ConstructorTests
public async Task NonPublicCtors_NotSupported(Type type)
{
NotSupportedException ex = await Assert.ThrowsAsync<NotSupportedException>(() => Serializer.DeserializeWrapper("{}", type));
Assert.Contains("JsonConstructorAttribute", ex.ToString());
Assert.Contains("JsonConstructorAttribute", ex.Message);
}

[Theory]
Expand All @@ -35,7 +35,7 @@ public async Task NonPublicCtors_WithJsonConstructorAttribute_WorksAsExpected(Ty
else
{
NotSupportedException ex = await Assert.ThrowsAsync<NotSupportedException>(() => Serializer.DeserializeWrapper("{}", type));
Assert.Contains("JsonConstructorAttribute", ex.ToString());
Assert.Contains("JsonConstructorAttribute", ex.Message);
}
}

Expand All @@ -53,7 +53,7 @@ public async Task NonPublicParameterlessCtors_WithJsonConstructorAttribute_Works
else
{
NotSupportedException ex = await Assert.ThrowsAsync<NotSupportedException>(() => Serializer.DeserializeWrapper("{}", type));
Assert.Contains("JsonConstructorAttribute", ex.ToString());
Assert.Contains("JsonConstructorAttribute", ex.Message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ public async Task ExtensionProperty_IgnoresCustomSerializerWithOptions_JsonObjec
InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await Serializer.DeserializeWrapper<ClassWithExtensionData<JsonObject>>(@"{""TestKey"":""TestValue""}", options));

Assert.Contains("JsonObject", ex.ToString());
Assert.Contains("JsonObject", ex.Message);
}

[Theory]
Expand Down Expand Up @@ -1303,7 +1303,7 @@ public async Task CustomJsonObjectConverterInExtensionProperty()
InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
await Serializer.DeserializeWrapper<ClassWithExtensionPropertyAsJsonObject>(Json, options));

Assert.Contains("JsonObject", ex.ToString());
Assert.Contains("JsonObject", ex.Message);
}

public class JsonObjectConverter : JsonConverter<JsonObject>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ public void JsonObjectCreationHandlingAttribute_InvalidConstructorArgument_Throw
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(
() => new JsonObjectCreationHandlingAttribute(handling));

Assert.Contains("handling", ex.ToString());
Assert.Contains("handling", ex.Message);
}

[Fact]
Expand Down
20 changes: 10 additions & 10 deletions src/libraries/System.Text.Json/tests/Common/NumberHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1734,13 +1734,13 @@ public static void JsonNumberHandling_ArgOutOfRangeFail()
// Global options
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(
() => new JsonSerializerOptions { NumberHandling = (JsonNumberHandling)(-1) });
Assert.Contains("value", ex.ToString());
Assert.Contains("value", ex.Message);
Assert.Throws<ArgumentOutOfRangeException>(
() => new JsonSerializerOptions { NumberHandling = (JsonNumberHandling)(8) });

ex = Assert.Throws<ArgumentOutOfRangeException>(
() => new JsonNumberHandlingAttribute((JsonNumberHandling)(-1)));
Assert.Contains("handling", ex.ToString());
Assert.Contains("handling", ex.Message);
Assert.Throws<ArgumentOutOfRangeException>(
() => new JsonNumberHandlingAttribute((JsonNumberHandling)(8)));
}
Expand Down Expand Up @@ -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<InvalidOperationException>(async () => await Serializer.DeserializeWrapper<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);

ex = await Assert.ThrowsAsync<InvalidOperationException>(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<InvalidOperationException>(async () => await Serializer.DeserializeWrapper<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);

ex = await Assert.ThrowsAsync<InvalidOperationException>(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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,20 @@ public static void NullPropertyNameFail()

var jObject = new JsonObject();
ex = Assert.Throws<ArgumentNullException>(() => jObject.Add(null, 42));
Assert.Contains("propertyName", ex.ToString());
Assert.Contains("propertyName", ex.Message);

ex = Assert.Throws<ArgumentNullException>(() => jObject[null] = 42);
Assert.Contains("propertyName", ex.ToString());
Assert.Contains("propertyName", ex.Message);

ex = Assert.Throws<ArgumentNullException>(() => jObject.ContainsKey(null));
Assert.Contains("propertyName", ex.ToString());
Assert.Contains("propertyName", ex.Message);

ex = Assert.Throws<ArgumentNullException>(() => jObject.Remove(null));
Assert.Contains("propertyName", ex.ToString());
Assert.Contains("propertyName", ex.Message);

var iDictionary = (IDictionary<string, JsonNode?>)jObject;
ex = Assert.Throws<ArgumentNullException>(() => iDictionary.TryGetValue(null, out JsonNode _));
Assert.Contains("propertyName", ex.ToString());
Assert.Contains("propertyName", ex.Message);
}

[Fact]
Expand Down Expand Up @@ -565,7 +565,7 @@ public static void ReAddSameNode_Throws()
var jObject = new JsonObject();
jObject.Add("Prop", jValue);
ArgumentException ex = Assert.Throws<ArgumentException>(() => jObject.Add("Prop", jValue));
Assert.Contains("Prop", ex.ToString());
Assert.Contains("Prop", ex.Message);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvalidOperationException>(() => JsonSerializer.Deserialize("{}", type));
Assert.Contains(type.FullName, ex.ToString());
Assert.Contains(type.FullName, ex.Message);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public InvalidTypeTests(JsonSerializerWrapper serializer)
public void DeserializeInvalidType(Type type)
{
InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => JsonSerializer.Deserialize("", type));
Assert.Contains(type.ToString(), ex.ToString());
Assert.Contains(type.ToString(), ex.Message);
}

[Theory]
Expand Down Expand Up @@ -107,7 +107,7 @@ public async Task SerializeOpenGeneric(Type type)
public async Task SerializeInvalidTypes_NullValue(Type type)
{
InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(null, type));
Assert.Contains(type.ToString(), ex.ToString());
Assert.Contains(type.ToString(), ex.Message);
}

[Fact]
Expand All @@ -117,7 +117,7 @@ public async Task SerializeOpenGeneric_NullableOfT()
object obj = Activator.CreateInstance(openNullableType.MakeGenericType(typeof(int)));

InvalidOperationException ex = await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(obj, openNullableType));
Assert.Contains(openNullableType.ToString(), ex.ToString());
Assert.Contains(openNullableType.ToString(), ex.Message);
}

private class Test<T> { }
Expand Down Expand Up @@ -191,7 +191,7 @@ public void ArraySegmentTest()
Assert.Equal(@"{""ArraySegment"":[1]}", serialized);

NotSupportedException ex = Assert.Throws<NotSupportedException>(() => JsonSerializer.Deserialize<ClassWithArraySegment>(serialized));
Assert.Contains(typeof(ArraySegment<byte>).ToString(), ex.ToString());
Assert.Contains(typeof(ArraySegment<byte>).ToString(), ex.Message);
}

private class ClassWithArraySegment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,10 +1174,10 @@ static void RunTest(int maxDepth, int effectiveMaxDepth)
string effectiveMaxDepthAsStr = effectiveMaxDepth.ToString();

JsonException ex = Assert.Throws<JsonException>(() => JsonSerializer.Serialize(myList, options));
Assert.Contains(effectiveMaxDepthAsStr, ex.ToString());
Assert.Contains(effectiveMaxDepthAsStr, ex.Message);

ex = Assert.Throws<JsonException>(() => JsonSerializer.Serialize(myList, newOptions));
Assert.Contains(effectiveMaxDepthAsStr, ex.ToString());
Assert.Contains(effectiveMaxDepthAsStr, ex.Message);
}

// Zero max depth
Expand Down Expand Up @@ -1219,7 +1219,7 @@ public static void CopyConstructor_CopiesJsonSerializerContext()
public static void CopyConstructor_NullInput()
{
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => new JsonSerializerOptions(null));
Assert.Contains("options", ex.ToString());
Assert.Contains("options", ex.Message);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentNullException>(() => JsonSerializer.Deserialize("", returnType: null));
Assert.Contains("returnType", ex.ToString());
Assert.Contains("returnType", ex.Message);

ex = Assert.Throws<ArgumentNullException>(() => JsonSerializer.Deserialize(new char[] { '1' }, returnType: null));
Assert.Contains("returnType", ex.ToString());
Assert.Contains("returnType", ex.Message);

ex = Assert.Throws<ArgumentNullException>(() => JsonSerializer.Deserialize(new byte[] { 1 }, returnType: null));
Assert.Contains("returnType", ex.ToString());
Assert.Contains("returnType", ex.Message);

ex = Assert.Throws<ArgumentNullException>(() => 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<ArgumentNullException>(() => JsonSerializer.Deserialize(json: (string)null, returnType: typeof(string)));
Assert.Contains("json", ex.ToString());
Assert.Contains("json", ex.Message);

ex = Assert.Throws<ArgumentNullException>(() => JsonSerializer.DeserializeAsync(utf8Json: null, returnType: null));
Assert.Contains("utf8Json", ex.ToString());
Assert.Contains("utf8Json", ex.Message);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
Loading
Loading