Merge branch 'let-assignment-variant' into 'release/0.8'

Implement assignment variant of let keyword

See merge request arctic-fox/spectator!11
This commit is contained in:
Mike Miller 2019-07-19 22:49:09 +00:00
commit 31fb1c3e81

View file

@ -789,7 +789,54 @@ module Spectator::DSL
# The name can be used in examples to retrieve the value (basically a method). # The name can be used in examples to retrieve the value (basically a method).
# This can be used to define a value once and reuse it in multiple examples. # This can be used to define a value once and reuse it in multiple examples.
# #
# This macro expects a name and a block. # There are two variants - assignment and block.
# Both must be given a name.
#
# For the assignment variant:
# ```
# let string = "foobar"
#
# it "isn't empty" do
# expect(string.empty?).to be_false
# end
#
# it "is six characters" do
# expect(string.size).to eq(6)
# end
# ```
#
# The value is evaluated and stored immediately.
# This is different from other `#let` variants that lazily-evaluate.
#
# ```
# let current_time = Time.utc
# let(lazy_time) { Time.utc }
#
# it "lazy evaluates" do
# now = current_time
# sleep 5
# expect(lazy_time).to_not eq(now)
# end
# ```
#
# However, the value is not reused across tests.
# It will be reconstructed when the next test starts.
#
# ```
# let array = [0, 1, 2]
#
# it "modifies the array" do
# array[0] = 42
# expect(array).to eq([42, 1, 2])
# end
#
# it "doesn't carry across tests" do
# array[1] = 777
# expect(array).to eq([0, 777, 2])
# end
# ```
#
# The block variant expects a name and a block.
# The name can be a symbol or a literal - same as `Object#getter`. # The name can be a symbol or a literal - same as `Object#getter`.
# The block should return the value. # The block should return the value.
# #
@ -838,6 +885,16 @@ module Spectator::DSL
# end # end
# ``` # ```
macro let(name, &block) macro let(name, &block)
{% if block.is_a?(Nop) %}
# Assignment variant.
@%value = {{name.value}}
def {{name.target}}
@%value
end
{% else %}
# Block variant.
# Create a block that returns the value. # Create a block that returns the value.
let!(%value) {{block}} let!(%value) {{block}}
@ -864,6 +921,7 @@ module Spectator::DSL
end end
end end
end end
{% end %}
end end
# The noisier sibling to `#let`. # The noisier sibling to `#let`.