File: respond_to_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (1018 lines) | stat: -rw-r--r-- 37,524 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
1012
1013
1014
1015
1016
1017
1018
RSpec.describe "expect(...).to respond_to(:sym)" do
  it_behaves_like "an RSpec value matcher", :valid_value => "s", :invalid_value => 5 do
    let(:matcher) { respond_to(:upcase) }
  end

  it "passes if target responds to :sym" do
    expect(Object.new).to respond_to(:methods)
  end

  it "passes if target responds to :sym but does not implement method" do
    # This simulates a behaviour of Rails, see #1162.
    klass = Class.new { def respond_to?(_); true; end }
    expect(klass.new).to respond_to(:my_method)
  end

  it "fails if target does not respond to :sym" do
    expect {
      expect("this string").to respond_to(:some_method)
    }.to fail_with('expected "this string" to respond to :some_method')
  end
end

RSpec.describe "expect(...).to respond_to(:sym).with(1).argument" do
  it "passes if target responds to :sym with 1 arg" do
    obj = Object.new
    def obj.foo(arg); end
    expect(obj).to respond_to(:foo).with(1).argument
  end

  it "passes if target responds to any number of arguments" do
    obj = Object.new
    def obj.foo(*args); end
    expect(obj).to respond_to(:foo).with(1).argument
  end

  it "passes if target responds to one or more arguments" do
    obj = Object.new
    def obj.foo(a, *args); end
    expect(obj).to respond_to(:foo).with(1).argument
  end

  it "verifes the method signature of new as if it was initialize" do
    klass = Class.new { def initialize(a, b); end; }
    expect(klass).to respond_to(:new).with(2).arguments
  end

  it "fails if target does not respond to :sym" do
    obj = Object.new
    expect {
      expect(obj).to respond_to(:some_method).with(1).argument
    }.to fail_with(/expected .* to respond to :some_method/)
  end

  it "fails if :sym expects 0 args" do
    obj = Object.new
    def obj.foo; end
    expect {
      expect(obj).to respond_to(:foo).with(1).argument
    }.to fail_with(/expected #<Object.*> to respond to :foo with 1 argument/)
  end

  it "fails if :sym expects 2 args" do
    obj = Object.new
    def obj.foo(arg, arg2); end
    expect {
      expect(obj).to respond_to(:foo).with(1).argument
    }.to fail_with(/expected #<Object.*> to respond to :foo with 1 argument/)
  end

  it "fails if :sym expects 2 or more args" do
    obj = Object.new
    def obj.foo(arg, arg2, *args); end
    expect {
      expect(obj).to respond_to(:foo).with(1).argument
    }.to fail_with(/expected #<Object.*> to respond to :foo with 1 argument/)
  end

  it "fails if the method signature of initialize does not match" do
    klass = Class.new { def initialize(a, b); end; }
    expect {
      expect(klass).to respond_to(:new).with(1).arguments
    }.to fail_with(/expected #<Class.*> to respond to :new with 1 argument/)
  end

  it "still works if target has overridden the method method" do
    obj = Object.new
    def obj.method; end
    def obj.other_method(arg); end
    expect(obj).to respond_to(:other_method).with(1).argument
  end

  it "warns that the subject does not have the implementation required when method does not exist" do
    # This simulates a behaviour of Rails, see #1162.
    klass = Class.new { def respond_to?(_); true; end }
    expect {
      expect(klass.new).to respond_to(:my_method).with(0).arguments
    }.to raise_error(ArgumentError)
  end
end

RSpec.describe "expect(...).to respond_to(:new)" do
  context "with no tampering" do
    it "will validate new as if it was initialize" do
      klass = Class.new { def initialize(a, b, c); end }
      expect(klass).not_to respond_to(:new).with(2).arguments
      expect(klass).to respond_to(:new).with(3).arguments
    end
  end

  context "on a class that has redefined `new`" do
    it "uses the method signature of the redefined `new` for arg verification" do
      klass = Class.new { def self.new(a); end }
      expect(klass).to respond_to(:new).with(1).argument
      expect {
        expect(klass).to respond_to(:new).with(2).arguments
      }.to fail_with(/expected #<Class.*> to respond to :new with 2 arguments/)
      expect {
        expect(klass).to_not respond_to(:new).with(1).argument
      }.to fail_with(/expected #<Class.*> not to respond to :new with 1 argument/)
    end
  end

  context "on a class that has undefined `new`" do
    it "will not respond to new" do
      klass =
        Class.new do
          class << self
            undef new
          end
        end

      expect {
        expect(klass).to respond_to(:new)
      }.to fail_with(/expected .* to respond to :new/)
    end
  end

  context "on a class with a private `new`" do
    it "will not respond to new" do
      klass = Class.new { private_class_method :new; def initialize(a, b, c); end }
      expect(klass).not_to respond_to(:new)
    end
  end
end

RSpec.describe "expect(...).to respond_to(message1, message2)" do
  it "passes if target responds to both messages" do
    expect(Object.new).to respond_to('methods', 'inspect')
  end

  it "fails if target does not respond to first message" do
    expect {
      expect(Object.new).to respond_to('method_one', 'inspect')
    }.to fail_with(/expected #<Object:.*> to respond to "method_one"/)
  end

  it "fails if target does not respond to second message" do
    expect {
      expect(Object.new).to respond_to('inspect', 'method_one')
    }.to fail_with(/expected #<Object:.*> to respond to "method_one"/)
  end

  it "fails if target does not respond to either message" do
    expect {
      expect(Object.new).to respond_to('method_one', 'method_two')
    }.to fail_with(/expected #<Object:.*> to respond to "method_one", "method_two"/)
  end
end

RSpec.describe "expect(...).to respond_to(:sym).with(2).arguments" do
  it "passes if target responds to :sym with 2 args" do
    obj = Object.new
    def obj.foo(a1, a2); end
    expect(obj).to respond_to(:foo).with(2).arguments
  end

  it "passes if target responds to any number of arguments" do
    obj = Object.new
    def obj.foo(*args); end
    expect(obj).to respond_to(:foo).with(2).arguments
  end

  it "passes if target responds to one or more arguments" do
    obj = Object.new
    def obj.foo(a, *args); end
    expect(obj).to respond_to(:foo).with(2).arguments
  end

  it "passes if target responds to two or more arguments" do
    obj = Object.new
    def obj.foo(a, b, *args); end
    expect(obj).to respond_to(:foo).with(2).arguments
  end

  it "fails if target does not respond to :sym" do
    obj = Object.new
    expect {
      expect(obj).to respond_to(:some_method).with(2).arguments
    }.to fail_with(/expected .* to respond to :some_method/)
  end

  it "fails if :sym expects 0 args" do
    obj = Object.new
    def obj.foo; end
    expect {
      expect(obj).to respond_to(:foo).with(2).arguments
    }.to fail_with(/expected #<Object.*> to respond to :foo with 2 arguments/)
  end

  it "fails if :sym expects 1 args" do
    obj = Object.new
    def obj.foo(arg); end
    expect {
      expect(obj).to respond_to(:foo).with(2).arguments
    }.to fail_with(/expected #<Object.*> to respond to :foo with 2 arguments/)
  end

  it "fails if :sym expects 3 or more args" do
    obj = Object.new
    def obj.foo(arg, arg2, arg3, *args); end
    expect {
      expect(obj).to respond_to(:foo).with(2).arguments
    }.to fail_with(/expected #<Object.*> to respond to :foo with 2 arguments/)
  end
end

RSpec.describe "expect(...).to respond_to(:sym).with(1..2).arguments" do
  it "passes if target responds to any number of arguments" do
    obj = Object.new
    def obj.foo(*args); end
    expect(obj).to respond_to(:foo).with(1..2).arguments
  end

  it "passes if target responds to one or more arguments" do
    obj = Object.new
    def obj.foo(a, *args); end
    expect(obj).to respond_to(:foo).with(1..2).arguments
  end

  it "passes if target responds to one or two arguments" do
    obj = Object.new
    def obj.foo(a, b=nil); end
    expect(obj).to respond_to(:foo).with(1..2).arguments
  end

  it "passes if target responds to one to three arguments" do
    obj = Object.new
    def obj.foo(a, b=nil, c=nil); end
    expect(obj).to respond_to(:foo).with(1..2).arguments
  end

  it "passes if target is new and initialize reponds to arguments" do
    klass = Class.new { def initialize(arg, arg2=nil, arg3=nil); end }
    expect(klass).to respond_to(:new).with(1..2).arguments
  end

  it "fails if target does not respond to :sym" do
    obj = Object.new
    expect {
      expect(obj).to respond_to(:some_method).with(1..2).arguments
    }.to fail_with(/expected .* to respond to :some_method/)
  end

  it "fails if :sym expects 0 args" do
    obj = Object.new
    def obj.foo; end
    expect {
      expect(obj).to respond_to(:foo).with(1..2).arguments
    }.to fail_with(/expected #<Object.*> to respond to :foo with 1..2 arguments/)
  end

  it "fails if :sym expects 1 args" do
    obj = Object.new
    def obj.foo(arg); end
    expect {
      expect(obj).to respond_to(:foo).with(1..2).arguments
    }.to fail_with(/expected #<Object.*> to respond to :foo with 1..2 arguments/)
  end

  it "fails if :sym expects 2 args" do
    obj = Object.new
    def obj.foo(a, b); end
    expect {
      expect(obj).to respond_to(:foo).with(1..2).arguments
    }.to fail_with(/expected #<Object.*> to respond to :foo with 1..2 arguments/)
  end

  it "fails if :sym expects 3 or more args" do
    obj = Object.new
    def obj.foo(arg, arg2, arg3, *args); end
    expect {
      expect(obj).to respond_to(:foo).with(1..2).arguments
    }.to fail_with(/expected #<Object.*> to respond to :foo with 1..2 arguments/)
  end

  it "fails when new unless initialize matches the signature" do
    klass = Class.new { def initialize(arg, arg2, arg3, *args); end }
    expect {
      expect(klass).to respond_to(:new).with(1..2).arguments
    }.to fail_with(/expected #<Class.*> to respond to :new with 1..2 arguments/)
  end
end

RSpec.describe "expect(...).to respond_to(:sym).with_unlimited_arguments" do
  it "passes if target responds to any number of arguments" do
    obj = Object.new
    def obj.foo(*args); end
    expect(obj).to respond_to(:foo).with_unlimited_arguments
  end

  it "passes if target responds to a minimum number of arguments" do
    obj = Object.new
    def obj.foo(arg, arg2, arg3, *args); end
    expect(obj).to respond_to(:foo).with(3).arguments.and_unlimited_arguments
  end

  it "passes when target is new and initialize responds to any number of arguments" do
    # note we can't use the metaobject definition for initialize
    klass_2 = Class.new { def initialize(*args); end }
    expect(klass_2).to respond_to(:new).with_unlimited_arguments
  end

  it "fails if target does not respond to :sym" do
    obj = Object.new
    expect {
      expect(obj).to respond_to(:some_method).with_unlimited_arguments
    }.to fail_with(/expected .* to respond to :some_method/)
  end

  it "fails if :sym expects a minimum number of arguments" do
    obj = Object.new
    def obj.some_method(arg, arg2, arg3, *args); end
    expect {
      expect(obj).to respond_to(:some_method).with_unlimited_arguments
    }.to fail_with(/expected .* to respond to :some_method with unlimited arguments/)
  end

  it "fails if :sym expects a limited number of arguments" do
    obj = Object.new
    def obj.some_method(arg); end
    expect {
      expect(obj).to respond_to(:some_method).with_unlimited_arguments
    }.to fail_with(/expected .* to respond to :some_method with unlimited arguments/)
  end

  it "fails when target is new and initialize responds to a set number of arguments" do
    klass = Class.new { def initialize(a); end }
    expect {
      expect(klass).to respond_to(:new).with_unlimited_arguments
    }.to fail_with(/expected .* to respond to :new with unlimited arguments/)
  end
end

RSpec.describe "expect(...).not_to respond_to(:sym)" do
  it "passes if target does not respond to :sym" do
    expect(Object.new).not_to respond_to(:some_method)
  end

  it "fails if target responds to :sym" do
    expect {
      expect(Object.new).not_to respond_to(:methods)
    }.to fail_with(/expected #<Object:.*> not to respond to :methods/)
  end
end

RSpec.describe "expect(...).not_to respond_to(:sym).with(1).argument" do
  it "fails if target responds to :sym with 1 arg" do
    obj = Object.new
    def obj.foo(arg); end
    expect {
      expect(obj).not_to respond_to(:foo).with(1).argument
    }.to fail_with(/expected #<Object:.*> not to respond to :foo with 1 argument/)
  end

  it "fails if target responds to :sym with any number of args" do
    obj = Object.new
    def obj.foo(*args); end
    expect {
      expect(obj).not_to respond_to(:foo).with(1).argument
    }.to fail_with(/expected #<Object:.*> not to respond to :foo with 1 argument/)
  end

  it "fails if target responds to :sym with one or more args" do
    obj = Object.new
    def obj.foo(a, *args); end
    expect {
      expect(obj).not_to respond_to(:foo).with(1).argument
    }.to fail_with(/expected #<Object:.*> not to respond to :foo with 1 argument/)
  end

  it "will fail when target is new and initialize matches the argument signature" do
    klass = Class.new { def initialize(a); end }
    expect {
      expect(klass).to_not respond_to(:new).with(1).argument
    }.to fail_with(/not to respond to :new with 1 argument/)
  end

  it "passes if target does not respond to :sym" do
    obj = Object.new
    expect(obj).not_to respond_to(:some_method).with(1).argument
  end

  it "passes if :sym expects 0 args" do
    obj = Object.new
    def obj.foo; end
    expect(obj).not_to respond_to(:foo).with(1).argument
  end

  it "passes if :sym expects 2 args" do
    obj = Object.new
    def obj.foo(arg, arg2); end
    expect(obj).not_to respond_to(:foo).with(1).argument
  end

  it "passes if :sym expects 2 or more args" do
    obj = Object.new
    def obj.foo(arg, arg2, *args); end
    expect(obj).not_to respond_to(:foo).with(1).argument
  end

  it "will pass when target is new and initialize does not matches the argument signature" do
    klass = Class.new { def initialize(a, b); end }
    expect(klass).to_not respond_to(:new).with(1).argument
  end
end

RSpec.describe "expect(...).not_to respond_to(message1, message2)" do
  it "passes if target does not respond to either message1 or message2" do
    expect(Object.new).not_to respond_to(:some_method, :some_other_method)
  end

  it "fails if target responds to message1 but not message2" do
    expect {
      expect(Object.new).not_to respond_to(:object_id, :some_method)
    }.to fail_with(/expected #<Object:.*> not to respond to :object_id/)
  end

  it "fails if target responds to message2 but not message1" do
    expect {
      expect(Object.new).not_to respond_to(:some_method, :object_id)
    }.to fail_with(/expected #<Object:.*> not to respond to :object_id/)
  end

  it "fails if target responds to both message1 and message2" do
    expect {
      expect(Object.new).not_to respond_to(:class, :object_id)
    }.to fail_with(/expected #<Object:.*> not to respond to :class, :object_id/)
  end
end

RSpec.describe "expect(...).not_to respond_to(:sym).with(2).arguments" do
  it "fails if target responds to :sym with 2 args" do
    obj = Object.new
    def obj.foo(a1, a2); end
    expect {
      expect(obj).not_to respond_to(:foo).with(2).arguments
    }.to fail_with(/expected .* not to respond to :foo with 2 arguments/)
  end

  it "fails if target responds to :sym with any number args" do
    obj = Object.new
    def obj.foo(*args); end
    expect {
      expect(obj).not_to respond_to(:foo).with(2).arguments
    }.to fail_with(/expected .* not to respond to :foo with 2 arguments/)
  end

  it "fails if target responds to :sym with one or more args" do
    obj = Object.new
    def obj.foo(a, *args); end
    expect {
      expect(obj).not_to respond_to(:foo).with(2).arguments
    }.to fail_with(/expected .* not to respond to :foo with 2 arguments/)
  end

  it "fails if target responds to :sym with two or more args" do
    obj = Object.new
    def obj.foo(a, b, *args); end
    expect {
      expect(obj).not_to respond_to(:foo).with(2).arguments
    }.to fail_with(/expected .* not to respond to :foo with 2 arguments/)
  end

  it "passes if target does not respond to :sym" do
    obj = Object.new
    expect(obj).not_to respond_to(:some_method).with(2).arguments
  end

  it "passes if :sym expects 0 args" do
    obj = Object.new
    def obj.foo; end
    expect(obj).not_to respond_to(:foo).with(2).arguments
  end

  it "passes if :sym expects 2 args" do
    obj = Object.new
    def obj.foo(arg); end
    expect(obj).not_to respond_to(:foo).with(2).arguments
  end

  it "passes if :sym expects 3 or more args" do
    obj = Object.new
    def obj.foo(a, b, c, *arg); end
    expect(obj).not_to respond_to(:foo).with(2).arguments
  end
end

RSpec.describe "expect(...).not_to respond_to(:sym).with(1..2).arguments" do
  it "fails if target responds to :sym with one or two args" do
    obj = Object.new
    def obj.foo(a1, a2=nil); end
    expect {
      expect(obj).not_to respond_to(:foo).with(1..2).arguments
    }.to fail_with(/expected .* not to respond to :foo with 1..2 arguments/)
  end

  it "fails if target responds to :sym with any number args" do
    obj = Object.new
    def obj.foo(*args); end
    expect {
      expect(obj).not_to respond_to(:foo).with(1..2).arguments
    }.to fail_with(/expected .* not to respond to :foo with 1..2 arguments/)
  end

  it "fails if target responds to :sym with one or more args" do
    obj = Object.new
    def obj.foo(a, *args); end
    expect {
      expect(obj).not_to respond_to(:foo).with(1..2).arguments
    }.to fail_with(/expected .* not to respond to :foo with 1..2 arguments/)
  end

  it "will fail when target is new and initialize matches the argument signature" do
    klass = Class.new { def initialize(a, *args); end }
    expect {
      expect(klass).to_not respond_to(:new).with(1..2).argument
    }.to fail_with(/not to respond to :new with 1..2 argument/)
  end

  it "passes if target does not respond to :sym" do
    obj = Object.new
    expect(obj).not_to respond_to(:some_method).with(1..2).arguments
  end

  it "passes if :sym expects 0 args" do
    obj = Object.new
    def obj.foo; end
    expect(obj).not_to respond_to(:foo).with(1..2).arguments
  end

  it "passes if :sym expects 1 arg" do
    obj = Object.new
    def obj.foo(arg); end
    expect(obj).not_to respond_to(:foo).with(1..2).arguments
  end

  it "passes if :sym expects 2 args" do
    obj = Object.new
    def obj.foo(a, b); end
    expect(obj).not_to respond_to(:foo).with(1..2).arguments
  end

  it "passes if :sym expects 3 or more args" do
    obj = Object.new
    def obj.foo(a, b, c, *arg); end
    expect(obj).not_to respond_to(:foo).with(1..2).arguments
  end

  it "passes when target is new and initialize does not match the argument signature" do
    klass = Class.new { def initialize(a); end }
    expect(klass).to_not respond_to(:new).with(1..2).argument
  end
end

RSpec.describe "expect(...).not_to respond_to(:sym).with_unlimited_arguments" do
  it "fails if target responds to :sym with any number args" do
    obj = Object.new
    def obj.foo(*args); end
    expect {
      expect(obj).not_to respond_to(:foo).with_unlimited_arguments
    }.to fail_with(/expected .* not to respond to :foo with unlimited arguments/)
  end

  it "will fail when target is new and initialize has unlimited arguments" do
    klass = Class.new { def initialize(*args); end }
    expect {
      expect(klass).to_not respond_to(:new).with_unlimited_arguments
    }.to fail_with(/not to respond to :new with unlimited argument/)
  end

  it "passes if target does not respond to :sym" do
    obj = Object.new
    expect(obj).not_to respond_to(:some_method).with_unlimited_arguments
  end

  it "passes if :sym expects a limited number of arguments" do
    obj = Object.new
    def obj.some_method(arg); end
    expect(obj).not_to respond_to(:some_method).with_unlimited_arguments
  end

  it "passes if :sym expects a minimum number of arguments" do
    obj = Object.new
    def obj.some_method(arg, arg2, arg3, *args); end
    expect(obj).not_to respond_to(:some_method).with_unlimited_arguments
  end

  it "passes when target is new and initialize has arguments" do
    klass = Class.new { def initialize(a, *args); end }
    expect(klass).to_not respond_to(:new).with_unlimited_arguments
  end
end

if RSpec::Support::RubyFeatures.kw_args_supported?
  RSpec.describe "expect(...).to respond_to(:sym).with_keywords(:foo, :bar)" do
    it 'passes if target responds to :sym with specified optional keywords' do
      obj = Object.new
      eval %{def obj.foo(a: nil, b: nil); end}
      expect(obj).to respond_to(:foo).with_keywords(:a, :b)
    end

    it 'passes if target responds to :sym with any keywords' do
      obj = Object.new
      eval %{def obj.foo(**kw_args); end}
      expect(obj).to respond_to(:foo).with_keywords(:a, :b)
    end

    it 'passes if target is :new with keywords' do
      # note we can't use the metaobject definition for initialize
      klass = eval %{Class.new { def initialize(a: nil, b: nil); end}}
      expect(klass).to respond_to(:new).with_keywords(:a, :b)

      # note we can't use the metaobject definition for initialize
      klass_2 = eval %{Class.new { def initialize(**kw_args); end}}
      expect(klass_2).to respond_to(:new).with_keywords(:a, :b)
    end

    it "fails if target does not respond to :sym" do
      obj = Object.new
      expect {
        expect(obj).to respond_to(:some_method).with_keywords(:a, :b)
      }.to fail_with(/expected .* to respond to :some_method with keywords :a and :b/)
    end

    it "fails if :sym does not expect specified keywords" do
      obj = Object.new
      def obj.foo; end
      expect {
        expect(obj).to respond_to(:foo).with_keywords(:a, :b)
      }.to fail_with(/expected .* to respond to :foo with keywords :a and :b/)
    end

    it "fails if :sym does not expect many specified keywords" do
      obj = Object.new
      def obj.foo; end
      expect {
        expect(obj).to respond_to(:foo).with_keywords(:a, :b, :c, :d, :e, :f)
      }.to fail_with(/expected .* to respond to :foo with keywords :a, :b, :c, :d, :e, and :f/)
    end

    it 'fails if target is :new but initialize does not expect the right keywords' do
      # note we can't use the metaobject definition for initialize
      klass = eval %{Class.new { def initialize(a: nil); end}}
      expect {
        expect(klass).to respond_to(:new).with_keywords(:a, :b)
      }.to fail_with(/expected .* to respond to :new with keywords :a and :b/)
    end

    if RSpec::Support::RubyFeatures.required_kw_args_supported?
      it "passes if target responds to :sym with specified required keywords" do
        obj = Object.new
        eval %{def obj.foo(a:, b:, c: nil, d: nil); end}
        expect(obj).to respond_to(:foo).with_keywords(:a, :b)
      end

      it "passes if target responds to :sym with keyword arg splat" do
        obj = Object.new
        eval %{def obj.foo(**rest); end}
        expect(obj).to respond_to(:foo).with_keywords(:a, :b)
      end

      it 'passes if target is :new and initialize has specified required keywords' do
        # note we can't use the metaobject definition for initialize
        klass = eval %{Class.new { def initialize(a:, b:); end}}
        expect(klass).to respond_to(:new).with_keywords(:a, :b)
      end

      it "fails if :sym expects specified optional keywords but expects missing required keywords" do
        obj = Object.new
        eval %{def obj.foo(a:, b:, c: nil, d: nil); end}
        expect {
          expect(obj).to respond_to(:some_method).with_keywords(:c, :d)
        }.to fail_with(/expected .* to respond to :some_method with keywords :c and :d/)
      end

      it "fails if target responds to :sym with keyword arg splat but missing required keywords" do
        obj = Object.new
        eval %{def obj.foo(a:, b:, **rest); end}
        expect {
          expect(obj).to respond_to(:some_method).with_keywords(:c, :d)
        }.to fail_with(/expected .* to respond to :some_method with keywords :c and :d/)
      end

      it 'fails if target is :new and initialize has is missing required keywords' do
        # note we can't use the metaobject definition for initialize
        klass = eval %{Class.new { def initialize(a:, b:); end}}
        expect {
          expect(klass).to respond_to(:new).with_keywords(:c, :d)
        }.to fail_with(/expected .* to respond to :new with keywords :c and :d/)
      end
    end
  end

  RSpec.describe "expect(...).to respond_to(:sym).with(2).arguments.and_keywords(:foo, :bar)" do
    it "passes if target responds to :sym with 2 args and specified optional keywords" do
      obj = Object.new
      eval %{def obj.foo(a, b, u: nil, v: nil); end}
      expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
    end

    it "passes if target responds to :sym with any number of arguments and specified optional keywords" do
      obj = Object.new
      eval %{def obj.foo(*args, u: nil, v: nil); end}
      expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
    end

    it "passes if target responds to :sym with one or more arguments and specified optional keywords" do
      obj = Object.new
      eval %{def obj.foo(a, *args, u: nil, v: nil); end}
      expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
    end

    it "passes if target responds to :sym with two or more arguments and specified optional keywords" do
      obj = Object.new
      eval %{def obj.foo(a, b, *args, u: nil, v: nil); end}
      expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
    end

    it "fails if target does not respond to :sym" do
      obj = Object.new
      expect {
        expect(obj).to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v)
      }.to fail_with(/expected .* to respond to :some_method with 2 arguments and keywords :u and :v/)
    end

    it "fails if :sym expects 1 argument" do
      obj = Object.new
      eval %{def obj.foo(a, u: nil, v: nil); end}
      expect {
        expect(obj).to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v)
      }.to fail_with(/expected .* to respond to :some_method with 2 arguments and keywords :u and :v/)
    end

    it "fails if :sym does not expect specified keywords" do
      obj = Object.new
      def obj.foo(a, b); end
      expect {
        expect(obj).to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v)
      }.to fail_with(/expected .* to respond to :some_method with 2 arguments and keywords :u and :v/)
    end

    if RSpec::Support::RubyFeatures.required_kw_args_supported?
      it "passes if target responds to :sym with 2 args and specified required keywords" do
        obj = Object.new
        eval %{def obj.foo(a, b, u:, v:); end}
        expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
      end

      it "passes if target responds to :sym with 2 args and keyword arg splat" do
        obj = Object.new
        eval %{def obj.foo(a, b, **rest); end}
        expect(obj).to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
      end

      it "passes for new when target responds to initialize with a mixture of arguments" do
        klass = eval %{Class.new { def initialize(a, b, c:, d: nil); end }}
        expect(klass).to respond_to(:new).with(2).arguments.and_keywords(:c, :d)
      end

      it "fails if :sym expects 2 arguments and specified optional keywords but expects missing required keywords" do
        obj = Object.new
        eval %{def obj.foo(a, b, u: nil, v: nil, x:, y:); end}
        expect {
          expect(obj).to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v)
        }.to fail_with(/expected .* to respond to :some_method with 2 arguments and keywords :u and :v/)
      end

      it "fails for new when target responds to initialize with the wrong mixture of arguments" do
        klass = eval %{Class.new { def initialize(a, b, c:); end }}
        expect {
          expect(klass).to respond_to(:new).with(2).arguments.and_keywords(:c, :d)
        }.to fail_with(/expected .* to respond to :new with 2 arguments and keywords :c and :d/)
      end
    end
  end

  RSpec.describe "expect(...).to respond_to(:sym).with_any_keywords" do
    it "passes if target responds to any keywords" do
      obj = Object.new
      eval %{def obj.foo(**kw_args); end}
      expect(obj).to respond_to(:foo).with_any_keywords
    end

    it "passes when initialize responds to any keywords and we check new" do
      klass = eval %{Class.new { def initialize(**kw_args); end }}
      expect(klass).to respond_to(:new).with_any_keywords
    end

    it "fails if target does not respond to :sym" do
      obj = Object.new
      expect {
        expect(obj).to respond_to(:some_method).with_any_keywords
      }.to fail_with(/expected .* to respond to :some_method/)
    end

    it "fails if :sym expects a limited set of keywords" do
      obj = Object.new
      eval %{def obj.some_method(a: nil, b: nil); end}
      expect {
        expect(obj).to respond_to(:some_method).with_any_keywords
      }.to fail_with(/expected .* to respond to :some_method with any keywords/)
    end

    it "fails when initialize expects a limited set of keywords and we check new" do
      klass = eval %{Class.new { def initialize(a: nil); end }}
      expect {
        expect(klass).to respond_to(:new).with_any_keywords
      }.to fail_with(/expected .* to respond to :new with any keywords/)
    end

    if RSpec::Support::RubyFeatures.required_kw_args_supported?
      it "fails if :sym expects missing required keywords" do
        obj = Object.new
        eval %{def obj.some_method(a:, b:, **kw_args); end}
        expect {
          expect(obj).to respond_to(:some_method).with_any_keywords
        }.to fail_with(/expected .* to respond to :some_method with any keywords/)
      end

      it "fails if :initialize expects missing required keywords when we test new" do
        klass = eval %{Class.new { def initialize(a:, **kw_args); end }}
        eval %{def initialize(a:, b:, **kw_args); end}
        expect {
          expect(klass).to respond_to(:new).with_any_keywords
        }.to fail_with(/expected .* to respond to :new with any keywords/)
      end
    end
  end

  RSpec.describe "expect(...).not_to respond_to(:sym).with_keywords(:foo, :bar)" do
    it "fails if target responds to :sym with specified optional keywords" do
      obj = Object.new
      eval %{def obj.foo(a: nil, b: nil); end}
      expect {
        expect(obj).not_to respond_to(:foo).with_keywords(:a, :b)
      }.to fail_with(/expected #<Object:.*> not to respond to :foo with keywords :a and :b/)
    end

    it "fails if target responds to :sym with any keywords" do
      obj = Object.new
      eval %{def obj.foo(**kw_args); end}
      expect {
        expect(obj).not_to respond_to(:foo).with_keywords(:a, :b)
      }.to fail_with(/expected #<Object:.*> not to respond to :foo with keywords :a and :b/)
    end

    it "fails if target initialize responds to expected keywords when checking new" do
      klass = eval %{Class.new { def initialize(**kw_args); end }}
      expect {
        expect(klass).not_to respond_to(:new).with_keywords(:a, :b)
      }.to fail_with(/expected .* not to respond to :new with keywords :a and :b/)
    end

    it "passes if target does not respond to :sym" do
      obj = Object.new
      expect(obj).not_to respond_to(:some_method).with_keywords(:a, :b)
    end

    it "passes if :sym does not expect specified keywords" do
      obj = Object.new
      eval %{def obj.foo(a: nil, b: nil); end}
      expect(obj).not_to respond_to(:some_method).with_keywords(:c, :d)
    end

    if RSpec::Support::RubyFeatures.required_kw_args_supported?
      it "fails if target responds to :sym with specified required keywords" do
        obj = Object.new
        eval %{def obj.foo(a:, b:); end}
        expect {
          expect(obj).not_to respond_to(:foo).with_keywords(:a, :b)
        }.to fail_with(/expected #<Object:.*> not to respond to :foo with keywords :a and :b/)
      end

      it "fails if target responds to :sym with keyword arg splat" do
        obj = Object.new
        eval %{def obj.foo(**rest); end}
        expect {
          expect(obj).not_to respond_to(:foo).with_keywords(:a, :b)
        }.to fail_with(/expected #<Object:.*> not to respond to :foo with keywords :a and :b/)
      end

      it "passes if :sym expects missing required keywords" do
        obj = Object.new
        eval %{def obj.foo(a:, b:, c: nil, d: nil); end}
        expect(obj).not_to respond_to(:some_method).with_keywords(:c, :d)
      end

      it "passes if :initialize expects missing required keywords for :new" do
        klass = eval %{Class.new { def initialize(a:, b:, c: nil, d: nil); end }}
        expect(klass).not_to respond_to(:new).with_keywords(:c, :d)
      end
    end
  end

  RSpec.describe "expect(...).not_to respond_to(:sym).with(2).arguments.and_keywords(:foo, :bar)" do
    it "fails if target responds to :sym with 2 args and specified optional keywords" do
      obj = Object.new
      eval %{def obj.foo(a, b, u: nil, v: nil); end}
      expect {
        expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
      }.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/)
    end

    it "fails if target responds to :sym with any number of arguments and specified optional keywords" do
      obj = Object.new
      eval %{def obj.foo(*args, u: nil, v: nil); end}
      expect {
        expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
      }.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/)
    end

    it "fails if target responds to :sym with one or more arguments and specified optional keywords" do
      obj = Object.new
      eval %{def obj.foo(a, *args, u: nil, v: nil); end}
      expect {
        expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
      }.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/)
    end

    it "fails if target responds to :sym with two or more arguments and specified optional keywords" do
      obj = Object.new
      eval %{def obj.foo(a, b, *args, u: nil, v: nil); end}
      expect {
        expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
      }.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/)
    end

    it "passes if target does not respond to :sym" do
      obj = Object.new
      expect(obj).not_to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v)
    end

    it "passes if :sym expects 1 argument" do
      obj = Object.new
      eval %{def obj.foo(a, u: nil, v: nil); end}
      expect(obj).not_to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v)
    end

    it "passes if :sym does not expect specified keywords" do
      obj = Object.new
      def obj.foo(a, b); end
      expect(obj).not_to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v)
    end

    if RSpec::Support::RubyFeatures.required_kw_args_supported?
      it "fails if target responds to :sym with 2 args and specified required keywords" do
        obj = Object.new
        eval %{def obj.foo(a, b, u:, v:); end}
        expect {
          expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
        }.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/)
      end

      it "fails if target responds to :sym with 2 args and keyword arg splat" do
        obj = Object.new
        eval %{def obj.foo(a, b, **rest); end}
        expect {
          expect(obj).not_to respond_to(:foo).with(2).arguments.and_keywords(:u, :v)
        }.to fail_with(/expected #<Object:.*> not to respond to :foo with 2 arguments and keywords :u and :v/)
      end

      it "passes if :sym expects 2 arguments and specified optional keywords but expects missing required keywords" do
        obj = Object.new
        eval %{def obj.foo(a, b, u: nil, v: nil, x:, y:); end}
        expect(obj).not_to respond_to(:some_method).with(2).arguments.and_keywords(:u, :v)
      end
    end
  end

  RSpec.describe "expect(...).not_to respond_to(:sym).with_any_keywords" do
    it "fails if target responds to any keywords" do
      obj = Object.new
      eval %{def obj.foo(**kw_args); end}
      expect {
        expect(obj).not_to respond_to(:foo).with_any_keywords
      }.to fail_with(/expected #<Object:.*> not to respond to :foo with any keywords/)
    end

    it "passes if target does not respond to :sym" do
      obj = Object.new
      expect(obj).not_to respond_to(:some_method).with_any_keywords
    end

    it "passes if :sym expects a limited set of keywords" do
      obj = Object.new
      eval %{def obj.some_method(a: nil, b: nil); end}
      expect(obj).not_to respond_to(:some_method).with_any_keywords
    end

    if RSpec::Support::RubyFeatures.required_kw_args_supported?
      it "passes if :sym expects missing required keywords" do
        obj = Object.new
        eval %{def obj.some_method(a:, b:, **kw_args); end}
        expect(obj).not_to respond_to(:some_method).with_any_keywords
      end
    end
  end
end