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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/pops'
require 'puppet/pops/evaluator/evaluator_impl'
require 'puppet/pops/types/type_factory'
# relative to this spec file (./) does not work as this file is loaded by rspec
require File.join(File.dirname(__FILE__), '/evaluator_rspec_helper')
describe 'Puppet::Pops::Evaluator::EvaluatorImpl/AccessOperator' do
include EvaluatorRspecHelper
def range(from, to)
Puppet::Pops::Types::TypeFactory.range(from, to)
end
def float_range(from, to)
Puppet::Pops::Types::TypeFactory.float_range(from, to)
end
context 'The evaluator when operating on a String' do
it 'can get a single character using a single key index to []' do
expect(evaluate(literal('abc')[1])).to eql('b')
end
it 'can get the last character using the key -1 in []' do
expect(evaluate(literal('abc')[-1])).to eql('c')
end
it 'can get a substring by giving two keys' do
expect(evaluate(literal('abcd')[1,2])).to eql('bc')
# flattens keys
expect(evaluate(literal('abcd')[[1,2]])).to eql('bc')
end
it 'produces empty string for a substring out of range' do
expect(evaluate(literal('abc')[100])).to eql('')
end
it 'raises an error if arity is wrong for []' do
expect{evaluate(literal('abc')[])}.to raise_error(/String supports \[\] with one or two arguments\. Got 0/)
expect{evaluate(literal('abc')[1,2,3])}.to raise_error(/String supports \[\] with one or two arguments\. Got 3/)
end
end
context 'The evaluator when operating on an Array' do
it 'is tested with the correct assumptions' do
expect(literal([1,2,3])[1].current.is_a?(Puppet::Pops::Model::AccessExpression)).to eql(true)
end
it 'can get an element using a single key index to []' do
expect(evaluate(literal([1,2,3])[1])).to eql(2)
end
it 'can get the last element using the key -1 in []' do
expect(evaluate(literal([1,2,3])[-1])).to eql(3)
end
it 'can get a slice of elements using two keys' do
expect(evaluate(literal([1,2,3,4])[1,2])).to eql([2,3])
# flattens keys
expect(evaluate(literal([1,2,3,4])[[1,2]])).to eql([2,3])
end
it 'produces nil for a missing entry' do
expect(evaluate(literal([1,2,3])[100])).to eql(nil)
end
it 'raises an error if arity is wrong for []' do
expect{evaluate(literal([1,2,3,4])[])}.to raise_error(/Array supports \[\] with one or two arguments\. Got 0/)
expect{evaluate(literal([1,2,3,4])[1,2,3])}.to raise_error(/Array supports \[\] with one or two arguments\. Got 3/)
end
end
context 'The evaluator when operating on a Hash' do
it 'can get a single element giving a single key to []' do
expect(evaluate(literal({'a'=>1,'b'=>2,'c'=>3})['b'])).to eql(2)
end
it 'can lookup an array' do
expect(evaluate(literal({[1]=>10,[2]=>20})[[2]])).to eql(20)
end
it 'produces nil for a missing key' do
expect(evaluate(literal({'a'=>1,'b'=>2,'c'=>3})['x'])).to eql(nil)
end
it 'can get multiple elements by giving multiple keys to []' do
expect(evaluate(literal({'a'=>1,'b'=>2,'c'=>3, 'd'=>4})['b', 'd'])).to eql([2, 4])
end
it 'compacts the result when using multiple keys' do
expect(evaluate(literal({'a'=>1,'b'=>2,'c'=>3, 'd'=>4})['b', 'x'])).to eql([2])
end
it 'produces an empty array if none of multiple given keys were missing' do
expect(evaluate(literal({'a'=>1,'b'=>2,'c'=>3, 'd'=>4})['x', 'y'])).to eql([])
end
it 'raises an error if arity is wrong for []' do
expect{evaluate(literal({'a'=>1,'b'=>2,'c'=>3})[])}.to raise_error(/Hash supports \[\] with one or more arguments\. Got 0/)
end
end
context "When applied to a type it" do
let(:types) { Puppet::Pops::Types::TypeFactory }
# Integer
#
it 'produces an Integer[from, to]' do
expr = fqr('Integer')[1, 3]
expect(evaluate(expr)).to eql(range(1,3))
# arguments are flattened
expr = fqr('Integer')[[1, 3]]
expect(evaluate(expr)).to eql(range(1,3))
end
it 'produces an Integer[1]' do
expr = fqr('Integer')[1]
expect(evaluate(expr)).to eql(range(1,1))
end
it 'produces an Integer[from, <from]' do
expr = fqr('Integer')[1,0]
expect(evaluate(expr)).to eql(range(1,0))
end
it 'produces an error for Integer[] if there are more than 2 keys' do
expr = fqr('Integer')[1,2,3]
expect { evaluate(expr)}.to raise_error(/with one or two arguments/)
end
# Float
#
it 'produces a Float[from, to]' do
expr = fqr('Float')[1, 3]
expect(evaluate(expr)).to eql(float_range(1.0,3.0))
# arguments are flattened
expr = fqr('Float')[[1, 3]]
expect(evaluate(expr)).to eql(float_range(1.0,3.0))
end
it 'produces a Float[1.0]' do
expr = fqr('Float')[1.0]
expect(evaluate(expr)).to eql(float_range(1.0,1.0))
end
it 'produces a Float[1]' do
expr = fqr('Float')[1]
expect(evaluate(expr)).to eql(float_range(1.0,1.0))
end
it 'produces a Float[from, <from]' do
expr = fqr('Float')[1.0,0.0]
expect(evaluate(expr)).to eql(float_range(1.0,0.0))
end
it 'produces an error for Float[] if there are more than 2 keys' do
expr = fqr('Float')[1,2,3]
expect { evaluate(expr)}.to raise_error(/with one or two arguments/)
end
# Hash Type
#
it 'produces a Hash[Scalar,String] from the expression Hash[String]' do
expr = fqr('Hash')[fqr('String')]
expect(evaluate(expr)).to be_the_type(types.hash_of(types.string, types.scalar))
# arguments are flattened
expr = fqr('Hash')[[fqr('String')]]
expect(evaluate(expr)).to be_the_type(types.hash_of(types.string, types.scalar))
end
it 'produces a Hash[String,String] from the expression Hash[String, String]' do
expr = fqr('Hash')[fqr('String'), fqr('String')]
expect(evaluate(expr)).to be_the_type(types.hash_of(types.string, types.string))
end
it 'produces a Hash[Scalar,String] from the expression Hash[Integer][String]' do
expr = fqr('Hash')[fqr('Integer')][fqr('String')]
expect(evaluate(expr)).to be_the_type(types.hash_of(types.string, types.scalar))
end
it "gives an error if parameter is not a type" do
expr = fqr('Hash')['String']
expect { evaluate(expr)}.to raise_error(/Hash-Type\[\] arguments must be types/)
end
# Array Type
#
it 'produces an Array[String] from the expression Array[String]' do
expr = fqr('Array')[fqr('String')]
expect(evaluate(expr)).to be_the_type(types.array_of(types.string))
# arguments are flattened
expr = fqr('Array')[[fqr('String')]]
expect(evaluate(expr)).to be_the_type(types.array_of(types.string))
end
it 'produces an Array[String] from the expression Array[Integer][String]' do
expr = fqr('Array')[fqr('Integer')][fqr('String')]
expect(evaluate(expr)).to be_the_type(types.array_of(types.string))
end
it 'produces a size constrained Array when the last two arguments specify this' do
expr = fqr('Array')[fqr('String'), 1]
expected_t = types.array_of(String)
types.constrain_size(expected_t, 1, :default)
expect(evaluate(expr)).to be_the_type(expected_t)
expr = fqr('Array')[fqr('String'), 1, 2]
expected_t = types.array_of(String)
types.constrain_size(expected_t, 1, 2)
expect(evaluate(expr)).to be_the_type(expected_t)
end
it "Array parameterization gives an error if parameter is not a type" do
expr = fqr('Array')['String']
expect { evaluate(expr)}.to raise_error(/Array-Type\[\] arguments must be types/)
end
# Tuple Type
#
it 'produces a Tuple[String] from the expression Tuple[String]' do
expr = fqr('Tuple')[fqr('String')]
expect(evaluate(expr)).to be_the_type(types.tuple(String))
# arguments are flattened
expr = fqr('Tuple')[[fqr('String')]]
expect(evaluate(expr)).to be_the_type(types.tuple(String))
end
it "Tuple parameterization gives an error if parameter is not a type" do
expr = fqr('Tuple')['String']
expect { evaluate(expr)}.to raise_error(/Tuple-Type, Cannot use String where Any-Type is expected/)
end
it 'produces a varargs Tuple when the last two arguments specify size constraint' do
expr = fqr('Tuple')[fqr('String'), 1]
expected_t = types.tuple(String)
types.constrain_size(expected_t, 1, :default)
expect(evaluate(expr)).to be_the_type(expected_t)
expr = fqr('Tuple')[fqr('String'), 1, 2]
expected_t = types.tuple(String)
types.constrain_size(expected_t, 1, 2)
expect(evaluate(expr)).to be_the_type(expected_t)
end
# Pattern Type
#
it 'creates a PPatternType instance when applied to a Pattern' do
regexp_expr = fqr('Pattern')['foo']
expect(evaluate(regexp_expr)).to eql(Puppet::Pops::Types::TypeFactory.pattern('foo'))
end
# Regexp Type
#
it 'creates a Regexp instance when applied to a Pattern' do
regexp_expr = fqr('Regexp')['foo']
expect(evaluate(regexp_expr)).to eql(Puppet::Pops::Types::TypeFactory.regexp('foo'))
# arguments are flattened
regexp_expr = fqr('Regexp')[['foo']]
expect(evaluate(regexp_expr)).to eql(Puppet::Pops::Types::TypeFactory.regexp('foo'))
end
# Class
#
it 'produces a specific class from Class[classname]' do
expr = fqr('Class')[fqn('apache')]
expect(evaluate(expr)).to be_the_type(types.host_class('apache'))
expr = fqr('Class')[literal('apache')]
expect(evaluate(expr)).to be_the_type(types.host_class('apache'))
end
it 'produces an array of Class when args are in an array' do
# arguments are flattened
expr = fqr('Class')[[fqn('apache')]]
expect(evaluate(expr)[0]).to be_the_type(types.host_class('apache'))
end
it 'produces undef for Class if arg is undef' do
# arguments are flattened
expr = fqr('Class')[nil]
expect(evaluate(expr)).to be_nil
end
it 'produces empty array for Class if arg is [undef]' do
# arguments are flattened
expr = fqr('Class')[[]]
expect(evaluate(expr)).to be_eql([])
expr = fqr('Class')[[nil]]
expect(evaluate(expr)).to be_eql([])
end
it 'raises error if access is to no keys' do
expr = fqr('Class')[fqn('apache')][]
expect { evaluate(expr) }.to raise_error(/Evaluation Error: Class\[apache\]\[\] accepts 1 or more arguments\. Got 0/)
end
it 'produces a collection of classes when multiple class names are given' do
expr = fqr('Class')[fqn('apache'), literal('nginx')]
result = evaluate(expr)
expect(result[0]).to be_the_type(types.host_class('apache'))
expect(result[1]).to be_the_type(types.host_class('nginx'))
end
it 'removes leading :: in class name' do
expr = fqr('Class')['::evoe']
expect(evaluate(expr)).to be_the_type(types.host_class('evoe'))
end
it 'raises error if the name is not a valid name' do
expr = fqr('Class')['fail-whale']
expect { evaluate(expr) }.to raise_error(/Illegal name/)
end
it 'downcases capitalized class names' do
expr = fqr('Class')['My::Class']
expect(evaluate(expr)).to be_the_type(types.host_class('my::class'))
end
it 'gives an error if no keys are given as argument' do
expr = fqr('Class')[]
expect {evaluate(expr)}.to raise_error(/Evaluation Error: Class\[\] accepts 1 or more arguments. Got 0/)
end
it 'produces an empty array if the keys reduce to empty array' do
expr = fqr('Class')[literal([[],[]])]
expect(evaluate(expr)).to be_eql([])
end
# Resource
it 'produces a specific resource type from Resource[type]' do
expr = fqr('Resource')[fqr('File')]
expect(evaluate(expr)).to be_the_type(types.resource('File'))
expr = fqr('Resource')[literal('File')]
expect(evaluate(expr)).to be_the_type(types.resource('File'))
end
it 'does not allow the type to be specified in an array' do
# arguments are flattened
expr = fqr('Resource')[[fqr('File')]]
expect{evaluate(expr)}.to raise_error(Puppet::ParseError, /must be a resource type or a String/)
end
it 'produces a specific resource reference type from File[title]' do
expr = fqr('File')[literal('/tmp/x')]
expect(evaluate(expr)).to be_the_type(types.resource('File', '/tmp/x'))
end
it 'produces a collection of specific resource references when multiple titles are used' do
# Using a resource type
expr = fqr('File')[literal('x'),literal('y')]
result = evaluate(expr)
expect(result[0]).to be_the_type(types.resource('File', 'x'))
expect(result[1]).to be_the_type(types.resource('File', 'y'))
# Using generic resource
expr = fqr('Resource')[fqr('File'), literal('x'),literal('y')]
result = evaluate(expr)
expect(result[0]).to be_the_type(types.resource('File', 'x'))
expect(result[1]).to be_the_type(types.resource('File', 'y'))
end
it 'produces undef for Resource if arg is undef' do
# arguments are flattened
expr = fqr('File')[nil]
expect(evaluate(expr)).to be_nil
end
it 'gives an error if no keys are given as argument to Resource' do
expr = fqr('Resource')[]
expect {evaluate(expr)}.to raise_error(/Evaluation Error: Resource\[\] accepts 1 or more arguments. Got 0/)
end
it 'produces an empty array if the type is given, and keys reduce to empty array for Resource' do
expr = fqr('Resource')[fqr('File'),literal([[],[]])]
expect(evaluate(expr)).to be_eql([])
end
it 'gives an error i no keys are given as argument to a specific Resource type' do
expr = fqr('File')[]
expect {evaluate(expr)}.to raise_error(/Evaluation Error: File\[\] accepts 1 or more arguments. Got 0/)
end
it 'produces an empty array if the keys reduce to empty array for a specific Resource tyoe' do
expr = fqr('File')[literal([[],[]])]
expect(evaluate(expr)).to be_eql([])
end
it 'gives an error if resource is not found' do
expr = fqr('File')[fqn('x')][fqn('y')]
expect {evaluate(expr)}.to raise_error(/Resource not found: File\['x'\]/)
end
# Type Type
#
it 'creates a Type instance when applied to a Type' do
type_expr = fqr('Type')[fqr('Integer')]
tf = Puppet::Pops::Types::TypeFactory
expect(evaluate(type_expr)).to eql(tf.type_type(tf.integer))
# arguments are flattened
type_expr = fqr('Type')[[fqr('Integer')]]
expect(evaluate(type_expr)).to eql(tf.type_type(tf.integer))
end
# Ruby Type
#
it 'creates a Ruby Type instance when applied to a Ruby Type' do
type_expr = fqr('Runtime')['ruby','String']
tf = Puppet::Pops::Types::TypeFactory
expect(evaluate(type_expr)).to eql(tf.ruby_type('String'))
# arguments are flattened
type_expr = fqr('Runtime')[['ruby', 'String']]
expect(evaluate(type_expr)).to eql(tf.ruby_type('String'))
end
end
matcher :be_the_type do |type|
calc = Puppet::Pops::Types::TypeCalculator.new
match do |actual|
calc.assignable?(actual, type) && calc.assignable?(type, actual)
end
failure_message_for_should do |actual|
"expected #{type.to_s}, but was #{actual.to_s}"
end
end
end
|