diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 55032cf..329c403 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -16,7 +16,7 @@ module Spectator include {{@type.id}} {% if what.is_a?(Path) || what.is_a?(Generic) %} - _described_class {{what}} + _spectator_described_class {{what}} {% end %} ::Spectator::DSL::Builder.start_group( @@ -37,14 +37,10 @@ module Spectator {{collection}} end - def %first - %collection.first - end - @%wrapper : ::Spectator::ValueWrapper def {{block.args.empty? ? "value".id : block.args.first}} - @%wrapper.as(::Spectator::TypedValueWrapper(typeof(%first))).value + @%wrapper.as(::Spectator::TypedValueWrapper(typeof(%collection.first))).value end def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper)) @@ -52,9 +48,7 @@ module Spectator @%wrapper = locals[:%group] end - _given_collection Collection%collection, %to_a do - {{collection}} - end + _spectator_given_collection Collection%collection, %to_a, %collection %to_a = Collection%collection.new.%to_a ::Spectator::DSL::Builder.start_given_group( @@ -69,28 +63,6 @@ module Spectator end end - macro _given_collection(class_name, to_a_method_name, &block) - class {{class_name.id}} - include {{@type.id}} - - def %collection - {{block.body}} - end - - def %first - %collection.first - end - - def {{to_a_method_name.id}} - Array(::Spectator::ValueWrapper).new.tap do |%array| - %collection.each do |%item| - %array << ::Spectator::TypedValueWrapper(typeof(%item)).new(%item) - end - end - end - end - end - macro subject(&block) let(:subject) {{block}} end @@ -142,61 +114,21 @@ module Spectator end macro it(description, &block) - class Wrapper%example - include ::Spectator::DSL::ExampleDSL - include {{@type.id}} - - def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper)) - super - end - - def %run - {{block.body}} - end - end - - class Example%example < ::Spectator::RunnableExample - def initialize(group : ::Spectator::ExampleGroup, locals : Hash(Symbol, ::Spectator::ValueWrapper)) - super - @instance = Wrapper%example.new(locals) - end + _spectator_example_wrapper(Wrapper%example, %run) {{block}} + _spectator_example(Example%example, Wrapper%example, ::Spectator::RunnableExample, {{description}}) do protected def run_instance @instance.%run end - - def description - {{description.is_a?(StringLiteral) ? description : description.stringify}} - end end ::Spectator::DSL::Builder.add_example(Example%example) end macro pending(description, &block) - class Wrapper%example - include ::Spectator::DSL::ExampleDSL - include {{@type.id}} + _spectator_example_wrapper(Wrapper%example, %run) {{block}} - def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper)) - super - end - - def %run - {{block.body}} - end - end - - class Example%example < ::Spectator::PendingExample - def initialize(group : ::Spectator::ExampleGroup, locals : Hash(Symbol, ::Spectator::ValueWrapper)) - super - @instance = Wrapper%example.new(locals) - end - - def description - {{description.is_a?(StringLiteral) ? description : description.stringify}} - end - end + _spectator_example(Example%example, Wrapper%example, ::Spectator::PendingExample, {{description}}) ::Spectator::DSL::Builder.add_example(Example%example) end @@ -205,7 +137,7 @@ module Spectator raise NotImplementedError.new("Spectator::DSL#it_behaves_like") end - macro _described_class(what) + macro _spectator_described_class(what) def described_class {{what}}.tap do |thing| raise "#{thing} must be a type name to use #described_class or #subject,\ @@ -213,14 +145,60 @@ module Spectator end end - _implicit_subject + _spectator_implicit_subject end - macro _implicit_subject + macro _spectator_implicit_subject def subject described_class.new end end + + macro _spectator_given_collection(class_name, to_a_method_name, collection_method_name) + class {{class_name.id}} + include {{@type.id}} + + def {{to_a_method_name.id}} + Array(::Spectator::ValueWrapper).new.tap do |array| + {{collection_method_name.id}}.each do |item| + array << ::Spectator::TypedValueWrapper(typeof(item)).new(item) + end + end + end + end + end + + macro _spectator_example_wrapper(class_name, run_method_name, &block) + class {{class_name.id}} + include ::Spectator::DSL::ExampleDSL + include {{@type.id}} + + def initialize(locals : Hash(Symbol, ::Spectator::ValueWrapper)) + super + end + + def {{run_method_name.id}} + {{block.body}} + end + end + end + + macro _spectator_example(example_class_name, wrapper_class_name, base_class, description, &block) + class {{example_class_name.id}} < {{base_class.id}} + def initialize(group : ::Spectator::ExampleGroup, locals : Hash(Symbol, ::Spectator::ValueWrapper)) + super + @instance = {{wrapper_class_name.id}}.new(locals) + end + + {% if block.is_a?(Block) %} + {{block.body}} + {% end %} + + def description + {{description.is_a?(StringLiteral) ? description : description.stringify}} + end + end + end end end end