Improve code coverage for System.Text.Json with targeted unit tests #120731
+180
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds targeted unit tests to improve code coverage for the System.Text.Json library, specifically focusing on
BitStack
andJsonEncodedText
classes. The changes include both new test coverage and a bug fix in an existing test.Changes
BitStackTests.cs
Bug Fix:
SetResetFirstBit()
test which was incorrectly assertingAssert.False(bitStack.Pop())
after callingSetFirstBit()
. SinceSetFirstBit()
sets the bit to 1 (true), the assertion should beAssert.True()
.New Tests:
PeekTest()
- Tests the previously uncoveredPeek()
method, validating it returns the correct value without modifying stack depthPeekAtDepth64()
- TestsPeek()
behavior at exactly the 64-depth allocation-free threshold, covering the boundary conditionPeekBeyondAllocationFreeDepth()
- TestsPeek()
behavior beyond 64 depth, ensuring the privatePeekInArray()
method is properly exercised with array-backed storageJsonEncodedTextTests.cs
New Tests:
EncodeEmptySpan()
- Tests encoding emptyReadOnlySpan<char>
EncodeEmptyUtf8Span()
- Tests encoding emptyReadOnlySpan<byte>
EncodedUtf8BytesProperty()
- Tests theEncodedUtf8Bytes
property accessorEqualsWithDifferentObjectType()
- TestsEquals()
method with non-JsonEncodedText objectsHashCodeConsistency()
- ValidatesGetHashCode()
returns consistent values across multiple callsEncodeWithEscapingRequired()
- Tests encoding strings that require character escapingEncodeUtf8WithEscapingRequired()
- Tests UTF-8 byte encoding with escaping requirementsEncodeSpanMatchesString()
- Theory test ensuring span encoding produces identical results to string encodingEncodeUtf8MatchesString()
- Theory test ensuring UTF-8 encoding produces identical results to string encodingCoverage Improvements
These tests target previously uncovered code paths:
BitStack.Peek()
andPeekInArray()
methodsTesting Approach
All new tests follow existing patterns in the repository:
[Fact]
,[Theory]
)Original prompt
This pull request was created as a result of the following prompt from Copilot chat.
💡 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.