Packages
espec
1.6.3
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/be.ex
defmodule ESpec.Assertions.Be do
@moduledoc """
Defines 'be' assertion.
it do: expect(2).to be :>, 1
"""
use ESpec.Assertions.Interface
alias ESpec.DatesTimes.Comparator
defp match(subject, [op, val, [{granularity, delta}]]) do
actual_delta =
subject
|> Comparator.diff(val, granularity)
|> abs()
result = apply(Kernel, op, [subject, val]) && apply(Kernel, op, [actual_delta, delta])
{result, {granularity, actual_delta}}
end
defp match(subject, [op, val, {granularity, delta}]) do
actual_delta =
subject
|> Comparator.diff(val, granularity)
|> abs()
result = apply(Kernel, op, [subject, val]) && apply(Kernel, op, [actual_delta, delta])
{result, {granularity, actual_delta}}
end
defp match(%Date{} = subject, [op, %Date{} = val]), do: match_date_times(subject, [op, val])
defp match(%Time{} = subject, [op, %Time{} = val]), do: match_date_times(subject, [op, val])
defp match(%DateTime{} = subject, [op, %DateTime{} = val]),
do: match_date_times(subject, [op, val])
defp match(%NaiveDateTime{} = subject, [op, %NaiveDateTime{} = val]),
do: match_date_times(subject, [op, val])
defp match(subject, [op, val]) do
result = apply(Kernel, op, [subject, val])
{result, result}
end
defp match_date_times(subject, [op, val]) do
delta = subject |> Comparator.diff(val, :microseconds)
result = apply(Kernel, op, [delta, 0])
{result, result}
end
defp success_message(subject, [op, val, [{granularity, delta}]], _result, positive) do
to = if positive, do: "is true", else: "is false"
"`#{inspect(subject)} #{op} #{inspect(val)}` #{to} with delta `[#{granularity}: #{delta}]`."
end
defp success_message(subject, [op, val, {granularity, delta}], _result, positive) do
to = if positive, do: "is true", else: "is false"
"`#{inspect(subject)} #{op} #{inspect(val)}` #{to} with delta `{:#{granularity}, #{delta}}`."
end
defp success_message(subject, [op, val], _result, positive) do
to = if positive, do: "is true", else: "is false"
"`#{inspect(subject)} #{op} #{inspect(val)}` #{to}."
end
defp error_message(
subject,
[op, val, [{granularity, delta}]],
{actual_granularity, actual_delta},
positive
) do
"Expected `#{inspect(subject)} #{op} #{inspect(val)}` to be `#{positive}` but got `#{
!positive
}` with delta `[#{granularity}: #{delta}]`. The actual delta is [#{actual_granularity}: #{
actual_delta
}], with an inclusive boundary."
end
defp error_message(
subject,
[op, val, {granularity, delta}],
{actual_granularity, actual_delta},
positive
) do
"Expected `#{inspect(subject)} #{op} #{inspect(val)}` to be `#{positive}` but got `#{
!positive
}` with delta `{:#{granularity}, #{delta}}`. The actual delta is {:#{actual_granularity}, #{
actual_delta
}}, with an inclusive boundary."
end
defp error_message(subject, [op, val], _result, positive) do
"Expected `#{inspect(subject)} #{op} #{inspect(val)}` to be `#{positive}` but got `#{
!positive
}`."
end
end