-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Improve code coverage for System.Text.Json with targeted unit tests #120731
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@copilot, please mark the DeepEquals_TooDeepJsonDocument_ThrowsInsufficientExecutionStackException test as [Outerloop] and try again, following my original instructions. |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Done. Marked the test as [OuterLoop] and reverted the other test changes that had incorrect assertions. All tests now pass. Changes committed in edead34. |
|
…Deserialize from Span Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Working on it:
Next steps: Will re-run coverage analysis and continue adding tests for remaining uncovered areas. Changes committed in eace0c3. |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@copilot, please keep iterating: run coverage, add more tests to fill gaps, re-run coverage, add more tests to fill gaps, etc. Please try to get line and branch coverage to greater than 95%. |
…th JsonTypeInfo Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…alize methods Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Continuing iterations: Current Progress:
Tests Added:
Next Steps to reach >95%:
Continuing to add more tests... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR systematically improves code coverage for the System.Text.Json library by analyzing coverage reports and adding targeted unit tests. The work progressed through 10 iterations, achieving a +0.54% increase in line coverage (from 93.73% to 94.27%), +0.08% in branch coverage, and +0.38% in method coverage through the addition of 136 new unit tests.
Key changes:
- Added comprehensive test coverage for JsonSerializer DOM APIs with JsonTypeInfo and JsonSerializerContext parameters
- Implemented edge case testing for Utf8JsonReader numeric and floating-point Get methods
- Created extensive multi-segment reading tests for Utf8JsonReader boundary conditions
- Added validation tests for Utf8JsonWriter with extreme input sizes
- Expanded testing for JsonNode, JsonArray, JsonObject, and JsonValue types
- Implemented comprehensive JsonNamingPolicy variant tests
- Added tests for immutable collection serialization
- Marked one long-running test with [OuterLoop] attribute to prevent blocking normal test runs
Reviewed Changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.
Show a summary per file
File | Description |
---|---|
Utf8JsonWriterTests.cs | Added 15 tests for extreme input validation, raw value writing, minimized/indented formatting, and various data type serialization edge cases |
Utf8JsonReaderTests.cs | Added 3 tests for comment handling (Skip and Allow modes) with single-line and multi-line comments |
Utf8JsonReaderTests.TryGet.cs | Added 2 comprehensive tests covering numeric boundary values (byte, int, long, etc.) and floating-point edge cases (single, double) |
Utf8JsonReaderTests.MultiSegment.cs | Added 9 tests for complex multi-segment scenarios including nested JSON, long strings, escaped sequences, numbers with exponents, literals, deep nesting, and comments across segment boundaries |
OptionsTests.cs | Added 8 tests for PreferredObjectCreationHandling, TypeInfoResolverChain, WriteIndented, and DefaultBufferSize properties |
JsonElementTests.cs | Added [OuterLoop] attribute to prevent long-running stack overflow test from blocking normal test runs |
DomTests.cs | Added 43 tests for JsonSerializer methods with JsonTypeInfo/JsonSerializerContext parameters, covering Document/Element/Node serialization, span/stream/reader deserialization, and async PipeWriter/PipeReader operations |
Array.ReadTests.cs | Added 3 tests for large array and nested array deserialization scenarios |
JsonValueTests.cs | Added 8 tests for JsonValue.Create methods with various types and JsonTypeInfo parameters |
JsonObjectTests.cs | Added 4 tests for DeepEquals, TryGetPropertyValue, and ContainsKey operations |
JsonNodeTests.cs | Added 2 tests for ReplaceWith and Replace operations on nodes and properties |
JsonArrayTests.cs | Added 3 tests for DeepEquals with nested arrays and Clear operation behavior |
JsonNamingPolicyTests.cs | Added new test file with 30 tests covering all 5 naming policy variants (CamelCase, SnakeCaseLower, SnakeCaseUpper, KebabCaseLower, KebabCaseUpper) with 6 test cases each |
JsonDocumentTests.cs | Added 6 tests for GetDouble/GetSingle edge cases, JsonDocumentOptions, and Try methods |
CollectionTests.Immutable.Write.cs | Added 4 tests for ImmutableStack, ImmutableQueue, ImmutableHashSet, and ImmutableDictionary serialization |
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/DomTests.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/DomTests.cs
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonDocumentTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/DomTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonNode/JsonValueTests.cs
Outdated
Show resolved
Hide resolved
@copilot, please address the feedback from @eiriktsarpalis |
…o avoid cache recreation Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Fixed in commit 0c7a2e5db. Replaced all instances of creating new JsonSerializerOptions with TypeInfoResolver with JsonSerializerOptions.Default to avoid wasteful cache recreation. |
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonNode/JsonValueTests.cs
Outdated
Show resolved
Hide resolved
…ons.Default Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
[Fact] | ||
public static void DeserializeFromSpan_WithJsonTypeInfo_NonGeneric() | ||
{ | ||
ReadOnlySpan<byte> utf8Json = "{\"StringProp\":\"Hello\",\"IntArrayProp\":[1,2]}"u8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More opportunities to use triple-quote strings here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eiriktsarpalis, copilot won't respond to address your feedback unless you tag it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Summary
Through systematic iterations, this PR improved System.Text.Json coverage from 93.73% to 94.27% (+0.54%) by adding 136 targeted unit tests covering previously untested API surfaces including JsonSerializer methods with JsonTypeInfo/JsonSerializerContext, multi-segment Utf8JsonReader scenarios, JsonWriter formatting options, JsonNode operations, JsonNamingPolicy variants, and collection serialization.
Remaining gaps to reach 95% (~215 lines) consist primarily of complex Utf8JsonReader edge cases requiring specialized buffer setups, ThrowHelper exception paths, and F# converter infrastructure.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.