mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add let and subject with variants
New behavior mimics RSpec - bang variant defines immediately instead of not storing.
This commit is contained in:
parent
c94b284ab3
commit
7002564ebd
1 changed files with 54 additions and 0 deletions
54
src/spectator/dsl/values.cr
Normal file
54
src/spectator/dsl/values.cr
Normal 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
|
Loading…
Reference in a new issue