Skip to content

Commit

Permalink
Add tests for text formatting of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Dalgona committed Nov 29, 2019
1 parent 6b150d8 commit d692e3c
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
32 changes: 32 additions & 0 deletions test/serum/error/exception_message_test.exs
@@ -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
24 changes: 24 additions & 0 deletions test/serum/error/posix_message_test.exs
@@ -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
24 changes: 24 additions & 0 deletions test/serum/error/simple_message_test.exs
@@ -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
56 changes: 56 additions & 0 deletions test/serum/error_test.exs
@@ -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

0 comments on commit d692e3c

Please sign in to comment.