Skip to content

Commit

Permalink
Let: Allow to pass a Keyword list to let_ok and let_error
Browse files Browse the repository at this point in the history
  • Loading branch information
alexocode committed Aug 31, 2017
1 parent c01e71a commit cb97921
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
44 changes: 44 additions & 0 deletions lib/espec/let/let.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,27 +132,71 @@ defmodule ESpec.Let do
do_result_let(var, block, :ok, false)
end

@doc """
Allows to define several 'let_ok's at once
"""
defmacro let_ok(keyword) when is_list keyword do
if Keyword.keyword?(keyword) do
Enum.map(keyword, fn{var, block} -> do_result_let(var, block, :ok, false) end)
else
raise "Argument must be a Keyword"
end
end

@doc """
Defines 'let!' for success result tuple.
"""
defmacro let_ok!(var, do: block) do
do_result_let(var, block, :ok, true)
end

@doc """
Allows to define several 'let_ok!'s at once
"""
defmacro let_ok!(keyword) when is_list keyword do
if Keyword.keyword?(keyword) do
Enum.map(keyword, fn{var, block} -> do_result_let(var, block, :ok, true) end)
else
raise "Argument must be a Keyword"
end
end

@doc """
Defines 'let' for error result tuple.
"""
defmacro let_error(var, do: block) do
do_result_let(var, block, :error, false)
end

@doc """
Allows to define several 'let_error's at once
"""
defmacro let_error(keyword) when is_list keyword do
if Keyword.keyword?(keyword) do
Enum.map(keyword, fn{var, block} -> do_result_let(var, block, :error, false) end)
else
raise "Argument must be a Keyword"
end
end

@doc """
Defines 'let!' for error result tuple.
"""
defmacro let_error!(var, do: block) do
do_result_let(var, block, :error, true)
end

@doc """
Allows to define several 'let_error!'s at once
"""
defmacro let_error!(keyword) when is_list keyword do
if Keyword.keyword?(keyword) do
Enum.map(keyword, fn{var, block} -> do_result_let(var, block, :error, true) end)
else
raise "Argument must be a Keyword"
end
end

defp do_result_let(var, block, key, bang?) do
new_block = quote do
{unquote(key), result} = unquote(block)
Expand Down
26 changes: 25 additions & 1 deletion spec/let/let_spec.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ defmodule LetSpec do
it do: b() |> should(eq 2)
end

describe "let is lazy and memoizes" do
describe "let is lazy and memorizes" do
let :a do
value = Application.get_env(:espec, :let_value, "") <> ".let"
Application.put_env(:espec, :let_value, value)
Expand All @@ -125,4 +125,28 @@ defmodule LetSpec do
expect(a()).to eq("initial.let")
end
end

describe "let_ok extracts the value from an {:ok, result}" do
let_ok :a, do: {:ok, 1}
let_ok! :b, do: {:ok, 2}
let_ok c: {:ok, 3}
let_ok d: {:ok, 4}

it do: a() |> should(eq 1)
it do: b() |> should(eq 2)
it do: c() |> should(eq 3)
it do: d() |> should(eq 4)
end

describe "let_error extracts the value from an {:error, result}" do
let_error :a, do: {:error, 1}
let_error! :b, do: {:error, 2}
let_error c: {:error, 3}
let_error d: {:error, 4}

it do: a() |> should(eq 1)
it do: b() |> should(eq 2)
it do: c() |> should(eq 3)
it do: d() |> should(eq 4)
end
end

0 comments on commit cb97921

Please sign in to comment.