From 9ba3fc898be2bfa41ee5eb7eec52ed93238734a9 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 22 Sep 2018 19:41:56 -0600 Subject: [PATCH] More refatoring --- src/spectator.cr | 6 +-- src/spectator/definitions.cr | 39 ------------------- src/spectator/dsl.cr | 7 ++++ src/spectator/dsl/abstract_example_factory.cr | 7 ++++ src/spectator/dsl/builder.cr | 2 +- src/spectator/dsl/example_factory.cr | 11 ++++++ src/spectator/dsl/structure_dsl.cr | 10 ++--- src/spectator/examples.cr | 19 --------- 8 files changed, 31 insertions(+), 70 deletions(-) delete mode 100644 src/spectator/definitions.cr create mode 100644 src/spectator/dsl/abstract_example_factory.cr create mode 100644 src/spectator/dsl/example_factory.cr delete mode 100644 src/spectator/examples.cr diff --git a/src/spectator.cr b/src/spectator.cr index b1e2b2f..2e39c04 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -4,10 +4,8 @@ require "./spectator/*" module Spectator VERSION = "0.1.0" - macro describe(what, source_file = __FILE__, source_line = __LINE__, &block) - module SpectatorExamples - ::Spectator::DSL::StructureDSL.describe({{what}}) {{block}} - end + macro describe(what, &block) + DSL.root({{what}}) {{block}} end at_exit do diff --git a/src/spectator/definitions.cr b/src/spectator/definitions.cr deleted file mode 100644 index 608c72a..0000000 --- a/src/spectator/definitions.cr +++ /dev/null @@ -1,39 +0,0 @@ -module Spectator - module Definitions - ALL = {} of Path => Object - GROUPS = {} of Symbol => ExampleGroup - - SPECIAL_CHARS = { - '~' => "Tilde", - '`' => "Tick", - '!' => "Bang", - '@' => "At", - '#' => "Hash", - '$' => "Dollar", - '%' => "Percent", - '^' => "Carret", - '&' => "And", - '*' => "Star", - '(' => "LParen", - ')' => "RParen", - '+' => "Plus", - '=' => "Eq", - '{' => "LBrace", - '}' => "RBrace", - '[' => "LBracket", - ']' => "RBracket", - ':' => "Colon", - ';' => "SColon", - '<' => "Lt", - '>' => "Gt", - ',' => "Comma", - '.' => "Dot", - '?' => "Question", - '/' => "Slash", - '"' => "DQuote", - '|' => "Or", - '\\' => "BSlash", - '\'' => "SQuote" - } - end -end diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr index 43047f3..63ad5f8 100644 --- a/src/spectator/dsl.cr +++ b/src/spectator/dsl.cr @@ -2,5 +2,12 @@ require "./dsl/*" module Spectator module DSL + macro root(what, &block) + module SpectatorExamples + include StructureDSL + + describe({{what}}) {{block}} + end + end end end diff --git a/src/spectator/dsl/abstract_example_factory.cr b/src/spectator/dsl/abstract_example_factory.cr new file mode 100644 index 0000000..f8199cb --- /dev/null +++ b/src/spectator/dsl/abstract_example_factory.cr @@ -0,0 +1,7 @@ +module Spectator + module DSL + abstract class AbstractExampleFactory + abstract def build(locals : Hash(Symbol, ValueWrapper)) : Example + end + end +end diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index dd69fbb..0180333 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -26,7 +26,7 @@ module Spectator @group_stack.pop end - def add_example(factory : ExampleFactory) : Nil + def add_example(factory : AbstractExampleFactory) : Nil current_group.add_child(factory) end diff --git a/src/spectator/dsl/example_factory.cr b/src/spectator/dsl/example_factory.cr new file mode 100644 index 0000000..46882cc --- /dev/null +++ b/src/spectator/dsl/example_factory.cr @@ -0,0 +1,11 @@ +require "./abstract_example_factory" + +module Spectator + module DSL + class ExampleFactory(T) < AbstractExampleFactory + def build(locals : Hash(Symbol, ValueWrapper)) : Example + T.new(locals) + end + end + end +end diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index d193bdf..bfb11da 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -165,13 +165,9 @@ module Spectator end end - class Factory%example < ::Spectator::ExampleFactory - def build(locals : Hash(Symbol, ValueWrapper)) - Example%example.new(locals) - end - end - - ::Spectator::Definitions::GROUPS[\{{@type.symbolize}}].children << Factory%example.new + ::Spectator::DSL::Builder.add_example_factory( + ::Spectator::DSL::ExampleFactory(Example%example).new + ) end macro pending(description, &block) diff --git a/src/spectator/examples.cr b/src/spectator/examples.cr deleted file mode 100644 index cac73f4..0000000 --- a/src/spectator/examples.cr +++ /dev/null @@ -1,19 +0,0 @@ -require "./dsl" -require "./definitions" - -module Spectator - module Examples - include ::Spectator::DSL::StructureDSL - - {% ::Spectator::Definitions::ALL[@type.id] = { - name: "ROOT", - parent: nil, - given: [] of Object, - children: [] of Object - } %} - ::Spectator::Definitions::GROUPS[{{@type.symbolize}}] = ::Spectator::ExampleGroup::ROOT - - def initialize(locals = {} of Symbol => ValueWrapper) - end - end -end