@@ -267,27 +267,84 @@ defmodule ProtocolTest do
267
267
String . to_charlist ( __ENV__ . file )
268
268
end
269
269
270
- test "cannot derive without any implementation" do
271
- assert_raise ArgumentError ,
272
- ~r" could not load module #{ inspect ( Sample.Any ) } due to reason :nofile, cannot derive #{ inspect ( Sample ) } " ,
273
- fn ->
274
- defmodule NotCompiled do
275
- @ derive [ Sample ]
276
- defstruct hello: :world
277
- end
278
- end
270
+ describe "warnings" do
271
+ import ExUnit.CaptureIO
272
+
273
+ test "with no definitions" do
274
+ assert capture_io ( :stderr , fn ->
275
+ defprotocol SampleWithNoDefinitions do
276
+ end
277
+ end ) =~ "protocols must define at least one function, but none was defined"
278
+ end
279
+
280
+ test "when @callbacks and friends are defined inside a protocol" do
281
+ message =
282
+ capture_io ( :stderr , fn ->
283
+ defprotocol SampleWithCallbacks do
284
+ @ spec with_specs ( any ( ) , keyword ( ) ) :: tuple ( )
285
+ def with_specs ( term , options \\ [ ] )
286
+
287
+ @ spec with_specs_and_when ( any ( ) , opts ) :: tuple ( ) when opts: keyword
288
+ def with_specs_and_when ( term , options \\ [ ] )
289
+
290
+ def without_specs ( term , options \\ [ ] )
291
+
292
+ @ callback foo :: { :ok , term }
293
+ @ callback foo ( term ) :: { :ok , term }
294
+ @ callback foo ( term , keyword ) :: { :ok , term , keyword }
295
+
296
+ @ callback foo_when :: { :ok , x } when x: term
297
+ @ callback foo_when ( x ) :: { :ok , x } when x: term
298
+ @ callback foo_when ( x , opts ) :: { :ok , x , opts } when x: term , opts: keyword
299
+
300
+ @ macrocallback bar ( term ) :: { :ok , term }
301
+ @ macrocallback bar ( term , keyword ) :: { :ok , term , keyword }
302
+
303
+ @ optional_callbacks [ foo: 1 , foo: 2 ]
304
+ @ optional_callbacks [ without_specs: 2 ]
305
+ end
306
+ end )
307
+
308
+ assert message =~
309
+ "cannot define @callback foo/0 inside protocol, use def/1 to outline your protocol definition"
310
+
311
+ assert message =~
312
+ "cannot define @callback foo/1 inside protocol, use def/1 to outline your protocol definition"
313
+
314
+ assert message =~
315
+ "cannot define @callback foo/2 inside protocol, use def/1 to outline your protocol definition"
316
+
317
+ assert message =~
318
+ "cannot define @callback foo_when/0 inside protocol, use def/1 to outline your protocol definition"
319
+
320
+ assert message =~
321
+ "cannot define @callback foo_when/1 inside protocol, use def/1 to outline your protocol definition"
322
+
323
+ assert message =~
324
+ "cannot define @callback foo_when/2 inside protocol, use def/1 to outline your protocol definition"
325
+
326
+ assert message =~
327
+ "cannot define @macrocallback bar/1 inside protocol, use def/1 to outline your protocol definition"
328
+
329
+ assert message =~
330
+ "cannot define @macrocallback bar/2 inside protocol, use def/1 to outline your protocol definition"
331
+
332
+ assert message =~
333
+ "cannot define @optional_callbacks inside protocol, all of the protocol definitions are required"
334
+ end
279
335
end
280
336
281
- test "malformed @callback raises with CompileError" do
282
- assert_raise CompileError ,
283
- "nofile:2: type specification missing return type: foo(term)" ,
284
- fn ->
285
- Code . eval_string ( """
286
- defprotocol WithMalformedCallback do
287
- @callback foo(term)
337
+ describe "errors" do
338
+ test "cannot derive without any implementation" do
339
+ assert_raise ArgumentError ,
340
+ ~r" could not load module #{ inspect ( Sample.Any ) } due to reason :nofile, cannot derive #{ inspect ( Sample ) } " ,
341
+ fn ->
342
+ defmodule NotCompiled do
343
+ @ derive [ Sample ]
344
+ defstruct hello: :world
345
+ end
288
346
end
289
- """ )
290
- end
347
+ end
291
348
end
292
349
end
293
350
@@ -299,6 +356,7 @@ defmodule Protocol.DebugInfoTest do
299
356
300
357
{ :module , _ , binary , _ } =
301
358
defprotocol DebugInfoProto do
359
+ def example ( info )
302
360
end
303
361
304
362
assert { :ok , { DebugInfoProto , [ debug_info: debug_info ] } } =
0 commit comments