Some macro cleanup

This commit is contained in:
Michael Miller 2018-09-12 19:23:27 -06:00
parent 9b07f669ed
commit e30da2be19

View file

@ -8,22 +8,25 @@ 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| ::Spectator::ContextDefinitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_") %} parent_module = @type
{% module_name = (type.id + safe_name.camelcase).id %} safe_name = what.id.stringify.chars.map { |c| ::Spectator::ContextDefinitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_")
{% absolute_module_name = [parent_module, module_name].join("::").id %} module_name = (type.id + safe_name.camelcase).id
{% what_arg = what.is_a?(StringLiteral) ? what : what.stringify %} absolute_module_name = [parent_module, module_name].join("::").id
{% parent_given = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given] %} what_arg = what.is_a?(StringLiteral) ? what : what.stringify
module {{module_name.id}} parent_given = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given]
include ::Spectator::DSL
{% ::Spectator::ContextDefinitions::ALL[absolute_module_name] = { ::Spectator::ContextDefinitions::ALL[absolute_module_name] = {
name: module_name, name: module_name,
parent: parent_module, parent: parent_module,
given: parent_given.map { |e| e } # Duplicate elements without dup method. given: parent_given.map { |e| e } # Duplicate elements without dup method.
} %} }
::Spectator::ContextDefinitions::MAPPING[{{absolute_module_name.stringify}}] = Context.new({{what_arg}}, ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}]) %}
::Spectator::ContextDefinitions::MAPPING[{{absolute_module_name.stringify}}] =
Context.new({{what_arg}}, ::Spectator::ContextDefinitions::MAPPING[{{parent_module.stringify}}])
module {{module_name.id}}
include {{parent_module}} include {{parent_module}}
{% if what.is_a?(Path) || what.is_a?(Generic) %} {% if what.is_a?(Path) || what.is_a?(Generic) %}
@ -46,11 +49,13 @@ module Spectator
macro given(collection, &block) macro given(collection, &block)
{% parent_module = @type %} {% parent_module = @type %}
context({{collection}}, "Given") do context({{collection}}, "Given") do
{% var_name = block.args.empty? ? "value".id : block.args.first %} {%
{% given_vars = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given] %} var_name = block.args.empty? ? "value".id : block.args.first
{% if given_vars.find { |v| v[:name] == var_name.id } %} given_vars = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given]
{% raise "Duplicate given variable name \"#{var_name.id}\"" %} if given_vars.find { |v| v[:name] == var_name.id }
{% end %} raise "Duplicate given variable name \"#{var_name.id}\""
end
%}
@%wrapper : ValueWrapper? @%wrapper : ValueWrapper?
@ -128,11 +133,14 @@ module Spectator
end end
macro it(description, &block) macro it(description, &block)
{% parent_module = @type %} {%
{% safe_name = description.id.stringify.chars.map { |c| ::Spectator::ContextDefinitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_") %} parent_module = @type
{% class_name = (safe_name.camelcase + "Example").id %} safe_name = description.id.stringify.chars.map { |c| ::Spectator::ContextDefinitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_")
{% given_vars = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given] %} class_name = (safe_name.camelcase + "Example").id
{% var_names = given_vars.map { |v| v[:name] } %} given_vars = ::Spectator::ContextDefinitions::ALL[parent_module.id][:given]
var_names = given_vars.map { |v| v[:name] }
%}
class Example%example class Example%example
include ExampleDSL include ExampleDSL
include {{parent_module}} include {{parent_module}}
@ -176,8 +184,10 @@ module Spectator
%current_context.examples << {{class_name.id}}.new(%current_context) %current_context.examples << {{class_name.id}}.new(%current_context)
{% else %} {% else %}
{% for given_var, i in given_vars %} {% for given_var, i in given_vars %}
{% var_name = given_var[:name] %} {%
{% collection = given_var[:collection] %} var_name = given_var[:name]
collection = given_var[:collection]
%}
{{collection}}.each do |%var{i}| {{collection}}.each do |%var{i}|
{% end %} {% end %}
%current_context.examples << {{class_name.id}}.new(%current_context {% for v, i in var_names %}, %var{i}{% end %}) %current_context.examples << {{class_name.id}}.new(%current_context {% for v, i in var_names %}, %var{i}{% end %})