Move tags code to a common location

This commit is contained in:
Michael Miller 2021-01-30 15:34:17 -07:00
parent bd942bb644
commit ba967218fa
No known key found for this signature in database
GPG Key ID: F9A0C5C65B162436
4 changed files with 35 additions and 48 deletions

View File

@ -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"

View File

@ -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

View File

@ -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

26
src/spectator/dsl/tags.cr Normal file
View File

@ -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