harnais_helper v0.1.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.

Link to this section Summary

Functions

harnais_helper_format_forms/1 takes a forms, normalises them, converts each to a text string (Macro.to_string/1) and then applies the standard Elixir code formatter (Code.format_string!/2) to each text, splits on line breaks (“\n”) and returns {:ok, texts}

harnais_helper_show_forms/1 takes a forms, normalises them, converts each to a text string (Macro.to_string/1), “cleans” each text and returns {:ok, texts}

Link to this section Types

Link to this section Functions

Link to this function harnais_helper_format_forms(forms, opts \\ []) View Source
harnais_helper_format_forms(any(), any()) ::
  {:ok, [String.t()]} | {:error, error()}

harnais_helper_format_forms/1 takes a forms, normalises them, converts each to a text string (Macro.to_string/1) and then applies the standard Elixir code formatter (Code.format_string!/2) to each text, splits on line breaks (“\n”) and returns {:ok, texts}.

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"]
Link to this function harnais_helper_format_forms!(forms, opts \\ []) View Source
harnais_helper_format_forms!(any(), any()) :: [String.t()] | no_return()
Link to this function harnais_helper_show_forms(forms, opts \\ []) View Source
harnais_helper_show_forms(any(), any()) ::
  {:ok, [String.t()]} | {:error, error()}

harnais_helper_show_forms/1 takes a forms, normalises them, converts each to a text string (Macro.to_string/1), “cleans” each text 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"]
Link to this function harnais_helper_show_forms!(forms, opts \\ []) View Source
harnais_helper_show_forms!(any(), any()) :: [String.t()] | no_return()
Link to this function harnais_helper_test_forms(forms, opts \\ []) View Source
harnais_helper_test_forms(any(), any()) ::
  {:ok, {any(), [String.t()]}} | {:error, error()}

harnais_helper_test_forms/1 takes a forms and an opts with a binding.

The forms are normalised and evaluated with the binding (Code.eval_quoted/3).

The forms are also passed through harnais_helper_show_forms/1.

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"]}}
Link to this function harnais_helper_test_forms!(forms, opts \\ []) View Source
harnais_helper_test_forms!(any(), any()) ::
  {any(), [String.t()]} | no_return()