@@ -5,18 +5,24 @@ defmodule ProtocolTest do
55
66 doctest Protocol
77
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
1415
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
2026
2127 defprotocol Derivable do
2228 def ok ( a )
@@ -123,11 +129,11 @@ defmodule ProtocolTest do
123129 end
124130
125131 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 , [ ] } ] } ]
128134
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 , [ ] } ] } ]
131137 end
132138
133139 test "protocol defines functions and attributes" do
@@ -182,8 +188,8 @@ defmodule ProtocolTest do
182188 assert Multi . test ( :a ) == :a
183189 end
184190
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 )
187193 List . keyfind ( callbacks , { name , arity } , 0 ) |> elem ( 1 )
188194 end
189195
@@ -308,6 +314,8 @@ defmodule Protocol.ConsolidationTest do
308314 { :ok , binary } = Protocol . consolidate ( Sample , [ Any , ImplStruct ] )
309315 :code . load_binary ( Sample , 'protocol_test.exs' , binary )
310316
317+ @ sample_binary binary
318+
311319 # Any should be moved to the end
312320 :code . purge ( WithAny )
313321 :code . delete ( WithAny )
@@ -367,7 +375,7 @@ defmodule Protocol.ConsolidationTest do
367375 end
368376
369377 test "consolidated keeps callbacks" do
370- callbacks = for { :callback , info } <- Sample . __info__ ( :attributes ) , do: hd ( info )
378+ callbacks = Kernel.Typespec . beam_callbacks ( @ sample_binary )
371379 assert callbacks != [ ]
372380 end
373381
0 commit comments