Skip to content

Commit

Permalink
Format doctests in moduledoc
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Feb 25, 2024
1 parent 5d03419 commit c16102d
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 15 deletions.
22 changes: 14 additions & 8 deletions lib/doctest_formatter/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@ defmodule DoctestFormatter.Formatter do
forms =
Macro.prewalk(forms, fn node ->
case node do
{:@, meta1, [{:doc, meta2, [{:__block__, meta3, [doc_content]}]}]}
when is_binary(doc_content) ->
{:@, meta1, [{attribute_name, meta2, [{:__block__, meta3, [doc_content]}]}]}
when attribute_name in [:doc, :moduledoc] and is_binary(doc_content) ->
formatted_doc_content = format_doc_content(doc_content, opts)
{:@, meta1, [{:doc, meta2, [{:__block__, meta3, [formatted_doc_content]}]}]}
{:@, meta1, [{attribute_name, meta2, [{:__block__, meta3, [formatted_doc_content]}]}]}

{:@, meta1, [{:doc, meta2, [doc_content]}]} when is_binary(doc_content) ->
{:@, meta1, [{attribute_name, meta2, [doc_content]}]}
when attribute_name in [:doc, :moduledoc] and is_binary(doc_content) ->
formatted_doc_content = format_doc_content(doc_content, opts)
{:@, meta1, [{:doc, meta2, [formatted_doc_content]}]}
{:@, meta1, [{attribute_name, meta2, [formatted_doc_content]}]}

{:@, meta1, [{:doc, meta2, [{sigil, meta3, [{:<<>>, meta4, [doc_content]}, []]}]}]}
when is_binary(doc_content) and sigil in [:sigil_S, :sigil_s] ->
{:@, meta1,
[{attribute_name, meta2, [{sigil, meta3, [{:<<>>, meta4, [doc_content]}, []]}]}]}
when attribute_name in [:doc, :moduledoc] and is_binary(doc_content) and
sigil in [:sigil_S, :sigil_s] ->
formatted_doc_content = format_doc_content(doc_content, opts)

{:@, meta1,
[{:doc, meta2, [{sigil, meta3, [{:<<>>, meta4, [formatted_doc_content]}, []]}]}]}
[
{attribute_name, meta2,
[{sigil, meta3, [{:<<>>, meta4, [formatted_doc_content]}, []]}]}
]}

node ->
node
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule ProjectWithFormattedCode do
@moduledoc """
Documentation for `ProjectWithFormattedCode`.
iex> ProjectWithFormattedCode.add(5, 5)
10
"""

@doc """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ defmodule ExpectedDiff do
def diff do
"""
diff --git a/smoke_test_data/elixir-1-13/project_with_unformatted_code/lib/project_with_unformatted_code.ex b/smoke_test_data/elixir-1-13/project_with_unformatted_code/lib/project_with_unformatted_code.ex
index 4014a07..e1c0611 100644
index 730bd33..53fdd6d 100644
--- a/smoke_test_data/elixir-1-13/project_with_unformatted_code/lib/project_with_unformatted_code.ex
+++ b/smoke_test_data/elixir-1-13/project_with_unformatted_code/lib/project_with_unformatted_code.ex
@@ -12,8 +12,8 @@ defmodule ProjectWithUnformattedCode do
@@ -2,7 +2,7 @@ defmodule ProjectWithUnformattedCode do
@moduledoc """
Documentation for `ProjectWithUnformattedCode`.
#{" "}
- iex> ProjectWithFormattedCode.add(5,5)
+ iex> ProjectWithFormattedCode.add(5, 5)
10
\"""
#{" "}
@@ -15,8 +15,8 @@ defmodule ProjectWithUnformattedCode do
3
#{" "}
iex> 1
Expand All @@ -16,7 +25,7 @@ defmodule ExpectedDiff do
#{" "}
\"""
def add(a, b) do
@@ -21,10 +21,18 @@ defmodule ProjectWithUnformattedCode do
@@ -24,10 +24,18 @@ defmodule ProjectWithUnformattedCode do
end
#{" "}
@doc \"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule ProjectWithUnformattedCode do
@moduledoc """
Documentation for `ProjectWithUnformattedCode`.
iex> ProjectWithFormattedCode.add(5,5)
10
"""

@doc """
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule ProjectWithFormattedCode do
@moduledoc """
Documentation for `ProjectWithFormattedCode`.
iex> ProjectWithFormattedCode.add(5, 5)
10
"""

@doc """
Expand Down
15 changes: 12 additions & 3 deletions smoke_test_data/project_with_unformatted_code/expected_diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ defmodule ExpectedDiff do
def diff do
"""
diff --git a/smoke_test_data/project_with_unformatted_code/lib/project_with_unformatted_code.ex b/smoke_test_data/project_with_unformatted_code/lib/project_with_unformatted_code.ex
index 4014a07..e1c0611 100644
index 730bd33..53fdd6d 100644
--- a/smoke_test_data/project_with_unformatted_code/lib/project_with_unformatted_code.ex
+++ b/smoke_test_data/project_with_unformatted_code/lib/project_with_unformatted_code.ex
@@ -12,8 +12,8 @@ defmodule ProjectWithUnformattedCode do
@@ -2,7 +2,7 @@ defmodule ProjectWithUnformattedCode do
@moduledoc """
Documentation for `ProjectWithUnformattedCode`.
#{" "}
- iex> ProjectWithFormattedCode.add(5,5)
+ iex> ProjectWithFormattedCode.add(5, 5)
10
\"""
#{" "}
@@ -15,8 +15,8 @@ defmodule ProjectWithUnformattedCode do
3
#{" "}
iex> 1
Expand All @@ -16,7 +25,7 @@ defmodule ExpectedDiff do
#{" "}
\"""
def add(a, b) do
@@ -21,10 +21,18 @@ defmodule ProjectWithUnformattedCode do
@@ -24,10 +24,18 @@ defmodule ProjectWithUnformattedCode do
end
#{" "}
@doc \"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
defmodule ProjectWithUnformattedCode do
@moduledoc """
Documentation for `ProjectWithUnformattedCode`.
iex> ProjectWithFormattedCode.add(5,5)
10
"""

@doc """
Expand Down
130 changes: 129 additions & 1 deletion test/doctest_formatter/formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule DoctestFormatter.FormatterTest do

import DoctestFormatter.Formatter

describe "format/2" do
describe "format/2 when no doctests" do
test "works for empty strings" do
assert format("", []) == "\n"
end
Expand Down Expand Up @@ -57,7 +57,9 @@ defmodule DoctestFormatter.FormatterTest do
test "keeps only one newline" do
assert format("\n\n", []) == "\n"
end
end

describe "format/2 on @docs" do
test "formats doctests in docs, multiline string" do
input =
"""
Expand Down Expand Up @@ -598,4 +600,130 @@ defmodule DoctestFormatter.FormatterTest do
assert output == desired_output
end
end

describe "format/2 on @moduledocs" do
test "formats doctests in moduledocs, multiline string" do
input =
"""
defmodule Foo do
@moduledoc \"""
It adds two numbers together
iex> Foo.add(4,2)
6
\"""
@spec add(a :: integer, b :: integer) :: integer
def add(a, b) do
a + b
end
end
"""

desired_output =
"""
defmodule Foo do
@moduledoc \"""
It adds two numbers together
iex> Foo.add(4, 2)
6
\"""
@spec add(a :: integer, b :: integer) :: integer
def add(a, b) do
a + b
end
end
"""

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

test "formats doctests in moduledocs, single line string" do
input =
"""
defmodule Foo do
@moduledoc \"It adds two numbers together\niex> Foo.add(4,2)\n6\"
@spec add(a :: integer, b :: integer) :: integer
def add(a, b) do
a + b
end
end
"""

desired_output =
"""
defmodule Foo do
@moduledoc \"It adds two numbers together\niex> Foo.add(4, 2)\n6\"
@spec add(a :: integer, b :: integer) :: integer
def add(a, b) do
a + b
end
end
"""

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

test "formats doctests in moduledocs, lowercase s sigil string" do
input =
"""
defmodule Foo do
@moduledoc ~s/It adds two numbers together\niex> Foo.add(4,2)\n6/
@spec add(a :: integer, b :: integer) :: integer
def add(a, b) do
a + b
end
end
"""

desired_output =
"""
defmodule Foo do
@moduledoc ~s/It adds two numbers together\niex> Foo.add(4, 2)\n6/
@spec add(a :: integer, b :: integer) :: integer
def add(a, b) do
a + b
end
end
"""

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

test "formats doctests in moduledocs, uppercase s sigil string" do
input =
"""
defmodule Foo do
@moduledoc ~S/It adds two numbers together
iex> Foo.add(4,2)
6
/
@spec add(a :: integer, b :: integer) :: integer
def add(a, b) do
a + b
end
end
"""

desired_output =
"""
defmodule Foo do
@moduledoc ~S/It adds two numbers together
iex> Foo.add(4, 2)
6
/
@spec add(a :: integer, b :: integer) :: integer
def add(a, b) do
a + b
end
end
"""

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

# TODO: test string interpolation?
end

0 comments on commit c16102d

Please sign in to comment.