harnais_helper v0.2.0 Harnais.Helper View Source
Harnais.Helper
is a collection of low-level test helpers, especially for doctests.
Documentation Terms
In the documentation these terms, usually in italics, are used to mean the same thing.
form and forms
A form is a quoted form (Macro.t
). A forms is a list of zero, one or more forms.
Bang Functions
The bang peers for the visible functions
(e.g. harnais_helper_format_forms/2
) are now
invisible (@doc false
) to avoid clutter in the function list.
Link to this section Summary
Functions
harnais_helper_format_forms/1
takes a forms, and optional opts, and formats each form
harnais_helper_show_forms/1
takes a forms, normalises
(Harnais.Utility.forms_normalise/1
) them, converts each form to a text
string (Macro.to_string/1
), “cleans” each text string and returns
{:ok, texts}
harnais_helper_test_forms/1
takes a forms and an opts with a binding
Link to this section Types
Link to this section Functions
harnais_helper_format_forms/1
takes a forms, and optional opts, and formats each form.
The forms are first normalised (Harnais.Utility.forms_normalise/1
) and
each converted to a text string (Macro.to_string/1
).
Each text string is passed, together with the opts, to
the Elixir code formatter (Code.format_string!/2
).
The returned iodata from the formatter is joined (Enum.join/1
) and split on line breaks (\n
)
returning {:ok, lines}
.
Examples
iex> [quote(do: x = x + 1),
...> quote(do: x = x * x ),
...> quote(do: x=x-1 )
...> ] |> harnais_helper_format_forms
{:ok, ["x = x + 1", "x = x * x", "x = x - 1"]}
iex> [quote(do: x = x + 1),
...> quote(do: x = x * x),
...> quote(do: x = x - 1)
...> ] |> harnais_helper_format_forms!
["x = x + 1", "x = x * x", "x = x - 1"]
iex> quote do
...> def what_is_x(x) do
...> x
...> |> case do
...> x when is_atom(x) -> :atom
...> x when is_binary(x) ->
...> :string
...> x when is_integer(x) ->
...> # x is an integer
...> :integer
...> _ -> :no_idea
...> end
...> end
...> end
...> |> harnais_helper_format_forms!
["def(what_is_x(x)) do",
" x",
" |> case do",
" x when is_atom(x) ->",
" :atom",
"",
" x when is_binary(x) ->",
" :string",
"",
" x when is_integer(x) ->",
" :integer",
"",
" _ ->",
" :no_idea",
" end",
"end"]
harnais_helper_show_forms/1
takes a forms, normalises
(Harnais.Utility.forms_normalise/1
) them, converts each form to a text
string (Macro.to_string/1
), “cleans” each text string and returns
{:ok, texts}
.
Examples
iex> [quote(do: x = x + 1),
...> quote(do: x = x * x ),
...> quote(do: x=x-1 )
...> ] |> harnais_helper_show_forms
{:ok, ["x = x + 1", "x = x * x", "x = x - 1"]}
iex> [quote(do: x = x + 1),
...> quote(do: x = x * x),
...> quote(do: x = x - 1)
...> ] |> harnais_helper_show_forms!
["x = x + 1", "x = x * x", "x = x - 1"]
harnais_helper_test_forms/1
takes a forms and an opts with a binding.
The forms are normalised (Harnais.Utility.forms_normalise/1
) and
evaluated with the binding (Code.eval_quoted/3
) to generate the eval_result
.
The forms are passed through harnais_helper_show_forms/1
to generate the forms_texts
.
The result of both are combined to returns {:ok, {eval_result, forms_texts}}
.
Examples
iex> [quote(do: x = x + 1),
...> quote(do: x = x * x),
...> quote(do: x = x - 1)
...> ] |> harnais_helper_test_forms(binding: [x: 7])
{:ok, {63, ["x = x + 1", "x = x * x", "x = x - 1"]}}
iex> [quote(do: x = x + 1),
...> quote(do: x = x * x ),
...> quote(do: x=x-1 )
...> ] |> harnais_helper_test_forms(binding: [x: 1])
{:ok, {3, ["x = x + 1", "x = x * x", "x = x - 1"]}}