Some code for repeating examples in given blocks

This commit is contained in:
Michael Miller 2018-09-19 20:49:01 -06:00
parent 830aa7b43a
commit a28f512ae4
2 changed files with 38 additions and 11 deletions

View file

@ -27,24 +27,30 @@ module Spectator
end end
macro given(collection, &block) macro given(collection, &block)
context({{collection}}, "Given") do module Given%given
include {{@type.id}}
def {{block.args.empty? ? "value".id : block.args.first}} def {{block.args.empty? ? "value".id : block.args.first}}
nil # TODO nil # TODO
end end
{{block.body}} _given_collection Collection%collection, %to_a do
_given_enumerator Collection%collection, %each do
{{collection}} {{collection}}
end end
%collection = Collection%collection.new.%to_a
Collection%collection.new.%each do |%item| ::Spectator::Definitions::GROUPS[\{{@type.stringify}}] =
# TODO GivenExampleGroup(typeof(%collection.first)).new(
end {{collection.stringify}},
%collection,
::Spectator::Definitions::GROUPS[{{@type.stringify}}]
)
{{block.body}}
end end
end end
macro _given_enumerator(class_name, each_method_name, &block) macro _given_collection(class_name, to_a_method_name, &block)
class {{class_name.id}} class {{class_name.id}}
include {{@type.id}} include {{@type.id}}
@ -52,9 +58,15 @@ module Spectator
{{block.body}} {{block.body}}
end end
def {{each_method_name.id}} def %first
%collection.each do |%item| %collection.first
yield %item end
def {{to_a_method_name.id}}
Array(typeof(%first)).new.tap do |%array|
%collection.each do |%item|
%array << %item
end
end end
end end
end end

View file

@ -0,0 +1,15 @@
require "./example_group"
module Spectator
class GivenExampleGroup(T) < ExampleGroup
def initialize(what, @collection : Array(T), parent = nil)
super(what, parent)
end
protected def add_examples(array : Array(Example))
@collection.each do |item|
super(array)
end
end
end
end