/
validate_inclusion_of_matcher_spec.rb
1106 lines (909 loc) · 33.2 KB
/
validate_inclusion_of_matcher_spec.rb
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
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveModel::ValidateInclusionOfMatcher, type: :model do
shared_context 'for a generic attribute' do
context 'against an integer attribute' do
it_behaves_like 'it supports in_array',
possible_values: (1..5).to_a,
zero: 0,
reserved_outside_value: described_class::ARBITRARY_OUTSIDE_INTEGER
it_behaves_like 'it supports in_range',
possible_values: 1..5,
zero: 0
context 'when attribute validates a range of values via custom validation' do
it 'matches ensuring the correct range and messages' do
expect_to_match_ensuring_range_and_messages(2..5, 2, 5)
expect_to_match_ensuring_range_and_messages(2...5, 2, 4)
end
end
def build_object(**options, &block)
build_object_with_generic_attribute(
**options.merge(column_type: :integer, value: 1),
&block
)
end
def add_outside_value_to(values)
values + [values.last + 1]
end
def validation_matcher_scenario_args
super.deep_merge(column_type: :integer, default_value: 1)
end
end
context 'against an attribute with a specific column limit' do
it 'does not raise an exception when attempting to use the matcher' do
possible_values = (1..5).to_a
builder = build_object_allowing(possible_values)
assertion = -> { expect_to_match_on_values(builder, possible_values) }
expect(&assertion).not_to raise_error
end
def build_object(**options, &block)
build_object_with_generic_attribute(
**options.merge(
column_type: :integer,
column_options: { limit: 2 },
value: 1,
),
&block
)
end
def expect_to_match_on_values(builder, values, &block)
expect_to_match_in_array(builder, values, &block)
end
end
context 'against a float attribute' do
it_behaves_like 'it supports in_array',
possible_values: [1.0, 2.0, 3.0, 4.0, 5.0],
zero: 0.0,
reserved_outside_value: described_class::ARBITRARY_OUTSIDE_INTEGER
it_behaves_like 'it supports in_range',
possible_values: 1.0..5.0,
zero: 0.0
def build_object(**options, &block)
build_object_with_generic_attribute(
**options.merge(column_type: :float, value: 1.0),
&block
)
end
def add_outside_value_to(values)
values + [values.last + 1]
end
def validation_matcher_scenario_args
super.deep_merge(column_type: :float, default_value: 1.0)
end
end
context 'against a decimal attribute' do
it_behaves_like 'it supports in_array',
possible_values: [1.0, 2.0, 3.0, 4.0, 5.0].map { |number|
BigDecimal(number.to_s)
},
zero: BigDecimal('0.0'),
reserved_outside_value: described_class::ARBITRARY_OUTSIDE_DECIMAL
it_behaves_like 'it supports in_range',
possible_values: BigDecimal('1.0')..BigDecimal('5.0'),
zero: BigDecimal('0.0')
def build_object(**options, &block)
build_object_with_generic_attribute(
**options.merge(column_type: :decimal, value: BigDecimal('1.0')),
&block
)
end
def add_outside_value_to(values)
values + [values.last + 1]
end
def validation_matcher_scenario_args
super.deep_merge(
column_type: :decimal,
default_value: BigDecimal('1.0'),
)
end
end
context 'against a date attribute' do
today = Date.today
define_method(:today) { today }
it_behaves_like 'it supports in_array',
possible_values: (1..5).map { |n| today + n },
reserved_outside_value: described_class::ARBITRARY_OUTSIDE_DATE
it_behaves_like 'it supports in_range',
possible_values: (today..today + 5)
define_method :build_object do |options = {}, &block|
build_object_with_generic_attribute(
**options.merge(column_type: :date, value: today),
&block
)
end
def add_outside_value_to(values)
values + [values.last + 1]
end
def validation_matcher_scenario_args
super.deep_merge(column_type: :date, default_value: today)
end
end
context 'against a datetime attribute' do
now = DateTime.now
define_method(:now) { now }
it_behaves_like 'it supports in_array',
possible_values: (1..5).map { |n| now + n },
reserved_outside_value: described_class::ARBITRARY_OUTSIDE_DATETIME
it_behaves_like 'it supports in_range',
possible_values: (now..now + 5)
define_method :build_object do |options = {}, &block|
build_object_with_generic_attribute(
**options.merge(column_type: :datetime, value: now),
&block
)
end
def add_outside_value_to(values)
values + [values.last + 1]
end
def validation_matcher_scenario_args
super.deep_merge(column_type: :datetime, default_value: now)
end
end
context 'against a time attribute' do
default_time = Time.zone.local(2000, 1, 1)
define_method(:default_time) { default_time }
it_behaves_like 'it supports in_array',
possible_values: (1..3).map { |hour| default_time.change(hour: hour) }
it_behaves_like 'it supports in_range', possible_values: (
default_time.change(hour: 1)..default_time.change(hour: 3)
)
define_method :build_object do |options = {}, &block|
build_object_with_generic_attribute(
**options.merge(column_type: :time, value: default_time),
&block
)
end
def add_outside_value_to(values)
values + [values.last + 1]
end
def validation_matcher_scenario_args
super.deep_merge(column_type: :time, default_value: default_time)
end
end
context 'against a string attribute' do
it_behaves_like 'it supports in_array',
possible_values: %w(foo bar baz),
reserved_outside_value: described_class::ARBITRARY_OUTSIDE_STRING
def build_object(**options, &block)
build_object_with_generic_attribute(
**options.merge(column_type: :string),
&block
)
end
def add_outside_value_to(values)
values + %w(qux)
end
def validation_matcher_scenario_args
super.deep_merge(column_type: :string)
end
end
end
shared_examples_for 'it supports allow_nil' do |args|
valid_values = args.fetch(:valid_values)
it 'matches when the validation specifies allow_nil' do
builder = build_object_allowing(
valid_values,
validation_options: { allow_nil: true },
)
# rubocop:disable Style/SymbolProc
expect_to_match_on_values(builder, valid_values) do |matcher|
matcher.allow_nil
end
# rubocop:enable Style/SymbolProc
end
it 'allows other qualifiers to be chained afterward' do
builder = build_object_allowing(valid_values)
using_matcher = -> do
matcher = validate_inclusion_of(builder.attribute)
matcher.allow_nil.allow_blank
end
expect(&using_matcher).not_to raise_error
end
def validation_matcher_scenario_args
super.deep_merge(validation_options: { allow_nil: true })
end
def configure_validation_matcher(matcher)
super(matcher).allow_nil
end
end
shared_examples_for 'it supports allow_blank' do |args|
valid_values = args.fetch(:valid_values)
it 'matches when the validation specifies allow_blank' do
builder = build_object_allowing(
valid_values,
validation_options: { allow_blank: true },
)
# rubocop:disable Style/SymbolProc
expect_to_match_on_values(builder, valid_values) do |matcher|
matcher.allow_blank
end
# rubocop:enable Style/SymbolProc
end
def validation_matcher_scenario_args
super.deep_merge(validation_options: { allow_blank: true })
end
def configure_validation_matcher(matcher)
super(matcher).allow_blank
end
end
shared_examples_for 'it supports with_message' do |args|
valid_values = args.fetch(:valid_values)
context 'given a string' do
it 'matches when validation uses given message' do
builder = build_object_allowing(
valid_values,
validation_options: { message: 'a message' },
)
expect_to_match_on_values(builder, valid_values) do |matcher|
matcher.with_message('a message')
end
end
it 'matches when validation uses given message that has an interpolated variable' do
builder = build_object_allowing(
valid_values,
validation_options: { message: '%{value} is not included' },
)
expect_to_match_on_values(builder, valid_values) do |matcher|
matcher.with_message(/ is not included\Z/)
end
end
it 'does not match when validation uses the default message instead of given message' do
builder = build_object_allowing(valid_values)
expect_not_to_match_on_values(builder, valid_values) do |matcher|
matcher.with_message('a message')
end
end
it 'does not match when validation uses a message but it is not same as given' do
builder = build_object_allowing(
valid_values,
validation_options: { message: 'a different message' },
)
expect_not_to_match_on_values(builder, valid_values) do |matcher|
matcher.with_message('a message')
end
end
end
context 'given a regex' do
it 'matches when validation uses a message that matches the regex' do
builder = build_object_allowing(
valid_values,
validation_options: { message: 'this is a message' },
)
expect_to_match_on_values(builder, valid_values) do |matcher|
matcher.with_message(/a message/)
end
end
it 'does not match when validation uses the default message instead of given message' do
builder = build_object_allowing(valid_values)
expect_not_to_match_on_values(builder, valid_values) do |matcher|
matcher.with_message(/a message/)
end
end
it 'does not match when validation uses a message but it does not match regex' do
builder = build_object_allowing(
valid_values,
validation_options: { message: 'a different message' },
)
expect_not_to_match_on_values(builder, valid_values) do |matcher|
matcher.with_message(/a message/)
end
end
end
context 'given nil' do
it 'is as if with_message had never been called' do
builder = build_object_allowing(valid_values)
expect_to_match_on_values(builder, valid_values) do |matcher|
matcher.with_message(nil)
end
end
end
context 'not having been qualified' do
it 'matches when the validation uses the default message' do
builder = build_object_allowing(valid_values)
expect_to_match_on_values(builder, valid_values)
end
it 'matches when the validation uses the default message and it is overridden in i18n' do
stubbing_translations(
'errors.messages.inclusion' => 'some message',
'errors.models.person.attributes.name.inclusion' => 'a different message',
) do
builder = build_object_allowing(
valid_values,
model_name: 'Person',
attribute_name: :name,
)
expect_to_match_on_values(builder, valid_values)
end
end
it 'does not match when the validation is configured with an overridden message' do
builder = build_object_allowing(
valid_values,
validation_options: { message: 'some message' },
)
expect_not_to_match_on_values(builder, valid_values)
end
end
end
shared_examples_for 'it supports in_array' do |args|
possible_values = args.fetch(:possible_values)
zero = args[:zero]
reserved_outside_value = args[:reserved_outside_value]
define_method(:valid_values) { args.fetch(:possible_values) }
it_behaves_like 'it supports allow_nil', valid_values: possible_values
it_behaves_like 'it supports allow_blank', valid_values: possible_values
it_behaves_like 'it supports with_message', valid_values: possible_values
it_supports(
'ignoring_interference_by_writer',
tests: {
reject_if_qualified_but_changing_value_interferes: {
attribute_name: :attr,
changing_values_with: :next_value,
expected_message_includes: <<-MESSAGE.strip,
As indicated in the message above, :attr seems to be changing certain
values as they are set, and this could have something to do with why
this test is failing. If you've overridden the writer method for this
attribute, then you may need to change it to make this test pass, or
do something else entirely.
MESSAGE
},
},
)
context 'when the record has no validations' do
it 'passes when used in the negative' do
builder = build_object
expect_not_to_match_on_values(builder, possible_values)
end
it 'fails when used in the positive with an appropriate failure message' do
builder = build_object
assertion = lambda do
expect_to_match_on_values(builder, possible_values)
end
expect(&assertion).to fail
end
end
it 'matches given the same array of valid values' do
builder = build_object_allowing(possible_values)
expect_to_match_on_values(builder, possible_values)
end
it 'matches given a subset of the valid values' do
builder = build_object_allowing(possible_values)
expect_to_match_on_values(builder, possible_values[1..])
end
if zero
it 'matches when one of the given values is a zero' do
valid_values = possible_values + [zero]
builder = build_object_allowing(valid_values)
expect_to_match_on_values(builder, valid_values)
end
end
it 'does not match when one of the given values is invalid' do
builder = build_object_allowing(possible_values)
expect_not_to_match_on_values(
builder,
add_outside_value_to(possible_values),
)
end
if reserved_outside_value
it 'raises an error when valid and given value is our test outside value' do
error_class = Shoulda::Matchers::ActiveModel::CouldNotDetermineValueOutsideOfArray
builder = build_object_allowing([reserved_outside_value])
expect { expect_to_match_on_values(builder, [reserved_outside_value]) }.
to raise_error(error_class)
end
end
it 'fails when used in the negative' do
builder = build_object_allowing(possible_values)
assertion = lambda do
expect_not_to_match_on_values(builder, possible_values)
end
expect(&assertion).to fail
end
it_behaves_like 'it supports allow_nil', valid_values: possible_values do
it 'does not match when the validation does not specify allow_nil' do
builder = build_object_allowing(valid_values)
# rubocop:disable Style/SymbolProc
expect_not_to_match_on_values(builder, valid_values) do |matcher|
matcher.allow_nil
end
# rubocop:enable Style/SymbolProc
end
end
it_behaves_like 'it supports allow_blank', valid_values: possible_values do
it 'does not match when the validation does not specify allow_blank' do
builder = build_object_allowing(valid_values)
# rubocop:disable Style/SymbolProc
expect_not_to_match_on_values(builder, valid_values) do |matcher|
matcher.allow_blank
end
# rubocop:enable Style/SymbolProc
end
end
it_behaves_like 'it supports with_message', valid_values: possible_values
if active_model_3_2?
context '+ strict' do
context 'when the validation specifies strict' do
it 'matches when the given values match the valid values' do
builder = build_object_allowing(
possible_values,
validation_options: { strict: true },
)
# rubocop:disable Style/SymbolProc
expect_to_match_on_values(builder, possible_values) do |matcher|
matcher.strict
end
# rubocop:enable Style/SymbolProc
end
it 'does not match when the given values do not match the valid values' do
builder = build_object_allowing(
possible_values,
validation_options: { strict: true },
)
values = add_outside_value_to(possible_values)
# rubocop:disable Style/SymbolProc
expect_not_to_match_on_values(builder, values) do |matcher|
matcher.strict
end
# rubocop:enable Style/SymbolProc
end
end
context 'when the validation does not specify strict' do
it 'does not match' do
builder = build_object_allowing(possible_values)
# rubocop:disable Style/SymbolProc
expect_not_to_match_on_values(builder, possible_values) do |matcher|
matcher.strict
end
# rubocop:enable Style/SymbolProc
end
end
end
end
def expect_to_match_on_values(builder, values, &block)
expect_to_match_in_array(builder, values, &block)
end
def expect_not_to_match_on_values(builder, values, &block)
expect_not_to_match_in_array(builder, values, &block)
end
def validation_matcher_scenario_args
super.deep_merge(validation_options: { in: valid_values })
end
def configure_validation_matcher(matcher)
super(matcher).in_array(valid_values)
end
end
shared_examples_for 'it supports in_range' do |args|
possible_values = args[:possible_values]
define_method(:valid_values) { args.fetch(:possible_values) }
it_behaves_like 'it supports allow_nil', valid_values: possible_values
it_behaves_like 'it supports allow_blank', valid_values: possible_values
it_behaves_like 'it supports with_message', valid_values: possible_values
it_supports(
'ignoring_interference_by_writer',
tests: {
reject_if_qualified_but_changing_value_interferes: {
attribute_name: :attr,
changing_values_with: :next_value,
expected_message_includes: <<-MESSAGE.strip,
As indicated in the message above, :attr seems to be changing certain
values as they are set, and this could have something to do with why
this test is failing. If you've overridden the writer method for this
attribute, then you may need to change it to make this test pass, or
do something else entirely.
MESSAGE
},
},
)
it 'does not match a record with no validations' do
builder = build_object
expect_not_to_match_on_values(builder, possible_values)
end
it 'matches given a range that exactly matches the valid range' do
builder = build_object_allowing(possible_values)
expect_to_match_on_values(builder, possible_values)
end
it 'does not match given a range whose start value falls outside valid range' do
builder = build_object_allowing(possible_values)
expect_not_to_match_on_values(
builder,
Range.new(possible_values.first - 1, possible_values.last),
)
end
it 'does not match given a range whose start value falls inside valid range' do
builder = build_object_allowing(possible_values)
expect_not_to_match_on_values(
builder,
Range.new(possible_values.first + 1, possible_values.last),
)
end
it 'does not match given a range whose end value falls inside valid range' do
builder = build_object_allowing(possible_values)
expect_not_to_match_on_values(
builder,
Range.new(possible_values.first, possible_values.last - 1),
)
end
it 'does not match given a range whose end value falls outside valid range' do
builder = build_object_allowing(possible_values)
expect_not_to_match_on_values(
builder,
Range.new(possible_values.first, possible_values.last + 1),
)
end
it_behaves_like 'it supports allow_nil', valid_values: possible_values do
it 'matches when the validation does not specify allow_nil' do
builder = build_object_allowing(valid_values)
# rubocop:disable Style/SymbolProc
expect_to_match_on_values(builder, valid_values) do |matcher|
matcher.allow_nil
end
# rubocop:enable Style/SymbolProc
end
end
it_behaves_like 'it supports allow_blank', valid_values: possible_values do
it 'matches when the validation does not specify allow_blank' do
builder = build_object_allowing(valid_values)
# rubocop:disable Style/SymbolProc
expect_to_match_on_values(builder, valid_values) do |matcher|
matcher.allow_blank
end
# rubocop:enable Style/SymbolProc
end
end
it_behaves_like 'it supports with_message', valid_values: possible_values
if active_model_3_2?
context '+ strict' do
context 'when the validation specifies strict' do
it 'matches when the given range matches the range in the validation' do
builder = build_object_allowing(
possible_values,
validation_options: { strict: true },
)
# rubocop:disable Style/SymbolProc
expect_to_match_on_values(builder, possible_values) do |matcher|
matcher.strict
end
# rubocop:enable Style/SymbolProc
end
it 'matches when the given range does not match the range in the validation' do
builder = build_object_allowing(
possible_values,
validation_options: { strict: true },
)
range = Range.new(possible_values.first, possible_values.last + 1)
# rubocop:disable Style/SymbolProc
expect_not_to_match_on_values(builder, range) do |matcher|
matcher.strict
end
# rubocop:enable Style/SymbolProc
end
end
context 'when the validation does not specify strict' do
it 'does not match' do
builder = build_object_allowing(possible_values)
# rubocop:disable Style/SymbolProc
expect_not_to_match_on_values(builder, possible_values) do |matcher|
matcher.strict
end
# rubocop:enable Style/SymbolProc
end
end
end
end
def expect_to_match_on_values(builder, range, &block)
expect_to_match_in_range(builder, range, &block)
end
def expect_not_to_match_on_values(builder, range, &block)
expect_not_to_match_in_range(builder, range, &block)
end
def validation_matcher_scenario_args
super.deep_merge(validation_options: { in: valid_values })
end
def configure_validation_matcher(matcher)
super(matcher).in_range(valid_values)
end
end
shared_context 'against a boolean attribute for true and false' do
context 'when ensuring inclusion of true' do
it 'matches' do
valid_values = [true]
builder = build_object_allowing(valid_values)
expect_to_match_in_array(builder, valid_values)
end
end
context 'when ensuring inclusion of false' do
it 'matches' do
valid_values = [false]
builder = build_object_allowing(valid_values)
expect_to_match_in_array(builder, valid_values)
end
end
context 'when ensuring inclusion of true and false' do
it 'matches' do
valid_values = [true, false]
builder = build_object_allowing(valid_values)
silence_stderr do
expect_to_match_in_array(builder, valid_values)
end
end
[[false, true], [true, false]].each do |booleans|
it 'prints a warning' do
valid_values = booleans
builder = build_object_allowing(valid_values)
message =
'You are using `validate_inclusion_of` to assert that a boolean '\
'column allows boolean values and disallows non-boolean ones'
stderr = capture(:stderr) do
expect_to_match_in_array(builder, valid_values)
end
expect(stderr.gsub(/\n+/, ' ')).to include(message)
end
end
end
end
context 'for a database column' do
include_context 'for a generic attribute'
context 'against a timestamp column' do
now = DateTime.now
define_method(:now) { now }
it_behaves_like 'it supports in_array',
possible_values: (1..5).map { |n| now + n },
reserved_outside_value: described_class::ARBITRARY_OUTSIDE_DATETIME
it_behaves_like 'it supports in_range',
possible_values: (now..now + 5)
define_method :build_object do |options = {}, &block|
build_object_with_generic_attribute(
**options.merge(column_type: :timestamp, value: now),
&block
)
end
def add_outside_value_to(values)
values + [values.last + 1]
end
def validation_matcher_scenario_args
super.deep_merge(column_type: :timestamp, default_value: now)
end
end
context 'against a boolean attribute' do
context 'which is nullable' do
include_context 'against a boolean attribute for true and false'
context 'when ensuring inclusion of nil' do
it 'matches' do
valid_values = [nil]
builder = build_object_allowing(valid_values)
silence_stderr do
expect_to_match_in_array(builder, valid_values)
end
end
it 'prints a warning' do
valid_values = [nil]
builder = build_object_allowing(valid_values)
message =
'You are using `validate_inclusion_of` to assert that a '\
'boolean column allows nil'
stderr = capture(:stderr) do
expect_to_match_in_array(builder, valid_values)
end
expect(stderr.gsub(/\n+/, ' ')).to include(message)
end
end
def build_object(**options, &block)
super(
**options.merge(column_options: { null: true }, value: true),
&block
)
end
end
context 'which is non-nullable' do
include_context 'against a boolean attribute for true and false'
context 'when ensuring inclusion of nil' do
it 'raises a specific error' do
valid_values = [nil]
builder = build_object_allowing(valid_values)
error_class = Shoulda::Matchers::ActiveModel::NonNullableBooleanError
expect {
expect_to_match_in_array(builder, valid_values)
}.to raise_error(error_class)
end
end
def build_object(**options, &block)
super(**options.merge(column_options: { null: false }), &block)
end
end
def build_object(**options, &block)
build_object_with_generic_attribute(
**options.merge(column_type: :boolean),
&block
)
end
end
def define_simple_model(
model_name: 'Example',
attribute_name: :attr,
column_options: {},
&block
)
define_model(model_name, attribute_name => column_options, &block)
end
def validation_matcher_scenario_args
super.deep_merge(model_creator: :active_record)
end
end
context 'for a plain Ruby attribute' do
include_context 'for a generic attribute'
context 'against a boolean attribute (designated by true)' do
include_context 'against a boolean attribute for true and false'
def build_object(**options, &block)
build_object_with_generic_attribute(**options.merge(value: true), &block)
end
end
context 'against a boolean attribute (designated by false)' do
include_context 'against a boolean attribute for true and false'
def build_object(**options, &block)
build_object_with_generic_attribute(**options.merge(value: false), &block)
end
end
def define_simple_model(
model_name: 'Example',
attribute_name: :attr,
**_rest,
&block
)
define_active_model_class(model_name, accessors: [attribute_name], &block)
end
def validation_matcher_scenario_args
super.deep_merge(model_creator: :active_model)
end
end
describe '#description' do
context 'given an array of values' do
context 'when there is one value' do
it 'returns the correct string' do
matcher = validate_inclusion_of(:attr).in_array([true])
expect(matcher.description).to eq(
'validate that :attr is ‹true›',
)
end
end
context 'when there are two values' do
it 'returns the correct string' do
matcher = validate_inclusion_of(:attr).in_array([true, 'dog'])
expect(matcher.description).to eq(
'validate that :attr is either ‹true› or ‹"dog"›',
)
end
end
context 'when there are three or more values' do
it 'returns the correct string' do
matcher = validate_inclusion_of(:attr).in_array([true, 'dog', 'cat'])
expect(matcher.description).to eq(
'validate that :attr is either ‹true›, ‹"dog"›, or ‹"cat"›',
)
end
end
end
context 'given a range of values' do
it 'returns the correct string' do
matcher = validate_inclusion_of(:attr).in_range(1..10)
expect(matcher.description).to eq(
'validate that :attr lies inside the range ‹1› to ‹10›',
)
end
end
end
def object_builder_class
@_object_builder_class ||= Struct.new(
:attribute,
:object,
:validation_options,
)
end
def build_object_with_generic_attribute(
model_name: nil,
attribute_name: :attr,
validation_options: nil,
value: nil,
**other_options
)
model = define_model_validating_inclusion(
model_name: model_name,
attribute_name: attribute_name,
validation_options: validation_options,
**other_options,
)
object = model.new
object.__send__("#{attribute_name}=", value)
object_builder_class.new(attribute_name, object, validation_options)
end
def define_model_validating_inclusion(
model_name: nil,
attribute_name: :attr,
column_type: :string,