@@ -5,18 +5,24 @@ defmodule ProtocolTest do
5
5
6
6
doctest Protocol
7
7
8
- defprotocol Sample do
9
- @ type t :: any
10
- @ doc "Ok"
11
- @ spec ok ( t ) :: boolean
12
- def ok ( term )
13
- end
8
+ { _ , _ , sample_binary , _ } =
9
+ defprotocol Sample do
10
+ @ type t :: any
11
+ @ doc "Ok"
12
+ @ spec ok ( t ) :: boolean
13
+ def ok ( term )
14
+ end
14
15
15
- defprotocol WithAny do
16
- @ fallback_to_any true
17
- @ doc "Ok"
18
- def ok ( term )
19
- end
16
+ @ sample_binary sample_binary
17
+
18
+ { _ , _ , with_any_binary , _ } =
19
+ defprotocol WithAny do
20
+ @ fallback_to_any true
21
+ @ doc "Ok"
22
+ def ok ( term )
23
+ end
24
+
25
+ @ with_any_binary with_any_binary
20
26
21
27
defprotocol Derivable do
22
28
def ok ( a )
@@ -123,11 +129,11 @@ defmodule ProtocolTest do
123
129
end
124
130
125
131
test "protocol defines callbacks" do
126
- assert get_callbacks ( Sample , :ok , 1 ) ==
127
- [ { :type , [ 11 ] , :fun , [ { :type , [ 11 ] , :product , [ { :user_type , [ 11 ] , :t , [ ] } ] } , { :type , [ 11 ] , :boolean , [ ] } ] } ]
132
+ assert get_callbacks ( @ sample_binary , :ok , 1 ) ==
133
+ [ { :type , 12 , :fun , [ { :type , 12 , :product , [ { :user_type , 12 , :t , [ ] } ] } , { :type , 12 , :boolean , [ ] } ] } ]
128
134
129
- assert get_callbacks ( WithAny , :ok , 1 ) ==
130
- [ { :type , [ 18 ] , :fun , [ { :type , [ 18 ] , :product , [ { :user_type , [ 18 ] , :t , [ ] } ] } , { :type , [ 18 ] , :term , [ ] } ] } ]
135
+ assert get_callbacks ( @ with_any_binary , :ok , 1 ) ==
136
+ [ { :type , 22 , :fun , [ { :type , 22 , :product , [ { :user_type , 22 , :t , [ ] } ] } , { :type , 22 , :term , [ ] } ] } ]
131
137
end
132
138
133
139
test "protocol defines functions and attributes" do
@@ -182,8 +188,8 @@ defmodule ProtocolTest do
182
188
assert Multi . test ( :a ) == :a
183
189
end
184
190
185
- defp get_callbacks ( module , name , arity ) do
186
- callbacks = for { :callback , info } <- module . __info__ ( :attributes ) , do: hd ( info )
191
+ defp get_callbacks ( beam , name , arity ) do
192
+ callbacks = Kernel.Typespec . beam_callbacks ( beam )
187
193
List . keyfind ( callbacks , { name , arity } , 0 ) |> elem ( 1 )
188
194
end
189
195
@@ -308,6 +314,8 @@ defmodule Protocol.ConsolidationTest do
308
314
{ :ok , binary } = Protocol . consolidate ( Sample , [ Any , ImplStruct ] )
309
315
:code . load_binary ( Sample , 'protocol_test.exs' , binary )
310
316
317
+ @ sample_binary binary
318
+
311
319
# Any should be moved to the end
312
320
:code . purge ( WithAny )
313
321
:code . delete ( WithAny )
@@ -367,7 +375,7 @@ defmodule Protocol.ConsolidationTest do
367
375
end
368
376
369
377
test "consolidated keeps callbacks" do
370
- callbacks = for { :callback , info } <- Sample . __info__ ( :attributes ) , do: hd ( info )
378
+ callbacks = Kernel.Typespec . beam_callbacks ( @ sample_binary )
371
379
assert callbacks != [ ]
372
380
end
373
381
0 commit comments