Skip to content

Commit afb7efd

Browse files
committed
Add raw string compare in doctests for #Module<...>
Will now do raw string compare for doctests of #Module<...> format: iex> HashDict.new a: 0, b: 1, c: 2 #HashDict<[a: 0, b: 1, c: 2]>
1 parent dfeeeac commit afb7efd

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

lib/elixir/lib/version.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ defmodule Version do
140140
141141
## Examples
142142
143-
> Version.parse("2.0.1-alpha1")
143+
iex> Version.parse("2.0.1-alpha1")
144144
#Version.Schema<2.0.1-alpha1>
145145
146146
"""
@@ -193,7 +193,7 @@ defmodule Version do
193193
194194
## Examples
195195
196-
> Version.from_matchable({2, 0, 1, ["alpha", 1]})
196+
iex> Version.from_matchable({2, 0, 1, ["alpha", 1]})
197197
#Version.Schema<2.0.1-alpha.1>
198198
199199
"""

lib/ex_unit/lib/ex_unit/doc_test.ex

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/ex_unit/test/ex_unit/doc_test_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ defmodule ExUnit.DocTestTest.GoodModule do
3232
** (ArithmeticError) bad argument in arithmetic expression
3333
"""
3434
def exception_test, do: :ok
35+
36+
@doc """
37+
iex> HashDict.new a: 0, b: 1, c: 2
38+
#HashDict<[a: 0, b: 1, c: 2]>
39+
"""
40+
def inspect1_test, do: :ok
41+
42+
@doc """
43+
iex> x = HashDict.new a: 0, b: 1, c: 2
44+
...> x
45+
#HashDict<[a: 0, b: 1, c: 2]>
46+
"""
47+
def inspect2_test, do: :ok
3548
end
3649

3750
defmodule ExUnit.DocTestTest.ExceptionModule do

0 commit comments

Comments
 (0)