File: functions4_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (1011 lines) | stat: -rw-r--r-- 33,919 bytes parent folder | download
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
require 'spec_helper'
require 'puppet/pops'
require 'puppet/loaders'
require 'puppet_spec/pops'
require 'puppet_spec/scope'

module FunctionAPISpecModule
  class TestDuck
  end

  class TestFunctionLoader < Puppet::Pops::Loader::StaticLoader
    def initialize
      @constants = {}
    end

    def add_function(name, function)
      set_entry(Puppet::Pops::Loader::TypedName.new(:function, name), function, __FILE__)
    end

    def add_type(name, type)
      set_entry(Puppet::Pops::Loader::TypedName.new(:type, name), type, __FILE__)
    end

    def set_entry(typed_name, value, origin = nil)
      @constants[typed_name] = Puppet::Pops::Loader::Loader::NamedEntry.new(typed_name, value, origin)
    end

    # override StaticLoader
    def load_constant(typed_name)
      @constants[typed_name]
    end
  end
end

describe 'the 4x function api' do
  include FunctionAPISpecModule
  include PuppetSpec::Pops
  include PuppetSpec::Scope

  let(:loader) { FunctionAPISpecModule::TestFunctionLoader.new }

  it 'allows a simple function to be created without dispatch declaration' do
    f = Puppet::Functions.create_function('min') do
      def min(x,y)
        x <= y ? x : y
      end
    end

    # the produced result is a Class inheriting from Function
    expect(f.class).to be(Class)
    expect(f.superclass).to be(Puppet::Functions::Function)
    # and this class had the given name (not a real Ruby class name)
    expect(f.name).to eql('min')
  end

  it 'refuses to create functions that are not based on the Function class' do
    expect do
      Puppet::Functions.create_function('testing', Object) {}
    end.to raise_error(ArgumentError, /function 'testing'.*Functions must be based on Puppet::Pops::Functions::Function. Got Object/)
  end

  it 'refuses to create functions with parameters that are not named with a symbol' do
    expect do
      Puppet::Functions.create_function('testing') do
        dispatch :test do
          param 'Integer', 'not_symbol'
        end
        def test(x)
        end
      end
    end.to raise_error(ArgumentError, /Parameter name argument must be a Symbol/)
  end

  it 'a function without arguments can be defined and called without dispatch declaration' do
    f = create_noargs_function_class()
    func = f.new(:closure_scope, :loader)
    expect(func.call({})).to eql(10)
  end

  it 'an error is raised when calling a no arguments function with arguments' do
    f = create_noargs_function_class()
    func = f.new(:closure_scope, :loader)
    expect{func.call({}, 'surprise')}.to raise_error(ArgumentError, "'test' expects no arguments, got 1")
  end

  it 'a simple function can be called' do
    f = create_min_function_class()
    # TODO: Bogus parameters, not yet used
    func = f.new(:closure_scope, :loader)
    expect(func.is_a?(Puppet::Functions::Function)).to be_truthy
    expect(func.call({}, 10,20)).to eql(10)
  end

  it 'an error is raised if called with too few arguments' do
    f = create_min_function_class()
    # TODO: Bogus parameters, not yet used
    func = f.new(:closure_scope, :loader)
    expect(func.is_a?(Puppet::Functions::Function)).to be_truthy
    expect do
      func.call({}, 10)
    end.to raise_error(ArgumentError, "'min' expects 2 arguments, got 1")
  end

  it 'an error is raised if called with too many arguments' do
    f = create_min_function_class()
    # TODO: Bogus parameters, not yet used
    func = f.new(:closure_scope, :loader)
    expect(func.is_a?(Puppet::Functions::Function)).to be_truthy
    expect do
      func.call({}, 10, 10, 10)
    end.to raise_error(ArgumentError, "'min' expects 2 arguments, got 3")
  end

  it 'correct dispatch is chosen when zero parameter dispatch exists' do
    f = create_function_with_no_parameter_dispatch
    func = f.new(:closure_scope, :loader)
    expect(func.is_a?(Puppet::Functions::Function)).to be_truthy
    expect(func.call({}, 1)).to eql(1)
  end

  it 'an error is raised if simple function-name and method are not matched' do
    expect do
      create_badly_named_method_function_class()
    end.to raise_error(ArgumentError, /Function Creation Error, cannot create a default dispatcher for function 'mix', no method with this name found/)
  end

  it 'the implementation separates dispatchers for different functions' do
    # this tests that meta programming / construction puts class attributes in the correct class
    f1 = create_min_function_class()
    f2 = create_max_function_class()

    d1 = f1.dispatcher
    d2 = f2.dispatcher

    expect(d1).to_not eql(d2)
    expect(d1.dispatchers[0]).to_not eql(d2.dispatchers[0])
  end

  context 'when using regular dispatch' do
    it 'a function can be created using dispatch and called' do
      f = create_min_function_class_using_dispatch()
      func = f.new(:closure_scope, :loader)
      expect(func.call({}, 3,4)).to eql(3)
    end

    it 'an error is raised with reference to given parameter names when called with mis-matched arguments' do
      f = create_min_function_class_using_dispatch()
      # TODO: Bogus parameters, not yet used
      func = f.new(:closure_scope, :loader)
      expect(func.is_a?(Puppet::Functions::Function)).to be_truthy
      expect do
        func.call({}, 10, 'ten')
      end.to raise_error(ArgumentError, "'min' parameter 'b' expects a Numeric value, got String")
    end

    it 'an error includes optional indicators for last element' do
      f = create_function_with_optionals_and_repeated_via_multiple_dispatch()
      # TODO: Bogus parameters, not yet used
      func = f.new(:closure_scope, :loader)
      expect(func.is_a?(Puppet::Functions::Function)).to be_truthy
      expect do
        func.call({}, 3, 10, 3, "4")
      end.to raise_error(ArgumentError, "'min' expects one of:
  (Numeric x, Numeric y, Numeric a?, Numeric b?, Numeric c*)
    rejected: parameter 'b' expects a Numeric value, got String
  (String x, String y, String a+)
    rejected: parameter 'x' expects a String value, got Integer")
    end

    it 'can create optional repeated parameter' do
      f = create_function_with_repeated
      func = f.new(:closure_scope, :loader)
      expect(func.call({})).to eql(0)
      expect(func.call({}, 1)).to eql(1)
      expect(func.call({}, 1, 2)).to eql(2)

      f = create_function_with_optional_repeated
      func = f.new(:closure_scope, :loader)
      expect(func.call({})).to eql(0)
      expect(func.call({}, 1)).to eql(1)
      expect(func.call({}, 1, 2)).to eql(2)
    end

    it 'can create required repeated parameter' do
      f = create_function_with_required_repeated
      func = f.new(:closure_scope, :loader)
      expect(func.call({}, 1)).to eql(1)
      expect(func.call({}, 1, 2)).to eql(2)
      expect { func.call({}) }.to raise_error(ArgumentError, "'count_args' expects at least 1 argument, got none")
    end

    it 'can create scope_param followed by repeated  parameter' do
      f = create_function_with_scope_param_required_repeat
      func = f.new(:closure_scope, :loader)
      expect(func.call({}, 'yay', 1,2,3)).to eql([{}, 'yay',1,2,3])
    end

    it 'a function can use inexact argument mapping' do
      f = create_function_with_inexact_dispatch
      func = f.new(:closure_scope, :loader)
      expect(func.call({}, 3.0,4.0,5.0)).to eql([Float, Float, Float])
      expect(func.call({}, 'Apple', 'Banana')).to eql([String, String])
    end

    it 'a function can be created using dispatch and called' do
      f = create_min_function_class_disptaching_to_two_methods()
      func = f.new(:closure_scope, :loader)
      expect(func.call({}, 3,4)).to eql(3)
      expect(func.call({}, 'Apple', 'Banana')).to eql('Apple')
    end

    it 'a function can not be created with parameters declared after a repeated parameter' do
      expect { create_function_with_param_after_repeated }.to raise_error(ArgumentError, 
        /function 't1'.*Parameters cannot be added after a repeated parameter/)
    end

    it 'a function can not be created with required parameters declared after optional ones' do
      expect { create_function_with_rq_after_opt }.to raise_error(ArgumentError, 
        /function 't1'.*A required parameter cannot be added after an optional parameter/)
    end

    it 'a function can not be created with required repeated parameters declared after optional ones' do
      expect { create_function_with_rq_repeated_after_opt }.to raise_error(ArgumentError,
        /function 't1'.*A required repeated parameter cannot be added after an optional parameter/)
    end

    it 'an error is raised with reference to multiple methods when called with mis-matched arguments' do
      f = create_min_function_class_disptaching_to_two_methods()
      # TODO: Bogus parameters, not yet used
      func = f.new(:closure_scope, :loader)
      expect(func.is_a?(Puppet::Functions::Function)).to be_truthy
      expect do
        func.call({}, 10, '20')
      end.to raise_error(ArgumentError, "'min' expects one of:
  (Numeric a, Numeric b)
    rejected: parameter 'b' expects a Numeric value, got String
  (String s1, String s2)
    rejected: parameter 's1' expects a String value, got Integer")
    end

    context 'an argument_mismatch handler' do
      let(:func) { create_function_with_mismatch_handler.new(:closure_scope, :loader) }

      it 'is called on matching arguments' do
        expect { func.call({}, '1') }.to raise_error(ArgumentError, "'test' It's not OK to pass a string")
      end

      it 'is not called unless arguments are matching' do
        expect { func.call({}, '1', 3) }.to raise_error(ArgumentError, "'test' expects 1 argument, got 2")
      end

      it 'is not included in a signature mismatch description' do
        expect { func.call({}, 2.3) }.to raise_error { |e| expect(e.message).not_to match(/String/) }
      end
    end

    context 'when requesting a type' do
      it 'responds with a Callable for a single signature' do
        tf = Puppet::Pops::Types::TypeFactory
        fc = create_min_function_class_using_dispatch()
        t = fc.dispatcher.to_type
        expect(t.class).to be(Puppet::Pops::Types::PCallableType)
        expect(t.param_types.class).to be(Puppet::Pops::Types::PTupleType)
        expect(t.param_types.types).to eql([tf.numeric(), tf.numeric()])
        expect(t.block_type).to be_nil
      end

      it 'responds with a Variant[Callable...] for multiple signatures' do
        tf = Puppet::Pops::Types::TypeFactory
        fc = create_min_function_class_disptaching_to_two_methods()
        t = fc.dispatcher.to_type
        expect(t.class).to be(Puppet::Pops::Types::PVariantType)
        expect(t.types.size).to eql(2)
        t1 = t.types[0]
        expect(t1.param_types.class).to be(Puppet::Pops::Types::PTupleType)
        expect(t1.param_types.types).to eql([tf.numeric(), tf.numeric()])
        expect(t1.block_type).to be_nil
        t2 = t.types[1]
        expect(t2.param_types.class).to be(Puppet::Pops::Types::PTupleType)
        expect(t2.param_types.types).to eql([tf.string(), tf.string()])
        expect(t2.block_type).to be_nil
      end
    end

    context 'supports lambdas' do
      it 'such that, a required block can be defined and given as an argument' do
        the_function = create_function_with_required_block_all_defaults().new(:closure_scope, :loader)
        result = the_function.call({}, 7) { |a,b| a < b ? a : b }
        expect(result).to eq(7)
      end

      it 'such that, a missing required block when called raises an error' do
        the_function = create_function_with_required_block_all_defaults().new(:closure_scope, :loader)
        expect do
          the_function.call({}, 10)
        end.to raise_error(ArgumentError, "'test' expects a block")
      end

      it 'such that, an optional block can be defined and given as an argument' do
        the_function = create_function_with_optional_block_all_defaults().new(:closure_scope, :loader)
        result = the_function.call({}, 4) { |a,b| a < b ? a : b }
        expect(result).to eql(4)
      end

      it 'such that, an optional block can be omitted when called and gets the value nil' do
        the_function = create_function_with_optional_block_all_defaults().new(:closure_scope, :loader)
        expect(the_function.call({}, 2)).to be_nil
      end

      it 'such that, a scope can be injected and a block can be used' do
        the_function = create_function_with_scope_required_block_all_defaults().new(:closure_scope, :loader)
        expect(the_function.call({}, 1) { |a,b| a < b ? a : b }).to eql(1)
      end
    end

    context 'provides signature information' do
      it 'about capture rest (varargs)' do
        fc = create_function_with_optionals_and_repeated
        signatures = fc.signatures
        expect(signatures.size).to eql(1)
        signature = signatures[0]
        expect(signature.last_captures_rest?).to be_truthy
      end

      it 'about optional and required parameters' do
        fc = create_function_with_optionals_and_repeated
        signature = fc.signatures[0]
        expect(signature.args_range).to eql( [2, Float::INFINITY ] )
        expect(signature.infinity?(signature.args_range[1])).to be_truthy
      end

      it 'about block not being allowed' do
        fc = create_function_with_optionals_and_repeated
        signature = fc.signatures[0]
        expect(signature.block_range).to eql( [ 0, 0 ] )
        expect(signature.block_type).to be_nil
      end

      it 'about required block' do
        fc = create_function_with_required_block_all_defaults
        signature = fc.signatures[0]
        expect(signature.block_range).to eql( [ 1, 1 ] )
        expect(signature.block_type).to_not be_nil
      end

      it 'about optional block' do
        fc = create_function_with_optional_block_all_defaults
        signature = fc.signatures[0]
        expect(signature.block_range).to eql( [ 0, 1 ] )
        expect(signature.block_type).to_not be_nil
      end

      it 'about the type' do
        fc = create_function_with_optional_block_all_defaults
        signature = fc.signatures[0]
        expect(signature.type.class).to be(Puppet::Pops::Types::PCallableType)
      end

      it 'about parameter names obtained from ruby introspection' do
        fc = create_min_function_class
        signature = fc.signatures[0]
        expect(signature.parameter_names).to eql(['x', 'y'])
      end

      it 'about parameter names specified with dispatch' do
        fc = create_min_function_class_using_dispatch
        signature = fc.signatures[0]
        expect(signature.parameter_names).to eql([:a, :b])
      end

      it 'about block_name when it is *not* given in the definition' do
        # neither type, nor name
        fc = create_function_with_required_block_all_defaults
        signature = fc.signatures[0]
        expect(signature.block_name).to eql(:block)
        # no name given, only type
        fc = create_function_with_required_block_given_type
        signature = fc.signatures[0]
        expect(signature.block_name).to eql(:block)
      end

      it 'about block_name when it *is* given in the definition' do
        # neither type, nor name
        fc = create_function_with_required_block_default_type
        signature = fc.signatures[0]
        expect(signature.block_name).to eql(:the_block)
        # no name given, only type
        fc = create_function_with_required_block_fully_specified
        signature = fc.signatures[0]
        expect(signature.block_name).to eql(:the_block)
      end
    end

    context 'supports calling other functions' do
      before(:all) do
        Puppet.push_context( {:loaders => Puppet::Pops::Loaders.new(Puppet::Node::Environment.create(:testing, []))})
      end

      after(:all) do
        Puppet.pop_context()
      end

      it 'such that, other functions are callable by name' do
        fc = Puppet::Functions.create_function('test') do
          def test()
            # Call a function available in the puppet system
            call_function('assert_type', 'Integer', 10)
          end
        end
        # initiate the function the same way the loader initiates it
        f = fc.new(:closure_scope, Puppet.lookup(:loaders).puppet_system_loader)
        expect(f.call({})).to eql(10)
      end

      it 'such that, calling a non existing function raises an error' do
        fc = Puppet::Functions.create_function('test') do
          def test()
            # Call a function not available in the puppet system
            call_function('no_such_function', 'Integer', 'hello')
          end
        end
        # initiate the function the same way the loader initiates it
        f = fc.new(:closure_scope, Puppet.lookup(:loaders).puppet_system_loader)
        expect{f.call({})}.to raise_error(ArgumentError, "Function test(): Unknown function: 'no_such_function'")
      end
    end

    context 'supports calling ruby functions with lambda from puppet' do
      before(:all) do
        Puppet.push_context( {:loaders => Puppet::Pops::Loaders.new(Puppet::Node::Environment.create(:testing, []))})
      end

      after(:all) do
        Puppet.pop_context()
      end

      before(:each) do
        Puppet[:strict_variables] = true
      end

      let(:parser) {  Puppet::Pops::Parser::EvaluatingParser.new }
      let(:node) { 'node.example.com' }
      let(:scope) { s = create_test_scope_for_node(node); s }
      let(:loader) { Puppet::Pops::Loaders.find_loader(nil) }

      it 'function with required block can be called' do
        # construct ruby function to call
        fc = Puppet::Functions.create_function('testing::test') do
          dispatch :test do
            param 'Integer', :x
            # block called 'the_block', and using "all_callables"
            required_block_param #(all_callables(), 'the_block')
          end
          def test(x)
            # call the block with x
            yield(x)
          end
        end
        # add the function to the loader (as if it had been loaded from somewhere)
        the_loader = loader
        f = fc.new({}, the_loader)
        loader.set_entry(Puppet::Pops::Loader::TypedName.new(:function, 'testing::test'), f)
        # evaluate a puppet call
        source = "testing::test(10) |$x| { $x+1 }"
        program = parser.parse_string(source, __FILE__)
        expect(Puppet::Pops::Adapters::LoaderAdapter).to receive(:loader_for_model_object).at_least(:once).and_return(the_loader)
        expect(parser.evaluate(scope, program)).to eql(11)
      end
    end

    context 'reports meaningful errors' do
      let(:parser) {  Puppet::Pops::Parser::EvaluatingParser.new }

      it 'syntax error in local type is reported with puppet source, puppet location, and ruby file containing function' do
        the_loader = loader()
        here = get_binding(the_loader)
        expect do
          eval(<<-CODE, here)
            Puppet::Functions.create_function('testing::test') do
              local_types do
                type 'MyType += Array[Integer]'
              end
              dispatch :test do
                param 'MyType', :x
              end
              def test(x)
                x
              end
            end
          CODE
        end.to raise_error(/MyType \+\= Array.*<Syntax error at '\+\=' \(line: 1, column: [0-9]+\)>.*functions4_spec\.rb.*/m)
        # Note that raised error reports this spec file as the function source since the function is defined here
      end

      it 'syntax error in param type is reported with puppet source, puppet location, and ruby file containing function' do
        the_loader = loader()
        here = get_binding(the_loader)
        expect do
          eval(<<-CODE, here)
            Puppet::Functions.create_function('testing::test') do
              dispatch :test do
                param 'Array[1+=1]', :x
              end
              def test(x)
                x
              end
            end
          CODE
        end.to raise_error(/Parsing of type string '"Array\[1\+=1\]"' failed with message: <Syntax error at '\]' \(line: 1, column: [0-9]+\)>/m)
      end

    end
    context 'can use a loader when parsing types in function dispatch, and' do
      let(:parser) {  Puppet::Pops::Parser::EvaluatingParser.new }

      it 'uses return_type to validate returned value' do
        the_loader = loader()
        here = get_binding(the_loader)
        fc = eval(<<-CODE, here)
          Puppet::Functions.create_function('testing::test') do
            dispatch :test do
              param 'Integer', :x
              return_type 'String'
            end
            def test(x)
              x
            end
          end
        CODE
        the_loader.add_function('testing::test', fc.new({}, the_loader))
        program = parser.parse_string('testing::test(10)', __FILE__)
        expect(Puppet::Pops::Adapters::LoaderAdapter).to receive(:loader_for_model_object).and_return(the_loader)
        expect { parser.evaluate({}, program) }.to raise_error(Puppet::Error,
          /value returned from function 'test' has wrong type, expects a String value, got Integer/)
      end

      it 'resolve a referenced Type alias' do
        the_loader = loader()
        the_loader.add_type('myalias', type_alias_t('MyAlias', 'Integer'))
        here = get_binding(the_loader)
        fc = eval(<<-CODE, here)
          Puppet::Functions.create_function('testing::test') do
            dispatch :test do
              param 'MyAlias', :x
              return_type 'MyAlias'
            end
            def test(x)
              x
            end
          end
        CODE
        the_loader.add_function('testing::test', fc.new({}, the_loader))
        program = parser.parse_string('testing::test(10)', __FILE__)
        expect(Puppet::Pops::Adapters::LoaderAdapter).to receive(:loader_for_model_object).and_return(the_loader)
        expect(parser.evaluate({}, program)).to eql(10)
      end

      it 'reports a reference to an unresolved type' do
        the_loader = loader()
        here = get_binding(the_loader)
        fc = eval(<<-CODE, here)
          Puppet::Functions.create_function('testing::test') do
            dispatch :test do
              param 'MyAlias', :x
            end
            def test(x)
              x
            end
          end
        CODE
        the_loader.add_function('testing::test', fc.new({}, the_loader))
        program = parser.parse_string('testing::test(10)', __FILE__)
        expect(Puppet::Pops::Adapters::LoaderAdapter).to receive(:loader_for_model_object).and_return(the_loader)
        expect { parser.evaluate({}, program) }.to raise_error(Puppet::Error, /parameter 'x' references an unresolved type 'MyAlias'/)
      end

      it 'create local Type aliases' do
        the_loader = loader()
        here = get_binding(the_loader)
        fc = eval(<<-CODE, here)
          Puppet::Functions.create_function('testing::test') do
            local_types do
              type 'MyType = Array[Integer]'
            end
            dispatch :test do
              param 'MyType', :x
            end
            def test(x)
              x
            end
          end
        CODE
        the_loader.add_function('testing::test', fc.new({}, the_loader))
        program = parser.parse_string('testing::test([10,20])', __FILE__)
        expect(Puppet::Pops::Adapters::LoaderAdapter).to receive(:loader_for_model_object).and_return(the_loader)
        expect(parser.evaluate({}, program)).to eq([10,20])
      end

      it 'create nested local Type aliases' do
        the_loader = loader()
        here = get_binding(the_loader)
        fc = eval(<<-CODE, here)
          Puppet::Functions.create_function('testing::test') do
            local_types do
              type 'InnerType = Array[Integer]'
              type 'OuterType = Hash[String,InnerType]'
            end
            dispatch :test do
              param 'OuterType', :x
            end
            def test(x)
              x
            end
          end
        CODE
        the_loader.add_function('testing::test', fc.new({}, the_loader))
        program = parser.parse_string("testing::test({'x' => [10,20]})", __FILE__)
        expect(Puppet::Pops::Adapters::LoaderAdapter).to receive(:loader_for_model_object).and_return(the_loader)
        expect(parser.evaluate({}, program)).to eq({'x' => [10,20]})
      end

      it 'create self referencing local Type aliases' do
        the_loader = loader()
        here = get_binding(the_loader)
        fc = eval(<<-CODE, here)
          Puppet::Functions.create_function('testing::test') do
            local_types do
              type 'Tree = Hash[String,Variant[String,Tree]]'
            end
            dispatch :test do
              param 'Tree', :x
            end
            def test(x)
              x
            end
          end
        CODE
        the_loader.add_function('testing::test', fc.new({}, the_loader))
        program = parser.parse_string("testing::test({'x' => {'y' => 'n'}})", __FILE__)
        expect(Puppet::Pops::Adapters::LoaderAdapter).to receive(:loader_for_model_object).and_return(the_loader)
        expect(parser.evaluate({}, program)).to eq({'x' => {'y' => 'n'}})
      end
    end
  end

  def create_noargs_function_class
    Puppet::Functions.create_function('test') do
      def test()
        10
      end
    end
  end

  def create_min_function_class
    Puppet::Functions.create_function('min') do
      def min(x,y)
        x <= y ? x : y
      end
    end
  end

  def create_max_function_class
    Puppet::Functions.create_function('max') do
      def max(x,y)
        x >= y ? x : y
      end
    end
  end

  def create_badly_named_method_function_class
    Puppet::Functions.create_function('mix') do
      def mix_up(x,y)
        x <= y ? x : y
      end
    end
  end

  def create_min_function_class_using_dispatch
    Puppet::Functions.create_function('min') do
        dispatch :min do
          param 'Numeric', :a
          param 'Numeric', :b
        end
      def min(x,y)
        x <= y ? x : y
      end
    end
  end

  def create_min_function_class_disptaching_to_two_methods
    Puppet::Functions.create_function('min') do
      dispatch :min do
        param 'Numeric', :a
        param 'Numeric', :b
      end

      dispatch :min_s do
        param 'String', :s1
        param 'String', :s2
      end

      def min(x,y)
        x <= y ? x : y
      end

      def min_s(x,y)
        cmp = (x.downcase <=> y.downcase)
        cmp <= 0 ? x : y
      end
    end
  end

  def create_function_with_optionals_and_repeated
    Puppet::Functions.create_function('min') do
      def min(x,y,a=1, b=1, *c)
        x <= y ? x : y
      end
    end
  end

  def create_function_with_optionals_and_repeated_via_dispatch
    Puppet::Functions.create_function('min') do
      dispatch :min do
        param 'Numeric', :x
        param 'Numeric', :y
        optional_param 'Numeric', :a
        optional_param 'Numeric', :b
        repeated_param 'Numeric', :c
      end
      def min(x,y,a=1, b=1, *c)
        x <= y ? x : y
      end
    end
  end

  def create_function_with_optionals_and_repeated_via_multiple_dispatch
    Puppet::Functions.create_function('min') do
      dispatch :min do
        param 'Numeric', :x
        param 'Numeric', :y
        optional_param 'Numeric', :a
        optional_param 'Numeric', :b
        repeated_param 'Numeric', :c
      end
      dispatch :min do
        param 'String', :x
        param 'String', :y
        required_repeated_param 'String', :a
      end
      def min(x,y,a=1, b=1, *c)
        x <= y ? x : y
      end
    end
  end

  def create_function_with_required_repeated_via_dispatch
    Puppet::Functions.create_function('min') do
      dispatch :min do
        param 'Numeric', :x
        param 'Numeric', :y
        required_repeated_param 'Numeric', :z
      end
      def min(x,y, *z)
        x <= y ? x : y
      end
    end
  end

  def create_function_with_repeated
    Puppet::Functions.create_function('count_args') do
      dispatch :count_args do
        repeated_param 'Any', :c
      end
      def count_args(*c)
        c.size
      end
    end
  end

  def create_function_with_optional_repeated
    Puppet::Functions.create_function('count_args') do
      dispatch :count_args do
        optional_repeated_param 'Any', :c
      end
      def count_args(*c)
        c.size
      end
    end
  end

  def create_function_with_required_repeated
    Puppet::Functions.create_function('count_args') do
      dispatch :count_args do
        required_repeated_param 'Any', :c
      end
      def count_args(*c)
        c.size
      end
    end
  end

  def create_function_with_inexact_dispatch
    Puppet::Functions.create_function('t1') do
      dispatch :t1 do
        param 'Numeric', :x
        param 'Numeric', :y
        repeated_param 'Numeric', :z
      end
      dispatch :t1 do
        param 'String', :x
        param 'String', :y
        repeated_param 'String', :z
      end
      def t1(first, *x)
        [first.class, *x.map {|e|e.class}]
      end
    end
  end

  def create_function_with_rq_after_opt
    Puppet::Functions.create_function('t1') do
      dispatch :t1 do
        optional_param 'Numeric', :x
        param 'Numeric', :y
      end
      def t1(*x)
        x
      end
    end
  end

  def create_function_with_rq_repeated_after_opt
    Puppet::Functions.create_function('t1') do
      dispatch :t1 do
        optional_param 'Numeric', :x
        required_repeated_param 'Numeric', :y
      end
      def t1(x, *y)
        x
      end
    end
  end

  def create_function_with_param_after_repeated
    Puppet::Functions.create_function('t1') do
      dispatch :t1 do
        repeated_param 'Numeric', :x
        param 'Numeric', :y
      end
      def t1(*x)
        x
      end
    end
  end

  def create_function_with_param_injection_regular
    Puppet::Functions.create_function('test', Puppet::Functions::InternalFunction) do
      attr_injected Puppet::Pops::Types::TypeFactory.type_of(FunctionAPISpecModule::TestDuck), :test_attr
      attr_injected Puppet::Pops::Types::TypeFactory.string(), :test_attr2, "a_string"
      attr_injected_producer Puppet::Pops::Types::TypeFactory.integer(), :serial, "an_int"

      dispatch :test do
        injected_param Puppet::Pops::Types::TypeFactory.string, :x, 'a_string'
        injected_producer_param Puppet::Pops::Types::TypeFactory.integer, :y, 'an_int'
        param 'Scalar', :a
        param 'Scalar', :b
      end

      def test(x,y,a,b)
        y_produced = y.produce(nil)
        "#{x}! #{a}, and #{b} < #{y_produced} = #{ !!(a < y_produced && b < y_produced)}"
      end
    end
  end

  def create_function_with_required_block_all_defaults
    Puppet::Functions.create_function('test') do
      dispatch :test do
        param 'Integer', :x
        # use defaults, any callable, name is 'block'
        block_param
      end
      def test(x)
        yield(8,x)
      end
    end
  end

  def create_function_with_scope_required_block_all_defaults
    Puppet::Functions.create_function('test', Puppet::Functions::InternalFunction) do
      dispatch :test do
        scope_param
        param 'Integer', :x
        # use defaults, any callable, name is 'block'
        required_block_param
      end
      def test(scope, x)
        yield(3,x)
      end
    end
  end

  def create_function_with_required_block_default_type
    Puppet::Functions.create_function('test') do
      dispatch :test do
        param 'Integer', :x
        # use defaults, any callable, name is 'block'
        required_block_param :the_block
      end
      def test(x)
        yield
      end
    end
  end

  def create_function_with_scope_param_required_repeat
    Puppet::Functions.create_function('test', Puppet::Functions::InternalFunction) do
      dispatch :test do
        scope_param
        param 'Any', :extra
        repeated_param 'Any', :the_block
      end
      def test(scope, *args)
        [scope, *args]
      end
    end
  end

  def create_function_with_required_block_given_type
    Puppet::Functions.create_function('test') do
      dispatch :test do
        param 'Integer', :x
        required_block_param
      end
      def test(x)
        yield
      end
    end
  end

  def create_function_with_required_block_fully_specified
    Puppet::Functions.create_function('test') do
      dispatch :test do
        param 'Integer', :x
        # use defaults, any callable, name is 'block'
        required_block_param('Callable', :the_block)
      end
      def test(x)
        yield
      end
    end
  end

  def create_function_with_optional_block_all_defaults
    Puppet::Functions.create_function('test') do
      dispatch :test do
        param 'Integer', :x
        # use defaults, any callable, name is 'block'
        optional_block_param
      end
      def test(x)
        yield(5,x) if block_given?
      end
    end
  end

  def create_function_with_no_parameter_dispatch
    Puppet::Functions.create_function('test') do
      dispatch :test_no_args do
      end
      dispatch :test_one_arg do
        param 'Integer', :x
      end
      def test_no_args
        0
      end
      def test_one_arg(x)
        x
      end
    end
  end

  def create_function_with_mismatch_handler
    Puppet::Functions.create_function('test') do
      dispatch :test do
        param 'Integer', :x
      end

      argument_mismatch :on_error do
        param 'String', :x
      end

      def test(x)
        yield(5,x) if block_given?
      end

      def on_error(x)
        "It's not OK to pass a string"
      end
    end
  end

  def type_alias_t(name, type_string)
    type_expr = Puppet::Pops::Parser::EvaluatingParser.new.parse_string(type_string)
    Puppet::Pops::Types::TypeFactory.type_alias(name, type_expr)
  end

  def get_binding(loader_injected_arg)
    binding
  end
end