Skip to content

Commit

Permalink
Clean up JsonElementWriteTests (dotnet/corefx#40468)
Browse files Browse the repository at this point in the history
* Make JsonElementWriteTests and JsonDocumentWriteTests share drivers
* Fix the casing of all escaped values in these tests (reducing the need for normalization)
* Hard-code the correct answer for a test that used NewtonSoft.Json to dynamically compute the same answer every run
* Make two async tests declare async, and await the async part.

As a followup the JsonElementWriteTests / JsonDocumentWriteTests / JsonReadonlyDomWriteTests classes can be split to multiple files, but in this PR it keeps the edits reduced to only the semantic changes (inheritance).

Commit migrated from dotnet/corefx@95d012c
  • Loading branch information
bartonjs authored and stephentoub committed Aug 26, 2019
1 parent 1e8663d commit 160b298
Show file tree
Hide file tree
Showing 2 changed files with 303 additions and 324 deletions.
52 changes: 4 additions & 48 deletions src/libraries/System.Text.Json/tests/JsonDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,11 @@ public static void ParseJson_Stream_ClearRentedBuffer_WhenThrow_CodeCoverage()
}

[Fact]
public static void ParseJson_Stream_Async_ClearRentedBuffer_WhenThrow_CodeCoverage()
public static async Task ParseJson_Stream_Async_ClearRentedBuffer_WhenThrow_CodeCoverage()
{
using (Stream stream = new ThrowOnReadStream(new byte[] { 1 }))
{
Assert.ThrowsAsync<EndOfStreamException>(async () => await JsonDocument.ParseAsync(stream));
await Assert.ThrowsAsync<EndOfStreamException>(async () => await JsonDocument.ParseAsync(stream));
}
}

Expand All @@ -439,11 +439,11 @@ public static void ParseJson_Stream_ThrowsOn_ArrayPoolRent_CodeCoverage()
}

[Fact]
public static void ParseJson_Stream_Async_ThrowsOn_ArrayPoolRent_CodeCoverage()
public static async Task ParseJson_Stream_Async_ThrowsOn_ArrayPoolRent_CodeCoverage()
{
using (Stream stream = new ThrowOnCanSeekStream(new byte[] { 1 }))
{
Assert.ThrowsAsync<InsufficientMemoryException>(async () => await JsonDocument.ParseAsync(stream));
await Assert.ThrowsAsync<InsufficientMemoryException>(async () => await JsonDocument.ParseAsync(stream));
}
}

Expand Down Expand Up @@ -1836,15 +1836,6 @@ public static void CheckUseDefault()
});
}

[Fact]
public static void CheckByPassingNullWriter()
{
using (JsonDocument doc = JsonDocument.Parse("true", default))
{
AssertExtensions.Throws<ArgumentNullException>("writer", () => doc.WriteTo(null));
}
}

[Fact]
public static void CheckInvalidString()
{
Expand Down Expand Up @@ -3678,41 +3669,6 @@ private static string GetCompactJson(TestCaseType testCaseType, string jsonStrin
return s_compactJson[testCaseType] = existing;
}

[Fact]
public static void WriteNumberTooLargeScientific()
{
// This value is a reference "potential interoperability problem" from
// https://tools.ietf.org/html/rfc7159#section-6
const string OneQuarticGoogol = "1e400";

// This just validates we write the literal number 1e400 even though it is too
// large to be represented by System.Double and would be converted to
// PositiveInfinity instead (or throw if using double.Parse on frameworks
// older than .NET Core 3.0).
var buffer = new ArrayBufferWriter<byte>(1024);
var expectedNonIndentedJson = $"[{OneQuarticGoogol}]";
using (JsonDocument doc = JsonDocument.Parse($"[ {OneQuarticGoogol} ]"))
{
using var writer = new Utf8JsonWriter(buffer, default);
doc.WriteTo(writer);
writer.Flush();

AssertContents(expectedNonIndentedJson, buffer);
}
}

private static void AssertContents(string expectedValue, ArrayBufferWriter<byte> buffer)
{
Assert.Equal(
expectedValue,
Encoding.UTF8.GetString(
buffer.WrittenSpan
#if netfx
.ToArray()
#endif
));
}

[Fact]
public static void VerifyMultiThreadedDispose()
{
Expand Down
Loading

0 comments on commit 160b298

Please sign in to comment.