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

Code before:
        }

        It 'Should serialize array of Guid with Extended properties via Pipeline' {
            $guids = @(
                [Guid]'11111111-1111-1111-1111-111111111111',
                [Guid]'22222222-2222-2222-2222-222222222222'
            )
            $json = $guids | ConvertTo-Json -Compress
            # Pipeline adds Extended properties to each Guid
            $json | Should -Match '"value":"11111111-1111-1111-1111-111111111111"'
            $json | Should -Match '"value":"22222222-2222-2222-2222-222222222222"'
        }

        It 'Should serialize array of enum correctly' {
            $enums = @([DayOfWeek]::Monday, [DayOfWeek]::Wednesday, [DayOfWeek]::Friday)
            $json = $enums | ConvertTo-Json -Compress
            $json | Should -BeExactly '[1,3,5]'
        }

        It 'Should serialize array of enum as strings with -EnumsAsStrings' {
            $enums = @([DayOfWeek]::Monday, [DayOfWeek]::Wednesday, [DayOfWeek]::Friday)
            $json = $enums | ConvertTo-Json -Compress -EnumsAsStrings
            $json | Should -BeExactly '["Monday","Wednesday","Friday"]'
        }

        It 'Should serialize mixed type array correctly' {
            $mixed = @(1, 'two', $true, 3.14)
            $json = $mixed | ConvertTo-Json -Compress
            $json | Should -BeExactly '[1,"two",true,3.14]'
        }

        It 'Should serialize array with null elements correctly' {
            $arr = @(1, $null, 'three')
            $json = $arr | ConvertTo-Json -Compress
            $json | Should -BeExactly '[1,null,"three"]'
        }
    }

    Context 'Scalars as values in hashtables and PSCustomObject' {
        It 'Should serialize hashtable with scalar values correctly' {
            $hash = [ordered]@{
                intVal = 42
                strVal = 'hello'
                boolVal = $true
                doubleVal = 3.14
                nullVal = $null
            }
            $json = $hash | ConvertTo-Json -Compress
            $json | Should -BeExactly '{"intVal":42,"strVal":"hello","boolVal":true,"doubleVal":3.14,"nullVal":null}'
        }


Review comment (suggestion):
I'd make one test with two checks:
1. array containing all types we are interesting on
2. second check for members with ETS (and for array itself).

For simplicity I'd use TestCases with member types (e.g. create array with one element per test.)

Code after:
            @{ EnumType = 'System.DayOfWeek'; Value = 'Monday'; Expected = '1' }
            @{ EnumType = 'System.DayOfWeek'; Value = 'Saturday'; Expected = '6' }
            @{ EnumType = 'System.ConsoleColor'; Value = 'Red'; Expected = '12' }
            @{ EnumType = 'System.IO.FileAttributes'; Value = 'ReadOnly'; Expected = '1' }
            @{ EnumType = 'System.IO.FileAttributes'; Value = 'Hidden'; Expected = '2' }
        ) {
            param($EnumType, $Value, $Expected)
            $enumValue = [Enum]::Parse($EnumType, $Value)
            $jsonPipeline = $enumValue | ConvertTo-Json -Compress
            $jsonInputObject = ConvertTo-Json -InputObject $enumValue -Compress
            $jsonPipeline | Should -BeExactly $Expected
            $jsonInputObject | Should -BeExactly $Expected
        }

        It 'Should serialize enum as "<Expected>" with -EnumsAsStrings' -TestCases @(
            @{ EnumType = 'System.DayOfWeek'; Value = 'Sunday'; Expected = 'Sunday' }
            @{ EnumType = 'System.DayOfWeek'; Value = 'Monday'; Expected = 'Monday' }
            @{ EnumType = 'System.ConsoleColor'; Value = 'Red'; Expected = 'Red' }
        ) {
            param($EnumType, $Value, $Expected)
            $enumValue = [Enum]::Parse($EnumType, $Value)
            $json = $enumValue | ConvertTo-Json -Compress -EnumsAsStrings
            $json | Should -BeExactly "`"$Expected`""
        }

        It 'Should serialize flags enum correctly via Pipeline and InputObject' {
            $flags = [System.IO.FileAttributes]::ReadOnly -bor [System.IO.FileAttributes]::Hidden
            $jsonPipeline = $flags | ConvertTo-Json -Compress
            $jsonInputObject = ConvertTo-Json -InputObject $flags -Compress
            $jsonPipeline | Should -BeExactly '3'
            $jsonInputObject | Should -BeExactly '3'
        }

        It 'Should serialize flags enum as string with -EnumsAsStrings' {
            $flags = [System.IO.FileAttributes]::ReadOnly -bor [System.IO.FileAttributes]::Hidden
            $json = $flags | ConvertTo-Json -Compress -EnumsAsStrings
            $json | Should -BeExactly '"ReadOnly, Hidden"'
        }

        It 'Should include ETS properties on Enum' {
            $enum = Add-Member -InputObject ([DayOfWeek]::Monday) -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
            $json = $enum | ConvertTo-Json -Compress
            $json | Should -BeExactly '{"value":1,"MyProp":"test"}'
        }
    }

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