From ba967218faa063d98d598be4d6a0a404ef23c7f5 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 30 Jan 2021 15:34:17 -0700 Subject: [PATCH] Move tags code to a common location --- src/spectator/dsl.cr | 1 + src/spectator/dsl/examples.cr | 26 +++----------------------- src/spectator/dsl/groups.cr | 30 +++++------------------------- src/spectator/dsl/tags.cr | 26 ++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 48 deletions(-) create mode 100644 src/spectator/dsl/tags.cr diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr index e7b4b79..81fc816 100644 --- a/src/spectator/dsl.cr +++ b/src/spectator/dsl.cr @@ -5,6 +5,7 @@ require "./dsl/expectations" require "./dsl/groups" require "./dsl/hooks" require "./dsl/matchers" +require "./dsl/tags" require "./dsl/top" require "./dsl/values" diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index 4798af0..a309901 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -1,10 +1,13 @@ require "../context" require "../source" require "./builder" +require "./tags" module Spectator::DSL # DSL methods for defining examples and test code. module Examples + include Tags + # Defines a macro to generate code for an example. # The *name* is the name given to the macro. # TODO: Mark example as pending if block is omitted. @@ -64,29 +67,6 @@ module Spectator::DSL {% end %} end - # Defines a class method named *name* that combines tags - # returned by *source* with *tags* and *metadata*. - # Any falsey items from *metadata* are removed. - private macro _spectator_tags(name, source, *tags, **metadata) - def self.{{name.id}} - %tags = {{source.id}} - {% unless tags.empty? %} - %tags.concat({ {{tags.map(&.id.symbolize).splat}} }) - {% end %} - {% for k, v in metadata %} - %cond = begin - {{v}} - end - if %cond - %tags.add({{k.id.symbolize}}) - else - %tags.delete({{k.id.symbolize}}) - end - {% end %} - %tags - end - end - define_example :example define_example :it diff --git a/src/spectator/dsl/groups.cr b/src/spectator/dsl/groups.cr index 6f0aef6..20f8a4e 100644 --- a/src/spectator/dsl/groups.cr +++ b/src/spectator/dsl/groups.cr @@ -1,10 +1,13 @@ require "../source" require "./builder" +require "./tags" module Spectator::DSL # DSL methods and macros for creating example groups. # This module should be included as a mix-in. module Groups + include Tags + macro define_example_group(name, *tags, **metadata) # Defines a new example group. # The *what* argument is a name or description of the group. @@ -16,8 +19,8 @@ module Spectator::DSL class Group\%group < \{{@type.id}} _spectator_group_subject(\{{what}}) - _spectator_tags_method(:tags, :super, {{tags.splat(", ")}} {{metadata.double_splat}}) - _spectator_tags_method(:tags, :previous_def, \{{tags.splat(", ")}} \{{metadata.double_splat}}) + _spectator_tags(:tags, :super, {{tags.splat(", ")}} {{metadata.double_splat}}) + _spectator_tags(:tags, :previous_def, \{{tags.splat(", ")}} \{{metadata.double_splat}}) ::Spectator::DSL::Builder.start_group( _spectator_group_name(\{{what}}), @@ -84,29 +87,6 @@ module Spectator::DSL {% end %} end - # Defines a class method named *name* that combines tags - # returned by *source* with *tags* and *metadata*. - # Any falsey items from *metadata* are removed. - private macro _spectator_tags_method(name, source, *tags, **metadata) - def self.{{name.id}} - %tags = {{source.id}} - {% unless tags.empty? %} - %tags.concat({ {{tags.map(&.id.symbolize).splat}} }) - {% end %} - {% for k, v in metadata %} - %cond = begin - {{v}} - end - if %cond - %tags.add({{k.id.symbolize}}) - else - %tags.delete({{k.id.symbolize}}) - end - {% end %} - %tags - end - end - define_example_group :example_group define_example_group :describe diff --git a/src/spectator/dsl/tags.cr b/src/spectator/dsl/tags.cr new file mode 100644 index 0000000..a73c396 --- /dev/null +++ b/src/spectator/dsl/tags.cr @@ -0,0 +1,26 @@ +module Spectator::DSL + module Tags + # Defines a class method named *name* that combines tags + # returned by *source* with *tags* and *metadata*. + # Any falsey items from *metadata* are removed. + private macro _spectator_tags(name, source, *tags, **metadata) + def self.{{name.id}} + %tags = {{source.id}} + {% unless tags.empty? %} + %tags.concat({ {{tags.map(&.id.symbolize).splat}} }) + {% end %} + {% for k, v in metadata %} + %cond = begin + {{v}} + end + if %cond + %tags.add({{k.id.symbolize}}) + else + %tags.delete({{k.id.symbolize}}) + end + {% end %} + %tags + end + end + end +end