Shorten GroupDefinitions to Definitions

This commit is contained in:
Michael Miller 2018-09-15 16:53:28 -06:00
parent 196187a98a
commit a06ea6088c
3 changed files with 22 additions and 22 deletions

View file

@ -1,5 +1,5 @@
module Spectator module Spectator
module GroupDefinitions module Definitions
ALL = {} of Path => Object ALL = {} of Path => Object
MAPPING = {} of String => ExampleGroup MAPPING = {} of String => ExampleGroup

View file

@ -11,21 +11,21 @@ module Spectator
macro context(what, type = "Context", &block) macro context(what, type = "Context", &block)
{% {%
parent_module = @type parent_module = @type
safe_name = what.id.stringify.chars.map { |c| ::Spectator::GroupDefinitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_") safe_name = what.id.stringify.chars.map { |c| ::Spectator::Definitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_")
module_name = (type.id + safe_name.camelcase).id module_name = (type.id + safe_name.camelcase).id
absolute_module_name = [parent_module, module_name].join("::").id absolute_module_name = [parent_module, module_name].join("::").id
what_arg = what.is_a?(StringLiteral) ? what : what.stringify what_arg = what.is_a?(StringLiteral) ? what : what.stringify
parent_given = ::Spectator::GroupDefinitions::ALL[parent_module.id][:given] parent_given = ::Spectator::Definitions::ALL[parent_module.id][:given]
::Spectator::GroupDefinitions::ALL[absolute_module_name] = { ::Spectator::Definitions::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::GroupDefinitions::MAPPING[{{absolute_module_name.stringify}}] = ::Spectator::Definitions::MAPPING[{{absolute_module_name.stringify}}] =
ExampleGroup.new({{what_arg}}, ::Spectator::GroupDefinitions::MAPPING[{{parent_module.stringify}}]) ExampleGroup.new({{what_arg}}, ::Spectator::Definitions::MAPPING[{{parent_module.stringify}}])
module {{module_name.id}} module {{module_name.id}}
include {{parent_module}} include {{parent_module}}
@ -52,7 +52,7 @@ module Spectator
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
given_vars = ::Spectator::GroupDefinitions::ALL[parent_module.id][:given] given_vars = ::Spectator::Definitions::ALL[parent_module.id][:given]
if given_vars.find { |v| v[:name] == var_name.id } 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
@ -66,7 +66,7 @@ module Spectator
%collection.first %collection.first
end end
\{% ::Spectator::GroupDefinitions::ALL[@type.id][:given] << {name: "{{var_name}}".id, collection: "{{collection}}".id, type_def: (@type.id + ".{{var_name}}").id} %} \{% ::Spectator::Definitions::ALL[@type.id][:given] << {name: "{{var_name}}".id, collection: "{{collection}}".id, type_def: (@type.id + ".{{var_name}}").id} %}
{{block.body}} {{block.body}}
end end
@ -99,23 +99,23 @@ module Spectator
end end
macro before_all(&block) macro before_all(&block)
::Spectator::GroupDefinitions::MAPPING[{{@type.stringify}}].before_all_hooks << -> {{block}} ::Spectator::Definitions::MAPPING[{{@type.stringify}}].before_all_hooks << -> {{block}}
end end
macro before_each(&block) macro before_each(&block)
::Spectator::GroupDefinitions::MAPPING[{{@type.stringify}}].before_each_hooks << -> {{block}} ::Spectator::Definitions::MAPPING[{{@type.stringify}}].before_each_hooks << -> {{block}}
end end
macro after_all(&block) macro after_all(&block)
::Spectator::GroupDefinitions::MAPPING[{{@type.stringify}}].after_all_hooks << -> {{block}} ::Spectator::Definitions::MAPPING[{{@type.stringify}}].after_all_hooks << -> {{block}}
end end
macro after_each(&block) macro after_each(&block)
::Spectator::GroupDefinitions::MAPPING[{{@type.stringify}}].after_each_hooks << -> {{block}} ::Spectator::Definitions::MAPPING[{{@type.stringify}}].after_each_hooks << -> {{block}}
end end
macro around_each(&block) macro around_each(&block)
::Spectator::GroupDefinitions::MAPPING[{{@type.stringify}}].around_each_hooks << Proc(Proc(Nil), Nil).new {{block}} ::Spectator::Definitions::MAPPING[{{@type.stringify}}].around_each_hooks << Proc(Proc(Nil), Nil).new {{block}}
end end
def include_examples def include_examples
@ -125,9 +125,9 @@ module Spectator
macro it(description, &block) macro it(description, &block)
{% {%
parent_module = @type parent_module = @type
safe_name = description.id.stringify.chars.map { |c| ::Spectator::GroupDefinitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_") safe_name = description.id.stringify.chars.map { |c| ::Spectator::Definitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_")
class_name = (safe_name.camelcase + "Example").id class_name = (safe_name.camelcase + "Example").id
given_vars = ::Spectator::GroupDefinitions::ALL[parent_module.id][:given] given_vars = ::Spectator::Definitions::ALL[parent_module.id][:given]
var_names = given_vars.map { |v| v[:name] } var_names = given_vars.map { |v| v[:name] }
%} %}
@ -169,7 +169,7 @@ module Spectator
end end
end end
%current_group = ::Spectator::GroupDefinitions::MAPPING[{{parent_module.stringify}}] %current_group = ::Spectator::Definitions::MAPPING[{{parent_module.stringify}}]
{% for given_var, i in given_vars %} {% for given_var, i in given_vars %}
{% {%
var_name = given_var[:name] var_name = given_var[:name]
@ -186,9 +186,9 @@ module Spectator
macro pending(description, &block) macro pending(description, &block)
{% {%
parent_module = @type parent_module = @type
safe_name = description.id.stringify.chars.map { |c| ::Spectator::GroupDefinitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_") safe_name = description.id.stringify.chars.map { |c| ::Spectator::Definitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_")
class_name = (safe_name.camelcase + "Example").id class_name = (safe_name.camelcase + "Example").id
given_vars = ::Spectator::GroupDefinitions::ALL[parent_module.id][:given] given_vars = ::Spectator::Definitions::ALL[parent_module.id][:given]
var_names = given_vars.map { |v| v[:name] } var_names = given_vars.map { |v| v[:name] }
%} %}
@ -226,7 +226,7 @@ module Spectator
end end
end end
%current_group = ::Spectator::GroupDefinitions::MAPPING[{{parent_module.stringify}}] %current_group = ::Spectator::Definitions::MAPPING[{{parent_module.stringify}}]
{% for given_var, i in given_vars %} {% for given_var, i in given_vars %}
{% {%
var_name = given_var[:name] var_name = given_var[:name]

View file

@ -1,15 +1,15 @@
require "./dsl" require "./dsl"
require "./group_definitions" require "./definitions"
module Spectator module Spectator
module Examples module Examples
include ::Spectator::DSL::StructureDSL include ::Spectator::DSL::StructureDSL
{% ::Spectator::GroupDefinitions::ALL[@type.id] = { {% ::Spectator::Definitions::ALL[@type.id] = {
name: "ROOT", name: "ROOT",
parent: nil, parent: nil,
given: [] of Object given: [] of Object
} %} } %}
::Spectator::GroupDefinitions::MAPPING[{{@type.stringify}}] = ::Spectator::ExampleGroup::ROOT ::Spectator::Definitions::MAPPING[{{@type.stringify}}] = ::Spectator::ExampleGroup::ROOT
end end
end end