Skip to content

Commit

Permalink
Support opaque types in doctest results (#18)
Browse files Browse the repository at this point in the history
* Support opaque types in doctest results

* Smoke tests
  • Loading branch information
angelikatyborska authored Mar 29, 2024
1 parent 7915b2b commit 7bd8635
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Support opaque types in doctest results (e.g. `#User<name: "", ...>`).

## 0.2.1 (2024-03-22)

- Do not crash if doctest has no expected result.
Expand Down
8 changes: 6 additions & 2 deletions lib/doctest_formatter/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ defmodule DoctestFormatter.Formatter do
|> Enum.join("\n")

string_result =
if exception_result?(string_result) do
if exception_result?(string_result) || opaque_type_result?(string_result) do
string_result
|> String.trim()
else
Expand All @@ -121,10 +121,14 @@ defmodule DoctestFormatter.Formatter do
end)
end

defp exception_result?(string) do
def exception_result?(string) do
string |> String.trim() |> String.starts_with?("** (")
end

def opaque_type_result?(string) do
string |> String.trim() |> String.match?(~r/#([A-z0-9\.]*)<(.*)>/)
end

defp get_prompt(chunk, line_index) do
iex_line_number =
if chunk.iex_line_number do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ defmodule ProjectWithFormattedCode do
def subtract(a, b) do
a - b
end

defmodule User do
@derive {Inspect, only: [:name]}
defstruct [:name, :email]
end

@doc """
iex> ProjectWithFormattedCode.alice()
#ProjectWithFormattedCode.User<name: "Alice", ...>
"""
def alice do
%User{name: "Alice", email: "[email protected]"}
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 5b65e4d..98f817c 100644
index 865085b..95fa7dc 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
@@ -2,7 +2,7 @@ defmodule ProjectWithUnformattedCode do
Expand Down Expand Up @@ -52,3 +52,12 @@ index 5b65e4d..98f817c 100644
"""
def subtract(a, b) do
a - b
@@ -41,7 +57,7 @@ defmodule ProjectWithUnformattedCode do
end

@doc """
- iex> ProjectWithUnformattedCode.alice()
+ iex> ProjectWithUnformattedCode.alice()
#ProjectWithUnformattedCode.User<name: "Alice", ...>
"""
def alice do
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ defmodule ProjectWithUnformattedCode do
def subtract(a, b) do
a - b
end

defmodule User do
@derive {Inspect, only: [:name]}
defstruct [:name, :email]
end

@doc """
iex> ProjectWithUnformattedCode.alice()
#ProjectWithUnformattedCode.User<name: "Alice", ...>
"""
def alice do
%User{name: "Alice", email: "[email protected]"}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,17 @@ defmodule ProjectWithFormattedCode do
def subtract(a, b) do
a - b
end

defmodule User do
@derive {Inspect, only: [:name]}
defstruct [:name, :email]
end

@doc """
iex> ProjectWithFormattedCode.alice()
#ProjectWithFormattedCode.User<name: "Alice", ...>
"""
def alice do
%User{name: "Alice", email: "[email protected]"}
end
end
11 changes: 10 additions & 1 deletion smoke_test_data/project_with_unformatted_code/expected_diff.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 5b65e4d..98f817c 100644
index 865085b..95fa7dc 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
@@ -2,7 +2,7 @@ defmodule ProjectWithUnformattedCode do
Expand Down Expand Up @@ -52,3 +52,12 @@ index 5b65e4d..98f817c 100644
"""
def subtract(a, b) do
a - b
@@ -41,7 +57,7 @@ defmodule ProjectWithUnformattedCode do
end

@doc """
- iex> ProjectWithUnformattedCode.alice()
+ iex> ProjectWithUnformattedCode.alice()
#ProjectWithUnformattedCode.User<name: "Alice", ...>
"""
def alice do
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ defmodule ProjectWithUnformattedCode do
def subtract(a, b) do
a - b
end

defmodule User do
@derive {Inspect, only: [:name]}
defstruct [:name, :email]
end

@doc """
iex> ProjectWithUnformattedCode.alice()
#ProjectWithUnformattedCode.User<name: "Alice", ...>
"""
def alice do
%User{name: "Alice", email: "[email protected]"}
end
end
68 changes: 68 additions & 0 deletions test/doctest_formatter/formatter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@ defmodule DoctestFormatter.FormatterTest do

import DoctestFormatter.Formatter

test "exception_result?/1" do
assert exception_result?("** (RuntimeError) some error")
assert exception_result?(" ** (RuntimeError) some error ")
assert exception_result?("\t** (SomeError) ")
assert exception_result?(" ** ( ")

refute exception_result?("**")
refute exception_result?("*")
refute exception_result?("")
refute exception_result?("3 + 4")
end

test "opaque_type_result?/1" do
assert opaque_type_result?("#User<>")
assert opaque_type_result?(" #User<> ")
assert opaque_type_result?("\t#User<> ")
assert opaque_type_result?("#Accounts.User<>")
assert opaque_type_result?("#Accounts.User<name: \"something\">")
assert opaque_type_result?("#Accounts.User<name: \"something\", ...>")
assert opaque_type_result?("#SomeModule345<>")
assert opaque_type_result?("#Foo.SomeModule345<>")
assert opaque_type_result?("#DateTime<2023-06-26 09:30:00+09:00 JST Asia/Tokyo>")

refute opaque_type_result?("#")
refute opaque_type_result?("# ")
refute opaque_type_result?("# comment")
refute opaque_type_result?("#not_a_module")
refute opaque_type_result?("")
refute opaque_type_result?("3 + 2")
end

describe "format/2 when no doctests" do
test "works for empty strings" do
assert format("", []) == "\n"
Expand Down Expand Up @@ -1049,4 +1080,41 @@ defmodule DoctestFormatter.FormatterTest do
assert output == desired_output
end
end

describe "format/2 on opaque types" do
test "can handle exceptions in results" do
input =
"""
defmodule Foo do
defmodule User do
@derive {Inspect, only: [:name]}
defstruct [:name, :email]
end
@doc \"""
iex> %Foo.User{name: "Bob", email: "[email protected]"}
#Foo.User<name: "Bob", ...>
\"""
end
"""

desired_output =
"""
defmodule Foo do
defmodule User do
@derive {Inspect, only: [:name]}
defstruct [:name, :email]
end
@doc \"""
iex> %Foo.User{name: "Bob", email: "[email protected]"}
#Foo.User<name: "Bob", ...>
\"""
end
"""

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

0 comments on commit 7bd8635

Please sign in to comment.