Skip to content

Commit 9c13adb

Browse files
committed
Resolve remarks
1 parent 371dbd7 commit 9c13adb

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

dsc/tests/dsc_functions.tests.ps1

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,23 +1061,25 @@ Describe 'tests for function expressions' {
10611061
$out.results[0].result.actualState.output | Should -BeExactly $expected
10621062
}
10631063

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 }
1064+
It 'json() works: <accessor>' -TestCases @(
1065+
@{ data = @{ name = 'John'; age = 30 }; accessor = '.name'; expected = 'John' }
1066+
@{ data = @{ name = 'John'; age = 30 }; accessor = '.age'; expected = 30 }
1067+
@{ data = @(1,2,3); accessor = '[0]'; expected = 1 }
1068+
@{ data = @(1,2,3); accessor = '[2]'; expected = 3 }
1069+
@{ data = 'hello'; accessor = ''; expected = 'hello' }
1070+
@{ data = 42; accessor = ''; expected = 42 }
1071+
@{ data = $true; accessor = ''; expected = $true }
1072+
@{ data = $false; accessor = ''; expected = $false }
1073+
@{ data = $null; accessor = ''; expected = $null }
1074+
@{ data = @{ users = @( @{ name = 'Alice' }, @{ name = 'Bob' } ) }; accessor = '.users[0].name'; expected = 'Alice' }
1075+
@{ data = @{ users = @( @{ name = 'Alice' }, @{ name = 'Bob' } ) }; accessor = '.users[1].name'; expected = 'Bob' }
1076+
@{ data = @{ key = 'value' }; accessor = '.key'; expected = 'value' }
1077+
@{ data = @{ nested = @{ value = 123 } }; accessor = '.nested.value'; expected = 123 }
10781078
) {
1079-
param($testInput, $expected)
1080-
$expression = "[json$($testInput -replace "'", "''")]"
1079+
param($data, $accessor, $expected)
1080+
1081+
$jsonString = ConvertTo-Json -Compress -InputObject $data
1082+
$expression = "[json(''$($jsonString)'')$accessor]"
10811083

10821084
$config_yaml = @"
10831085
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
@@ -1092,13 +1094,13 @@ Describe 'tests for function expressions' {
10921094
}
10931095

10941096
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' }
1097+
@{ expression = "[json('not valid json')]" }
1098+
@{ expression = "[json('{""key"":""value""')]" }
1099+
@{ expression = "[json('')]" }
1100+
@{ expression = "[json('{incomplete')]" }
1101+
@{ expression = "[json('[1,2,')]" }
11001102
) {
1101-
param($expression, $expectedError)
1103+
param($expression)
11021104

11031105
$escapedExpression = $expression -replace "'", "''"
11041106
$config_yaml = @"
@@ -1112,6 +1114,6 @@ Describe 'tests for function expressions' {
11121114
$null = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log
11131115
$LASTEXITCODE | Should -Not -Be 0
11141116
$errorContent = Get-Content $TestDrive/error.log -Raw
1115-
$errorContent | Should -Match ([regex]::Escape($expectedError))
1117+
$errorContent | Should -Match ([regex]::Escape('Invalid JSON string'))
11161118
}
11171119
}

0 commit comments

Comments
 (0)