Mostly complete code for #given

This commit is contained in:
Michael Miller 2018-09-06 21:13:03 -06:00
parent 3e7f43009f
commit 7c8be54e5c
2 changed files with 43 additions and 1 deletions

View file

@ -10,10 +10,14 @@ module Spectator
{% safe_name = what.id.stringify.gsub(/\W+/, "_") %}
{% module_name = (type.id + safe_name.camelcase).id %}
{% context_module = CONTEXT_MODULE %}
{% parent_given_vars = GIVEN_VARIABLES %}
module {{module_name.id}}
include ::Spectator::DSL
CONTEXT_MODULE = {{context_module.id}}::{{module_name.id}}
GIVEN_VARIABLES = [
{{ parent_given_vars.join(", ").id }}
]{% if parent_given_vars.empty? %} of Object{% end %}
module Locals
include {{context_module.id}}::Locals
@ -32,19 +36,42 @@ module Spectator
macro it(description, source_file = __FILE__, source_line = __LINE__, &block)
{% safe_name = description.id.stringify.gsub(/\W+/, "_") %}
{% class_name = (safe_name.camelcase + "Example").id %}
{% given_vars = GIVEN_VARIABLES %}
{% var_names = given_vars.map { |v| v[0] } %}
class {{class_name.id}} < ::Spectator::Example
include Locals
{% unless given_vars.empty? %}
def initialize({{ var_names.map { |v| "@#{v}" }.join(", ").id }})
end
{% end %}
def source
Source.new({{source_file}}, {{source_line}})
end
def num
0
end
def run
{{block.body}}
end
end
::Spectator::ALL_EXAMPLES << {{class_name.id}}.new
{% if given_vars.empty? %}
::Spectator::ALL_EXAMPLES << {{class_name.id}}.new
{% else %}
{% for given_var in given_vars %}
{% var_name = given_var[0] %}
{% collection = given_var[1] %}
{{collection}}.each do |{{var_name}}|
{% end %}
::Spectator::ALL_EXAMPLES << {{class_name.id}}.new({{var_names.join(", ").id}})
{% for given_var in given_vars %}
end
{% end %}
{% end %}
end
def it_behaves_like
@ -81,6 +108,20 @@ module Spectator
end
end
macro given(collection, source_file = __FILE__, source_line = __LINE__, &block)
context({{collection}}, {{source_file}}, {{source_line}}, "Given") do
{% var_name = block.args.empty? ? "value" : block.args.first %}
module Locals
getter {{var_name.id}}
end
{% GIVEN_VARIABLES << {var_name, collection} %}
{{block.body}}
end
end
def before_all
raise NotImplementedError.new("Spectator::DSL#before_all")
end

View file

@ -5,6 +5,7 @@ module Spectator
include ::Spectator::DSL
CONTEXT_MODULE = ::Spectator::Examples
GIVEN_VARIABLES = [] of Object
module Locals
end