Skip to content

Commit

Permalink
Merge pull request antonmi#235 from antonmi/let_in_generated_examples
Browse files Browse the repository at this point in the history
Improve 'let' so it works in generated examples
  • Loading branch information
antonmi authored Sep 10, 2017
2 parents ff40a7e + 516ae8f commit 6e96e2b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/espec/let/let.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ defmodule ESpec.Let do
defmacro let_overridable(var), do: do_let(var, nil, true)

defp do_let(var, block, shared \\ false) do
function = ESpec.Let.Impl.random_let_name
block = Macro.escape((quote do: unquote(block)), unquote: true)

quote do
if unquote(shared) && !@shared do
raise ESpec.LetError, ESpec.Let.__overridable_error_message__(unquote(var), __MODULE__)
quote bind_quoted: [block: block, var: var, shared: shared] do
function = ESpec.Let.Impl.random_let_name

if shared && !@shared do
raise ESpec.LetError, ESpec.Let.__overridable_error_message__(var, __MODULE__)
end

tail = @context
head = %ESpec.Let{var: unquote(var), module: __MODULE__, shared_module: __MODULE__, function: unquote(function), shared: unquote(shared)}
head = %ESpec.Let{var: var, module: __MODULE__, shared_module: __MODULE__, function: function, shared: shared}

def unquote(function)(var!(shared)) do
var!(shared)
Expand All @@ -55,7 +57,7 @@ defmodule ESpec.Let do

@context [head | tail]

unless Module.defines?(__MODULE__, {unquote(var), 0}, :def) do
unless Module.defines?(__MODULE__, {var, 0}, :def) do
def unquote(var)() do
ESpec.Let.Impl.let_eval(__MODULE__, unquote(var))
end
Expand Down
28 changes: 28 additions & 0 deletions spec/let/let_in_generated_examples_spec.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule LetInGeneratedExamples do
use ESpec

context "using let in nested contexts" do
Enum.each [:a, :b, :c], fn(value) ->
context "with value #{inspect value}" do
let :x, do: unquote(value)

it "should equal #{inspect value}" do
x() |> should(eq unquote(value))
end
end
end
end

context "using subject in nested contexts" do
Enum.each [:a, :b, :c], fn(value) ->
context "with value #{inspect value}" do
subject(unquote(value))

it "should equal #{inspect value}" do
should(eq unquote(value))
should(eq unquote(value))
end
end
end
end
end

0 comments on commit 6e96e2b

Please sign in to comment.