Add compatibility tests for ConvertTo-Json#26639
Add compatibility tests for ConvertTo-Json#26639yotsuda wants to merge 10 commits intoPowerShell:masterPowerShell/PowerShell:masterfrom yotsuda:add-convertto-json-compatibility-testsyotsuda/PowerShell:add-convertto-json-compatibility-testsCopy head branch name to clipboard
Conversation
|
I guess we can add more tests while working on V2 JSON serializer. So I will keep this open some time. |
There was a problem hiding this comment.
Pull request overview
This test-only PR adds compatibility tests for ConvertTo-Json to improve test coverage, extracted from PR #26637. While the intention is good, several of the new tests duplicate existing test coverage.
Key Points:
- Adds 14 new test cases for
ConvertTo-Jsoncmdlet - Several tests provide genuinely new coverage (Uri, enums, nested objects, EscapeNonAscii, depth validation)
- Multiple tests are redundant with existing test coverage (null handling, default escaping, HTML escaping, AsArray, Depth, multiple pipeline objects)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| It 'Should escape HTML with -EscapeHandling EscapeHtml' { | ||
| $json = @{ text = "<>&" } | ConvertTo-Json -Compress -EscapeHandling EscapeHtml | ||
| $json | Should -Match '\\u003C' | ||
| $json | Should -Match '\\u003E' | ||
| $json | Should -Match '\\u0026' | ||
| } |
There was a problem hiding this comment.
The HTML escape handling test is redundant. Lines 84-92 already test -EscapeHandling EscapeHtml with similar escaping validation. The existing test checks for \u0027 (escaped apostrophe), while this test checks for \u003C, \u003E, and \u0026 (escaped HTML characters), but the mechanism and parameter are already covered.
There was a problem hiding this comment.
Kept, but consolidated into TestCases format. Line 87 tests apostrophe escaping ('), while this tests HTML tag characters (<>&). Different character sets require explicit coverage.
test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1
Outdated
Show resolved
Hide resolved
| It 'Should not escape by default' { | ||
| $json = @{ text = "<>&" } | ConvertTo-Json -Compress | ||
| $json | Should -BeExactly '{"text":"<>&"}' | ||
| } |
There was a problem hiding this comment.
The default escaping test is redundant. Lines 84-92 already test escaping behavior including:
- Default behavior (no escaping)
- Explicit
-EscapeHandling Default -EscapeHandling EscapeHtml
This test duplicates the existing coverage without adding new scenarios.
There was a problem hiding this comment.
Kept with renamed test. Line 85 tests apostrophe default behavior, while this tests HTML tag characters (<>&) default behavior. Since EscapeHtml specifically targets HTML characters, explicitly testing their default (non-escaped) behavior adds value.
|
If there are duplicates we could simply add comments in existing tests to describe additional scenarios are tested. If we can not create clear comments please keep duplicate tests. |
|
@iSazonov Sorry for the initial submission with redundant tests. I've refactored based on your feedback: Removed (covered by existing tests):
Consolidated into TestCases format:
Kept:
Added:
Result: +29/-51 lines, removing redundancy while improving edge case coverage. I'll add more test cases if I find other scenarios worth covering while working on V2 improvements. |
|
@yotsuda Have we tests to cover all code paths for |
|
@iSazonov Added tests covering both code paths for
# Pure PSObject - depth exceeded (isPurePSObj = true)
$pso = [PSCustomObject]@{ Name = 'test' }
$pso | Add-Member -NotePropertyName ETSProp -NotePropertyValue 'ets'
@{ inner = $pso } | ConvertTo-Json -Depth 0 -Compress
# Output: {"inner":"@{Name=test; ETSProp=ets}"}
# Non-pure PSObject (Version) - depth exceeded (isPurePSObj = false)
$version = [version]'1.2.3'
$version | Add-Member -NotePropertyName ETSProp -NotePropertyValue 'ets'
@{ inner = $version } | ConvertTo-Json -Depth 0 -Compress
# Output: {"inner":"1.2.3"}Also added a comparison test for pure PSObject serialization within depth limit. Also fixed the DateTime ETS test (f3025f7) to use UTC, avoiding timezone conversion issues that caused intermittent failures. |
|
@yotsuda Thanks! I see if object is pure PSObject LanguagePrimitives.ConvertTo() does all work and add ETS properties so we skipp AddPSProperty(). But I wonder why these properties are not added if isPurePSObj == false - I'd expect AddPSProperty() should add. Thoughts? |
78befa8 to
13e911d
Compare
|
@iSazonov Let me trace through the code flow (referring to V1/JsonObject.cs). When
In
After
So the ETS properties are added for
The properties are not added when:
Note for #26654 (V2/STJ): While testing this, I found an issue in |
|
@yotsuda Please look failed tests. I think it is time to switch to the PR and add more compatibility tests. I will share my thoughts as you will ready. |
|
@yotsuda It would be a pity to lose the experience that we gained while exploring the possibility of migrating to STJ. Object types:
The following options are needed for each type of object:
Additionally, tests are needed for scenarios (most likely it is more convenient to combine them):
By Multilevel compositions, I mean that, for example, the elements of an array can be scalars, enums, array, dictionary, PSCustomObject and Composite object; the same applies to all other composite types. Of course, for all combinations, this would be a huge set of tests. Therefore, they should be reduced reasonably. So we need to check all scalars, but for composite types it makes no sense to check all scalars - you can limit the list to string, datetime, int, enum plus nested composite types with the same restrictions. |
|
@iSazonov Thank you for sharing this comprehensive test plan! I completely agree that having thorough test coverage is essential before any changes to ImplementationI've implemented Phase 1 (scalars + scalars as elements) in commit 735c6cf. The tests cover:
Total: 77 new tests I plan to continue adding the remaining phases (Array, Dictionary, PSCustomObject, Composite, Depth truncation) in this same PR, unless you'd prefer separate PRs. Interesting findingsWhile writing tests, I discovered some behaviors worth documenting:
All my PRs to this repo have been fully automated via PowerShell.MCP - investigating existing behavior, coding, building, and testing without writing a single line myself. Claude Code couldn't handle PowerShell this well. The bigger picture: MCP connects AI to external tools, and countless purpose-specific MCP servers are being developed - Azure MCP Server, GitHub MCP Server, etc. PowerShell.MCP makes all of them obsolete - Az module is far more capable than Azure MCP Server, git.exe/gh.exe far more than GitHub MCP Server. AI can simply use these mature, full-featured tools directly through PowerShell. This positions PowerShell as the universal AI interface - I hope this could be good news for the PowerShell community. Feedback welcome! cc: @mklement0 |
|
|
||
| } | ||
|
|
||
| #region Comprehensive Scalar Type Tests (Phase 1) |
There was a problem hiding this comment.
There are too many new tests above. We can get confused. I think it will be easier for us if we make a separate PR for each phase.
There was a problem hiding this comment.
Agreed. I think the comprehensive tests should be in separate PRs to make review easier.
I've restructured this PR:
-
This PR (Add compatibility tests for ConvertTo-Json #26639) - Existing tests only:
- Core behavior tests (null handling, ETS properties, EscapeHandling)
- Depth exceeded behavior tests (isPurePSObj distinction)
- Nested raw object serialization tests
-
Separate PR (to follow) - Phase 1 scalar type tests:
- Primitive types (int, long, double, bool, etc.)
- String types (escape sequences, unicode, emoji)
- DateTime and related types
- Guid, Uri, Enum, IPAddress
- Pipeline vs InputObject behavior
- Scalars in arrays and hashtables
- ETS properties on scalar types
I'll follow up with separate PRs for subsequent phases.
735c6cf to
53f6daf
Compare
…o-Json Add 34 new Pester tests covering: - Depth parameter basic behavior (default and explicit depth values) - Depth truncation with arrays and hashtables - Depth truncation string representations - Multilevel compositions (Array/Dictionary/PSCustomObject/Class) - Complex nested mixed type structures - Pipeline vs InputObject behavior This is Phase 4 of the test plan from PR PowerShell#26639. Co-Authored-By: Claude <noreply@anthropic.com>
…o-Json Add 34 new Pester tests covering: - Depth parameter basic behavior (default and explicit depth values) - Depth truncation with arrays and hashtables - Depth truncation string representations - Multilevel compositions (Array/Dictionary/PSCustomObject/Class) - Complex nested mixed type structures - Pipeline vs InputObject behavior This is Phase 4 of the test plan from PR PowerShell#26639. Co-Authored-By: Claude <noreply@anthropic.com>
…o-Json Add 34 new Pester tests covering: - Depth parameter basic behavior (default and explicit depth values) - Depth truncation with arrays and hashtables - Depth truncation string representations - Multilevel compositions (Array/Dictionary/PSCustomObject/Class) - Complex nested mixed type structures - Pipeline vs InputObject behavior This is Phase 4 of the test plan from PR PowerShell#26639. Co-Authored-By: Claude <noreply@anthropic.com>
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
PR Summary
Add compatibility tests for
ConvertTo-Jsonto improve test coverage.PR Context
This PR adds tests to validate
ConvertTo-Jsonbehavior, extracted from #26637 as suggested by @iSazonov.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerTests Added
The following tests are added to
ConvertTo-Json.Tests.ps1: