@@ -25,73 +25,208 @@ def test_Array
2525 assert_send_type "(nil) -> []" ,
2626 Kernel , :Array , nil
2727
28- with_array ( 1 r, 2 r) . chain ( [ ToA . new ( 1 r, 2 r) ] ) . each do |ary |
29- assert_send_type "(::array[Rational] | ::_ToA[Rational]) -> Array[Rational]" ,
30- Kernel , :Array , ary
28+ with_untyped do |ele |
29+ with_array ( ele , ele ) . and ToA . new ( ele , ele ) do |ary |
30+ assert_send_type "[T] (array[T] | _ToA[T]) -> Array[T]" ,
31+ Kernel , :Array , ary
32+ end
33+
34+ next if defined? ( ele . to_a ) || defined? ( ele . to_ary )
35+ assert_send_type "[T] (T) -> [T]" ,
36+ Kernel , :Array , ele
3137 end
38+ end
39+
40+ def test_Complex
41+ # (_ToC complex_like, ?exception: true) -> Complex
42+ assert_send_type "(_ToC) -> Complex" ,
43+ Kernel , :Complex , ToC . new
44+ assert_send_type "(_ToC, exception: true) -> Complex" ,
45+ Kernel , :Complex , ToC . new , exception : true
46+
47+ # (_ToC complex_like, exception: bool) -> Complex?
48+ assert_send_type "(_ToC, exception: bool) -> Complex" ,
49+ Kernel , :Complex , ToC . new , exception : false
50+ assert_send_type "(_ToC, exception: bool) -> nil" ,
51+ Kernel , :Complex , Class . new ( BlankSlate ) { def to_c = fail } . new , exception : false
3252
33- assert_send_type "(Rational) -> [Rational]" ,
34- Kernel , :Array , 1 r
53+ numeric = Class . new ( Numeric ) . new
54+
55+ # (Numeric numeric, ?exception: bool) -> Complex
56+ with 1 , 1 r, 1.0 , ( 1 +0 i) , numeric do |real |
57+ assert_send_type "(Numeric) -> Complex" ,
58+ Kernel , :Complex , real
59+
60+ # Single `Numeric`s can never fail
61+ with_bool do |exception |
62+ assert_send_type "(Numeric, exception: bool) -> Complex" ,
63+ Kernel , :Complex , real , exception : exception
64+ end
65+ end
66+
67+ # (String real_or_both, ?exception: true) -> Complex
68+ assert_send_type "(String) -> Complex" ,
69+ Kernel , :Complex , '1'
70+ assert_send_type "(String, exception: true) -> Complex" ,
71+ Kernel , :Complex , '1' , exception : true
72+
73+ # (untyped real_or_both, exception: bool) -> Complex?
74+ with_untyped . and 'oops' do |real_untype |
75+ assert_send_type '(untyped, exception: bool) -> Complex?' ,
76+ Kernel , :Complex , real_untype , exception : false
77+ end
78+
79+ with '1' , 1 , 1 r, 1.0 , ( 1 +0 i) , numeric do |real |
80+ with '2' , 2 , 2 r, 2.0 , ( 2 +0 i) , numeric do |imag |
81+ # (Numeric | String real, Numeric | String imag, ?exception: true) -> Complex
82+ assert_send_type "(Numeric | String, Numeric | String) -> Complex" ,
83+ Kernel , :Complex , real , imag
84+ assert_send_type "(Numeric | String, Numeric | String, exception: true) -> Complex" ,
85+ Kernel , :Complex , real , imag , exception : true
86+
87+ # Complex has an awkward edgecase where `exception: false` will unconditionally return `nil`
88+ # if the imaginary argument is not one of the builtin `Numeric`s. Oddly enough, it's not for
89+ # the `real` one...
90+ case imag
91+ when Integer , Float , Rational , Complex
92+ # (Numeric | String real, Integer | Float | Rational | Complex imag, exception: bool) -> Complex
93+ assert_send_type "(Numeric | String, Integer | Float | Rational | Complex, exception: bool) -> Complex" ,
94+ Kernel , :Complex , real , imag , exception : false
95+ end
96+ end
97+
98+ # (Numeric | String real, untyped, exception: bool) -> Complex?
99+ with_untyped . and 'oops' , numeric do |imag |
100+ next if [ Integer , Float , Rational , Complex ] . any? { _1 === imag }
101+ assert_send_type "(Numeric | String, untyped, exception: bool) -> nil" ,
102+ Kernel , :Complex , real , imag , exception : false
103+ end
104+ end
35105 end
36106
107+
37108 def test_Float
38- with_float 1.0 do |float |
39- assert_send_type "(::float ) -> Float" ,
40- Kernel , :Float , float
41- assert_send_type "(::float , exception: true) -> Float" ,
42- Kernel , :Float , float , exception : true
43- assert_send_type "(::float , exception: bool) -> Float? " ,
44- Kernel , :Float , float , exception : false
109+ with 1 , 1.0 , ToF . new ( 1.0 ) , '1e3' do |float_like |
110+ assert_send_type "(_ToF ) -> Float" ,
111+ Kernel , :Float , float_like
112+ assert_send_type "(_ToF , exception: true) -> Float" ,
113+ Kernel , :Float , float_like , exception : true
114+ assert_send_type "(_ToF , exception: bool) -> Float" ,
115+ Kernel , :Float , float_like , exception : false
45116 end
46117
47- assert_send_type "(untyped, ?exception: bool) -> Float?" ,
48- Kernel , :Float , :hello , exception : false
118+ with_untyped do |untyped |
119+ next if defined? untyped . to_f
120+ assert_send_type "(untyped, exception: bool) -> nil" ,
121+ Kernel , :Float , untyped , exception : false
122+ end
49123 end
50124
51125 def test_Hash
52- assert_send_type "(nil) -> Hash[untyped, untyped ]" ,
126+ assert_send_type "[K, V] (nil) -> Hash[K, V ]" ,
53127 Kernel , :Hash , nil
54- assert_send_type "([]) -> Hash[untyped, untyped ]" ,
128+ assert_send_type "[K, V] ([]) -> Hash[K, V ]" ,
55129 Kernel , :Hash , [ ]
56130
57131 with_hash 'a' => 3 do |hash |
58- assert_send_type "(:: hash[String, Integer ]) -> Hash[String, Integer ]" ,
132+ assert_send_type "[K, V] ( hash[K, V ]) -> Hash[K, V ]" ,
59133 Kernel , :Hash , hash
60134 end
61135 end
62136
63137 def test_Integer
64- with_int ( 1 ) . chain ( [ ToI . new ( 1 ) ] ) . each do |int |
65- assert_send_type "(:: int | :: _ToI) -> Integer" ,
138+ with_int . and ToI . new do |int |
139+ assert_send_type "(int | _ToI) -> Integer" ,
66140 Kernel , :Integer , int
67- assert_send_type "(:: int | :: _ToI, exception: true) -> Integer" ,
141+ assert_send_type "(int | _ToI, exception: true) -> Integer" ,
68142 Kernel , :Integer , int , exception : true
69- assert_send_type "(:: int | :: _ToI, exception: bool) -> Integer?" ,
143+ assert_send_type "(int | _ToI, exception: bool) -> Integer?" ,
70144 Kernel , :Integer , int , exception : false
71145 end
72146
73147 with_string "123" do |string |
74148 with_int 8 do |base |
75- assert_send_type "(:: string, :: int) -> Integer" ,
149+ assert_send_type "(string, int) -> Integer" ,
76150 Kernel , :Integer , string , base
77- assert_send_type "(:: string, :: int, exception: true) -> Integer" ,
151+ assert_send_type "(string, int, exception: true) -> Integer" ,
78152 Kernel , :Integer , string , base , exception : true
79- assert_send_type "(:: string, :: int, exception: bool) -> Integer?" ,
153+ assert_send_type "(string, int, exception: bool) -> Integer?" ,
80154 Kernel , :Integer , string , base , exception : false
81155 end
82156 end
83157
84- assert_send_type "(untyped, ?exception: bool) -> Integer?" ,
85- Kernel , :Integer , :hello , exception : false
158+ with_untyped do |untyped |
159+ assert_send_type "(untyped, exception: bool) -> Integer?" ,
160+ Kernel , :Integer , untyped , exception : false
161+
162+ with_int 10 do |base |
163+ assert_send_type "(untyped, int, exception: bool) -> Integer?" ,
164+ Kernel , :Integer , untyped , base , exception : false
165+ end
166+ end
167+ end
168+
169+ def test_Rational
170+ with_int ( 1 ) . and ToR . new ( 1 r) do |numer |
171+ assert_send_type "(int | _ToR) -> Rational" ,
172+ Kernel , :Rational , numer
173+ assert_send_type "(int | _ToR, exception: true) -> Rational" ,
174+ Kernel , :Rational , numer , exception : true
175+ assert_send_type "(int | _ToR, exception: bool) -> Rational" ,
176+ Kernel , :Rational , numer , exception : false
177+
178+ with_int ( 2 ) . and ToR . new ( 2 r) do |denom |
179+ assert_send_type "(int | _ToR, int | _ToR) -> Rational" ,
180+ Kernel , :Rational , numer , denom
181+ assert_send_type "(int | _ToR, int | _ToR, exception: true) -> Rational" ,
182+ Kernel , :Rational , numer , denom , exception : true
183+ assert_send_type "(int | _ToR, int | _ToR, exception: bool) -> Rational" ,
184+ Kernel , :Rational , numer , denom , exception : false
185+ end
186+ end
187+
188+ bad_int = Class . new ( BlankSlate ) { def to_int = fail } . new
189+ bad_rat = Class . new ( BlankSlate ) { def to_r = fail } . new
190+ with bad_int , bad_rat do |bad_numer |
191+ assert_send_type "(int | _ToR, exception: bool) -> nil" ,
192+ Kernel , :Rational , bad_numer , exception : false
193+ assert_send_type "(int | _ToR, int | _ToR, exception: bool) -> nil" ,
194+ Kernel , :Rational , bad_numer , bad_numer , exception : false
195+ end
196+
197+
198+ numeric = Class . new ( Numeric ) . new
199+ assert_send_type "[T < _Numeric] (T numer, 1) -> T" ,
200+ Kernel , :Rational , numeric , 1
201+ assert_send_type "[T < _Numeric] (T numer, 1, exception: bool) -> T" ,
202+ Kernel , :Rational , numeric , 1 , exception : true
203+ assert_send_type "[T < _Numeric] (T numer, 1, exception: bool) -> T" ,
204+ Kernel , :Rational , numeric , 1 , exception : false
205+
206+ numeric_div = Class . new ( Numeric ) { def /( other ) = :hello } . new
207+
208+ assert_send_type "[T] (Numeric & Kernel::_RationalDiv[T] numer, Numeric denom) -> T" ,
209+ Kernel , :Rational , numeric_div , numeric
210+ assert_send_type "[T] (Numeric & Kernel::_RationalDiv[T] numer, Numeric denom, exception: bool) -> T" ,
211+ Kernel , :Rational , numeric_div , numeric , exception : true
212+ assert_send_type "[T] (Numeric & Kernel::_RationalDiv[T] numer, Numeric denom, exception: bool) -> T" ,
213+ Kernel , :Rational , numeric_div , numeric , exception : false
214+
215+ with_untyped do |numer |
216+ with_untyped do |denom |
217+ assert_send_type "(untyped, untyped, exception: bool) -> Rational?" ,
218+ Kernel , :Rational , numer , denom , exception : false
219+ end
220+ end
86221 end
87222
88223 def test_String
89224 with_string do |string |
90- assert_send_type "(:: string) -> String" ,
225+ assert_send_type "(string) -> String" ,
91226 Kernel , :String , string
92227 end
93228
94- assert_send_type "(:: _ToS) -> String" ,
229+ assert_send_type "(_ToS) -> String" ,
95230 Kernel , :String , ToS . new
96231 end
97232
0 commit comments