summaryrefslogtreecommitdiff
path: root/spec/unit/property_spec.rb
blob: 533f0f4e4d131e493fae4c09e22d20101bc4d334 (plain)
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
require 'support/shared/integration/integration_helper'

describe "Chef::Resource.property" do
  include IntegrationSupport

  module Namer
    @i = 0
    def self.next_resource_name
      "chef_resource_property_spec_#{@i += 1}"
    end
    def self.reset_index
      @current_index = 0
    end
    def self.current_index
      @current_index
    end
    def self.next_index
      @current_index += 1
    end
  end

  def lazy(&block)
    Chef::DelayedEvaluator.new(&block)
  end

  before do
    Namer.reset_index
  end

  def self.new_resource_name
    Namer.next_resource_name
  end

  let(:resource_class) do
    new_resource_name = self.class.new_resource_name
    Class.new(Chef::Resource) do
      resource_name new_resource_name
      def next_index
        Namer.next_index
      end
    end
  end

  let(:resource) do
    resource_class.new("blah")
  end

  def self.english_join(values)
    return '<nothing>' if values.size == 0
    return values[0].inspect if values.size == 1
    "#{values[0..-2].map { |v| v.inspect }.join(", ")} and #{values[-1].inspect}"
  end

  def self.with_property(*properties, &block)
    tags_index = properties.find_index { |p| !p.is_a?(String)}
    if tags_index
      properties, tags = properties[0..tags_index-1], properties[tags_index..-1]
    else
      tags = []
    end
    if properties.size == 1
      description = "With property #{properties.first}"
    else
      description = "With properties #{english_join(properties.map { |property| "#{property.inspect}" })}"
    end
    context description, *tags do
      before do
        properties.each do |property_str|
          resource_class.class_eval("property #{property_str}", __FILE__, __LINE__)
        end
      end
      instance_eval(&block)
    end
  end

  # Basic properties
  with_property ':bare_property' do
    it "can be set" do
      expect(resource.bare_property 10).to eq 10
      expect(resource.bare_property).to eq 10
    end
    it "emits a deprecation warning and does a get, if set to nil" do
      expect(resource.bare_property 10).to eq 10
      expect { resource.bare_property nil }.to raise_error Chef::Exceptions::DeprecatedFeatureError
      Chef::Config[:treat_deprecation_warnings_as_errors] = false
      expect(resource.bare_property nil).to eq 10
      expect(resource.bare_property).to eq 10
    end
    it "can be updated" do
      expect(resource.bare_property 10).to eq 10
      expect(resource.bare_property 20).to eq 20
      expect(resource.bare_property).to eq 20
    end
    it "can be set with =" do
      expect(resource.bare_property 10).to eq 10
      expect(resource.bare_property).to eq 10
    end
    it "can be set to nil with =" do
      expect(resource.bare_property 10).to eq 10
      expect(resource.bare_property = nil).to be_nil
      expect(resource.bare_property).to be_nil
    end
    it "can be updated with =" do
      expect(resource.bare_property 10).to eq 10
      expect(resource.bare_property = 20).to eq 20
      expect(resource.bare_property).to eq 20
    end
  end

  with_property ":x, Integer" do
    context "and subclass" do
      let(:subresource_class) do
        new_resource_name = self.class.new_resource_name
        Class.new(resource_class) do
          resource_name new_resource_name
        end
      end
      let(:subresource) do
        subresource_class.new('blah')
      end

      it "x is inherited" do
        expect(subresource.x 10).to eq 10
        expect(subresource.x).to eq 10
        expect(subresource.x = 20).to eq 20
        expect(subresource.x).to eq 20
        expect(subresource_class.properties[:x]).not_to be_nil
      end

      it "x's validation is inherited" do
        expect { subresource.x 'ohno' }.to raise_error Chef::Exceptions::ValidationFailed
      end

      context "with property :y on the subclass" do
        before do
          subresource_class.class_eval do
            property :y
          end
        end

        it "x is still there" do
          expect(subresource.x 10).to eq 10
          expect(subresource.x).to eq 10
          expect(subresource.x = 20).to eq 20
          expect(subresource.x).to eq 20
          expect(subresource_class.properties[:x]).not_to be_nil
        end
        it "y is there" do
          expect(subresource.y 10).to eq 10
          expect(subresource.y).to eq 10
          expect(subresource.y = 20).to eq 20
          expect(subresource.y).to eq 20
          expect(subresource_class.properties[:y]).not_to be_nil
        end
        it "y is not on the superclass" do
          expect { resource_class.y 10 }.to raise_error
          expect(resource_class.properties[:y]).to be_nil
        end
      end

      context "with property :x on the subclass" do
        before do
          subresource_class.class_eval do
            property :x
          end
        end

        it "x is still there" do
          expect(subresource.x 10).to eq 10
          expect(subresource.x).to eq 10
          expect(subresource.x = 20).to eq 20
          expect(subresource.x).to eq 20
          expect(subresource_class.properties[:x]).not_to be_nil
          expect(subresource_class.properties[:x]).not_to eq resource_class.properties[:x]
        end

        it "x's validation is inherited" do
          expect { subresource.x 'ohno' }.to raise_error Chef::Exceptions::ValidationFailed
        end
      end

      context "with property :x, default: 80 on the subclass" do
        before do
          subresource_class.class_eval do
            property :x, default: 80
          end
        end

        it "x is still there" do
          expect(subresource.x 10).to eq 10
          expect(subresource.x).to eq 10
          expect(subresource.x = 20).to eq 20
          expect(subresource.x).to eq 20
          expect(subresource_class.properties[:x]).not_to be_nil
          expect(subresource_class.properties[:x]).not_to eq resource_class.properties[:x]
        end

        it "x defaults to 80" do
          expect(subresource.x).to eq 80
        end

        it "x's validation is inherited" do
          expect { subresource.x 'ohno' }.to raise_error Chef::Exceptions::ValidationFailed
        end
      end

      context "with property :x, String on the subclass" do
        before do
          subresource_class.class_eval do
            property :x, String
          end
        end

        it "x is still there" do
          expect(subresource.x "10").to eq "10"
          expect(subresource.x).to eq "10"
          expect(subresource.x = "20").to eq "20"
          expect(subresource.x).to eq "20"
          expect(subresource_class.properties[:x]).not_to be_nil
          expect(subresource_class.properties[:x]).not_to eq resource_class.properties[:x]
        end

        it "x's validation is overwritten" do
          expect { subresource.x 10 }.to raise_error Chef::Exceptions::ValidationFailed
          expect(subresource.x 'ohno').to eq 'ohno'
          expect(subresource.x).to eq 'ohno'
        end

        it "the superclass's validation for x is still there" do
          expect { resource.x 'ohno' }.to raise_error Chef::Exceptions::ValidationFailed
          expect(resource.x 10).to eq 10
          expect(resource.x).to eq 10
        end
      end
    end
  end

  context "Chef::Resource::Property#reset_property" do
    it "when a resource is newly created, reset_property(:name) sets property to nil" do
      expect(resource.property_is_set?(:name)).to be_truthy
      resource.reset_property(:name)
      expect(resource.property_is_set?(:name)).to be_falsey
      expect(resource.name).to be_nil
    end

    it "when referencing an undefined property, reset_property(:x) raises an error" do
      expect { resource.reset_property(:x) }.to raise_error(ArgumentError)
    end

    with_property ':x' do
      it "when the resource is newly created, reset_property(:x) does nothing" do
        expect(resource.property_is_set?(:x)).to be_falsey
        resource.reset_property(:x)
        expect(resource.property_is_set?(:x)).to be_falsey
        expect(resource.x).to be_nil
      end
      it "when x is set, reset_property resets it" do
        resource.x 10
        expect(resource.property_is_set?(:x)).to be_truthy
        resource.reset_property(:x)
        expect(resource.property_is_set?(:x)).to be_falsey
        expect(resource.x).to be_nil
      end
    end

    with_property ':x, Integer' do
      it "when the resource is newly created, reset_property(:x) does nothing" do
        expect(resource.property_is_set?(:x)).to be_falsey
        resource.reset_property(:x)
        expect(resource.property_is_set?(:x)).to be_falsey
        expect(resource.x).to be_nil
      end
      it "when x is set, reset_property resets it even though `nil` is technically invalid" do
        resource.x 10
        expect(resource.property_is_set?(:x)).to be_truthy
        resource.reset_property(:x)
        expect(resource.property_is_set?(:x)).to be_falsey
        expect(resource.x).to be_nil
      end
    end

    with_property ':x, default: 10' do
      it "when the resource is newly created, reset_property(:x) does nothing" do
        expect(resource.property_is_set?(:x)).to be_falsey
        resource.reset_property(:x)
        expect(resource.property_is_set?(:x)).to be_falsey
        expect(resource.x).to eq 10
      end
      it "when x is set, reset_property resets it and it returns the default" do
        resource.x 20
        resource.reset_property(:x)
        expect(resource.property_is_set?(:x)).to be_falsey
        expect(resource.x).to eq 10
      end
    end

    with_property ':x, default: lazy { 10 }' do
      it "when the resource is newly created, reset_property(:x) does nothing" do
        expect(resource.property_is_set?(:x)).to be_falsey
        resource.reset_property(:x)
        expect(resource.property_is_set?(:x)).to be_falsey
        expect(resource.x).to eq 10
      end
      it "when x is set, reset_property resets it and it returns the default" do
        resource.x 20
        resource.reset_property(:x)
        expect(resource.property_is_set?(:x)).to be_falsey
        expect(resource.x).to eq 10
      end
    end
  end

  context "Chef::Resource::Property#property_is_set?" do
    it "when a resource is newly created, property_is_set?(:name) is true" do
      expect(resource.property_is_set?(:name)).to be_truthy
    end

    it "when referencing an undefined property, property_is_set?(:x) raises an error" do
      expect { resource.property_is_set?(:x) }.to raise_error(ArgumentError)
    end

    with_property ':x' do
      it "when the resource is newly created, property_is_set?(:x) is false" do
        expect(resource.property_is_set?(:x)).to be_falsey
      end
      it "when x is set, property_is_set?(:x) is true" do
        resource.x 10
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is set with =, property_is_set?(:x) is true" do
        resource.x = 10
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is set to a lazy value, property_is_set?(:x) is true" do
        resource.x lazy { 10 }
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is retrieved, property_is_set?(:x) is false" do
        resource.x
        expect(resource.property_is_set?(:x)).to be_falsey
      end
    end

    with_property ':x, default: 10' do
      it "when the resource is newly created, property_is_set?(:x) is false" do
        expect(resource.property_is_set?(:x)).to be_falsey
      end
      it "when x is set, property_is_set?(:x) is true" do
        resource.x 10
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is set with =, property_is_set?(:x) is true" do
        resource.x = 10
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is set to a lazy value, property_is_set?(:x) is true" do
        resource.x lazy { 10 }
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is retrieved, property_is_set?(:x) is false" do
        resource.x
        expect(resource.property_is_set?(:x)).to be_falsey
      end
    end

    with_property ':x, default: nil' do
      it "when the resource is newly created, property_is_set?(:x) is false" do
        expect(resource.property_is_set?(:x)).to be_falsey
      end
      it "when x is set, property_is_set?(:x) is true" do
        resource.x 10
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is set with =, property_is_set?(:x) is true" do
        resource.x = 10
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is set to a lazy value, property_is_set?(:x) is true" do
        resource.x lazy { 10 }
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is retrieved, property_is_set?(:x) is false" do
        resource.x
        expect(resource.property_is_set?(:x)).to be_falsey
      end
    end

    with_property ':x, default: lazy { 10 }' do
      it "when the resource is newly created, property_is_set?(:x) is false" do
        expect(resource.property_is_set?(:x)).to be_falsey
      end
      it "when x is set, property_is_set?(:x) is true" do
        resource.x 10
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is set with =, property_is_set?(:x) is true" do
        resource.x = 10
        expect(resource.property_is_set?(:x)).to be_truthy
      end
      it "when x is retrieved, property_is_set?(:x) is false" do
        resource.x
        expect(resource.property_is_set?(:x)).to be_falsey
      end
    end
  end

  context "Chef::Resource::Property#default" do
    with_property ':x, default: 10' do
      it "when x is set, it returns its value" do
        expect(resource.x 20).to eq 20
        expect(resource.property_is_set?(:x)).to be_truthy
        expect(resource.x).to eq 20
      end
      it "when x is not set, it returns 10" do
        expect(resource.x).to eq 10
      end
      it "when x is not set, it is not included in state" do
        expect(resource.state_for_resource_reporter).to eq({})
      end
      it "when x is set to nil, it returns nil" do
        resource.instance_eval { @x = nil }
        expect(resource.x).to be_nil
      end

      context "With a subclass" do
        let(:subresource_class) do
          new_resource_name = self.class.new_resource_name
          Class.new(resource_class) do
            resource_name new_resource_name
          end
        end
        let(:subresource) { subresource_class.new('blah') }
        it "The default is inherited" do
          expect(subresource.x).to eq 10
        end
      end
    end

    with_property ':x, default: 10, identity: true' do
      it "when x is not set, it is included in identity" do
        expect(resource.identity).to eq(10)
      end
    end

    with_property ':x, default: 1, identity: true', ':y, default: 2, identity: true' do
      it "when x is not set, it is still included in identity" do
        resource.y 20
        expect(resource.identity).to eq(x: 1, y: 20)
      end
    end

    with_property ':x, default: nil' do
      it "when x is not set, it returns nil" do
        expect(resource.x).to be_nil
      end
    end

    with_property ':x' do
      it "when x is not set, it returns nil" do
        expect(resource.x).to be_nil
      end
    end

    context "hash default" do
      context "(deprecations allowed)" do
        before { Chef::Config[:treat_deprecation_warnings_as_errors] = false }

        with_property ':x, default: {}' do
          it "when x is not set, it returns {}" do
            expect(resource.x).to eq({})
          end
          it "The same exact value is returned multiple times in a row" do
            value = resource.x
            expect(value).to eq({})
            expect(resource.x.object_id).to eq(value.object_id)
          end
          it "Multiple instances of x receive the exact same value" do
            expect(resource.x.object_id).to eq(resource_class.new('blah2').x.object_id)
          end
        end
      end

      with_property ':x, default: lazy { {} }' do
        it "when x is not set, it returns {}" do
          expect(resource.x).to eq({})
        end
        # it "The value is different each time it is called" do
        #   value = resource.x
        #   expect(value).to eq({})
        #   expect(resource.x.object_id).not_to eq(value.object_id)
        # end
        it "Multiple instances of x receive different values" do
          expect(resource.x.object_id).not_to eq(resource_class.new('blah2').x.object_id)
        end
      end
    end

    context "with a class with 'blah' as both class and instance methods" do
      before do
        resource_class.class_eval do
          def self.blah
            'class'
          end
          def blah
            "#{name}#{next_index}"
          end
        end
      end

      with_property ':x, default: lazy { blah }' do
        it "x is run in context of the instance" do
          expect(resource.x).to eq "blah1"
        end
        it "x is run in the context of each instance it is run in" do
          expect(resource.x).to eq "blah1"
          expect(resource_class.new('another').x).to eq "another2"
          # expect(resource.x).to eq "blah3"
        end
      end

      with_property ':x, default: lazy { |x| "#{blah}#{x.blah}" }' do
        it "x is run in context of the class (where it was defined) and passed the instance" do
          expect(resource.x).to eq "classblah1"
        end
        it "x is passed the value of each instance it is run in" do
          expect(resource.x).to eq "classblah1"
          expect(resource_class.new('another').x).to eq "classanother2"
          # expect(resource.x).to eq "classblah3"
        end
      end
    end

    context "validation of defaults" do
      with_property ':x, String, default: 10' do
        it "when the resource is created, no error is raised" do
          resource
        end
        it "when x is set, no error is raised" do
          expect(resource.x 'hi').to eq 'hi'
          expect(resource.x).to eq 'hi'
        end
        it "when x is retrieved, no validation error is raised" do
          expect(resource.x).to eq 10
        end
        # it "when x is retrieved, a validation error is raised" do
        #   expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed
        # end
      end

      with_property ":x, String, default: lazy { Namer.next_index }" do
        it "when the resource is created, no error is raised" do
          resource
        end
        it "when x is set, no error is raised" do
          expect(resource.x 'hi').to eq 'hi'
          expect(resource.x).to eq 'hi'
        end
        it "when x is retrieved, no validation error is raised" do
          expect(resource.x).to eq 1
          expect(Namer.current_index).to eq 1
        end
        # it "when x is retrieved, a validation error is raised" do
        #   expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed
        #   expect(Namer.current_index).to eq 1
        # end
      end

      with_property ":x, default: lazy { Namer.next_index.to_s }, is: proc { |v| Namer.next_index; true }" do
        it "validation is not run at all on the default value" do
          expect(resource.x).to eq '1'
          expect(Namer.current_index).to eq 1
        end
        # it "validation is run each time" do
        #   expect(resource.x).to eq '1'
        #   expect(Namer.current_index).to eq 2
        #   expect(resource.x).to eq '1'
        #   expect(Namer.current_index).to eq 2
        # end
      end

      with_property ":x, default: lazy { Namer.next_index.to_s.freeze }, is: proc { |v| Namer.next_index; true }" do
        it "validation is not run at all on the default value" do
          expect(resource.x).to eq '1'
          expect(Namer.current_index).to eq 1
        end
        # it "validation is only run the first time" do
        #   expect(resource.x).to eq '1'
        #   expect(Namer.current_index).to eq 2
        #   expect(resource.x).to eq '1'
        #   expect(Namer.current_index).to eq 2
        # end
      end
    end

    context "coercion of defaults" do
      # Frozen default, non-frozen coerce
      with_property ':x, coerce: proc { |v| "#{v}#{next_index}" }, default: 10' do
        it "when the resource is created, the proc is not yet run" do
          resource
          expect(Namer.current_index).to eq 0
        end
        it "when x is set, coercion is run" do
          expect(resource.x 'hi').to eq 'hi1'
          expect(resource.x).to eq 'hi1'
          expect(Namer.current_index).to eq 1
        end
        it "when x is retrieved, coercion is run exactly once" do
          expect(resource.x).to eq '101'
          expect(resource.x).to eq '101'
          expect(Namer.current_index).to eq 1
        end
      end

      # Frozen default, frozen coerce
      with_property ':x, coerce: proc { |v| "#{v}#{next_index}".freeze }, default: 10' do
        it "when the resource is created, the proc is not yet run" do
          resource
          expect(Namer.current_index).to eq 0
        end
        it "when x is set, coercion is run" do
          expect(resource.x 'hi').to eq 'hi1'
          expect(resource.x).to eq 'hi1'
          expect(Namer.current_index).to eq 1
        end
        it "when x is retrieved, coercion is run each time" do
          expect(resource.x).to eq '101'
          expect(resource.x).to eq '102'
          expect(Namer.current_index).to eq 2
        end
      end

      # Frozen lazy default, non-frozen coerce
      with_property ':x, coerce: proc { |v| "#{v}#{next_index}" }, default: lazy { 10 }' do
        it "when the resource is created, the proc is not yet run" do
          resource
          expect(Namer.current_index).to eq 0
        end
        it "when x is set, coercion is run" do
          expect(resource.x 'hi').to eq 'hi1'
          expect(resource.x).to eq 'hi1'
          expect(Namer.current_index).to eq 1
        end
        it "when x is retrieved, coercion is run exactly once" do
          expect(resource.x).to eq '101'
          expect(resource.x).to eq '101'
          expect(Namer.current_index).to eq 1
        end
      end

      # Non-frozen lazy default, frozen coerce
      with_property ':x, coerce: proc { |v| "#{v}#{next_index}".freeze }, default: lazy { "10" }' do
        it "when the resource is created, the proc is not yet run" do
          resource
          expect(Namer.current_index).to eq 0
        end
        it "when x is set, coercion is run" do
          expect(resource.x 'hi').to eq 'hi1'
          expect(resource.x).to eq 'hi1'
          expect(Namer.current_index).to eq 1
        end
        it "when x is retrieved, coercion is run each time" do
          expect(resource.x).to eq '101'
          expect(resource.x).to eq '102'
          expect(Namer.current_index).to eq 2
        end
      end

      with_property ':x, proc { |v| Namer.next_index; true }, coerce: proc { |v| "#{v}#{next_index}" }, default: lazy { 10 }' do
        it "coercion is only run the first time x is retrieved, and validation is not run" do
          expect(Namer.current_index).to eq 0
          expect(resource.x).to eq '101'
          expect(Namer.current_index).to eq 1
          expect(resource.x).to eq '101'
          expect(Namer.current_index).to eq 1
        end
      end

      context "validation and coercion of defaults" do
        with_property ':x, String, coerce: proc { |v| "#{v}#{next_index}" }, default: 10' do
          it "when x is retrieved, it is coerced before validating and passes" do
            expect(resource.x).to eq '101'
          end
        end
        with_property ':x, Integer, coerce: proc { |v| "#{v}#{next_index}" }, default: 10' do
          it "when x is retrieved, it is coerced and not validated" do
            expect(resource.x).to eq '101'
          end
          # it "when x is retrieved, it is coerced before validating and fails" do
          #   expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed
          # end
        end
        with_property ':x, String, coerce: proc { |v| "#{v}#{next_index}" }, default: lazy { 10 }' do
          it "when x is retrieved, it is coerced before validating and passes" do
            expect(resource.x).to eq '101'
          end
        end
        with_property ':x, Integer, coerce: proc { |v| "#{v}#{next_index}" }, default: lazy { 10 }' do
          it "when x is retrieved, it is coerced and not validated" do
            expect(resource.x).to eq '101'
          end
          # it "when x is retrieved, it is coerced before validating and fails" do
          #   expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed
          # end
        end
        with_property ':x, proc { |v| Namer.next_index; true }, coerce: proc { |v| "#{v}#{next_index}" }, default: lazy { 10 }' do
          it "coercion is only run the first time x is retrieved, and validation is not run" do
            expect(Namer.current_index).to eq 0
            expect(resource.x).to eq '101'
            expect(Namer.current_index).to eq 1
            expect(resource.x).to eq '101'
            expect(Namer.current_index).to eq 1
          end
        end
      end
    end
  end

  context "Chef::Resource#lazy" do
    with_property ':x' do
      it "setting x to a lazy value does not run it immediately" do
        resource.x lazy { Namer.next_index }
        expect(Namer.current_index).to eq 0
      end
      it "you can set x to a lazy value in the instance" do
        resource.instance_eval do
          x lazy { Namer.next_index }
        end
        expect(resource.x).to eq 1
        expect(Namer.current_index).to eq 1
      end
      it "retrieving a lazy value pops it open" do
        resource.x lazy { Namer.next_index }
        expect(resource.x).to eq 1
        expect(Namer.current_index).to eq 1
      end
      it "retrieving a lazy value twice evaluates it twice" do
        resource.x lazy { Namer.next_index }
        expect(resource.x).to eq 1
        expect(resource.x).to eq 2
        expect(Namer.current_index).to eq 2
      end
      it "setting the same lazy value on two different instances runs it on each instancee" do
        resource2 = resource_class.new("blah2")
        l = lazy { Namer.next_index }
        resource.x l
        resource2.x l
        expect(resource2.x).to eq 1
        expect(resource.x).to eq 2
        expect(resource2.x).to eq 3
      end

      context "when the class has a class and instance method named blah" do
        before do
          resource_class.class_eval do
            def self.blah
              "class"
            end
            def blah
              "#{name}#{Namer.next_index}"
            end
          end
        end
        def blah
          "example"
        end
        # it "retrieving lazy { blah } gets the instance variable" do
        #   resource.x lazy { blah }
        #   expect(resource.x).to eq "blah1"
        # end
        # it "retrieving lazy { blah } from two different instances gets two different instance variables" do
        #   resource2 = resource_class.new("another")
        #   l = lazy { blah }
        #   resource2.x l
        #   resource.x l
        #   expect(resource2.x).to eq "another1"
        #   expect(resource.x).to eq "blah2"
        #   expect(resource2.x).to eq "another3"
        # end
        it 'retrieving lazy { |x| "#{blah}#{x.blah}" } gets the example and instance variables' do
          resource.x lazy { |x| "#{blah}#{x.blah}" }
          expect(resource.x).to eq "exampleblah1"
        end
        it 'retrieving lazy { |x| "#{blah}#{x.blah}" } from two different instances gets two different instance variables' do
          resource2 = resource_class.new("another")
          l = lazy { |x| "#{blah}#{x.blah}" }
          resource2.x l
          resource.x l
          expect(resource2.x).to eq "exampleanother1"
          expect(resource.x).to eq "exampleblah2"
          expect(resource2.x).to eq "exampleanother3"
        end
      end
    end

    with_property ':x, coerce: proc { |v| "#{v}#{Namer.next_index}" }' do
      it "lazy values are not coerced on set" do
        resource.x lazy { Namer.next_index }
        expect(Namer.current_index).to eq 0
      end
      it "lazy values are coerced on get" do
        resource.x lazy { Namer.next_index }
        expect(resource.x).to eq "12"
        expect(Namer.current_index).to eq 2
      end
      it "lazy values are coerced on each access" do
        resource.x lazy { Namer.next_index }
        expect(resource.x).to eq "12"
        expect(Namer.current_index).to eq 2
        expect(resource.x).to eq "34"
        expect(Namer.current_index).to eq 4
      end
    end

    with_property ':x, String' do
      it "lazy values are not validated on set" do
        resource.x lazy { Namer.next_index }
        expect(Namer.current_index).to eq 0
      end
      it "lazy values are validated on get" do
        resource.x lazy { Namer.next_index }
        expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed
        expect(Namer.current_index).to eq 1
      end
    end

    with_property ':x, is: proc { |v| Namer.next_index; true }' do
      it "lazy values are validated on each access" do
        resource.x lazy { Namer.next_index }
        expect(resource.x).to eq 1
        expect(Namer.current_index).to eq 2
        expect(resource.x).to eq 3
        expect(Namer.current_index).to eq 4
      end
    end

    with_property ':x, Integer, coerce: proc { |v| "#{v}#{Namer.next_index}" }' do
      it "lazy values are not validated or coerced on set" do
        resource.x lazy { Namer.next_index }
        expect(Namer.current_index).to eq 0
      end
      it "lazy values are coerced before being validated, which fails" do
        resource.x lazy { Namer.next_index }
        expect(Namer.current_index).to eq 0
        expect { resource.x }.to raise_error Chef::Exceptions::ValidationFailed
        expect(Namer.current_index).to eq 2
      end
    end

    with_property ':x, coerce: proc { |v| "#{v}#{Namer.next_index}" }, is: proc { |v| Namer.next_index; true }' do
      it "lazy values are coerced and validated exactly once" do
        resource.x lazy { Namer.next_index }
        expect(resource.x).to eq "12"
        expect(Namer.current_index).to eq 3
        expect(resource.x).to eq "45"
        expect(Namer.current_index).to eq 6
      end
    end

    with_property ':x, String, coerce: proc { |v| "#{v}#{Namer.next_index}" }' do
      it "lazy values are coerced before being validated, which succeeds" do
        resource.x lazy { Namer.next_index }
        expect(resource.x).to eq "12"
        expect(Namer.current_index).to eq 2
      end
    end
  end

  context "Chef::Resource::Property#coerce" do
    with_property ':x, coerce: proc { |v| "#{v}#{Namer.next_index}" }' do
      it "coercion runs on set" do
        expect(resource.x 10).to eq "101"
        expect(Namer.current_index).to eq 1
      end
      it "coercion sets the value (and coercion does not run on get)" do
        expect(resource.x 10).to eq "101"
        expect(resource.x).to eq "101"
        expect(Namer.current_index).to eq 1
      end
      it "coercion runs each time set happens" do
        expect(resource.x 10).to eq "101"
        expect(Namer.current_index).to eq 1
        expect(resource.x 10).to eq "102"
        expect(Namer.current_index).to eq 2
      end
    end
    with_property ':x, coerce: proc { |x| Namer.next_index; raise "hi" if x == 10; x }, is: proc { |x| Namer.next_index; x != 10 }' do
      it "failed coercion fails to set the value" do
        resource.x 20
        expect(resource.x).to eq 20
        expect(Namer.current_index).to eq 2
        expect { resource.x 10 }.to raise_error 'hi'
        expect(resource.x).to eq 20
        expect(Namer.current_index).to eq 3
      end
      it "validation does not run if coercion fails" do
        expect { resource.x 10 }.to raise_error 'hi'
        expect(Namer.current_index).to eq 1
      end
    end
  end

  context "Chef::Resource::Property validation" do
    with_property ':x, is: proc { |v| Namer.next_index; v.is_a?(Integer) }' do
      it "validation runs on set" do
        expect(resource.x 10).to eq 10
        expect(Namer.current_index).to eq 1
      end
      it "validation sets the value (and validation does not run on get)" do
        expect(resource.x 10).to eq 10
        expect(resource.x).to eq 10
        expect(Namer.current_index).to eq 1
      end
      it "validation runs each time set happens" do
        expect(resource.x 10).to eq 10
        expect(Namer.current_index).to eq 1
        expect(resource.x 10).to eq 10
        expect(Namer.current_index).to eq 2
      end
      it "failed validation fails to set the value" do
        expect(resource.x 10).to eq 10
        expect(Namer.current_index).to eq 1
        expect { resource.x 'blah' }.to raise_error Chef::Exceptions::ValidationFailed
        expect(resource.x).to eq 10
        expect(Namer.current_index).to eq 2
      end
    end
  end

  [ 'name_attribute', 'name_property' ].each do |name|
    context "Chef::Resource::Property##{name}" do
      with_property ":x, #{name}: true" do
        it "defaults x to resource.name" do
          expect(resource.x).to eq 'blah'
        end
        it "does not pick up resource.name if set" do
          expect(resource.x 10).to eq 10
          expect(resource.x).to eq 10
        end
        it "binds to the latest resource.name when run" do
          resource.name 'foo'
          expect(resource.x).to eq 'foo'
        end
        it "caches resource.name" do
          expect(resource.x).to eq 'blah'
          resource.name 'foo'
          expect(resource.x).to eq 'blah'
        end
      end
      with_property ":x, default: 10, #{name}: true" do
        it "chooses default over #{name}" do
          expect(resource.x).to eq 10
        end
      end
      with_property ":x, #{name}: true, default: 10" do
        it "chooses #{name} over default" do
          expect(resource.x).to eq 'blah'
        end
      end
      with_property ":x, #{name}: true, required: true" do
        it "defaults x to resource.name" do
          expect(resource.x).to eq 'blah'
        end
      end
    end
  end
end