@@ -74,6 +74,19 @@ defmodule ExUnit.DocTest do
7474 We also allow you to select or skip some functions when calling
7575 `doctest`. See the documentation for more info.
7676
77+ ## Opaque types
78+
79+ Some types internal structure are kept hidden and instead show a
80+ user-friendly structure when inspecting the value. The idiom in
81+ Elixir is to print those data types as `#Name<...>`. Doctest will
82+ test these values by doing a string compare.
83+
84+ iex> HashDict.new(a: 10, b: 20)
85+ #HashDict<[a: 10, b: 20]>
86+
87+ The above example will be tested with the following match:
88+ `"#HashDict<[a: 10, b: 20]>" = inspect (HashDict.new(a: 10, b: 20))`.
89+
7790 ## Exceptions
7891
7992 You can also showcase expressions raising an exception, for example:
@@ -260,6 +273,27 @@ defmodule ExUnit.DocTest do
260273 end
261274 end
262275
276+ defp test_case_content ( expr , { :inspect , expected } , module , line , file , stack ) do
277+ expr_ast = string_to_quoted ( module , line , file , expr )
278+ expr_ast = quote do: inspect ( unquote ( expr_ast ) )
279+ expected_ast = string_to_quoted ( module , line , file , expected )
280+
281+ quote do
282+ v = unquote ( expected_ast )
283+ case unquote ( expr_ast ) do
284+ ^ v -> :ok
285+ actual ->
286+ raise ExUnit.ExpectationError ,
287+ [ prelude: "Expected doctest" ,
288+ expr: unquote ( expr ) ,
289+ expected: inspect ( v ) ,
290+ assertion: "inspect as" ,
291+ actual: inspect ( actual ) ] ,
292+ unquote ( stack )
293+ end
294+ end
295+ end
296+
263297 defp test_case_content ( expr , { :error , exception , message } , module , line , file , stack ) do
264298 expr_ast = string_to_quoted ( module , line , file , expr )
265299
@@ -468,8 +502,14 @@ defmodule ExUnit.DocTest do
468502 end
469503
470504 # Finally, parse expected_acc.
471- defp extract_tests( [ expected | lines ] , line , expr_acc , expected_acc , acc , newtest ) do
472- extract_tests ( lines , line , expr_acc , expected_acc <> "\n " <> expected , acc , newtest )
505+ defp extract_tests( [ expected | lines ] , line , expr_acc , expected_acc , [ test = Test [ exprs : exprs ] | t ] = acc , newtest ) do
506+ if expected =~ % r / ^ #[A-Z][\w\.]*<.*>$/ do
507+ expected = expected_acc <> "\n " <> Inspect.BitString . inspect ( expected , [ ] )
508+ test = test . exprs ( [ { expr_acc , { :inspect , expected } } | exprs ] )
509+ extract_tests ( lines , line , "" , "" , [ test | t ] , newtest )
510+ else
511+ extract_tests( lines, line , expr_acc , expected_acc <> "\n " <> expected , acc , newtest )
512+ end
473513 end
474514
475515 defp extract_error ( << ")" , t :: binary >> , acc ) do
0 commit comments