Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exceptions in results #7

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Support parsing multiline doctests with `iex>` on all lines, but reformat them using `...>` on every line but the first one.
- Fix implementation for multiline results. Multiline results are allowed, and they can be terminated with an empty new line or another doctest.
- Support exception expressions (`** (ModuleName) message`) in results.

## 0.1.0 (2024-02-25)

Expand Down
21 changes: 18 additions & 3 deletions lib/doctest_formatter/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,31 @@ defmodule DoctestFormatter.Formatter do
Indentation.indent(symbol <> line, chunk.indentation)
end)

formatted_result =
string_result =
chunk.result
|> Enum.join("\n")
|> Code.format_string!(opts)
|> IO.iodata_to_binary()

string_result =
if exception_result?(string_result) do
string_result
|> String.trim()
else
string_result
|> Code.format_string!(opts)
|> IO.iodata_to_binary()
end

formatted_result =
string_result
|> String.split("\n")
|> Enum.map(fn line ->
Indentation.indent(line, chunk.indentation)
end)

formatted_lines ++ formatted_result
end

defp exception_result?(string) do
string |> String.trim() |> String.starts_with?("** (")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ defmodule ProjectWithFormattedCode do
...> |> ProjectWithFormattedCode.add(2)
3

iex> 3 + "3"
** (ArithmeticError) bad argument in arithmetic expression

"""
def add(a, b) do
a + b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ defmodule ProjectWithFormattedCode do
...> |> ProjectWithFormattedCode.add(2)
3

iex> 3 + "3"
** (ArithmeticError) bad argument in arithmetic expression

"""
def add(a, b) do
a + b
Expand Down
29 changes: 29 additions & 0 deletions test/doctest_formatter/formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,35 @@ defmodule DoctestFormatter.FormatterTest do
output = format(input, opts)
assert output == desired_output
end

test "can handle exceptions in results" do
input =
"""
defmodule Foo do
@doc \"""
iex> "Fizz"
iex> |> Kernel.<>( "Buzz" )
iex> |> Kernel.<>(nil)
** (ArgumentError) expected binary argument in <> operator but got: nil
\"""
end
"""

desired_output =
"""
defmodule Foo do
@doc \"""
iex> "Fizz"
...> |> Kernel.<>("Buzz")
...> |> Kernel.<>(nil)
** (ArgumentError) expected binary argument in <> operator but got: nil
\"""
end
"""

output = format(input, [])
assert output == desired_output
end
end

describe "format/2 on @moduledocs" do
Expand Down
Loading