-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathString_test.rb
1752 lines (1463 loc) · 56.7 KB
/
String_test.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_relative 'test_helper'
# TODO: encode, encode!, byteslice
class StringSingletonTest < Test::Unit::TestCase
include TestHelper
testing 'singleton(::String)'
def test_try_convert
assert_send_type '(String) -> String',
String, :try_convert, 'foo'
assert_send_type '(_ToStr) -> String',
String, :try_convert, ToStr.new
with_untyped do |object|
# These are covered by the previous cases.
next if ::Kernel.instance_method(:respond_to?).bind_call(object, :to_str)
assert_send_type '(untyped) -> nil',
String, :try_convert, object
end
end
end
# Note, all methods which modify string literals have `+'string'` in case we later add
# `frozen_string_literal: true` to the top (or ruby makes frozen strings default).
class StringInstanceTest < Test::Unit::TestCase
include TestHelper
testing '::String'
def assert_case_method(method, include_fold: false)
assert_send_type '() -> String',
'hello', method
options = %i[ascii lithuanian turkic] + (include_fold ? [:fold] : [])
options.each do |option|
assert_send_type "(#{options.map(&:inspect).join('|')}) -> String",
'hello', method, option
end
assert_send_type '(:lithuanian, :turkic) -> String',
'hello', method, :lithuanian, :turkic
assert_send_type '(:turkic, :lithuanian) -> String',
'hello', method, :turkic, :lithuanian
end
def assert_case_method!(method, normal:, nochange:, include_fold: false)
assert_send_type '() -> String',
normal.dup, method
assert_send_type '() -> nil',
nochange.dup, method
options = %i[ascii lithuanian turkic] + (include_fold ? [:fold] : [])
options.each do |option|
assert_send_type "(#{options.map(&:inspect).join('|')}) -> String",
normal.dup, method, option
assert_send_type "(#{options.map(&:inspect).join('|')}) -> nil",
nochange.dup, method, option
end
assert_send_type '(:lithuanian, :turkic) -> String',
normal.dup, method, :lithuanian, :turkic
assert_send_type '(:lithuanian, :turkic) -> nil',
nochange.dup, method, :lithuanian, :turkic
assert_send_type '(:turkic, :lithuanian) -> String',
normal.dup, method, :turkic, :lithuanian
assert_send_type '(:turkic, :lithuanian) -> nil',
nochange.dup, method, :turkic, :lithuanian
end
def test_initialize
assert_send_type '() -> String',
String.allocate, :initialize
with_string do |source|
assert_send_type '(string) -> String',
String.allocate, :initialize, source
end
with_encoding.and_nil do |encoding|
assert_send_type '(encoding: encoding?) -> String',
String.allocate, :initialize, encoding: encoding
end
with_int.and_nil do |capacity|
assert_send_type '(capacity: int?) -> String',
String.allocate, :initialize, capacity: capacity
end
end
def test_initialize_copy
test_replace(:initialize_copy)
end
def test_mod
with_array 1, 2 do |ary|
assert_send_type '(array[untyped]) -> String',
'%d %d', :%, ary
end
with_hash a: 3, b: 4 do |named|
assert_send_type '(hash[Symbol, untyped]) -> String',
'%<a>d %<b>d', :%, named
end
with_untyped do |arg|
# Our test uses `'%s'` so we need to make sure the thing can respond to it.
def arg.to_s = "A" unless defined? arg.to_s
assert_send_type '(untyped) -> String',
'%s', :%, arg
end
end
def test_mul
with_int do |amount|
assert_send_type '(int) -> String',
'hello', :*, amount
end
end
def test_add
with_string do |str|
assert_send_type '(string) -> String',
'hello', :+, str
end
end
def test_upos
assert_send_type '() -> String',
'hi'.dup, :+@ # `.dup` in case we have frozen_string_literal as we're testing `+@`
assert_send_type '() -> String',
Class.new(String).new('hi').freeze, :+@
end
def test_uneg(method = :-@)
assert_send_type '() -> String',
Class.new(String).new('hi'), method
assert_send_type '() -> String',
'hi'.freeze, method
end
def test_lshift
assert_send_type '(Integer) -> String',
+'hi', :<<, 38
refute_send_type '(_ToInt) -> untyped',
+'hi', :<<, ToInt.new(38)
with_string do |string|
assert_send_type '(string) -> String',
+'hi', :<<, string
end
end
def test_cmp
with_string 's' do |other|
assert_send_type '(string) -> -1',
'a', :<=>, other
assert_send_type '(string) -> 0',
's', :<=>, other
assert_send_type '(string) -> 1',
'z', :<=>, other
end
blank = BlankSlate.new.__with_object_methods(:define_singleton_method)
[-123, 0, 123].each do |comparison|
blank.define_singleton_method(:<=>) do |x|
raise '`x` must be `s`' unless 's' == x
ret = BlankSlate.new.__with_object_methods(:define_singleton_method)
ret.define_singleton_method(:>) { |x| comparison > x ? :some_truthy_value : nil }
ret.define_singleton_method(:<) { |x| comparison < x ? :some_truthy_value : nil }
ret
end
assert_send_type '(untyped other) -> (-1 | 0 | 1)',
's', :<=>, blank
end
end
def test_eq(method = :==)
with_untyped do |other|
assert_send_type '(untyped) -> bool',
'hello', method, other
end
end
def test_eqq
test_eq :===
end
def test_match_op
assert_send_type '(Regexp) -> Integer',
'hello', :=~, /./
assert_send_type '(Regexp) -> nil',
'hello', :=~, /doesn't match/
matcher = BlankSlate.new
def matcher.=~(rhs)
fail unless rhs == 'hello'
:world
end
assert_send_type '[T] (String::_MatchAgainst[String, T]) -> T',
'hello', :=~, matcher
end
def test_aref(method = :[])
# (int start, ?int length) -> String?
with_int(3) do |start|
assert_send_type '(int) -> String',
'hello, world', method, start
assert_send_type '(int) -> nil',
'q', method, start
with_int 3 do |length|
assert_send_type '(int, int) -> String',
'hello, world', method, start, length
assert_send_type '(int, int) -> nil',
'q', method, start, length
end
end
# (range[int?] range) -> String?
with_range with_int(3).and_nil, with_int(5).and_nil do |range|
assert_send_type '(range[int?]) -> String',
'hello', method, range
next if nil == range.begin # if the starting value is `nil`, you can't get `nil` outputs.
assert_send_type '(range[int?]) -> nil',
'hi', method, range
end
# (Regexp regexp, ?MatchData::capture backref) -> String?
assert_send_type '(Regexp) -> String',
'hello', method, /./
assert_send_type '(Regexp) -> nil',
'hello', method, /doesn't match/
with_int(1).and 'a', :a do |backref|
assert_send_type '(Regexp, MatchData::capture) -> String',
'hallo', method, /(?<a>)./, backref
assert_send_type '(Regexp, MatchData::capture) -> nil',
'hallo', method, /(?<a>)doesn't match/, backref
end
# (String substring) -> String?
assert_send_type '(String) -> String',
'hello', method, 'hello'
assert_send_type '(String) -> nil',
'hello', method, 'does not exist'
refute_send_type '(_ToStr) -> untyped',
'hello', method, ToStr.new('e')
end
def test_aset
with_string 'world' do |replacement|
# [T < _ToStr] (int index, T replacement) -> T
with_int(3) do |start|
assert_send_type '[T < _ToStr] (int, T) -> T',
'hello, world', :[]=, start, replacement
# [T < _ToStr] (int start, int length, T replacement) -> T
with_int 3 do |length|
assert_send_type '[T < _ToStr] (int, int, T) -> T',
'hello, world', :[]=, start, length, replacement
end
end
# [T < _ToStr] (range[int?] range, T replacement) -> T
with_range with_int(3).and_nil, with_int(5).and_nil do |range|
assert_send_type '[T < _ToStr] (range[int?] range, T replacement) -> T',
'hello', :[]=, range, replacement
end
# [T < _ToStr] (Regexp regexp, T replacement) -> T
assert_send_type '[T < _ToStr] (Regexp regexp, T replacement) -> T',
'hello', :[]=, /./, replacement
# [T < _ToStr] (Regexp regexp, MatchData::capture backref, T replacement) -> T
with_int(1).and 'a', :a do |backref|
assert_send_type '[T < _ToStr] (Regexp regexp, MatchData::capture backref, T replacement) -> T',
'hallo', :[]=, /(?<a>)./, backref, replacement
end
# [T < _ToStr] (String substring, T replacement) -> T
assert_send_type '[T < _ToStr] (String substring, T replacement) -> T',
'hello', :[]=, 'hello', replacement
refute_send_type '[T < _ToStr] (_ToStr, T replacement) -> untyped',
'hello', :[]=, ToStr.new('e'), replacement
end
end
def test_ascii_only?
assert_send_type '() -> bool',
'hello', :ascii_only?
assert_send_type '() -> bool',
"hello\u{6666}", :ascii_only?
end
def test_b
assert_send_type '() -> String',
'hello', :b
end
def test_byteindex
omit_if RUBY_VERSION < '3.2'
with_string('e').and /e/ do |pattern|
assert_send_type '(Regexp | string) -> Integer',
'hello', :byteindex, pattern
assert_send_type '(Regexp | string) -> nil',
'hallo', :byteindex, pattern
with_int(1) do |offset|
assert_send_type '(Regexp | string, int) -> Integer',
'hello', :byteindex, pattern, offset
assert_send_type '(Regexp | string, int) -> nil',
'hallo', :byteindex, pattern, offset
end
end
end
def test_byterindex
omit_if RUBY_VERSION < '3.2'
with_string('e').and /e/ do |pattern|
assert_send_type '(Regexp | string) -> Integer',
'hello', :byterindex, pattern
assert_send_type '(Regexp | string) -> nil',
'hallo', :byterindex, pattern
with_int(1) do |offset|
assert_send_type '(Regexp | string, int) -> Integer',
'hello', :byterindex, pattern, offset
assert_send_type '(Regexp | string, int) -> nil',
'hallo', :byterindex, pattern, offset
end
end
end
def test_bytes
assert_send_type '() -> Array[Integer]',
'hello', :bytes
assert_send_type '() { (Integer) -> void } -> String',
'hello', :bytes do end
end
def test_bytesize
assert_send_type '() -> Integer',
'hello', :bytesize
end
def test_byteslice
with_int 3 do |start|
assert_send_type '(int) -> String',
'hello', :byteslice, start
assert_send_type '(int) -> nil',
'q', :byteslice, start
with_int 3 do |length|
assert_send_type '(int, int) -> String',
'hello', :byteslice, start, length
assert_send_type '(int, int) -> nil',
'q', :byteslice, start, length
end
end
# TODO: | (range[int?] range) -> String?
end
def test_bytesplice
omit_if(RUBY_VERSION < '3.2', 'String#bytesplice was added in 3.2')
# In 3.3 and onwards (and backported to 3.2.16), the return type is `self`. This variable
# is in case the test suite is run in a version under 3.2.16; tests for the variants only
# supported in 3.3 and onwards use `self`. If we ever stop supporting 3.2, we can remove this.
with_string ', world! :-D' do |string|
assert_send_type "(Integer, Integer, string) -> String",
+'hello', :bytesplice, 1, 2, string
if RUBY_VERSION >= "3.3.0"
assert_send_type '(Integer, Integer, string, Integer, Integer) -> String',
+'hello', :bytesplice, 1, 2, string, 3, 4
end
with_range with_int(1).and_nil, with_int(2).and_nil do |range|
assert_send_type "(range[int?], string) -> String",
+'hello', :bytesplice, range, string
if RUBY_VERSION >= '3.3.0'
with_range with_int(3).and_nil, with_int(4).and_nil do |string_range|
assert_send_type '(range[int?], string, range[int?]) -> String',
+'hello', :bytesplice, range, string, string_range
end
end
end
end
end
def test_capitalize
assert_case_method :capitalize
end
def test_capitalize!
assert_case_method! :capitalize!, normal: 'hello', nochange: 'Hello'
end
def test_casecmp
with_string 's' do |other|
assert_send_type '(string) -> -1',
'a', :casecmp, other
assert_send_type '(string) -> 1',
'z', :casecmp, other
assert_send_type '(string) -> 0',
's', :casecmp, other
end
# incompatible encodings yield nil
with_string '😊'.force_encoding('IBM437') do |other|
assert_send_type '(string) -> nil',
'😊'.force_encoding('UTF-8'), :casecmp, other
end
with_untyped do |other|
next if defined? other.to_str # omit the string cases of `with_untyped`.
assert_send_type '(untyped) -> nil',
's', :casecmp, other
end
end
def test_casecmp?
with_string 's' do |other|
assert_send_type '(string) -> bool',
'a', :casecmp?, other
assert_send_type '(string) -> bool',
'z', :casecmp?, other
assert_send_type '(string) -> bool',
's', :casecmp?, other
end
# incompatible encodings yield nil
with_string '😊'.force_encoding('IBM437') do |other|
assert_send_type '(string) -> nil',
'😊'.force_encoding('UTF-8'), :casecmp?, other
end
with_untyped do |other|
next if defined? other.to_str # omit the string cases of `with_untyped`.
assert_send_type '(untyped) -> nil',
's', :casecmp?, other
end
end
def test_center
with_int 10 do |width|
assert_send_type '(int) -> String',
'hello', :center, width
with_string '&' do |padding|
assert_send_type '(int, string) -> String',
'hello', :center, width, padding
end
end
end
def test_chars
assert_send_type '() -> Array[String]',
'hello', :chars
assert_send_type '() { (String) -> void } -> String',
'hello', :chars do end
end
def test_chomp
assert_send_type '() -> String',
"hello\n", :chomp
assert_send_type '() -> String',
"hello", :chomp
with_string("\n").and_nil do |separator|
assert_send_type '(string?) -> String',
"hello\n", :chomp, separator
assert_send_type '(string?) -> String',
"hello", :chomp, separator
end
end
def test_chomp!
assert_send_type '() -> String',
+"hello\n", :chomp!
assert_send_type '() -> nil',
+'hello', :chomp!
with_string(".") do |separator|
assert_send_type '(string) -> String',
+"hello.", :chomp!, separator
assert_send_type '(string) -> nil',
+"hello", :chomp!, separator
end
assert_send_type '(nil) -> nil',
"a\n", :chomp!, nil
end
def test_chop
assert_send_type '() -> String',
'hello', :chop
assert_send_type '() -> String',
'', :chop
end
def test_chop!
assert_send_type '() -> String',
+'hello', :chop!
assert_send_type '() -> nil',
+'', :chop!
end
def test_chr
assert_send_type '() -> String',
'hello', :chr
end
def test_clear
assert_send_type '() -> String',
+'hello', :clear
end
def test_codepoints
assert_send_type '() -> Array[Integer]',
'hello', :codepoints
assert_send_type '() { (Integer) -> void } -> String',
'hello', :codepoints do end
end
def test_concat
with_string do |str|
assert_send_type '(*string | Integer) -> String',
+'hello', :concat, str, 38
end
end
def test_count
with_string 'l' do |selector|
assert_send_type '(String::selector) -> Integer',
'hello', :count, selector
assert_send_type '(String::selector, *String::selector) -> Integer',
'hello', :count, selector, selector
end
end
def test_dedup
omit_if RUBY_VERSION < '3.2.0'
test_uneg :dedup
end
def test_crypt
with_string 'hello' do |salt|
assert_send_type '(string) -> String',
'world', :crypt, salt
end
end
def test_delete
with_string 'l' do |selector|
assert_send_type '(String::selector) -> String',
'hello', :delete, selector
assert_send_type '(String::selector, *String::selector) -> String',
'hello', :delete, selector, selector
end
end
def test_delete!
with_string 'l' do |selector|
assert_send_type '(String::selector) -> String',
+'hello', :delete!, selector
assert_send_type '(String::selector, *String::selector) -> String',
+'hello', :delete!, selector, selector
assert_send_type '(String::selector) -> nil',
+'heya', :delete!, selector
assert_send_type '(String::selector, *String::selector) -> nil',
+'heya', :delete!, selector, selector
end
end
def test_delete_prefix
with_string 'he' do |prefix|
assert_send_type '(string) -> String',
'hello', :delete_prefix, prefix
end
end
def test_delete_prefix!
with_string 'he' do |prefix|
assert_send_type '(string) -> String',
+'hello', :delete_prefix!, prefix
assert_send_type '(string) -> nil',
+'ello', :delete_prefix!, prefix
end
end
def test_delete_suffix
with_string 'lo' do |suffix|
assert_send_type '(string) -> String',
'hello', :delete_suffix, suffix
end
end
def test_delete_suffix!
with_string 'lo' do |suffix|
assert_send_type '(string) -> String',
+'hello', :delete_suffix!, suffix
assert_send_type '(string) -> nil',
+'heya', :delete_suffix!, suffix
end
end
def test_downcase
assert_case_method :downcase, include_fold: true
end
def test_downcase!
assert_case_method! :downcase!, normal: 'HELLO', nochange: 'hello', include_fold: true
end
def test_dump
assert_send_type '() -> String',
'hello', :dump
end
def test_each_byte
assert_send_type "() -> Enumerator[Integer, String]",
"hello", :each_byte
assert_send_type "() { (Integer) -> void } -> String",
"hello", :each_byte do |c| c end
end
def test_each_char
assert_send_type "() -> Enumerator[String, String]",
"hello", :each_char
assert_send_type "() { (String) -> void } -> String",
"hello", :each_char do |c| c end
end
def test_each_codepoint
assert_send_type "() -> Enumerator[Integer, String]",
"hello", :each_codepoint
assert_send_type "() { (Integer) -> void } -> String",
"hello", :each_codepoint do |c| c end
end
def test_each_grapheme_cluster
assert_send_type "() -> Enumerator[String, String]",
"hello", :each_grapheme_cluster
assert_send_type "() { (String) -> void } -> String",
"hello", :each_grapheme_cluster do |c| c end
end
def test_each_line
assert_send_type '() -> Enumerator[String, String]',
"hello\nworld", :each_line
assert_send_type '() { (String) -> void } -> String',
"hello\nworld", :each_line do end
with_string('_').and_nil do |separator|
assert_send_type '(string?) -> Enumerator[String, String]',
"hello\nworld", :each_line, separator
assert_send_type '(string?) { (String) -> void } -> String',
"hello\nworld", :each_line, separator do end
with_boolish do |chomp|
assert_send_type '(string?, chomp: boolish) -> Enumerator[String, String]',
"hello\nworld", :each_line, separator, chomp: chomp
assert_send_type '(string?, chomp: boolish) { (String) -> void } -> String',
"hello\nworld", :each_line, separator, chomp: chomp do end
end
end
with_boolish do |chomp|
assert_send_type '(chomp: boolish) -> Enumerator[String, String]',
"hello\nworld", :each_line, chomp: chomp
assert_send_type '(chomp: boolish) { (String) -> void } -> String',
"hello\nworld", :each_line, chomp: chomp do end
end
end
def test_empty?
assert_send_type '() -> bool',
'hello', :empty?
assert_send_type '() -> bool',
'', :empty?
end
def test_encode
# `encode` returns an `instance`, not a `String`, unlike most other functions.
ruby = Class.new(String).new "Ruby\u05E2"
assert_send_type '() -> String',
ruby, :encode
with_encoding 'UTF-8' do |source|
assert_send_type '(encoding) -> String',
ruby, :encode, source
with_encoding 'ISO-8859-1' do |target|
assert_send_type '(encoding, encoding) -> String',
ruby, :encode, source, target
end
end
# There's no real way to know (without inspecting the output of `.encode`) whether or not the
# keyword arguments that're supplied are defined, as `.encode` (and `.encode!`) silently swallow
# any unknown arguments. So there's no way to know for sure if tests are passing because we have
# the correct signature, or if the arguments are unknown (and thus accepted).
#
# We're also going to do all keywords individually, as it's too hard to do all possible
# combinations.
with(:replace).and_nil do |replace|
assert_send_type '(invalid: :replace ?) -> String',
ruby, :encode, invalid: replace
assert_send_type '(undef: :replace ?) -> String',
ruby, :encode, undef: replace
rescue; require 'pry'; binding.pry
end
with_string('&').and_nil do |replace|
assert_send_type '(replace: string?) -> String',
ruby, :encode, replace: replace
end
with(:text, :attr).and_nil do |xml|
assert_send_type '(xml: (:text | :attr)?) -> String',
ruby, :encode, xml: xml
end
with(:universal, :crlf, :cr, :lf).and_nil do |newline|
assert_send_type '(newline: (:universal | :crlf | :cr | :lf)?) -> String',
ruby, :encode, newline: newline
end
with_boolish do |boolish|
assert_send_type '(universal_newline: boolish) -> String',
ruby, :encode, universal_newline: boolish
assert_send_type '(cr_newline: boolish) -> String',
ruby, :encode, cr_newline: boolish
assert_send_type '(crlf_newline: boolish) -> String',
ruby, :encode, crlf_newline: boolish
assert_send_type '(lf_newline: boolish) -> String',
ruby, :encode, lf_newline: boolish
end
iso_8859_1 = Encoding::ISO_8859_1
test_fallback = proc do |type, &block|
with_string('&') do |string|
assert_send_type "(Encoding, fallback: #{type}) -> String",
ruby, :encode, iso_8859_1, fallback: block.call(string)
end
begin
ruby.encode iso_8859_1, fallback: block.call(nil)
rescue Encoding::UndefinedConversionError => err
pass '`nil` causes an error to be thrown'
else
flunk 'fallback of nil should cause an error'
end
end
test_fallback.call '^(String) -> string?' do |string|
->_ignore { string }
end
test_fallback.call 'Method' do |string|
bs = BlankSlate.new.__with_object_methods(:method, :define_singleton_method)
bs.define_singleton_method(:method_name) { |_ignore| string }
bs.method(:method_name)
end
test_fallback.call '::String::_EncodeFallbackAref' do |string|
bs = BlankSlate.new.__with_object_methods(:define_singleton_method)
bs.define_singleton_method(:[]) { |_ignore| string }
bs
end
end
def test_encode!
# `encode` returns an `instance`, not a `String`, unlike most other functions.
ruby = Class.new(String).new "Ruby\u05E2"
assert_send_type '() -> String',
ruby.dup, :encode!
with_encoding 'UTF-8' do |source|
assert_send_type '(encoding) -> String',
ruby.dup, :encode!, source
with_encoding 'ISO-8859-1' do |target|
assert_send_type '(encoding, encoding) -> String',
ruby.dup, :encode!, source, target
end
end
# There's no real way to know (without inspecting the output of `.encode`) whether or not the
# keyword arguments that're supplied are defined, as `.encode` (and `.encode!`) silently swallow
# any unknown arguments. So there's no way to know for sure if tests are passing because we have
# the correct signature, or if the arguments are unknown (and thus accepted).
#
# We're also going to do all keywords individually, as it's too hard to do all possible
# combinations.
with(:replace).and_nil do |replace|
assert_send_type '(invalid: :replace ?) -> String',
ruby.dup, :encode!, invalid: replace
assert_send_type '(undef: :replace ?) -> String',
ruby.dup, :encode!, undef: replace
rescue; require 'pry'; binding.pry
end
with_string('&').and_nil do |replace|
assert_send_type '(replace: string?) -> String',
ruby.dup, :encode!, replace: replace
end
with(:text, :attr).and_nil do |xml|
assert_send_type '(xml: (:text | :attr)?) -> String',
ruby.dup, :encode!, xml: xml
end
with(:universal, :crlf, :cr, :lf).and_nil do |newline|
assert_send_type '(newline: (:universal | :crlf | :cr | :lf)?) -> String',
ruby.dup, :encode!, newline: newline
end
with_boolish do |boolish|
assert_send_type '(universal_newline: boolish) -> String',
ruby.dup, :encode!, universal_newline: boolish
assert_send_type '(cr_newline: boolish) -> String',
ruby.dup, :encode!, cr_newline: boolish
assert_send_type '(crlf_newline: boolish) -> String',
ruby.dup, :encode!, crlf_newline: boolish
assert_send_type '(lf_newline: boolish) -> String',
ruby.dup, :encode!, lf_newline: boolish
end
iso_8859_1 = Encoding::ISO_8859_1
test_fallback = proc do |type, &block|
with_string('&') do |string|
assert_send_type "(Encoding, fallback: #{type}) -> String",
ruby.dup, :encode!, iso_8859_1, fallback: block.call(string)
end
begin
ruby.encode iso_8859_1, fallback: block.call(nil)
rescue Encoding::UndefinedConversionError => err
pass '`nil` causes an error to be thrown'
else
flunk 'fallback of nil should cause an error'
end
end
test_fallback.call '^(String) -> string?' do |string|
->_ignore { string }
end
test_fallback.call 'Method' do |string|
bs = BlankSlate.new.__with_object_methods(:method, :define_singleton_method)
bs.define_singleton_method(:method_name) { |_ignore| string }
bs.method(:method_name)
end
test_fallback.call '::String::_EncodeFallbackAref' do |string|
bs = BlankSlate.new.__with_object_methods(:define_singleton_method)
bs.define_singleton_method(:[]) { |_ignore| string }
bs
end
end
def test_encoding
assert_send_type '() -> Encoding',
'hello', :encoding
end
def test_end_with?
assert_send_type '() -> bool',
'hello', :end_with?
with_string 'lo' do |suffix|
assert_send_type '(*string) -> bool',
'hello', :end_with?, suffix
assert_send_type '(*string) -> bool',
'hello', :end_with?, suffix, suffix
end
end
def test_eql?
with_untyped do |other|
assert_send_type '(untyped) -> bool',
'hello', :eql?, other
end
end
def test_force_encoding
with_encoding do |encoding|
assert_send_type '(encoding) -> String',
'hello', :force_encoding, encoding
end
end
def test_freeze
assert_send_type '() -> String',
'hello', :freeze
end
def test_getbyte
with_int 3 do |index|
assert_send_type '(int) -> Integer',
'hello', :getbyte, index
assert_send_type '(int) -> nil',
'hi', :getbyte, index
end
end
def test_grapheme_clusters
assert_send_type '() -> Array[String]',
'hello', :grapheme_clusters
assert_send_type '() { (String) -> void } -> String',
'hello', :grapheme_clusters do end
end
def test_gsub
with_string('l').and /l/ do |pattern|
assert_send_type '(Regexp | string) -> Enumerator[String, String]',
'hello', :gsub, pattern
assert_send_type '(Regexp | string) { (String) -> _ToS } -> String',
'hello', :gsub, pattern do ToS.new end
with_string '!' do |replacement|
assert_send_type '(Regexp | string, string) -> String',
'hello', :gsub, pattern, replacement
end
with_hash 'l' => ToS.new('!') do |replacement|
assert_send_type '(Regexp | string, hash[String, _ToS]) -> String',
'hello', :gsub, pattern, replacement
end
end
end
def test_gsub!
omit 'There is currently a bug that prevents `.gsub!` from being testable'
with_string('l').and /l/ do |pattern|
assert_send_type '(Regexp | string) -> Enumerator[String, String]',
+'hello', :gsub!, pattern
assert_send_type '(Regexp | string) -> Enumerator[String, String]',
+'heya', :gsub!, pattern
assert_send_type '(Regexp | string) { (String) -> _ToS } -> String',
+'hello', :gsub!, pattern do ToS.new end
assert_send_type '(Regexp | string) { (String) -> _ToS } -> nil',
+'heya', :gsub!, pattern do ToS.new end
with_string '!' do |replacement|
assert_send_type '(Regexp | string, string) -> String',
+'hello', :gsub!, pattern, replacement
assert_send_type '(Regexp | string, string) -> nil',
+'heya', :gsub!, pattern, replacement
end
with_hash 'l' => ToS.new('!') do |replacement|
assert_send_type '(Regexp | string, hash[String, _ToS]) -> String',
+'hello', :gsub!, pattern, replacement
assert_send_type '(Regexp | string, hash[String, _ToS]) -> nil',
+'heya', :gsub!, pattern, replacement
end
end
end
def test_hash
assert_send_type '() -> Integer',
'hello', :hash
end
def test_hex
assert_send_type '() -> Integer',
'0x12', :hex
end
def test_include?