Hide constants used to build test hiearchy

This commit is contained in:
Michael Miller 2018-09-12 14:53:11 -06:00
parent c2ee9fd520
commit 8b55d9139d
3 changed files with 37 additions and 23 deletions

View file

@ -0,0 +1,6 @@
module Spectator
module ContextDefinitions
ALL = {} of Path => Object
MAPPING = {} of String => Context
end
end

View file

@ -41,23 +41,24 @@ module Spectator
end end
macro context(what, type = "Context", &block) macro context(what, type = "Context", &block)
{% parent_module = @type %}
{% safe_name = what.id.stringify.chars.map { |c| SPECIAL_CHAR_MAPPING[c] || c }.join("").gsub(/\W+/, "_") %} {% safe_name = what.id.stringify.chars.map { |c| SPECIAL_CHAR_MAPPING[c] || c }.join("").gsub(/\W+/, "_") %}
{% module_name = (type.id + safe_name.camelcase).id %} {% module_name = (type.id + safe_name.camelcase).id %}
{% context_module = CONTEXT_MODULE %} {% absolute_module_name = [parent_module, module_name].join("::").id %}
{% parent_given_vars = GIVEN_VARIABLES %} {% what_arg = what.is_a?(StringLiteral) ? what : what.stringify %}
{% parent_given = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given] %}
module {{module_name.id}} module {{module_name.id}}
include ::Spectator::DSL include ::Spectator::DSL
PARENT_CONTEXT = {{context_module.id}}::CURRENT_CONTEXT {% ::Spectator::ContextDefinitions::ALL[absolute_module_name] = {
CURRENT_CONTEXT = ::Spectator::Context.new({{what.is_a?(StringLiteral) ? what : what.stringify}}, PARENT_CONTEXT) name: module_name,
parent: parent_module,
CONTEXT_MODULE = {{context_module.id}}::{{module_name.id}} given: parent_given.map { |e| e } # Duplicate elements without dup method.
GIVEN_VARIABLES = [ } %}
{{ parent_given_vars.join(", ").id }} ::Spectator::ContextDefinitions::MAPPING[{{absolute_module_name.stringify}}] = Context.new({{what_arg}}, ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}])
]{% if parent_given_vars.empty? %} of Object{% end %}
module Locals module Locals
include {{context_module.id}}::Locals include {{parent_module}}::Locals
{% if what.is_a?(Path) %} {% if what.is_a?(Path) %}
def described_class def described_class
@ -71,9 +72,10 @@ module Spectator
end end
macro it(description, &block) macro it(description, &block)
{% parent_module = @type %}
{% safe_name = description.id.stringify.chars.map { |c| SPECIAL_CHAR_MAPPING[c] || c }.join("").gsub(/\W+/, "_") %} {% safe_name = description.id.stringify.chars.map { |c| SPECIAL_CHAR_MAPPING[c] || c }.join("").gsub(/\W+/, "_") %}
{% class_name = (safe_name.camelcase + "Example").id %} {% class_name = (safe_name.camelcase + "Example").id %}
{% given_vars = GIVEN_VARIABLES %} {% given_vars = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given] %}
{% var_names = given_vars.map { |v| v[:name] } %} {% var_names = given_vars.map { |v| v[:name] } %}
class {{class_name.id}} < ::Spectator::Example class {{class_name.id}} < ::Spectator::Example
include Locals include Locals
@ -104,15 +106,16 @@ module Spectator
end end
end end
%current_context = ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}]
{% if given_vars.empty? %} {% if given_vars.empty? %}
CURRENT_CONTEXT.examples << {{class_name.id}}.new(CURRENT_CONTEXT) %current_context.examples << {{class_name.id}}.new(%current_context)
{% else %} {% else %}
{% for given_var in given_vars %} {% for given_var in given_vars %}
{% var_name = given_var[:name] %} {% var_name = given_var[:name] %}
{% collection = given_var[:collection] %} {% collection = given_var[:collection] %}
{{collection}}.each do |{{var_name}}| {{collection}}.each do |{{var_name}}|
{% end %} {% end %}
CURRENT_CONTEXT.examples << {{class_name.id}}.new(CURRENT_CONTEXT, {{var_names.join(", ").id}}) %current_context.examples << {{class_name.id}}.new(%current_context, {{var_names.join(", ").id}})
{% for given_var in given_vars %} {% for given_var in given_vars %}
end end
{% end %} {% end %}
@ -154,9 +157,11 @@ module Spectator
end end
macro given(collection, &block) macro given(collection, &block)
{% parent_module = @type %}
context({{collection}}, "Given") do context({{collection}}, "Given") do
{% var_name = block.args.empty? ? "value".id : block.args.first %} {% var_name = block.args.empty? ? "value".id : block.args.first %}
{% if GIVEN_VARIABLES.find { |v| v[:name] == var_name.id } %} {% given_vars = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given] %}
{% if given_vars.find { |v| v[:name] == var_name.id } %}
{% raise "Duplicate given variable name \"#{var_name.id}\"" %} {% raise "Duplicate given variable name \"#{var_name.id}\"" %}
{% end %} {% end %}
@ -181,30 +186,30 @@ module Spectator
end end
end end
{% GIVEN_VARIABLES << {name: var_name, collection: collection, setter: setter} %} {% given_vars << {name: var_name, collection: collection, setter: setter} %}
{{block.body}} {{block.body}}
end end
end end
macro before_all(&block) macro before_all(&block)
CURRENT_CONTEXT.before_all_hooks << -> {{block}} ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}].before_all_hooks << -> {{block}}
end end
macro before_each(&block) macro before_each(&block)
CURRENT_CONTEXT.before_each_hooks << -> {{block}} ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}].before_each_hooks << -> {{block}}
end end
macro after_all(&block) macro after_all(&block)
CURRENT_CONTEXT.after_all_hooks << -> {{block}} ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}].after_all_hooks << -> {{block}}
end end
macro after_each(&block) macro after_each(&block)
CURRENT_CONTEXT.after_each_hooks << -> {{block}} ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}].after_each_hooks << -> {{block}}
end end
macro around_each(&block) macro around_each(&block)
CURRENT_CONTEXT.around_each_hooks << -> {{block}} ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}].around_each_hooks << -> {{block}}
end end
def include_examples def include_examples

View file

@ -4,9 +4,12 @@ module Spectator
module Examples module Examples
include ::Spectator::DSL include ::Spectator::DSL
CURRENT_CONTEXT = ::Spectator::Context::ROOT {% ::Spectator::ContextDefinitions::ALL[@type.id] = {
CONTEXT_MODULE = ::Spectator::Examples name: "ROOT",
GIVEN_VARIABLES = [] of Object parent: nil,
given: [] of Object
} %}
::Spectator::ContextDefinitions::MAPPING[{{@type.stringify}}] = ::Spectator::Context::ROOT
module Locals module Locals
end end