Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 53b8e52

Browse files
hughbestephentoub
authored andcommitted
Add more System.Json tests and remove dead xunit workarounds (#35057)
* Add more Json tests and remove old xunit workarounds * Move files to the right place * Fix tests
1 parent 7d96540 commit 53b8e52

File tree

5 files changed

+75
-71
lines changed

5 files changed

+75
-71
lines changed

src/System.Json/tests/System.Json.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<Configurations>netstandard-Debug;netstandard-Release</Configurations>
66
</PropertyGroup>
77
<ItemGroup>
8-
<Compile Include="JsonArrayTests.cs" />
9-
<Compile Include="JsonObjectTests.cs" />
10-
<Compile Include="JsonPrimitiveTests.cs" />
11-
<Compile Include="JsonValueTests.cs" />
8+
<Compile Include="System\Json\JsonArrayTests.cs" />
9+
<Compile Include="System\Json\JsonObjectTests.cs" />
10+
<Compile Include="System\Json\JsonPrimitiveTests.cs" />
11+
<Compile Include="System\Json\JsonValueTests.cs" />
1212
<ProjectReference Include="$(CommonTestPath)\System\Diagnostics\RemoteExecutorConsoleApp\RemoteExecutorConsoleApp.csproj">
1313
<Project>{69e46a6f-9966-45a5-8945-2559fe337827}</Project>
1414
<Name>RemoteExecutorConsoleApp</Name>

src/System.Json/tests/JsonArrayTests.cs renamed to src/System.Json/tests/System/Json/JsonArrayTests.cs

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,57 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// See the LICENSE file in the project root for more information.
33

4-
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
56
using System.IO;
7+
using System.Linq;
68
using System.Text;
79
using Xunit;
8-
using System.Collections.Generic;
9-
using System.Collections;
1010

1111
namespace System.Json.Tests
1212
{
1313
public class JsonArrayTests
1414
{
15-
public static IEnumerable<object[]> JsonValues_TestData()
15+
public static IEnumerable<object[]> Ctor_JsonValueArray_TestData()
1616
{
17+
yield return new object[] { null };
1718
yield return new object[] { new JsonValue[0] };
1819
yield return new object[] { new JsonValue[] { null } };
20+
yield return new object[] { new JsonValue[] { new JsonPrimitive(true) } };
1921
}
2022

2123
[Theory]
22-
[MemberData(nameof(JsonValues_TestData))]
23-
public void Ctor(JsonValue[] items)
24+
[MemberData(nameof(Ctor_JsonValueArray_TestData))]
25+
public void Ctor_JsonValueArray(JsonValue[] items)
2426
{
25-
VerifyJsonArray(new JsonArray(items), items);
26-
VerifyJsonArray(new JsonArray((IEnumerable<JsonValue>)items), items);
27-
}
27+
var array = new JsonArray(items);
2828

29-
private static void VerifyJsonArray(JsonArray array, JsonValue[] values)
30-
{
31-
Assert.Equal(values.Length, array.Count);
32-
for (int i = 0; i < values.Length; i++)
29+
Assert.Equal(items?.Length ?? 0, array.Count);
30+
for (int i = 0; i < (items?.Length ?? 0); i++)
3331
{
34-
Assert.Equal(values[i], array[i]);
32+
Assert.Same(items[i], array[i]);
3533
}
3634
}
37-
38-
[Fact]
39-
public void Ctor_Array_Works()
40-
{
41-
// Workaround xunit/xunit#987: InvalidOperationException thrown if this is in MemberData
42-
JsonValue[] items = new JsonValue[] { new JsonPrimitive(true) };
43-
JsonArray array = new JsonArray(items);
44-
Assert.Equal(1, array.Count);
45-
Assert.Same(items[0], array[0]);
46-
}
4735

48-
[Fact]
49-
public void Ctor_IEnumerable_Works()
36+
public static IEnumerable<object[]> Ctor_IEnumerableJsonValue_TestData()
5037
{
51-
// Workaround xunit/xunit#987: InvalidOperationException thrown if this is in MemberData
52-
JsonValue[] items = new JsonValue[] { new JsonPrimitive(true) };
53-
JsonArray array = new JsonArray((IEnumerable<JsonValue>)items);
54-
Assert.Equal(1, array.Count);
55-
Assert.Same(items[0], array[0]);
38+
yield return new object[] { new JsonValue[0] };
39+
yield return new object[] { new JsonValue[] { null } };
40+
yield return new object[] { new JsonValue[] { new JsonPrimitive(true) } };
5641
}
5742

58-
[Fact]
59-
public void Ctor_NullArray_Works()
43+
[Theory]
44+
[MemberData(nameof(Ctor_IEnumerableJsonValue_TestData))]
45+
public void Ctor_IEnumerableJsonValue(IEnumerable<JsonValue> items)
6046
{
61-
JsonArray array = new JsonArray(null);
62-
Assert.Equal(0, array.Count);
47+
var array = new JsonArray(items);
48+
49+
JsonValue[] expectedItems = items.ToArray();
50+
Assert.Equal(expectedItems.Length, array.Count);
51+
for (int i = 0; i < expectedItems.Length; i++)
52+
{
53+
Assert.Same(expectedItems[i], array[i]);
54+
}
6355
}
6456

6557
[Fact]

src/System.Json/tests/JsonObjectTests.cs renamed to src/System.Json/tests/System/Json/JsonObjectTests.cs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@ namespace System.Json.Tests
1212
{
1313
public class JsonObjectTests
1414
{
15-
[Fact]
16-
public void Ctor_EmptyArray_Works()
15+
public static IEnumerable<object[]> Ctor_Array_TestData()
1716
{
18-
Assert.Equal(0, new JsonObject(new KeyValuePair<string, JsonValue>[0]).Count);
19-
Assert.Equal(0, new JsonObject(Enumerable.Empty<KeyValuePair<string, JsonValue>>()).Count);
17+
yield return new object[] { null };
18+
yield return new object[] { new KeyValuePair<string, JsonValue>[0] };
19+
yield return new object[] { new KeyValuePair<string, JsonValue>[] { new KeyValuePair<string, JsonValue>("key", new JsonPrimitive(true)) } };
2020
}
2121

22-
[Fact]
23-
public void Ctor_Array()
22+
[Theory]
23+
[MemberData(nameof(Ctor_Array_TestData))]
24+
public void Ctor_Array(KeyValuePair<string, JsonValue>[] items)
2425
{
25-
// Workaround xunit/xunit#987: InvalidOperationException thrown if this is in MemberData
26-
KeyValuePair<string, JsonValue>[] items = new KeyValuePair<string, JsonValue>[] { new KeyValuePair<string, JsonValue>("key", new JsonPrimitive(true)) };
27-
JsonObject obj = new JsonObject(items);
26+
var obj = new JsonObject(items);
2827

29-
Assert.Equal(items.Length, obj.Count);
30-
for (int i = 0; i < items.Length; i++)
28+
Assert.Equal(items?.Length ?? 0, obj.Count);
29+
for (int i = 0; i < (items?.Length ?? 0); i++)
3130
{
3231
Assert.Equal(items[i].Value.ToString(), obj[items[i].Key].ToString());
3332

@@ -37,52 +36,51 @@ public void Ctor_Array()
3736
}
3837
}
3938

40-
[Fact]
41-
public void Ctor_IEnumerable()
39+
public static IEnumerable<object[]> Ctor_Enumerable_TestData()
4240
{
43-
// Workaround xunit/xunit#987: InvalidOperationException thrown if this is in MemberData
44-
KeyValuePair<string, JsonValue>[] items = new KeyValuePair<string, JsonValue>[] { new KeyValuePair<string, JsonValue>("key", new JsonPrimitive(true)) };
45-
JsonObject obj = new JsonObject((IEnumerable<KeyValuePair<string, JsonValue>>)items);
41+
yield return new object[] { Enumerable.Empty<KeyValuePair<string, JsonValue>>() };
42+
yield return new object[] { new KeyValuePair<string, JsonValue>[] { new KeyValuePair<string, JsonValue>("key", new JsonPrimitive(true)) } };
43+
}
4644

47-
Assert.Equal(items.Length, obj.Count);
48-
for (int i = 0; i < items.Length; i++)
45+
[Theory]
46+
[MemberData(nameof(Ctor_Enumerable_TestData))]
47+
public void Ctor_IEnumerable(IEnumerable<KeyValuePair<string, JsonValue>> items)
48+
{
49+
var obj = new JsonObject(items);
50+
51+
KeyValuePair<string, JsonValue>[] expectedItems = items.ToArray();
52+
Assert.Equal(expectedItems.Length, obj.Count);
53+
for (int i = 0; i < expectedItems.Length; i++)
4954
{
50-
Assert.Equal(items[i].Value.ToString(), obj[items[i].Key].ToString());
55+
Assert.Equal(expectedItems[i].Value.ToString(), obj[expectedItems[i].Key].ToString());
5156

5257
JsonValue value;
53-
Assert.True(obj.TryGetValue(items[i].Key, out value));
54-
Assert.Equal(items[i].Value.ToString(), value.ToString());
58+
Assert.True(obj.TryGetValue(expectedItems[i].Key, out value));
59+
Assert.Equal(expectedItems[i].Value.ToString(), value.ToString());
5560
}
5661
}
5762

58-
[Fact]
59-
public void Ctor_NullArray_Works()
60-
{
61-
JsonObject obj = new JsonObject(null);
62-
Assert.Equal(0, obj.Count);
63-
}
64-
6563
[Fact]
6664
public void Ctor_NullIEnumerable_ThrowsArgumentNullException()
6765
{
6866
AssertExtensions.Throws<ArgumentNullException>("items", () => new JsonObject((IEnumerable<KeyValuePair<string, JsonValue>>)null));
6967
}
7068

7169
[Fact]
72-
public void JsonType_ReturnsObject()
70+
public void JsonType_Get_ReturnsObject()
7371
{
7472
Assert.Equal(JsonType.Object, new JsonObject().JsonType);
7573
}
7674

7775
[Fact]
78-
public void IsReadOnly_ReturnsFalse()
76+
public void IsReadOnly_Get_ReturnsFalse()
7977
{
8078
ICollection<KeyValuePair<string, JsonValue>> iCollection = new JsonObject();
8179
Assert.False(iCollection.IsReadOnly);
8280
}
8381

8482
[Fact]
85-
public void Item_Set_Get()
83+
public void Item_Set_GetReturnsExpected()
8684
{
8785
JsonObject obj = new JsonObject();
8886

src/System.Json/tests/JsonValueTests.cs renamed to src/System.Json/tests/System/Json/JsonValueTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static IEnumerable<object[]> ParseIntegralBoundaries_TestData()
5656
yield return new object[] { "4294967297", "4294967297" };
5757
yield return new object[] { "9223372036854775807", "9223372036854775807" };
5858
yield return new object[] { "18446744073709551615", "18446744073709551615" };
59+
yield return new object[] { "79228162514264337593543950335", "79228162514264337593543950335" };
5960
}
6061

6162
public static IEnumerable<object[]> ParseIntegralBoundaries_TestData_NetFramework()
@@ -206,9 +207,11 @@ public void Parse_NullJsonString_ThrowArgumentNullException()
206207
[InlineData("{\"name\":1")]
207208
[InlineData("1e")]
208209
[InlineData("1e-")]
210+
[InlineData("1.1a")]
209211
[InlineData("\0")]
210212
[InlineData("\u000B1")]
211213
[InlineData("\u000C1")]
214+
[InlineData("\"\\u\"")]
212215
[InlineData("{\"\\a\"}")]
213216
[InlineData("{\"\\z\"}")]
214217
public void Parse_InvalidInput_ThrowsArgumentException(string value)
@@ -248,6 +251,7 @@ public void Parse_InvalidNumericString_ThrowsFormatException()
248251

249252
[Theory]
250253
[InlineData("0", 0)]
254+
[InlineData("9", 9)]
251255
[InlineData("-0", 0)]
252256
[InlineData("0.00", 0)]
253257
[InlineData("-0.00", 0)]
@@ -279,6 +283,17 @@ public void JsonValue_Parse_Double(string json, double expected)
279283
}, json, expected.ToString("R", CultureInfo.InvariantCulture)).Dispose();
280284
}
281285

286+
[Theory]
287+
[InlineData("\"\"", "")]
288+
[InlineData("\"abc\"", "abc")]
289+
[InlineData("\"\\u1234\"", "\u1234")]
290+
[InlineData("\"\\u@234\"", "\u0234")]
291+
[InlineData("\"\\b\\f\\n\\r\\t\"", "\b\f\n\r\t")]
292+
public void JsonValue_Parse_String(string json, string expected)
293+
{
294+
Assert.Equal(expected, (string)JsonValue.Parse(json));
295+
}
296+
282297
// Convert a number to json and parse the string, then compare the result to the original value
283298
[Theory]
284299
[InlineData(1)]
@@ -348,7 +363,6 @@ public void JsonValue_Parse_MinMax_Integers_ViaJsonPrimitive()
348363

349364
[Theory]
350365
[InlineData("Fact\b\f\n\r\t\"\\/</\0x")]
351-
[InlineData("\ud800")]
352366
[InlineData("x\ud800")]
353367
[InlineData("\udfff\ud800")]
354368
[InlineData("\ude03\ud912")]

0 commit comments

Comments
 (0)