@@ -1060,4 +1060,58 @@ Describe 'tests for function expressions' {
10601060 $out = $config_yaml | dsc config get -f - | ConvertFrom-Json
10611061 $out.results [0 ].result.actualState.output | Should - BeExactly $expected
10621062 }
1063+
1064+ It ' json() works: <testInput>' - TestCases @ (
1065+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject @ { name = ' John' ; age = 30 }) ').name" ; expected = ' John' }
1066+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject @ { name = ' John' ; age = 30 }) ').age" ; expected = 30 }
1067+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject @ (1 , 2 , 3 )) ')[0]" ; expected = 1 }
1068+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject @ (1 , 2 , 3 )) ')[2]" ; expected = 3 }
1069+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject ' hello' ) ')" ; expected = ' hello' }
1070+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject 42 ) ')" ; expected = 42 }
1071+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject $true ) ')" ; expected = $true }
1072+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject $false ) ')" ; expected = $false }
1073+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject $null ) ')" ; expected = $null }
1074+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject @ { users = @ ( @ { name = ' Alice' }, @ { name = ' Bob' } ) }) ').users[0].name" ; expected = ' Alice' }
1075+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject @ { users = @ ( @ { name = ' Alice' }, @ { name = ' Bob' } ) }) ').users[1].name" ; expected = ' Bob' }
1076+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject @ { key = ' value' }) ').key" ; expected = ' value' }
1077+ @ { testInput = " ('$ ( ConvertTo-Json - Compress - InputObject @ { nested = @ { value = 123 } }) ').nested.value" ; expected = 123 }
1078+ ) {
1079+ param ($testInput , $expected )
1080+ $expression = " [json$ ( $testInput -replace " '" , " ''" ) ]"
1081+
1082+ $config_yaml = @"
1083+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
1084+ resources:
1085+ - name: Echo
1086+ type: Microsoft.DSC.Debug/Echo
1087+ properties:
1088+ output: '$expression '
1089+ "@
1090+ $out = $config_yaml | dsc config get -f - | ConvertFrom-Json
1091+ $out.results [0 ].result.actualState.output | Should - Be $expected
1092+ }
1093+
1094+ It ' json() error handling: <expression>' - TestCases @ (
1095+ @ { expression = " [json('not valid json')]" ; expectedError = ' Invalid JSON string' }
1096+ @ { expression = " [json('{"" key"" :"" value"" ')]" ; expectedError = ' Invalid JSON string' }
1097+ @ { expression = " [json('')]" ; expectedError = ' Invalid JSON string' }
1098+ @ { expression = " [json('{incomplete')]" ; expectedError = ' Invalid JSON string' }
1099+ @ { expression = " [json('[1,2,')]" ; expectedError = ' Invalid JSON string' }
1100+ ) {
1101+ param ($expression , $expectedError )
1102+
1103+ $escapedExpression = $expression -replace " '" , " ''"
1104+ $config_yaml = @"
1105+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
1106+ resources:
1107+ - name: Echo
1108+ type: Microsoft.DSC.Debug/Echo
1109+ properties:
1110+ output: '$escapedExpression '
1111+ "@
1112+ $null = dsc - l trace config get - i $config_yaml 2> $TestDrive / error.log
1113+ $LASTEXITCODE | Should -Not - Be 0
1114+ $errorContent = Get-Content $TestDrive / error.log - Raw
1115+ $errorContent | Should -Match ([regex ]::Escape($expectedError ))
1116+ }
10631117}
0 commit comments