Initial work on values DSL

This commit is contained in:
Michael Miller 2021-01-09 17:01:33 -07:00
parent 4108a6602d
commit 391325d431
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
3 changed files with 10 additions and 14 deletions

View file

@ -4,6 +4,7 @@ require "./dsl/examples"
require "./dsl/groups"
require "./dsl/hooks"
require "./dsl/top"
require "./dsl/values"
module Spectator
# Namespace containing methods representing the spec domain specific language.

View file

@ -1,24 +1,18 @@
require "../lazy_wrapper"
module Spectator::DSL
# DSL methods for defining test values (subjects).
module Values
end
macro let(name, &block)
{% raise "Block required for 'let'" unless block %}
macro let(name, &block)
@%wrapper : ::Spectator::ValueWrapper?
@%value = ::Spectator::LazyWrapper.new
def {{name.id}}
{{block.body}}
end
def {{name.id}}
if (wrapper = @%wrapper)
wrapper.as(::Spectator::TypedValueWrapper(typeof(previous_def))).value
else
previous_def.tap do |value|
@%wrapper = ::Spectator::TypedValueWrapper.new(value)
end
end
@%value.get {{block}}
end
end
end
macro let!(name, &block)
@%wrapper : ::Spectator::ValueWrapper?

View file

@ -9,6 +9,7 @@ class SpectatorTestContext < SpectatorContext
include ::Spectator::DSL::Examples
include ::Spectator::DSL::Groups
include ::Spectator::DSL::Hooks
include ::Spectator::DSL::Values
# Initial implicit subject for tests.
# This method should be overridden by example groups when an object is described.