@@ -3,49 +3,89 @@ import { assertSliced, withShell } from '../../_helper/shell';
3
3
import { useConfigForTest } from '../../_helper/config' ;
4
4
import { label } from '../../_helper/label' ;
5
5
6
- describe . sequential ( 'Vector Single Index Based Access' , withShell ( shell => {
7
- describe . each ( [ { type : '[[' } , { type : '[' } ] ) ( 'Access using $type' , ( { type } ) => {
6
+ describe . sequential ( 'Container Single Index Based Access' , withShell ( shell => {
7
+ describe . each (
8
+ [
9
+ { container : 'c' , type : '[[' , hasNamedArguments : false } ,
10
+ { container : 'c' , type : '[' , hasNamedArguments : false } ,
11
+ { container : 'list' , type : '[[' , hasNamedArguments : false } ,
12
+ { container : 'list' , type : '[' , hasNamedArguments : false } ,
13
+ { container : 'list' , type : '[[' , hasNamedArguments : true } ,
14
+ { container : 'list' , type : '[' , hasNamedArguments : true } ,
15
+ ]
16
+ ) ( 'Access for container $container using $type and hasNamedArguments $hasNamedArguments' , ( { container, type, hasNamedArguments } ) => {
8
17
const basicCapabilities = [
9
18
'name-normal' ,
10
19
'function-calls' ,
11
- 'unnamed-arguments' ,
20
+ hasNamedArguments ? 'named-arguments' : 'unnamed-arguments' ,
12
21
'subsetting' ,
13
22
type === '[[' ? 'double-bracket-access' : 'single-bracket-access'
14
23
] as const ;
15
24
useConfigForTest ( { solver : { pointerTracking : true } } ) ;
16
25
26
+ /**
27
+ * Creates access string
28
+ *
29
+ * Example for name='numbers', index=1 and type='[[':
30
+ * ```r
31
+ * numbers[[1]]
32
+ * ```
33
+ */
17
34
function acc ( name : string , index : number ) {
18
35
const closingBracket = type === '[[' ? ']]' : ']' ;
19
36
return `${ name } ${ type } ${ index } ${ closingBracket } ` ;
20
37
}
21
-
38
+
39
+ /**
40
+ * Creates definition string
41
+ *
42
+ * Example for values=['1', '2', '3', '4'], container='list' and hasNamedArguments=true:
43
+ * ```r
44
+ * list(arg1 = 1, arg2 = 2, arg3 = 3, arg4 = 4)
45
+ * ```
46
+ */
47
+ function def ( ...values : string [ ] ) {
48
+ const parameterList = values . map ( ( value , i ) => {
49
+ if ( hasNamedArguments ) {
50
+ return `arg${ i + 1 } = ${ value } ` ;
51
+ } else {
52
+ return value ;
53
+ }
54
+ } ) . join ( ', ' ) ;
55
+ return `${ container } (${ parameterList } )` ;
56
+ }
57
+
22
58
describe ( 'Simple access' , ( ) => {
23
- assertSliced ( label ( 'Vector with single argument' , basicCapabilities ) , shell ,
24
- `numbers <- c(2)\nprint(${ acc ( 'numbers' , 1 ) } )` ,
59
+ assertSliced ( label ( 'Container with single argument' , basicCapabilities ) , shell ,
60
+ `numbers <- ${ def ( '2' ) }
61
+ print(${ acc ( 'numbers' , 1 ) } )` ,
25
62
[ '2@print' ] ,
26
- `numbers <- c(2)\nprint(${ acc ( 'numbers' , 1 ) } )` ,
63
+ `numbers <- ${ def ( '2' ) }
64
+ print(${ acc ( 'numbers' , 1 ) } )` ,
27
65
) ;
28
66
29
67
/* we reconstruct everything as every other modification could mess with the correctness of the result */
30
- assertSliced ( label ( 'vector with several arguments' , basicCapabilities ) , shell ,
31
- `numbers <- c(1, 2, 3, 4)\nprint(${ acc ( 'numbers' , 1 ) } )` ,
68
+ assertSliced ( label ( 'Container with several arguments' , basicCapabilities ) , shell ,
69
+ `numbers <- ${ def ( '1' , '2' , '3' , '4' ) }
70
+ print(${ acc ( 'numbers' , 1 ) } )` ,
32
71
[ '2@print' ] ,
33
- `numbers <- c(1, 2, 3, 4)\nprint(${ acc ( 'numbers' , 1 ) } )` ,
72
+ `numbers <- ${ def ( '1' , '2' , '3' , '4' ) }
73
+ print(${ acc ( 'numbers' , 1 ) } )` ,
34
74
) ;
35
75
} ) ;
36
76
37
- describe ( 'Whole vector access' , ( ) => {
77
+ describe ( 'Whole container access' , ( ) => {
38
78
assertSliced (
39
- label ( 'When each argument of a vector is redefined, then original vector is still in slice' , basicCapabilities ) ,
79
+ label ( 'When each argument of a container is redefined, then original container is still in slice' , basicCapabilities ) ,
40
80
shell ,
41
- `numbers <- c(1, 2, 3, 4)
81
+ `numbers <- ${ def ( '1' , '2' , '3' , '4' ) }
42
82
${ acc ( 'numbers' , 1 ) } <- 4
43
83
${ acc ( 'numbers' , 2 ) } <- 3
44
84
${ acc ( 'numbers' , 3 ) } <- 2
45
85
${ acc ( 'numbers' , 4 ) } <- 1
46
86
print(numbers)` ,
47
87
[ '6@print' ] ,
48
- `numbers <- c(1, 2, 3, 4)
88
+ `numbers <- ${ def ( '1' , '2' , '3' , '4' ) }
49
89
${ acc ( 'numbers' , 1 ) } <- 4
50
90
${ acc ( 'numbers' , 2 ) } <- 3
51
91
${ acc ( 'numbers' , 3 ) } <- 2
@@ -54,16 +94,16 @@ print(numbers)`
54
94
) ;
55
95
56
96
assertSliced (
57
- label ( 'When arguments are added to an empty vector , then original vector is in slice' , basicCapabilities ) ,
97
+ label ( 'When arguments are added to an empty container , then original container is in slice' , basicCapabilities ) ,
58
98
shell ,
59
- `x <- c()
99
+ `x <- ${ def ( ) }
60
100
${ acc ( 'x' , 1 ) } <- 1
61
101
${ acc ( 'x' , 2 ) } <- 2
62
102
${ acc ( 'x' , 3 ) } <- 3
63
103
${ acc ( 'x' , 4 ) } <- 4
64
104
print(x)` ,
65
105
[ '6@print' ] ,
66
- `x <- c()
106
+ `x <- ${ def ( ) }
67
107
${ acc ( 'x' , 1 ) } <- 1
68
108
${ acc ( 'x' , 2 ) } <- 2
69
109
${ acc ( 'x' , 3 ) } <- 3
@@ -72,17 +112,17 @@ print(x)`
72
112
) ;
73
113
74
114
assertSliced (
75
- label ( 'When whole vector is redefined, then every vector assignment before is not in slice' , basicCapabilities ) ,
115
+ label ( 'When whole container is redefined, then every container assignment before is not in slice' , basicCapabilities ) ,
76
116
shell ,
77
- `numbers <- c(1, 2)
117
+ `numbers <- ${ def ( '1' , '2' ) }
78
118
${ acc ( 'numbers' , 1 ) } <- 2
79
119
${ acc ( 'numbers' , 2 ) } <- 1
80
- numbers <- c(3, 4)
120
+ numbers <- ${ def ( '3' , '4' ) }
81
121
${ acc ( 'numbers' , 1 ) } <- 4
82
122
${ acc ( 'numbers' , 2 ) } <- 3
83
123
print(numbers)` ,
84
124
[ '7@print' ] ,
85
- `numbers <- c(3, 4)
125
+ `numbers <- ${ def ( '3' , '4' ) }
86
126
${ acc ( 'numbers' , 1 ) } <- 4
87
127
${ acc ( 'numbers' , 2 ) } <- 3
88
128
print(numbers)`
@@ -91,26 +131,26 @@ print(numbers)`
91
131
92
132
describe ( 'Access with other accesses' , ( ) => {
93
133
assertSliced (
94
- label ( 'With other vector ' , basicCapabilities ) ,
134
+ label ( 'With other container ' , basicCapabilities ) ,
95
135
shell ,
96
- `numbers <- c(1, 2)
97
- other_numbers <- c(3, 4)
136
+ `numbers <- ${ def ( '1' , '2' ) }
137
+ other_numbers <- ${ def ( '3' , '4' ) }
98
138
a <- ${ acc ( 'other_numbers' , 1 ) }
99
139
print(${ acc ( 'numbers' , 1 ) } )` ,
100
140
[ '4@print' ] ,
101
- `numbers <- c(1, 2)
141
+ `numbers <- ${ def ( '1' , '2' ) }
102
142
print(${ acc ( 'numbers' , 1 ) } )` ,
103
143
) ;
104
144
105
145
assertSliced (
106
146
label ( 'With other accesses' , basicCapabilities ) ,
107
147
shell ,
108
- `numbers <- c(1, 2)
148
+ `numbers <- ${ def ( '1' , '2' ) }
109
149
a <- ${ acc ( 'numbers' , 1 ) }
110
150
b <- ${ acc ( 'numbers' , 2 ) }
111
151
print(${ acc ( 'numbers' , 1 ) } )` ,
112
152
[ '4@print' ] ,
113
- `numbers <- c(1, 2)
153
+ `numbers <- ${ def ( '1' , '2' ) }
114
154
print(${ acc ( 'numbers' , 1 ) } )` ,
115
155
) ;
116
156
} ) ;
@@ -119,72 +159,72 @@ print(${acc('numbers', 1)})`,
119
159
assertSliced (
120
160
label ( 'When there is more than one assignment to the same index, then the last assignment is in the slice' , basicCapabilities ) ,
121
161
shell ,
122
- `numbers <- c(1, 2)
162
+ `numbers <- ${ def ( '1' , '2' ) }
123
163
${ acc ( 'numbers' , 1 ) } <- 3
124
164
${ acc ( 'numbers' , 1 ) } <- 4
125
165
${ acc ( 'numbers' , 1 ) } <- 5
126
166
print(${ acc ( 'numbers' , 1 ) } )` ,
127
167
[ '5@print' ] ,
128
- `numbers <- c(1, 2)
168
+ `numbers <- ${ def ( '1' , '2' ) }
129
169
${ acc ( 'numbers' , 1 ) } <- 5
130
170
print(${ acc ( 'numbers' , 1 ) } )` ,
131
171
) ;
132
172
133
173
assertSliced (
134
174
label ( 'When there are assignments to the other indices, then they are not in the slice' , basicCapabilities ) ,
135
175
shell ,
136
- `numbers <- c(1, 2, 3)
176
+ `numbers <- ${ def ( '1' , '2' , '3' ) }
137
177
${ acc ( 'numbers' , 1 ) } <- 4
138
178
${ acc ( 'numbers' , 2 ) } <- 5
139
179
${ acc ( 'numbers' , 3 ) } <- 6
140
180
print(${ acc ( 'numbers' , 1 ) } )` ,
141
181
[ '5@print' ] ,
142
- `numbers <- c(1, 2, 3)
182
+ `numbers <- ${ def ( '1' , '2' , '3' ) }
143
183
${ acc ( 'numbers' , 1 ) } <- 4
144
184
print(${ acc ( 'numbers' , 1 ) } )` ,
145
185
) ;
146
186
147
187
assertSliced (
148
- label ( 'When there are assignments to only other indices, then only vector is in the slice' , basicCapabilities ) ,
188
+ label ( 'When there are assignments to only other indices, then only container is in the slice' , basicCapabilities ) ,
149
189
shell ,
150
- `numbers <- c(1, 2, 3)
190
+ `numbers <- ${ def ( '1' , '2' , '3' ) }
151
191
${ acc ( 'numbers' , 2 ) } <- 5
152
192
${ acc ( 'numbers' , 3 ) } <- 6
153
193
print(${ acc ( 'numbers' , 1 ) } )` ,
154
194
[ '4@print' ] ,
155
- `numbers <- c(1, 2, 3)
195
+ `numbers <- ${ def ( '1' , '2' , '3' ) }
156
196
print(${ acc ( 'numbers' , 1 ) } )` ,
157
197
) ;
158
198
159
199
describe ( 'Access within conditionals' , ( ) => {
160
200
assertSliced (
161
201
label ( 'Only a potential overwrite' , basicCapabilities ) ,
162
202
shell ,
163
- `numbers <- c(1)
203
+ `numbers <- ${ def ( '1' ) }
164
204
if(u)
165
205
${ acc ( 'numbers' , 1 ) } <- 2
166
206
print(${ acc ( 'numbers' , 1 ) } )` ,
167
207
[ '4@print' ] ,
168
- `numbers <- c(1)
208
+ `numbers <- ${ def ( '1' ) }
169
209
if(u) ${ acc ( 'numbers' , 1 ) } <- 2
170
210
print(${ acc ( 'numbers' , 1 ) } )`
171
211
) ;
172
212
173
213
assertSliced (
174
214
label ( 'Potential wipe' , basicCapabilities ) ,
175
215
shell ,
176
- `numbers <- c(1)
216
+ `numbers <- ${ def ( '1' ) }
177
217
if(u) {
178
218
${ acc ( 'numbers' , 1 ) } <- 2
179
219
} else {
180
220
${ acc ( 'numbers' , 1 ) } <- 3
181
- numbers <- c()
221
+ numbers <- ${ def ( ) }
182
222
}
183
223
print(${ acc ( 'numbers' , 1 ) } )` ,
184
224
[ '8@print' ] ,
185
- `numbers <- c(1)
225
+ `numbers <- ${ def ( '1' ) }
186
226
if(u) { ${ acc ( 'numbers' , 1 ) } <- 2 } else
187
- { numbers <- c() }
227
+ { numbers <- ${ def ( ) } }
188
228
print(${ acc ( 'numbers' , 1 ) } )`
189
229
) ;
190
230
} ) ;
@@ -193,14 +233,14 @@ print(${acc('numbers', 1)})`
193
233
describe ( 'Config flag' , ( ) => {
194
234
useConfigForTest ( { solver : { pointerTracking : false } } ) ;
195
235
assertSliced (
196
- label ( 'When flag is false, then vector access is not in slice' , [ 'call-normal' ] ) ,
236
+ label ( 'When flag is false, then container access is not in slice' , [ 'call-normal' ] ) ,
197
237
shell ,
198
- `numbers <- c(1, 2)
238
+ `numbers <- ${ def ( '1' , '2' ) }
199
239
${ acc ( 'numbers' , 1 ) } <- 3
200
240
${ acc ( 'numbers' , 2 ) } <- 4
201
241
print(${ acc ( 'numbers' , 1 ) } )` ,
202
242
[ '4@print' ] ,
203
- `numbers <- c(1, 2)
243
+ `numbers <- ${ def ( '1' , '2' ) }
204
244
${ acc ( 'numbers' , 1 ) } <- 3
205
245
${ acc ( 'numbers' , 2 ) } <- 4
206
246
print(${ acc ( 'numbers' , 1 ) } )`
0 commit comments