@@ -62,4 +62,186 @@ Describe 'tests for function expressions' {
62
62
$LASTEXITCODE | Should - Be 0
63
63
$out.results [0 ].result.actualState.output | Should - BeExactly $expected
64
64
}
65
+
66
+ It ' union function works for: <expression>' - TestCases @ (
67
+ @ { expression = " [union(parameters('firstArray'), parameters('secondArray'))]" ; expected = @ (' ab' , ' cd' , ' ef' ) }
68
+ @ { expression = " [union(parameters('firstObject'), parameters('secondObject'))]" ; expected = [pscustomobject ]@ { one = ' a' ; two = ' c' ; three = ' d' } }
69
+ @ { expression = " [union(parameters('secondArray'), parameters('secondArray'))]" ; expected = @ (' cd' , ' ef' ) }
70
+ @ { expression = " [union(parameters('secondObject'), parameters('secondObject'))]" ; expected = [pscustomobject ]@ { two = ' c' ; three = ' d' } }
71
+ @ { expression = " [union(parameters('firstObject'), parameters('firstArray'))]" ; isError = $true }
72
+ ) {
73
+ param ($expression , $expected , $isError )
74
+
75
+ $config_yaml = @"
76
+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
77
+ parameters:
78
+ firstObject:
79
+ type: object
80
+ defaultValue:
81
+ one: a
82
+ two: b
83
+ secondObject:
84
+ type: object
85
+ defaultValue:
86
+ two: c
87
+ three: d
88
+ firstArray:
89
+ type: array
90
+ defaultValue:
91
+ - ab
92
+ - cd
93
+ secondArray:
94
+ type: array
95
+ defaultValue:
96
+ - cd
97
+ - ef
98
+ resources:
99
+ - name: Echo
100
+ type: Microsoft.DSC.Debug/Echo
101
+ properties:
102
+ output: "$expression "
103
+ "@
104
+ $out = dsc - l trace config get - i $config_yaml 2> $TestDrive / error.log | ConvertFrom-Json
105
+ if ($isError ) {
106
+ $LASTEXITCODE | Should - Be 2 - Because (Get-Content $TestDrive / error.log - Raw)
107
+ (Get-Content $TestDrive / error.log - Raw) | Should -Match ' All arguments must either be arrays or objects'
108
+ } else {
109
+ $LASTEXITCODE | Should - Be 0 - Because (Get-Content $TestDrive / error.log - Raw)
110
+ ($out.results [0 ].result.actualState.output | Out-String ) | Should - BeExactly ($expected | Out-String )
111
+ }
112
+ }
113
+
114
+ It ' contain function works for: <expression>' - TestCases @ (
115
+ @ { expression = " [contains(parameters('array'), 'a')]" ; expected = $true }
116
+ @ { expression = " [contains(parameters('array'), 2)]" ; expected = $false }
117
+ @ { expression = " [contains(parameters('array'), 1)]" ; expected = $true }
118
+ @ { expression = " [contains(parameters('array'), 'z')]" ; expected = $false }
119
+ @ { expression = " [contains(parameters('object'), 'a')]" ; expected = $true }
120
+ @ { expression = " [contains(parameters('object'), 'c')]" ; expected = $false }
121
+ @ { expression = " [contains(parameters('object'), 3)]" ; expected = $true }
122
+ @ { expression = " [contains(parameters('object'), parameters('object'))]" ; isError = $true }
123
+ @ { expression = " [contains(parameters('array'), parameters('array'))]" ; isError = $true }
124
+ @ { expression = " [contains(parameters('string'), 'not found')]" ; expected = $false }
125
+ @ { expression = " [contains(parameters('string'), 'hello')]" ; expected = $true }
126
+ @ { expression = " [contains(parameters('string'), 12)]" ; expected = $true }
127
+ ) {
128
+ param ($expression , $expected , $isError )
129
+
130
+ $config_yaml = @"
131
+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
132
+ parameters:
133
+ array:
134
+ type: array
135
+ defaultValue:
136
+ - a
137
+ - b
138
+ - 0
139
+ - 1
140
+ object:
141
+ type: object
142
+ defaultValue:
143
+ a: 1
144
+ b: 2
145
+ 3: c
146
+ string:
147
+ type: string
148
+ defaultValue: 'hello 123 world!'
149
+ resources:
150
+ - name: Echo
151
+ type: Microsoft.DSC.Debug/Echo
152
+ properties:
153
+ output: "$expression "
154
+ "@
155
+ $out = dsc - l trace config get - i $config_yaml 2> $TestDrive / error.log | ConvertFrom-Json
156
+ if ($isError ) {
157
+ $LASTEXITCODE | Should - Be 2 - Because (Get-Content $TestDrive / error.log - Raw)
158
+ (Get-Content $TestDrive / error.log - Raw) | Should -Match ' Invalid item to find, must be a string or number'
159
+ } else {
160
+ $LASTEXITCODE | Should - Be 0 - Because (Get-Content $TestDrive / error.log - Raw)
161
+ ($out.results [0 ].result.actualState.output | Out-String ) | Should - BeExactly ($expected | Out-String )
162
+ }
163
+ }
164
+
165
+ It ' length function works for: <expression>' - TestCases @ (
166
+ @ { expression = " [length(parameters('array'))]" ; expected = 3 }
167
+ @ { expression = " [length(parameters('object'))]" ; expected = 4 }
168
+ @ { expression = " [length(parameters('string'))]" ; expected = 12 }
169
+ @ { expression = " [length('')]" ; expected = 0 }
170
+ ) {
171
+ param ($expression , $expected , $isError )
172
+
173
+ $config_yaml = @"
174
+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
175
+ parameters:
176
+ array:
177
+ type: array
178
+ defaultValue:
179
+ - a
180
+ - b
181
+ - c
182
+ object:
183
+ type: object
184
+ defaultValue:
185
+ one: a
186
+ two: b
187
+ three: c
188
+ four: d
189
+ string:
190
+ type: string
191
+ defaultValue: 'hello world!'
192
+ resources:
193
+ - name: Echo
194
+ type: Microsoft.DSC.Debug/Echo
195
+ properties:
196
+ output: "$expression "
197
+ "@
198
+ $out = dsc - l trace config get - i $config_yaml 2> $TestDrive / error.log | ConvertFrom-Json
199
+ $LASTEXITCODE | Should - Be 0 - Because (Get-Content $TestDrive / error.log - Raw)
200
+ ($out.results [0 ].result.actualState.output | Out-String ) | Should - BeExactly ($expected | Out-String )
201
+ }
202
+
203
+ It ' empty function works for: <expression>' - TestCases @ (
204
+ @ { expression = " [empty(parameters('array'))]" ; expected = $false }
205
+ @ { expression = " [empty(parameters('object'))]" ; expected = $false }
206
+ @ { expression = " [empty(parameters('string'))]" ; expected = $false }
207
+ @ { expression = " [empty(parameters('emptyArray'))]" ; expected = $true }
208
+ @ { expression = " [empty(parameters('emptyObject'))]" ; expected = $true }
209
+ @ { expression = " [empty('')]" ; expected = $true }
210
+ ) {
211
+ param ($expression , $expected )
212
+
213
+ $config_yaml = @"
214
+ `$ schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
215
+ parameters:
216
+ array:
217
+ type: array
218
+ defaultValue:
219
+ - a
220
+ - b
221
+ - c
222
+ emptyArray:
223
+ type: array
224
+ defaultValue: []
225
+ object:
226
+ type: object
227
+ defaultValue:
228
+ one: a
229
+ two: b
230
+ three: c
231
+ emptyObject:
232
+ type: object
233
+ defaultValue: {}
234
+ string:
235
+ type: string
236
+ defaultValue: 'hello world!'
237
+ resources:
238
+ - name: Echo
239
+ type: Microsoft.DSC.Debug/Echo
240
+ properties:
241
+ output: "$expression "
242
+ "@
243
+ $out = dsc - l trace config get - i $config_yaml 2> $TestDrive / error.log | ConvertFrom-Json
244
+ $LASTEXITCODE | Should - Be 0 - Because (Get-Content $TestDrive / error.log - Raw)
245
+ ($out.results [0 ].result.actualState.output | Out-String ) | Should - BeExactly ($expected | Out-String )
246
+ }
65
247
}
0 commit comments