-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathdsc_lambda.tests.ps1
More file actions
134 lines (126 loc) · 4.34 KB
/
dsc_lambda.tests.ps1
File metadata and controls
134 lines (126 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'map() function with lambda tests' {
It 'map with simple lambda multiplies each element by 2' {
$config_yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
parameters:
numbers:
type: array
defaultValue: [1, 2, 3]
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "[map(parameters('numbers'), lambda('x', mul(lambdaVariables('x'), 2)))]"
'@
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -Be @(2,4,6)
}
It 'map with lambda using index parameter' {
$config_yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
parameters:
items:
type: array
defaultValue: [10, 20, 30]
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "[map(parameters('items'), lambda('val', 'i', add(lambdaVariables('val'), lambdaVariables('i'))))]"
'@
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -Be @(10,21,32)
}
It 'map with range generates array' {
$config_yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "[map(range(0, 3), lambda('x', mul(lambdaVariables('x'), 3)))]"
'@
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -Be @(0,3,6)
}
It 'map returns empty array for empty input' {
$config_yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "[map(createArray(), lambda('x', mul(lambdaVariables('x'), 2)))]"
'@
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output.Count | Should -Be 0
}
}
Describe 'filter() function with lambda tests' {
It 'filter with simple lambda filters elements greater than 2' {
$config_yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
parameters:
numbers:
type: array
defaultValue: [1, 2, 3, 4, 5]
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "[filter(parameters('numbers'), lambda('x', greater(lambdaVariables('x'), 2)))]"
'@
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -Be @(3,4,5)
}
It 'filter with lambda using index parameter' {
$config_yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
parameters:
items:
type: array
defaultValue: [10, 20, 30, 40]
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "[filter(parameters('items'), lambda('val', 'i', less(lambdaVariables('i'), 2)))]"
'@
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -Be @(10,20)
}
It 'filter returns empty array when no elements match' {
$config_yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "[filter(createArray(1, 2, 3), lambda('x', greater(lambdaVariables('x'), 10)))]"
'@
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output.Count | Should -Be 0
}
It 'filter returns all elements when all match' {
$config_yaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "[filter(createArray(5, 6, 7), lambda('x', greater(lambdaVariables('x'), 2)))]"
'@
$out = $config_yaml | dsc config get -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.actualState.output | Should -Be @(5,6,7)
}
}