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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
|
require 'spec_helper'
require 'puppet/pops'
require 'puppet/pops/evaluator/evaluator_impl'
require 'puppet/loaders'
require 'puppet_spec/pops'
require 'puppet_spec/scope'
require 'puppet/parser/e4_parser_adapter'
# 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' do
include PuppetSpec::Pops
include PuppetSpec::Scope
before(:each) do
Puppet[:strict_variables] = true
# These must be set since the 3x logic switches some behaviors on these even if the tests explicitly
# use the 4x parser and evaluator.
#
Puppet[:parser] = 'future'
# Puppetx cannot be loaded until the correct parser has been set (injector is turned off otherwise)
require 'puppetx'
# Tests needs a known configuration of node/scope/compiler since it parses and evaluates
# snippets as the compiler will evaluate them, butwithout the overhead of compiling a complete
# catalog for each tested expression.
#
@parser = Puppet::Pops::Parser::EvaluatingParser.new
@node = Puppet::Node.new('node.example.com')
@node.environment = Puppet::Node::Environment.create(:testing, [])
@compiler = Puppet::Parser::Compiler.new(@node)
@scope = Puppet::Parser::Scope.new(@compiler)
@scope.source = Puppet::Resource::Type.new(:node, 'node.example.com')
@scope.parent = @compiler.topscope
end
let(:parser) { @parser }
let(:scope) { @scope }
types = Puppet::Pops::Types::TypeFactory
context "When evaluator evaluates literals" do
{
"1" => 1,
"010" => 8,
"0x10" => 16,
"3.14" => 3.14,
"0.314e1" => 3.14,
"31.4e-1" => 3.14,
"'1'" => '1',
"'banana'" => 'banana',
'"banana"' => 'banana',
"banana" => 'banana',
"banana::split" => 'banana::split',
"false" => false,
"true" => true,
"Array" => types.array_of_data(),
"/.*/" => /.*/
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
end
context "When the evaluator evaluates Lists and Hashes" do
{
"[]" => [],
"[1,2,3]" => [1,2,3],
"[1,[2.0, 2.1, [2.2]],[3.0, 3.1]]" => [1,[2.0, 2.1, [2.2]],[3.0, 3.1]],
"[2 + 2]" => [4],
"[1,2,3] == [1,2,3]" => true,
"[1,2,3] != [2,3,4]" => true,
"[1,2,3] == [2,2,3]" => false,
"[1,2,3] != [1,2,3]" => false,
"[1,2,3][2]" => 3,
"[1,2,3] + [4,5]" => [1,2,3,4,5],
"[1,2,3] + [[4,5]]" => [1,2,3,[4,5]],
"[1,2,3] + 4" => [1,2,3,4],
"[1,2,3] << [4,5]" => [1,2,3,[4,5]],
"[1,2,3] << {'a' => 1, 'b'=>2}" => [1,2,3,{'a' => 1, 'b'=>2}],
"[1,2,3] << 4" => [1,2,3,4],
"[1,2,3,4] - [2,3]" => [1,4],
"[1,2,3,4] - [2,5]" => [1,3,4],
"[1,2,3,4] - 2" => [1,3,4],
"[1,2,3,[2],4] - 2" => [1,3,[2],4],
"[1,2,3,[2,3],4] - [[2,3]]" => [1,2,3,4],
"[1,2,3,3,2,4,2,3] - [2,3]" => [1,4],
"[1,2,3,['a',1],['b',2]] - {'a' => 1, 'b'=>2}" => [1,2,3],
"[1,2,3,{'a'=>1,'b'=>2}] - [{'a' => 1, 'b'=>2}]" => [1,2,3],
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"[1,2,3] + {'a' => 1, 'b'=>2}" => [1,2,3,['a',1],['b',2]],
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
# This test must be done with match_array since the order of the hash
# is undefined and Ruby 1.8.7 and 1.9.3 produce different results.
expect(parser.evaluate_string(scope, source, __FILE__)).to match_array(result)
end
end
{
"[1,2,3][a]" => :error,
}.each do |source, result|
it "should parse and raise error for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__) }.to raise_error(Puppet::ParseError)
end
end
{
"{}" => {},
"{'a'=>1,'b'=>2}" => {'a'=>1,'b'=>2},
"{'a'=>1,'b'=>{'x'=>2.1,'y'=>2.2}}" => {'a'=>1,'b'=>{'x'=>2.1,'y'=>2.2}},
"{'a'=> 2 + 2}" => {'a'=> 4},
"{'a'=> 1, 'b'=>2} == {'a'=> 1, 'b'=>2}" => true,
"{'a'=> 1, 'b'=>2} != {'x'=> 1, 'b'=>2}" => true,
"{'a'=> 1, 'b'=>2} == {'a'=> 2, 'b'=>3}" => false,
"{'a'=> 1, 'b'=>2} != {'a'=> 1, 'b'=>2}" => false,
"{a => 1, b => 2}[b]" => 2,
"{2+2 => sum, b => 2}[4]" => 'sum',
"{'a'=>1, 'b'=>2} + {'c'=>3}" => {'a'=>1,'b'=>2,'c'=>3},
"{'a'=>1, 'b'=>2} + {'b'=>3}" => {'a'=>1,'b'=>3},
"{'a'=>1, 'b'=>2} + ['c', 3, 'b', 3]" => {'a'=>1,'b'=>3, 'c'=>3},
"{'a'=>1, 'b'=>2} + [['c', 3], ['b', 3]]" => {'a'=>1,'b'=>3, 'c'=>3},
"{'a'=>1, 'b'=>2} - {'b' => 3}" => {'a'=>1},
"{'a'=>1, 'b'=>2, 'c'=>3} - ['b', 'c']" => {'a'=>1},
"{'a'=>1, 'b'=>2, 'c'=>3} - 'c'" => {'a'=>1, 'b'=>2},
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"{'a' => 1, 'b'=>2} << 1" => :error,
}.each do |source, result|
it "should parse and raise error for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__) }.to raise_error(Puppet::ParseError)
end
end
end
context "When the evaluator perform comparisons" do
{
"'a' == 'a'" => true,
"'a' == 'b'" => false,
"'a' != 'a'" => false,
"'a' != 'b'" => true,
"'a' < 'b' " => true,
"'a' < 'a' " => false,
"'b' < 'a' " => false,
"'a' <= 'b'" => true,
"'a' <= 'a'" => true,
"'b' <= 'a'" => false,
"'a' > 'b' " => false,
"'a' > 'a' " => false,
"'b' > 'a' " => true,
"'a' >= 'b'" => false,
"'a' >= 'a'" => true,
"'b' >= 'a'" => true,
"'a' == 'A'" => true,
"'a' != 'A'" => false,
"'a' > 'A'" => false,
"'a' >= 'A'" => true,
"'A' < 'a'" => false,
"'A' <= 'a'" => true,
"1 == 1" => true,
"1 == 2" => false,
"1 != 1" => false,
"1 != 2" => true,
"1 < 2 " => true,
"1 < 1 " => false,
"2 < 1 " => false,
"1 <= 2" => true,
"1 <= 1" => true,
"2 <= 1" => false,
"1 > 2 " => false,
"1 > 1 " => false,
"2 > 1 " => true,
"1 >= 2" => false,
"1 >= 1" => true,
"2 >= 1" => true,
"1 == 1.0 " => true,
"1 < 1.1 " => true,
"'1' < 1.1" => true,
"1.0 == 1 " => true,
"1.0 < 2 " => true,
"1.0 < 'a'" => true,
"'1.0' < 1.1" => true,
"'1.0' < 'a'" => true,
"'1.0' < '' " => true,
"'1.0' < ' '" => true,
"'a' > '1.0'" => true,
"/.*/ == /.*/ " => true,
"/.*/ != /a.*/" => true,
"true == true " => true,
"false == false" => true,
"true == false" => false,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"'a' =~ /.*/" => true,
"'a' =~ '.*'" => true,
"/.*/ != /a.*/" => true,
"'a' !~ /b.*/" => true,
"'a' !~ 'b.*'" => true,
'$x = a; a =~ "$x.*"' => true,
"a =~ Pattern['a.*']" => true,
"a =~ Regexp['a.*']" => false, # String is not subtype of Regexp. PUP-957
"$x = /a.*/ a =~ $x" => true,
"$x = Pattern['a.*'] a =~ $x" => true,
"1 =~ Integer" => true,
"1 !~ Integer" => false,
"[1,2,3] =~ Array[Integer[1,10]]" => true,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"666 =~ /6/" => :error,
"[a] =~ /a/" => :error,
"{a=>1} =~ /a/" => :error,
"/a/ =~ /a/" => :error,
"Array =~ /A/" => :error,
}.each do |source, result|
it "should parse and raise error for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__) }.to raise_error(Puppet::ParseError)
end
end
{
"1 in [1,2,3]" => true,
"4 in [1,2,3]" => false,
"a in {x=>1, a=>2}" => true,
"z in {x=>1, a=>2}" => false,
"ana in bananas" => true,
"xxx in bananas" => false,
"/ana/ in bananas" => true,
"/xxx/ in bananas" => false,
"ANA in bananas" => false, # ANA is a type, not a String
"String[1] in bananas" => false, # Philosophically true though :-)
"'ANA' in bananas" => true,
"ana in 'BANANAS'" => true,
"/ana/ in 'BANANAS'" => false,
"/ANA/ in 'BANANAS'" => true,
"xxx in 'BANANAS'" => false,
"[2,3] in [1,[2,3],4]" => true,
"[2,4] in [1,[2,3],4]" => false,
"[a,b] in ['A',['A','B'],'C']" => true,
"[x,y] in ['A',['A','B'],'C']" => false,
"a in {a=>1}" => true,
"x in {a=>1}" => false,
"'A' in {a=>1}" => true,
"'X' in {a=>1}" => false,
"a in {'A'=>1}" => true,
"x in {'A'=>1}" => false,
"/xxx/ in {'aaaxxxbbb'=>1}" => true,
"/yyy/ in {'aaaxxxbbb'=>1}" => false,
"15 in [1, 0xf]" => true,
"15 in [1, '0xf']" => true,
"'15' in [1, 0xf]" => true,
"15 in [1, 115]" => false,
"1 in [11, '111']" => false,
"'1' in [11, '111']" => false,
"Array[Integer] in [2, 3]" => false,
"Array[Integer] in [2, [3, 4]]" => true,
"Array[Integer] in [2, [a, 4]]" => false,
"Integer in { 2 =>'a'}" => true,
"Integer[5,10] in [1,5,3]" => true,
"Integer[5,10] in [1,2,3]" => false,
"Integer in {'a'=>'a'}" => false,
"Integer in {'a'=>1}" => false,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"if /(ana)/ in bananas {$1}" => 'ana',
"if /(xyz)/ in bananas {$1} else {$1}" => nil,
"$a = bananas =~ /(ana)/; $b = /(xyz)/ in bananas; $1" => 'ana',
"$a = xyz =~ /(xyz)/; $b = /(ana)/ in bananas; $1" => 'ana',
"if /p/ in [pineapple, bananas] {$0}" => 'p',
"if /b/ in [pineapple, bananas] {$0}" => 'b',
}.each do |source, result|
it "sets match variables for a regexp search using in such that '#{source}' produces '#{result}'" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
'Any' => ['Data', 'Scalar', 'Numeric', 'Integer', 'Float', 'Boolean', 'String', 'Pattern', 'Collection',
'Array', 'Hash', 'CatalogEntry', 'Resource', 'Class', 'Undef', 'File', 'NotYetKnownResourceType'],
# Note, Data > Collection is false (so not included)
'Data' => ['Scalar', 'Numeric', 'Integer', 'Float', 'Boolean', 'String', 'Pattern', 'Array', 'Hash',],
'Scalar' => ['Numeric', 'Integer', 'Float', 'Boolean', 'String', 'Pattern'],
'Numeric' => ['Integer', 'Float'],
'CatalogEntry' => ['Class', 'Resource', 'File', 'NotYetKnownResourceType'],
'Integer[1,10]' => ['Integer[2,3]'],
}.each do |general, specials|
specials.each do |special |
it "should compute that #{general} > #{special}" do
parser.evaluate_string(scope, "#{general} > #{special}", __FILE__).should == true
end
it "should compute that #{special} < #{general}" do
parser.evaluate_string(scope, "#{special} < #{general}", __FILE__).should == true
end
it "should compute that #{general} != #{special}" do
parser.evaluate_string(scope, "#{special} != #{general}", __FILE__).should == true
end
end
end
{
'Integer[1,10] > Integer[2,3]' => true,
'Integer[1,10] == Integer[2,3]' => false,
'Integer[1,10] > Integer[0,5]' => false,
'Integer[1,10] > Integer[1,10]' => false,
'Integer[1,10] >= Integer[1,10]' => true,
'Integer[1,10] == Integer[1,10]' => true,
}.each do |source, result|
it "should parse and evaluate the integer range comparison expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
end
context "When the evaluator performs arithmetic" do
context "on Integers" do
{ "2+2" => 4,
"2 + 2" => 4,
"7 - 3" => 4,
"6 * 3" => 18,
"6 / 3" => 2,
"6 % 3" => 0,
"10 % 3" => 1,
"-(6/3)" => -2,
"-6/3 " => -2,
"8 >> 1" => 4,
"8 << 1" => 16,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
context "on Floats" do
{
"2.2 + 2.2" => 4.4,
"7.7 - 3.3" => 4.4,
"6.1 * 3.1" => 18.91,
"6.6 / 3.3" => 2.0,
"-(6.0/3.0)" => -2.0,
"-6.0/3.0 " => -2.0,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"3.14 << 2" => :error,
"3.14 >> 2" => :error,
"6.6 % 3.3" => 0.0,
"10.0 % 3.0" => 1.0,
}.each do |source, result|
it "should parse and raise error for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__) }.to raise_error(Puppet::ParseError)
end
end
end
context "on strings requiring boxing to Numeric" do
{
"'2' + '2'" => 4,
"'-2' + '2'" => 0,
"'- 2' + '2'" => 0,
'"-\t 2" + "2"' => 0,
"'+2' + '2'" => 4,
"'+ 2' + '2'" => 4,
"'2.2' + '2.2'" => 4.4,
"'-2.2' + '2.2'" => 0.0,
"'0xF7' + '010'" => 0xFF,
"'0xF7' + '0x8'" => 0xFF,
"'0367' + '010'" => 0xFF,
"'012.3' + '010'" => 20.3,
"'-0x2' + '0x4'" => 2,
"'+0x2' + '0x4'" => 6,
"'-02' + '04'" => 2,
"'+02' + '04'" => 6,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"'0888' + '010'" => :error,
"'0xWTF' + '010'" => :error,
"'0x12.3' + '010'" => :error,
"'0x12.3' + '010'" => :error,
'"-\n 2" + "2"' => :error,
'"-\v 2" + "2"' => :error,
'"-2\n" + "2"' => :error,
'"-2\n " + "2"' => :error,
}.each do |source, result|
it "should parse and raise error for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__) }.to raise_error(Puppet::ParseError)
end
end
end
end
end # arithmetic
context "When the evaluator evaluates assignment" do
{
"$a = 5" => 5,
"$a = 5; $a" => 5,
"$a = 5; $b = 6; $a" => 5,
"$a = $b = 5; $a == $b" => true,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"[a,b,c] = [1,2,3]" => /attempt to assign to 'an Array Expression'/,
"[a,b,c] = {b=>2,c=>3,a=>1}" => /attempt to assign to 'an Array Expression'/,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to error with #{result}" do
expect { parser.evaluate_string(scope, source, __FILE__)}.to raise_error(Puppet::ParseError, result)
end
end
end
context "When the evaluator evaluates conditionals" do
{
"if true {5}" => 5,
"if false {5}" => nil,
"if false {2} else {5}" => 5,
"if false {2} elsif true {5}" => 5,
"if false {2} elsif false {5}" => nil,
"unless false {5}" => 5,
"unless true {5}" => nil,
"unless true {2} else {5}" => 5,
"$a = if true {5} $a" => 5,
"$a = if false {5} $a" => nil,
"$a = if false {2} else {5} $a" => 5,
"$a = if false {2} elsif true {5} $a" => 5,
"$a = if false {2} elsif false {5} $a" => nil,
"$a = unless false {5} $a" => 5,
"$a = unless true {5} $a" => nil,
"$a = unless true {2} else {5} $a" => 5,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"case 1 { 1 : { yes } }" => 'yes',
"case 2 { 1,2,3 : { yes} }" => 'yes',
"case 2 { 1,3 : { no } 2: { yes} }" => 'yes',
"case 2 { 1,3 : { no } 5: { no } default: { yes }}" => 'yes',
"case 2 { 1,3 : { no } 5: { no } }" => nil,
"case 'banana' { 1,3 : { no } /.*ana.*/: { yes } }" => 'yes',
"case 'banana' { /.*(ana).*/: { $1 } }" => 'ana',
"case [1] { Array : { yes } }" => 'yes',
"case [1] {
Array[String] : { no }
Array[Integer]: { yes }
}" => 'yes',
"case 1 {
Integer : { yes }
Type[Integer] : { no } }" => 'yes',
"case Integer {
Integer : { no }
Type[Integer] : { yes } }" => 'yes',
# supports unfold
"case ringo {
*[paul, john, ringo, george] : { 'beatle' } }" => 'beatle',
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"2 ? { 1 => no, 2 => yes}" => 'yes',
"3 ? { 1 => no, 2 => no, default => yes }" => 'yes',
"3 ? { 1 => no, default => yes, 3 => no }" => 'no',
"3 ? { 1 => no, 3 => no, default => yes }" => 'no',
"4 ? { 1 => no, default => yes, 3 => no }" => 'yes',
"4 ? { 1 => no, 3 => no, default => yes }" => 'yes',
"'banana' ? { /.*(ana).*/ => $1 }" => 'ana',
"[2] ? { Array[String] => yes, Array => yes}" => 'yes',
"ringo ? *[paul, john, ringo, george] => 'beatle'" => 'beatle',
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
it 'fails if a selector does not match' do
expect{parser.evaluate_string(scope, "2 ? 3 => 4")}.to raise_error(/No matching entry for selector parameter with value '2'/)
end
end
context "When evaluator evaluated unfold" do
{
"*[1,2,3]" => [1,2,3],
"*1" => [1],
"*'a'" => ['a']
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
it "should parse and evaluate the expression '*{a=>10, b=>20} to [['a',10],['b',20]]" do
result = parser.evaluate_string(scope, '*{a=>10, b=>20}', __FILE__)
expect(result).to include(['a', 10])
expect(result).to include(['b', 20])
end
end
context "When evaluator performs [] operations" do
{
"[1,2,3][0]" => 1,
"[1,2,3][2]" => 3,
"[1,2,3][3]" => nil,
"[1,2,3][-1]" => 3,
"[1,2,3][-2]" => 2,
"[1,2,3][-4]" => nil,
"[1,2,3,4][0,2]" => [1,2],
"[1,2,3,4][1,3]" => [2,3,4],
"[1,2,3,4][-2,2]" => [3,4],
"[1,2,3,4][-3,2]" => [2,3],
"[1,2,3,4][3,5]" => [4],
"[1,2,3,4][5,2]" => [],
"[1,2,3,4][0,-1]" => [1,2,3,4],
"[1,2,3,4][0,-2]" => [1,2,3],
"[1,2,3,4][0,-4]" => [1],
"[1,2,3,4][0,-5]" => [],
"[1,2,3,4][-5,2]" => [1],
"[1,2,3,4][-5,-3]" => [1,2],
"[1,2,3,4][-6,-3]" => [1,2],
"[1,2,3,4][2,-3]" => [],
"[1,*[2,3],4]" => [1,2,3,4],
"[1,*[2,3],4][1]" => 2,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"{a=>1, b=>2, c=>3}[a]" => 1,
"{a=>1, b=>2, c=>3}[c]" => 3,
"{a=>1, b=>2, c=>3}[x]" => nil,
"{a=>1, b=>2, c=>3}[c,b]" => [3,2],
"{a=>1, b=>2, c=>3}[a,b,c]" => [1,2,3],
"{a=>{b=>{c=>'it works'}}}[a][b][c]" => 'it works',
"$a = {undef => 10} $a[free_lunch]" => nil,
"$a = {undef => 10} $a[undef]" => 10,
"$a = {undef => 10} $a[$a[free_lunch]]" => 10,
"$a = {} $a[free_lunch] == undef" => true,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"'abc'[0]" => 'a',
"'abc'[2]" => 'c',
"'abc'[-1]" => 'c',
"'abc'[-2]" => 'b',
"'abc'[-3]" => 'a',
"'abc'[-4]" => '',
"'abc'[3]" => '',
"abc[0]" => 'a',
"abc[2]" => 'c',
"abc[-1]" => 'c',
"abc[-2]" => 'b',
"abc[-3]" => 'a',
"abc[-4]" => '',
"abc[3]" => '',
"'abcd'[0,2]" => 'ab',
"'abcd'[1,3]" => 'bcd',
"'abcd'[-2,2]" => 'cd',
"'abcd'[-3,2]" => 'bc',
"'abcd'[3,5]" => 'd',
"'abcd'[5,2]" => '',
"'abcd'[0,-1]" => 'abcd',
"'abcd'[0,-2]" => 'abc',
"'abcd'[0,-4]" => 'a',
"'abcd'[0,-5]" => '',
"'abcd'[-5,2]" => 'a',
"'abcd'[-5,-3]" => 'ab',
"'abcd'[-6,-3]" => 'ab',
"'abcd'[2,-3]" => '',
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
# Type operations (full set tested by tests covering type calculator)
{
"Array[Integer]" => types.array_of(types.integer),
"Array[Integer,1]" => types.constrain_size(types.array_of(types.integer),1, :default),
"Array[Integer,1,2]" => types.constrain_size(types.array_of(types.integer),1, 2),
"Array[Integer,Integer[1,2]]" => types.constrain_size(types.array_of(types.integer),1, 2),
"Array[Integer,Integer[1]]" => types.constrain_size(types.array_of(types.integer),1, :default),
"Hash[Integer,Integer]" => types.hash_of(types.integer, types.integer),
"Hash[Integer,Integer,1]" => types.constrain_size(types.hash_of(types.integer, types.integer),1, :default),
"Hash[Integer,Integer,1,2]" => types.constrain_size(types.hash_of(types.integer, types.integer),1, 2),
"Hash[Integer,Integer,Integer[1,2]]" => types.constrain_size(types.hash_of(types.integer, types.integer),1, 2),
"Hash[Integer,Integer,Integer[1]]" => types.constrain_size(types.hash_of(types.integer, types.integer),1, :default),
"Resource[File]" => types.resource('File'),
"Resource['File']" => types.resource(types.resource('File')),
"File[foo]" => types.resource('file', 'foo'),
"File[foo, bar]" => [types.resource('file', 'foo'), types.resource('file', 'bar')],
"Pattern[a, /b/, Pattern[c], Regexp[d]]" => types.pattern('a', 'b', 'c', 'd'),
"String[1,2]" => types.constrain_size(types.string,1, 2),
"String[Integer[1,2]]" => types.constrain_size(types.string,1, 2),
"String[Integer[1]]" => types.constrain_size(types.string,1, :default),
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
# LHS where [] not supported, and missing key(s)
{
"Array[]" => :error,
"'abc'[]" => :error,
"Resource[]" => :error,
"File[]" => :error,
"String[]" => :error,
"1[]" => :error,
"3.14[]" => :error,
"/.*/[]" => :error,
"$a=[1] $a[]" => :error,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
expect { parser.evaluate_string(scope, source, __FILE__)}.to raise_error(/Syntax error/)
end
end
# Errors when wrong number/type of keys are used
{
"Array[0]" => 'Array-Type[] arguments must be types. Got Fixnum',
"Hash[0]" => 'Hash-Type[] arguments must be types. Got Fixnum',
"Hash[Integer, 0]" => 'Hash-Type[] arguments must be types. Got Fixnum',
"Array[Integer,1,2,3]" => 'Array-Type[] accepts 1 to 3 arguments. Got 4',
"Array[Integer,String]" => "A Type's size constraint arguments must be a single Integer type, or 1-2 integers (or default). Got a String-Type",
"Hash[Integer,String, 1,2,3]" => 'Hash-Type[] accepts 1 to 4 arguments. Got 5',
"'abc'[x]" => "The value 'x' cannot be converted to Numeric",
"'abc'[1.0]" => "A String[] cannot use Float where Integer is expected",
"'abc'[1,2,3]" => "String supports [] with one or two arguments. Got 3",
"Resource[0]" => 'First argument to Resource[] must be a resource type or a String. Got Integer',
"Resource[a, 0]" => 'Error creating type specialization of a Resource-Type, Cannot use Integer where a resource title String is expected',
"File[0]" => 'Error creating type specialization of a File-Type, Cannot use Integer where a resource title String is expected',
"String[a]" => "A Type's size constraint arguments must be a single Integer type, or 1-2 integers (or default). Got a String",
"Pattern[0]" => 'Error creating type specialization of a Pattern-Type, Cannot use Integer where String or Regexp or Pattern-Type or Regexp-Type is expected',
"Regexp[0]" => 'Error creating type specialization of a Regexp-Type, Cannot use Integer where String or Regexp is expected',
"Regexp[a,b]" => 'A Regexp-Type[] accepts 1 argument. Got 2',
"true[0]" => "Operator '[]' is not applicable to a Boolean",
"1[0]" => "Operator '[]' is not applicable to an Integer",
"3.14[0]" => "Operator '[]' is not applicable to a Float",
"/.*/[0]" => "Operator '[]' is not applicable to a Regexp",
"[1][a]" => "The value 'a' cannot be converted to Numeric",
"[1][0.0]" => "An Array[] cannot use Float where Integer is expected",
"[1]['0.0']" => "An Array[] cannot use Float where Integer is expected",
"[1,2][1, 0.0]" => "An Array[] cannot use Float where Integer is expected",
"[1,2][1.0, -1]" => "An Array[] cannot use Float where Integer is expected",
"[1,2][1, -1.0]" => "An Array[] cannot use Float where Integer is expected",
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
expect { parser.evaluate_string(scope, source, __FILE__)}.to raise_error(Regexp.new(Regexp.quote(result)))
end
end
context "on catalog types" do
it "[n] gets resource parameter [n]" do
source = "notify { 'hello': message=>'yo'} Notify[hello][message]"
parser.evaluate_string(scope, source, __FILE__).should == 'yo'
end
it "[n] gets class parameter [n]" do
source = "class wonka($produces='chocolate'){ }
include wonka
Class[wonka][produces]"
# This is more complicated since it needs to run like 3.x and do an import_ast
adapted_parser = Puppet::Parser::E4ParserAdapter.new
adapted_parser.file = __FILE__
ast = adapted_parser.parse(source)
Puppet.override({:global_scope => scope}, "test") do
scope.known_resource_types.import_ast(ast, '')
ast.code.safeevaluate(scope).should == 'chocolate'
end
end
# Resource default and override expressions and resource parameter access with []
{
# Properties
"notify { id: message=>explicit} Notify[id][message]" => "explicit",
"Notify { message=>by_default} notify {foo:} Notify[foo][message]" => "by_default",
"notify {foo:} Notify[foo]{message =>by_override} Notify[foo][message]" => "by_override",
# Parameters
"notify { id: withpath=>explicit} Notify[id][withpath]" => "explicit",
"Notify { withpath=>by_default } notify { foo: } Notify[foo][withpath]" => "by_default",
"notify {foo:}
Notify[foo]{withpath=>by_override}
Notify[foo][withpath]" => "by_override",
# Metaparameters
"notify { foo: tag => evoe} Notify[foo][tag]" => "evoe",
# Does not produce the defaults for tag parameter (title, type or names of scopes)
"notify { foo: } Notify[foo][tag]" => nil,
# But a default may be specified on the type
"Notify { tag=>by_default } notify { foo: } Notify[foo][tag]" => "by_default",
"Notify { tag=>by_default }
notify { foo: }
Notify[foo]{ tag=>by_override }
Notify[foo][tag]" => "by_override",
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
# Virtual and realized resource default and overridden resource parameter access with []
{
# Properties
"@notify { id: message=>explicit } Notify[id][message]" => "explicit",
"@notify { id: message=>explicit }
realize Notify[id]
Notify[id][message]" => "explicit",
"Notify { message=>by_default } @notify { id: } Notify[id][message]" => "by_default",
"Notify { message=>by_default }
@notify { id: tag=>thisone }
Notify <| tag == thisone |>;
Notify[id][message]" => "by_default",
"@notify { id: } Notify[id]{message=>by_override} Notify[id][message]" => "by_override",
# Parameters
"@notify { id: withpath=>explicit } Notify[id][withpath]" => "explicit",
"Notify { withpath=>by_default }
@notify { id: }
Notify[id][withpath]" => "by_default",
"@notify { id: }
realize Notify[id]
Notify[id]{withpath=>by_override}
Notify[id][withpath]" => "by_override",
# Metaparameters
"@notify { id: tag=>explicit } Notify[id][tag]" => "explicit",
}.each do |source, result|
it "parses and evaluates virtual and realized resources in the expression '#{source}' to #{result}" do
expect(parser.evaluate_string(scope, source, __FILE__)).to eq(result)
end
end
# Exported resource attributes
{
"@@notify { id: message=>explicit } Notify[id][message]" => "explicit",
"@@notify { id: message=>explicit, tag=>thisone }
Notify <<| tag == thisone |>>
Notify[id][message]" => "explicit",
}.each do |source, result|
it "parses and evaluates exported resources in the expression '#{source}' to #{result}" do
expect(parser.evaluate_string(scope, source, __FILE__)).to eq(result)
end
end
# Resource default and override expressions and resource parameter access error conditions
{
"notify { xid: message=>explicit} Notify[id][message]" => /Resource not found/,
"notify { id: message=>explicit} Notify[id][mustard]" => /does not have a parameter called 'mustard'/,
# NOTE: these meta-esque parameters are not recognized as such
"notify { id: message=>explicit} Notify[id][title]" => /does not have a parameter called 'title'/,
"notify { id: message=>explicit} Notify[id]['type']" => /does not have a parameter called 'type'/,
"notify { id: message=>explicit } Notify[id]{message=>override}" => /'message' is already set on Notify\[id\]/
}.each do |source, result|
it "should parse '#{source}' and raise error matching #{result}" do
expect { parser.evaluate_string(scope, source, __FILE__)}.to raise_error(result)
end
end
context 'with errors' do
{ "Class['fail-whale']" => /Illegal name/,
"Class[0]" => /An Integer cannot be used where a String is expected/,
"Class[/.*/]" => /A Regexp cannot be used where a String is expected/,
"Class[4.1415]" => /A Float cannot be used where a String is expected/,
"Class[Integer]" => /An Integer-Type cannot be used where a String is expected/,
"Class[File['tmp']]" => /A File\['tmp'\] Resource-Reference cannot be used where a String is expected/,
}.each do | source, error_pattern|
it "an error is flagged for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__)}.to raise_error(error_pattern)
end
end
end
end
# end [] operations
end
context "When the evaluator performs boolean operations" do
{
"true and true" => true,
"false and true" => false,
"true and false" => false,
"false and false" => false,
"true or true" => true,
"false or true" => true,
"true or false" => true,
"false or false" => false,
"! true" => false,
"!! true" => true,
"!! false" => false,
"! 'x'" => false,
"! ''" => false,
"! undef" => true,
"! [a]" => false,
"! []" => false,
"! {a=>1}" => false,
"! {}" => false,
"true and false and '0xwtf' + 1" => false,
"false or true or '0xwtf' + 1" => true,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
"false || false || '0xwtf' + 1" => :error,
}.each do |source, result|
it "should parse and raise error for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__) }.to raise_error(Puppet::ParseError)
end
end
end
context "When evaluator performs operations on literal undef" do
it "computes non existing hash lookup as undef" do
parser.evaluate_string(scope, "{a => 1}[b] == undef", __FILE__).should == true
parser.evaluate_string(scope, "undef == {a => 1}[b]", __FILE__).should == true
end
end
context "When evaluator performs calls" do
let(:populate) do
parser.evaluate_string(scope, "$a = 10 $b = [1,2,3]")
end
{
'sprintf( "x%iy", $a )' => "x10y",
# unfolds
'sprintf( *["x%iy", $a] )' => "x10y",
'"x%iy".sprintf( $a )' => "x10y",
'$b.reduce |$memo,$x| { $memo + $x }' => 6,
'reduce($b) |$memo,$x| { $memo + $x }' => 6,
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
populate
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
{
'"value is ${a*2} yo"' => :error,
}.each do |source, result|
it "should parse and raise error for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__) }.to raise_error(Puppet::ParseError)
end
end
it "provides location information on error in unparenthesized call logic" do
expect{parser.evaluate_string(scope, "include non_existing_class", __FILE__)}.to raise_error(Puppet::ParseError, /line 1\:1/)
end
it 'defaults can be given in a lambda and used only when arg is missing' do
env_loader = @compiler.loaders.public_environment_loader
fc = Puppet::Functions.create_function(:test) do
dispatch :test do
param 'Integer', 'count'
required_block_param
end
def test(count, block)
block.call({}, *[].fill(10, 0, count))
end
end
the_func = fc.new({}, env_loader)
env_loader.add_entry(:function, 'test', the_func, __FILE__)
expect(parser.evaluate_string(scope, "test(1) |$x, $y=20| { $x + $y}")).to eql(30)
expect(parser.evaluate_string(scope, "test(2) |$x, $y=20| { $x + $y}")).to eql(20)
end
it 'a given undef does not select the default value' do
env_loader = @compiler.loaders.public_environment_loader
fc = Puppet::Functions.create_function(:test) do
dispatch :test do
param 'Any', 'lambda_arg'
required_block_param
end
def test(lambda_arg, block)
block.call({}, lambda_arg)
end
end
the_func = fc.new({}, env_loader)
env_loader.add_entry(:function, 'test', the_func, __FILE__)
expect(parser.evaluate_string(scope, "test(undef) |$x=20| { $x == undef}")).to eql(true)
end
it 'a given undef is given as nil' do
env_loader = @compiler.loaders.public_environment_loader
fc = Puppet::Functions.create_function(:assert_no_undef) do
dispatch :assert_no_undef do
param 'Any', 'x'
end
def assert_no_undef(x)
case x
when Array
return unless x.include?(:undef)
when Hash
return unless x.keys.include?(:undef) || x.values.include?(:undef)
else
return unless x == :undef
end
raise "contains :undef"
end
end
the_func = fc.new({}, env_loader)
env_loader.add_entry(:function, 'assert_no_undef', the_func, __FILE__)
expect{parser.evaluate_string(scope, "assert_no_undef(undef)")}.to_not raise_error()
expect{parser.evaluate_string(scope, "assert_no_undef([undef])")}.to_not raise_error()
expect{parser.evaluate_string(scope, "assert_no_undef({undef => 1})")}.to_not raise_error()
expect{parser.evaluate_string(scope, "assert_no_undef({1 => undef})")}.to_not raise_error()
end
context 'using the 3x function api' do
it 'can call a 3x function' do
Puppet::Parser::Functions.newfunction("bazinga", :type => :rvalue) { |args| args[0] }
parser.evaluate_string(scope, "bazinga(42)", __FILE__).should == 42
end
it 'maps :undef to empty string' do
Puppet::Parser::Functions.newfunction("bazinga", :type => :rvalue) { |args| args[0] }
parser.evaluate_string(scope, "$a = {} bazinga($a[nope])", __FILE__).should == ''
parser.evaluate_string(scope, "bazinga(undef)", __FILE__).should == ''
end
it 'does not map :undef to empty string in arrays' do
Puppet::Parser::Functions.newfunction("bazinga", :type => :rvalue) { |args| args[0][0] }
parser.evaluate_string(scope, "$a = {} $b = [$a[nope]] bazinga($b)", __FILE__).should == :undef
parser.evaluate_string(scope, "bazinga([undef])", __FILE__).should == :undef
end
it 'does not map :undef to empty string in hashes' do
Puppet::Parser::Functions.newfunction("bazinga", :type => :rvalue) { |args| args[0]['a'] }
parser.evaluate_string(scope, "$a = {} $b = {a => $a[nope]} bazinga($b)", __FILE__).should == :undef
parser.evaluate_string(scope, "bazinga({a => undef})", __FILE__).should == :undef
end
end
end
context "When evaluator performs string interpolation" do
let(:populate) do
parser.evaluate_string(scope, "$a = 10 $b = [1,2,3]")
end
{
'"value is $a yo"' => "value is 10 yo",
'"value is \$a yo"' => "value is $a yo",
'"value is ${a} yo"' => "value is 10 yo",
'"value is \${a} yo"' => "value is ${a} yo",
'"value is ${$a} yo"' => "value is 10 yo",
'"value is ${$a*2} yo"' => "value is 20 yo",
'"value is ${sprintf("x%iy",$a)} yo"' => "value is x10y yo",
'"value is ${"x%iy".sprintf($a)} yo"' => "value is x10y yo",
'"value is ${[1,2,3]} yo"' => "value is [1, 2, 3] yo",
'"value is ${/.*/} yo"' => "value is /.*/ yo",
'$x = undef "value is $x yo"' => "value is yo",
'$x = default "value is $x yo"' => "value is default yo",
'$x = Array[Integer] "value is $x yo"' => "value is Array[Integer] yo",
'"value is ${Array[Integer]} yo"' => "value is Array[Integer] yo",
}.each do |source, result|
it "should parse and evaluate the expression '#{source}' to #{result}" do
populate
parser.evaluate_string(scope, source, __FILE__).should == result
end
end
it "should parse and evaluate an interpolation of a hash" do
source = '"value is ${{a=>1,b=>2}} yo"'
# This test requires testing against two options because a hash to string
# produces a result that is unordered
hashstr = {'a' => 1, 'b' => 2}.to_s
alt_results = ["value is {a => 1, b => 2} yo", "value is {b => 2, a => 1} yo" ]
populate
parse_result = parser.evaluate_string(scope, source, __FILE__)
alt_results.include?(parse_result).should == true
end
it 'should accept a variable with leading underscore when used directly' do
source = '$_x = 10; "$_x"'
expect(parser.evaluate_string(scope, source, __FILE__)).to eql('10')
end
it 'should accept a variable with leading underscore when used as an expression' do
source = '$_x = 10; "${_x}"'
expect(parser.evaluate_string(scope, source, __FILE__)).to eql('10')
end
{
'"value is ${a*2} yo"' => :error,
}.each do |source, result|
it "should parse and raise error for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__) }.to raise_error(Puppet::ParseError)
end
end
end
context "When evaluating variables" do
context "that are non existing an error is raised for" do
it "unqualified variable" do
expect { parser.evaluate_string(scope, "$quantum_gravity", __FILE__) }.to raise_error(/Unknown variable/)
end
it "qualified variable" do
expect { parser.evaluate_string(scope, "$quantum_gravity::graviton", __FILE__) }.to raise_error(/Unknown variable/)
end
end
it "a lex error should be raised for '$foo::::bar'" do
expect { parser.evaluate_string(scope, "$foo::::bar") }.to raise_error(Puppet::LexError, /Illegal fully qualified name at line 1:7/)
end
{ '$a = $0' => nil,
'$a = $1' => nil,
}.each do |source, value|
it "it is ok to reference numeric unassigned variables '#{source}'" do
parser.evaluate_string(scope, source, __FILE__).should == value
end
end
{ '$00 = 0' => /must be a decimal value/,
'$0xf = 0' => /must be a decimal value/,
'$0777 = 0' => /must be a decimal value/,
'$123a = 0' => /must be a decimal value/,
}.each do |source, error_pattern|
it "should raise an error for '#{source}'" do
expect { parser.evaluate_string(scope, source, __FILE__) }.to raise_error(error_pattern)
end
end
context "an initial underscore in the last segment of a var name is allowed" do
{ '$_a = 1' => 1,
'$__a = 1' => 1,
}.each do |source, value|
it "as in this example '#{source}'" do
parser.evaluate_string(scope, source, __FILE__).should == value
end
end
end
end
context "When evaluating relationships" do
it 'should form a relation with File[a] -> File[b]' do
source = "File[a] -> File[b]"
parser.evaluate_string(scope, source, __FILE__)
scope.compiler.should have_relationship(['File', 'a', '->', 'File', 'b'])
end
it 'should form a relation with resource -> resource' do
source = "notify{a:} -> notify{b:}"
parser.evaluate_string(scope, source, __FILE__)
scope.compiler.should have_relationship(['Notify', 'a', '->', 'Notify', 'b'])
end
it 'should form a relation with [File[a], File[b]] -> [File[x], File[y]]' do
source = "[File[a], File[b]] -> [File[x], File[y]]"
parser.evaluate_string(scope, source, __FILE__)
scope.compiler.should have_relationship(['File', 'a', '->', 'File', 'x'])
scope.compiler.should have_relationship(['File', 'b', '->', 'File', 'x'])
scope.compiler.should have_relationship(['File', 'a', '->', 'File', 'y'])
scope.compiler.should have_relationship(['File', 'b', '->', 'File', 'y'])
end
it 'should tolerate (eliminate) duplicates in operands' do
source = "[File[a], File[a]] -> File[x]"
parser.evaluate_string(scope, source, __FILE__)
scope.compiler.should have_relationship(['File', 'a', '->', 'File', 'x'])
scope.compiler.relationships.size.should == 1
end
it 'should form a relation with <-' do
source = "File[a] <- File[b]"
parser.evaluate_string(scope, source, __FILE__)
scope.compiler.should have_relationship(['File', 'b', '->', 'File', 'a'])
end
it 'should form a relation with <-' do
source = "File[a] <~ File[b]"
parser.evaluate_string(scope, source, __FILE__)
scope.compiler.should have_relationship(['File', 'b', '~>', 'File', 'a'])
end
end
context "When evaluating heredoc" do
it "evaluates plain heredoc" do
src = "@(END)\nThis is\nheredoc text\nEND\n"
parser.evaluate_string(scope, src).should == "This is\nheredoc text\n"
end
it "parses heredoc with margin" do
src = [
"@(END)",
" This is",
" heredoc text",
" | END",
""
].join("\n")
parser.evaluate_string(scope, src).should == "This is\nheredoc text\n"
end
it "parses heredoc with margin and right newline trim" do
src = [
"@(END)",
" This is",
" heredoc text",
" |- END",
""
].join("\n")
parser.evaluate_string(scope, src).should == "This is\nheredoc text"
end
it "parses escape specification" do
src = <<-CODE
@(END/t)
Tex\\tt\\n
|- END
CODE
parser.evaluate_string(scope, src).should == "Tex\tt\\n"
end
it "parses syntax checked specification" do
src = <<-CODE
@(END:json)
["foo", "bar"]
|- END
CODE
parser.evaluate_string(scope, src).should == '["foo", "bar"]'
end
it "parses syntax checked specification with error and reports it" do
src = <<-CODE
@(END:json)
['foo', "bar"]
|- END
CODE
expect { parser.evaluate_string(scope, src)}.to raise_error(/Cannot parse invalid JSON string/)
end
it "parses interpolated heredoc expression" do
src = <<-CODE
$name = 'Fjodor'
@("END")
Hello $name
|- END
CODE
parser.evaluate_string(scope, src).should == "Hello Fjodor"
end
end
context "Handles Deprecations and Discontinuations" do
it 'of import statements' do
source = "\nimport foo"
# Error references position 5 at the opening '{'
# Set file to nil to make it easier to match with line number (no file name in output)
expect { parser.evaluate_string(scope, source) }.to raise_error(/'import' has been discontinued.*line 2:1/)
end
end
context "Detailed Error messages are reported" do
it 'for illegal type references' do
source = '1+1 { "title": }'
# Error references position 5 at the opening '{'
# Set file to nil to make it easier to match with line number (no file name in output)
expect { parser.evaluate_string(scope, source) }.to raise_error(
/Illegal Resource Type expression, expected result to be a type name, or untitled Resource.*line 1:2/)
end
it 'for non r-value producing <| |>' do
expect { parser.parse_string("$a = File <| |>", nil) }.to raise_error(/A Virtual Query does not produce a value at line 1:6/)
end
it 'for non r-value producing <<| |>>' do
expect { parser.parse_string("$a = File <<| |>>", nil) }.to raise_error(/An Exported Query does not produce a value at line 1:6/)
end
it 'for non r-value producing define' do
Puppet.expects(:err).with("Invalid use of expression. A 'define' expression does not produce a value at line 1:6")
Puppet.expects(:err).with("Classes, definitions, and nodes may only appear at toplevel or inside other classes at line 1:6")
expect { parser.parse_string("$a = define foo { }", nil) }.to raise_error(/2 errors/)
end
it 'for non r-value producing class' do
Puppet.expects(:err).with("Invalid use of expression. A Host Class Definition does not produce a value at line 1:6")
Puppet.expects(:err).with("Classes, definitions, and nodes may only appear at toplevel or inside other classes at line 1:6")
expect { parser.parse_string("$a = class foo { }", nil) }.to raise_error(/2 errors/)
end
it 'for unclosed quote with indication of start position of string' do
source = <<-SOURCE.gsub(/^ {6}/,'')
$a = "xx
yyy
SOURCE
# first char after opening " reported as being in error.
expect { parser.parse_string(source) }.to raise_error(/Unclosed quote after '"' followed by 'xx\\nyy\.\.\.' at line 1:7/)
end
it 'for multiple errors with a summary exception' do
Puppet.expects(:err).with("Invalid use of expression. A Node Definition does not produce a value at line 1:6")
Puppet.expects(:err).with("Classes, definitions, and nodes may only appear at toplevel or inside other classes at line 1:6")
expect { parser.parse_string("$a = node x { }",nil) }.to raise_error(/2 errors/)
end
it 'for a bad hostname' do
expect {
parser.parse_string("node 'macbook+owned+by+name' { }", nil)
}.to raise_error(/The hostname 'macbook\+owned\+by\+name' contains illegal characters.*at line 1:6/)
end
it 'for a hostname with interpolation' do
source = <<-SOURCE.gsub(/^ {6}/,'')
$name = 'fred'
node "macbook-owned-by$name" { }
SOURCE
expect {
parser.parse_string(source, nil)
}.to raise_error(/An interpolated expression is not allowed in a hostname of a node at line 2:23/)
end
end
context 'does not leak variables' do
it 'local variables are gone when lambda ends' do
source = <<-SOURCE
[1,2,3].each |$x| { $y = $x}
$a = $y
SOURCE
expect do
parser.evaluate_string(scope, source)
end.to raise_error(/Unknown variable: 'y'/)
end
it 'lambda parameters are gone when lambda ends' do
source = <<-SOURCE
[1,2,3].each |$x| { $y = $x}
$a = $x
SOURCE
expect do
parser.evaluate_string(scope, source)
end.to raise_error(/Unknown variable: 'x'/)
end
it 'does not leak match variables' do
source = <<-SOURCE
if 'xyz' =~ /(x)(y)(z)/ { notice $2 }
case 'abc' {
/(a)(b)(c)/ : { $x = $2 }
}
"-$x-$2-"
SOURCE
expect(parser.evaluate_string(scope, source)).to eq('-b--')
end
end
matcher :have_relationship do |expected|
calc = Puppet::Pops::Types::TypeCalculator.new
match do |compiler|
op_name = {'->' => :relationship, '~>' => :subscription}
compiler.relationships.any? do | relation |
relation.source.type == expected[0] &&
relation.source.title == expected[1] &&
relation.type == op_name[expected[2]] &&
relation.target.type == expected[3] &&
relation.target.title == expected[4]
end
end
failure_message_for_should do |actual|
"Relationship #{expected[0]}[#{expected[1]}] #{expected[2]} #{expected[3]}[#{expected[4]}] but was unknown to compiler"
end
end
end
|