Packages
espec
0.8.12
1.10.0
1.9.2
1.9.1
1.9.0
1.8.3
1.8.2
1.8.1
1.8.0
1.7.0
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.2
1.2.1
1.2.0
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
0.8.28
0.8.27
0.8.26
0.8.25
0.8.24
0.8.23
0.8.22
0.8.21
0.8.20
0.8.19
0.8.18
0.8.17
0.8.16
0.8.15
0.8.14
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.2
0.4.1
0.4.0
0.3.7
0.3.5
0.3.0
0.2.0
0.1.0
BDD testing framework for Elixir inspired by RSpec.
Current section
Files
Jump to
Current section
Files
lib/espec/assertions/raise_exception.ex
defmodule ESpec.Assertions.RaiseException do
@moduledoc """
Defines 'raise_exception' assertion.
it do: expect(function).to raise_exception
it do: expect(function).to raise_exception(ErrorModule)
it do: expect(function).to raise_exception(ErrorModule, "message")
"""
use ESpec.Assertions.Interface
defp match(subject, []) do
try do
subject.()
{false, false}
rescue
_error -> {true, false}
end
end
defp match(subject, [module]) do
try do
subject.()
{false, {false, nil}}
rescue
error ->
if error.__struct__ == module do
{true, error.__struct__}
else
{false, error.__struct__}
end
end
end
defp match(subject, [module, mes]) do
try do
subject.()
{false, false}
rescue
error ->
if error.__struct__ == module && Exception.message(error) == mes do
{true, [error.__struct__, Exception.message(error)]}
else
{false, [error.__struct__, Exception.message(error)]}
end
end
end
defp success_message(subject, [], _result, positive) do
to = if positive, do: "raises", else: "doesn't raise"
"#{inspect subject} #{to} an exception."
end
defp success_message(subject, [module], _result, positive) do
to = if positive, do: "raises", else: "doesn't raise"
"#{inspect subject} #{to} the `#{module}` exception."
end
defp success_message(subject, [module, message], _result, positive) do
to = if positive, do: "raises", else: "doesn't raise"
"#{inspect subject} #{to} the `#{module}` exception with the message `#{message}`."
end
defp error_message(subject, [], false, positive) do
if positive do
"Expected #{inspect subject} to raise an exception, but nothing was raised."
else
"Expected #{inspect subject} not to raise an exception, but an exception was raised."
end
end
defp error_message(subject, [module], err_module, positive) do
if positive do
"Expected #{inspect subject} to raise the `#{module}` exception, but nothing was raised."
else
"Expected #{inspect subject} not to raise the `#{module}` exception, but the `#{err_module}` exception was raised."
end
end
defp error_message(subject, [module, message], false, positive) do
to = if positive, do: "to", else: "not to"
"Expected #{inspect subject} #{to} raise the `#{module}` exception with the message `#{message}`, but nothing was raised."
end
defp error_message(subject, [module, message], [err_module, err_message], positive) do
to = if positive, do: "to", else: "not to"
"Expected #{inspect subject} #{to} raise the `#{module}` exception with the message `#{message}`, but the `#{err_module}` exception was raised with the message `#{err_message}`."
end
end