Add let and subject with variants

New behavior mimics RSpec - bang variant defines immediately instead of 
not storing.
This commit is contained in:
Michael Miller 2019-09-15 09:45:48 -06:00
parent c94b284ab3
commit 7002564ebd

View file

@ -0,0 +1,54 @@
module Spectator
module DSL
macro let(name, &block)
def %value
{{block.body}}
end
@%wrapper : ::Spectator::Internals::ValueWrapper?
def {{name.id}}
if (wrapper = @%wrapper)
wrapper.as(::Spectator::Internals::TypedValueWrapper(typeof(%value))).value
else
%value.tap do |value|
@%wrapper = ::Spectator::Internals::TypedValueWrapper.new(value)
end
end
end
end
macro let!(name, &block)
# TODO: Doesn't work with late-defined values (let).
@%value = {{yield}}
def {{name.id}}
@%value
end
end
macro subject(&block)
let(:subject) {{block}}
end
macro subject(name, &block)
let({{name.id}}) {{block}}
def subject
{{name.id}}
end
end
macro subject!(&block)
let!(:subject) {{block}}
end
macro subject!(name, &block)
let!({{name.id}}) {{block}}
def subject
{{name.id}}
end
end
end
end