Initial changes for DSL refactor

No examples are picked up right now.
This commit is contained in:
Michael Miller 2018-09-19 18:53:09 -06:00
parent fa73bf3a60
commit 41c0d9abc7

View file

@ -9,70 +9,34 @@ module Spectator
end end
macro context(what, type = "Context", &block) macro context(what, type = "Context", &block)
{% module {{type.id}}%context
parent_module = @type include {{@type.id}}
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
absolute_module_name = [parent_module, module_name].join("::").id
what_arg = what.is_a?(StringLiteral) ? what : what.stringify
parent_def = ::Spectator::Definitions::ALL[parent_module.id]
parent_def[:children] << absolute_module_name
::Spectator::Definitions::ALL[absolute_module_name] = { ::Spectator::Definitions::MAPPING[\{{@type.stringify}}] =
name: module_name, ExampleGroup.new(
parent: parent_module, {{what.is_a?(StringLiteral) ? what : what.stringify}},
given: parent_def[:given].map { |e| e }, # Duplicate elements without dup method. ::Spectator::Definitions::MAPPING[{{@type.stringify}}]
children: [] of Object )
}
%}
::Spectator::Definitions::MAPPING[{{absolute_module_name.stringify}}] = _described_class {{what}}
ExampleGroup.new({{what_arg}}, ::Spectator::Definitions::MAPPING[{{parent_module.stringify}}])
module {{module_name.id}}
include {{parent_module}}
{% if what.is_a?(Path) || what.is_a?(Generic) %}
def described_class
{{what}}.tap do |thing|
raise "#{thing} must be a type name to use #described_class or #subject,\
but it is a #{typeof(thing)}" unless thing.is_a?(Class)
end
end
def subject
described_class.new
end
{% end %}
{{block.body}} {{block.body}}
end end
end end
macro given(collection, &block) macro given(collection, &block)
{% parent_module = @type %}
context({{collection}}, "Given") do context({{collection}}, "Given") do
{% def %collection
var_name = block.args.empty? ? "value".id : block.args.first
given_vars = ::Spectator::Definitions::ALL[parent_module.id][:given]
if given_vars.find { |v| v[:name] == var_name.id }
raise "Duplicate given variable name \"#{var_name.id}\""
end
%}
def self.%collection
{{collection}} {{collection}}
end end
def self.%first def %first
%collection.first %collection.first
end end
\{% ::Spectator::Definitions::ALL[@type.id][:given] << { def {{block.args.empty? ? "value".id : block.args.first}}
name: "{{var_name}}".id, nil # TODO
collection: "{{collection}}".id, end
type_def: (@type.id + '.' + :%first.stringify[1..-1]).id
} %}
{{block.body}} {{block.body}}
end end
@ -129,132 +93,51 @@ module Spectator
end end
macro it(description, &block) macro it(description, &block)
{%
parent_module = @type
safe_name = description.id.stringify.chars.map { |c| ::Spectator::Definitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_")
class_name = (safe_name.camelcase + "Example").id
absolute_class_name = [parent_module.id, class_name].join("::").id
parent_def = ::Spectator::Definitions::ALL[parent_module.id]
given_vars = parent_def[:given]
var_names = given_vars.map { |v| v[:name] }
parent_def[:children] << absolute_class_name
%}
class Example%example class Example%example
include ::Spectator::DSL::ExampleDSL include ::Spectator::DSL::ExampleDSL
include {{parent_module}} include {{@type.id}}
def %run({{ var_names.join(", ").id }}) def %run
{{block.body}} {{block.body}}
end end
end end
class {{class_name.id}} < ::Spectator::RunnableExample class Wrapper%wrapper < ::Spectator::RunnableExample
{% for given_var, i in given_vars %}
@%var{i} : ValueWrapper
private def %var{i}
@%var{i}.unsafe_as(TypedValueWrapper(typeof({{given_var[:type_def]}}))).value
end
{% end %}
def initialize(group{% for v, i in var_names %}, %var{i}{% end %})
super(group)
{% for given_var, i in given_vars %}
@%var{i} = TypedValueWrapper(typeof({{given_var[:type_def]}})).new(%var{i})
{% end %}
end
protected def run_instance protected def run_instance
Example%example.new.%run({% for v, i in var_names %}%var{i}{% if i < var_names.size - 1 %}, {% end %}{% end %}) Example%example.new.%run
end end
def description def description
{% if description.is_a?(StringLiteral) %} {{description.is_a?(StringLiteral) ? description : description.stringify}}
{{description}}
{% else %}
{{description.stringify}}
{% end %}
end end
end end
%current_group = ::Spectator::Definitions::MAPPING[{{parent_module.stringify}}]
{% for given_var, i in given_vars %}
{%
var_name = given_var[:name]
collection = given_var[:collection]
%}
{{collection}}.each do |%var{i}|
{% end %}
%current_group.examples << {{class_name.id}}.new(%current_group {% for v, i in var_names %}, %var{i}{% end %})
{% for given_var in given_vars %}
end
{% end %}
end end
macro pending(description, &block) macro pending(description, &block)
{%
parent_module = @type
safe_name = description.id.stringify.chars.map { |c| ::Spectator::Definitions::SPECIAL_CHARS[c] || c }.join("").gsub(/\W+/, "_")
class_name = (safe_name.camelcase + "Example").id
absolute_class_name = [parent_module.id, class_name].join("::").id
parent_def = ::Spectator::Definitions::ALL[parent_module.id]
given_vars = parent_def[:given]
var_names = given_vars.map { |v| v[:name] }
parent_def[:children] << absolute_class_name
%}
class Example%example
include ::Spectator::DSL::ExampleDSL
include {{parent_module}}
def %run({{ var_names.join(", ").id }})
{{block.body}}
end
end
class {{class_name.id}} < ::Spectator::PendingExample
{% for given_var, i in given_vars %}
@%var{i} : ValueWrapper
private def %var{i}
@%var{i}.unsafe_as(TypedValueWrapper(typeof({{given_var[:type_def]}}))).value
end
{% end %}
def initialize(group{% for v, i in var_names %}, %var{i}{% end %})
super(group)
{% for given_var, i in given_vars %}
@%var{i} = TypedValueWrapper(typeof({{given_var[:type_def]}})).new(%var{i})
{% end %}
end
def description
{% if description.is_a?(StringLiteral) %}
{{description}}
{% else %}
{{description.stringify}}
{% end %}
end
end
%current_group = ::Spectator::Definitions::MAPPING[{{parent_module.stringify}}]
{% for given_var, i in given_vars %}
{%
var_name = given_var[:name]
collection = given_var[:collection]
%}
{{collection}}.each do |%var{i}|
{% end %}
%current_group.examples << {{class_name.id}}.new(%current_group {% for v, i in var_names %}, %var{i}{% end %})
{% for given_var in given_vars %}
end
{% end %}
end end
def it_behaves_like def it_behaves_like
raise NotImplementedError.new("Spectator::DSL#it_behaves_like") raise NotImplementedError.new("Spectator::DSL#it_behaves_like")
end end
macro _described_class(what)
{% if what.is_a?(Path) || what.is_a?(Generic) %}
def described_class
{{what}}.tap do |thing|
raise "#{thing} must be a type name to use #described_class or #subject,\
but it is a #{typeof(thing)}" unless thing.is_a?(Class)
end
end
_implicit_subject
{% end %}
end
macro _implicit_subject
def subject
described_class.new
end
end
end end
end end
end end