Repository: PowerShell/PowerShell
File: test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 (PowerShell)

Code before:
            $ip = [System.Net.IPAddress]::Parse('192.168.1.1')
            $json = $ip | ConvertTo-Json -Compress
            $json | Should -BeExactly '{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952,"IPAddressToString":"192.168.1.1"}'
        }

        It 'Should serialize IPAddress v6 correctly via InputObject' {
            $ip = [System.Net.IPAddress]::Parse('::1')
            $json = ConvertTo-Json -InputObject $ip -Compress
            $json | Should -BeExactly '{"AddressFamily":23,"ScopeId":0,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":null}'
        }

        It 'Should serialize IPAddress v6 correctly via Pipeline' {
            $ip = [System.Net.IPAddress]::Parse('::1')
            $json = $ip | ConvertTo-Json -Compress
            $json | Should -BeExactly '{"AddressFamily":23,"ScopeId":0,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":null,"IPAddressToString":"::1"}'
        }

        It 'Should include ETS properties on IPAddress' {
            $ip = [System.Net.IPAddress]::Parse('192.168.1.1')
            $ip = Add-Member -InputObject $ip -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
            $json = $ip | ConvertTo-Json -Compress
            $json | Should -BeExactly '{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952,"MyProp":"test","IPAddressToString":"192.168.1.1"}'
        }
    }


    Context 'Scalars as elements of arrays' {
        It 'Should serialize array of <TypeName> correctly via Pipeline and InputObject' -TestCases @(
            @{ TypeName = 'int'; Values = @(1, 2, 3); Expected = '[1,2,3]' }
            @{ TypeName = 'string'; Values = @('a', 'b', 'c'); Expected = '["a","b","c"]' }
            @{ TypeName = 'double'; Values = @(1.1, 2.2, 3.3); Expected = '[1.1,2.2,3.3]' }
        ) {
            param($TypeName, $Values, $Expected)
            $jsonPipeline = $Values | ConvertTo-Json -Compress
            $jsonInputObject = ConvertTo-Json -InputObject $Values -Compress
            $jsonPipeline | Should -BeExactly $Expected
            $jsonInputObject | Should -BeExactly $Expected
        }

        # Note: bool array test uses InputObject only because $true/$false are singletons
        # and ETS properties added in other tests would affect Pipeline serialization
        It 'Should serialize array of bool correctly via InputObject' {
            $bools = @($true, $false, $true)
            $json = ConvertTo-Json -InputObject $bools -Compress
            $json | Should -BeExactly '[true,false,true]'
        }

        It 'Should serialize array of Guid with Extended properties via Pipeline' {
            $guids = @(
                [Guid]'11111111-1111-1111-1111-111111111111',
                [Guid]'22222222-2222-2222-2222-222222222222'

Review comment (suggestion):
Extra line.
```suggestion

    Context 'Scalars as elements of arrays' {
```

Code after:
            $ip = [System.Net.IPAddress]::Parse('192.168.1.1')
            $json = $ip | ConvertTo-Json -Compress
            $json | Should -BeExactly '{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952,"IPAddressToString":"192.168.1.1"}'
        }

        It 'Should serialize IPAddress v6 correctly via InputObject' {
            $ip = [System.Net.IPAddress]::Parse('::1')
            $json = ConvertTo-Json -InputObject $ip -Compress
            $json | Should -BeExactly '{"AddressFamily":23,"ScopeId":0,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":null}'
        }

        It 'Should serialize IPAddress v6 correctly via Pipeline' {
            $ip = [System.Net.IPAddress]::Parse('::1')
            $json = $ip | ConvertTo-Json -Compress
            $json | Should -BeExactly '{"AddressFamily":23,"ScopeId":0,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":null,"IPAddressToString":"::1"}'
        }

        It 'Should include ETS properties on IPAddress' {
            $ip = [System.Net.IPAddress]::Parse('192.168.1.1')
            $ip = Add-Member -InputObject $ip -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
            $json = $ip | ConvertTo-Json -Compress
            $json | Should -BeExactly '{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952,"MyProp":"test","IPAddressToString":"192.168.1.1"}'
        }
    }

    Context 'Scalars as elements of arrays' {
        It 'Should serialize array of <TypeName> correctly via Pipeline and InputObject' -TestCases @(
            @{ TypeName = 'int'; Values = @(1, 2, 3); Expected = '[1,2,3]' }
            @{ TypeName = 'string'; Values = @('a', 'b', 'c'); Expected = '["a","b","c"]' }
            @{ TypeName = 'double'; Values = @(1.1, 2.2, 3.3); Expected = '[1.1,2.2,3.3]' }
        ) {
            param($TypeName, $Values, $Expected)
            $jsonPipeline = $Values | ConvertTo-Json -Compress
            $jsonInputObject = ConvertTo-Json -InputObject $Values -Compress
            $jsonPipeline | Should -BeExactly $Expected
            $jsonInputObject | Should -BeExactly $Expected
        }

        # Note: bool array test uses InputObject only because $true/$false are singletons
        # and ETS properties added in other tests would affect Pipeline serialization
        It 'Should serialize array of bool correctly via InputObject' {
            $bools = @($true, $false, $true)
            $json = ConvertTo-Json -InputObject $bools -Compress
            $json | Should -BeExactly '[true,false,true]'
        }

        It 'Should serialize array of Guid with Extended properties via Pipeline' {
            $guids = @(
                [Guid]'11111111-1111-1111-1111-111111111111',
                [Guid]'22222222-2222-2222-2222-222222222222'
            )