Packages
espec
1.10.0
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/assertion_helpers.ex
defmodule ESpec.AssertionHelpers do
@moduledoc """
Defines helper functions for modules which use ESpec.
These functions wrap arguments for ESpec.ExpectTo module.
See `ESpec.Assertion` module for corresponding 'assertion modules'
"""
alias ESpec.Assertions
@elixir_types ~w(atom binary bitstring boolean float function integer list map number pid port reference tuple)a
def eq(value), do: {Assertions.Eq, value}
def eql(value), do: {Assertions.Eql, value}
def be(value), do: {Assertions.Eq, value}
def be(operator, value), do: {Assertions.Be, [operator, value]}
def be(operator, value, [{granularity, delta}]),
do: {Assertions.Be, [operator, value, [{granularity, delta}]]}
def be(operator, value, {granularity, delta}),
do: {Assertions.Be, [operator, value, {granularity, delta}]}
def be_between(min, max), do: {Assertions.BeBetween, [min, max]}
def be_close_to(value, delta), do: {Assertions.BeCloseTo, [value, delta]}
def match(value), do: {Assertions.Match, value}
defmacro match_pattern(pattern) do
pattern = Macro.escape(pattern)
quote do
{Assertions.MatchPattern, [unquote(pattern), __ENV__, binding()]}
end
end
def be_true, do: {Assertions.Boolean.BeTrue, []}
def be_false, do: {Assertions.Boolean.BeFalse, []}
def be_truthy, do: {Assertions.Boolean.BeTruthy, []}
def be_falsy, do: {Assertions.Boolean.BeFalsy, []}
def raise_exception(exception, message) when is_atom(exception) and is_binary(message) do
{Assertions.RaiseException, [exception, message]}
end
def raise_exception(exception) when is_atom(exception),
do: {Assertions.RaiseException, [exception]}
def raise_exception(), do: {Assertions.RaiseException, []}
def throw_term(term), do: {Assertions.ThrowTerm, [term]}
def throw_term(), do: {Assertions.ThrowTerm, []}
def change(func) when is_function(func), do: {Assertions.Change, [func]}
def change(func, value) when is_function(func) and is_integer(value),
do: {Assertions.ChangeTo, [func, value]}
def change(func, value) when is_function(func) and is_list(value),
do: {Assertions.ChangeBy, [func, value]}
def change(func, before, value) when is_function(func),
do: {Assertions.ChangeFromTo, [func, before, value]}
def have_all(func) when is_function(func), do: {Assertions.Enum.HaveAll, func}
def have_any(func) when is_function(func), do: {Assertions.Enum.HaveAny, func}
def have_count_by(func, val) when is_function(func),
do: {Assertions.Enum.HaveCountBy, [func, val]}
def be_empty, do: {Assertions.Enum.BeEmpty, []}
def have_max(value), do: {Assertions.Enum.HaveMax, value}
def have_max_by(func, value) when is_function(func),
do: {Assertions.Enum.HaveMaxBy, [func, value]}
def have_min(value), do: {Assertions.Enum.HaveMin, value}
def have_min_by(func, value) when is_function(func),
do: {Assertions.Enum.HaveMinBy, [func, value]}
def match_list(value) when is_list(value), do: {Assertions.ContainExactly, value}
def contain_exactly(value) when is_list(value), do: {Assertions.ContainExactly, value}
def have(val), do: {Assertions.EnumString.Have, val}
def have_at(pos, val) when is_number(pos), do: {Assertions.EnumString.HaveAt, [pos, val]}
def have_first(value), do: {Assertions.ListString.HaveFirst, value}
def have_last(value), do: {Assertions.ListString.HaveLast, value}
def have_count(value), do: {Assertions.EnumString.HaveCount, value}
def have_size(value), do: {Assertions.EnumString.HaveCount, value}
def have_length(value), do: {Assertions.EnumString.HaveCount, value}
def have_hd(value), do: {Assertions.List.HaveHd, value}
def have_tl(value), do: {Assertions.List.HaveTl, value}
def have_byte_size(value), do: {Assertions.Binary.HaveByteSize, value}
def start_with(value), do: {Assertions.String.StartWith, value}
def end_with(value), do: {Assertions.String.EndWith, value}
def be_printable(), do: {Assertions.String.BePrintable, []}
def be_valid_string(), do: {Assertions.String.BeValidString, []}
def be_blank(), do: {Assertions.String.BeBlank, []}
def have_key(value), do: {Assertions.Map.HaveKey, value}
def have_value(value), do: {Assertions.Map.HaveValue, value}
def be_alive(), do: {Assertions.PID.BeAlive, []}
Enum.each(@elixir_types, fn type ->
def unquote(String.to_atom("be_#{type}"))() do
{Assertions.BeType, unquote(Macro.escape(type))}
end
end)
def be_nil, do: {Assertions.BeType, :null}
def be_function(arity), do: {Assertions.BeType, [:function, arity]}
def be_struct, do: {Assertions.BeType, :struct}
def be_struct(name), do: {Assertions.BeType, [:struct, name]}
def accepted(func, args \\ :any, opts \\ [pid: :any, count: :any]),
do: {Assertions.Accepted, [func, args, opts]}
def be_ok_result(), do: {Assertions.Result.BeOkResult, []}
def be_error_result(), do: {Assertions.Result.BeErrorResult, []}
end