Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for text formatting of errors
- Loading branch information
Dalgona
committed
Nov 29, 2019
1 parent
6b150d8
commit d692e3c
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule Serum.Error.ExceptionMessageTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Serum.Error.ExceptionMessage | ||
alias Serum.Error.Format | ||
|
||
setup_all do | ||
{exception, stacktrace} = | ||
try do | ||
raise ArgumentError | ||
rescue | ||
e -> {e, __STACKTRACE__} | ||
end | ||
|
||
message = %ExceptionMessage{exception: exception, stacktrace: stacktrace} | ||
|
||
{:ok, message: message} | ||
end | ||
|
||
describe "with text formatter" do | ||
test "the formatted text contains the exception message", ctx do | ||
text = | ||
ctx.message | ||
|> Format.format_text(0) | ||
|> IO.ANSI.format() | ||
|> IO.iodata_to_binary() | ||
|
||
assert text =~ "ArgumentError" | ||
assert text =~ "argument error" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Serum.Error.POSIXMessageTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Serum.Error.Format | ||
alias Serum.Error.POSIXMessage | ||
|
||
setup_all do | ||
message = %POSIXMessage{reason: :enoent} | ||
|
||
{:ok, message: message} | ||
end | ||
|
||
describe "with text formatter" do | ||
test "the formatted text contains the string representation of POSIX error", ctx do | ||
text = | ||
ctx.message | ||
|> Format.format_text(0) | ||
|> IO.ANSI.format() | ||
|> IO.iodata_to_binary() | ||
|
||
assert text =~ "no such file or directory" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Serum.Error.SimpleMessageTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Serum.Error.Format | ||
alias Serum.Error.SimpleMessage | ||
|
||
setup_all do | ||
message = %SimpleMessage{text: "test error"} | ||
|
||
{:ok, message: message} | ||
end | ||
|
||
describe "with text formatter" do | ||
test "the formatted text contains simple text message", ctx do | ||
text = | ||
ctx.message | ||
|> Format.format_text(0) | ||
|> IO.ANSI.format() | ||
|> IO.iodata_to_binary() | ||
|
||
assert text =~ "test error" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule Serum.ErrorTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Serum.Error | ||
alias Serum.Error.Format | ||
alias Serum.Error.SimpleMessage | ||
|
||
setup_all do | ||
error = %Error{ | ||
message: %SimpleMessage{text: "test error"}, | ||
caused_by: [], | ||
file: %Serum.File{src: "testfile"}, | ||
line: 3 | ||
} | ||
|
||
{:ok, error: error} | ||
end | ||
|
||
describe "with text formatter" do | ||
test "the formatted text contains the error message", ctx do | ||
text = make_string(ctx.error) | ||
|
||
assert text =~ "test error" | ||
end | ||
|
||
test "the formatted text contains file and line information if available", ctx do | ||
text = make_string(ctx.error) | ||
|
||
assert text =~ "testfile:3: " | ||
end | ||
|
||
test "formats nested errors", ctx do | ||
nested_error = %Error{ | ||
ctx.error | ||
| message: %SimpleMessage{text: "nested error"} | ||
} | ||
|
||
error = %Error{ | ||
ctx.error | ||
| caused_by: [nested_error] | ||
} | ||
|
||
text = make_string(error) | ||
|
||
assert text =~ "test error" | ||
assert text =~ "nested error" | ||
end | ||
end | ||
|
||
defp make_string(error) do | ||
error | ||
|> Format.format_text(0) | ||
|> IO.ANSI.format() | ||
|> IO.iodata_to_binary() | ||
end | ||
end |