From a28f512ae440cc9633023f18506e8309cda72466 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 19 Sep 2018 20:49:01 -0600 Subject: [PATCH] Some code for repeating examples in given blocks --- src/spectator/dsl/structure_dsl.cr | 34 +++++++++++++++++++--------- src/spectator/given_example_group.cr | 15 ++++++++++++ 2 files changed, 38 insertions(+), 11 deletions(-) create mode 100644 src/spectator/given_example_group.cr diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 9036968..ac1f473 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -27,24 +27,30 @@ module Spectator end macro given(collection, &block) - context({{collection}}, "Given") do + module Given%given + include {{@type.id}} + def {{block.args.empty? ? "value".id : block.args.first}} nil # TODO end - {{block.body}} - - _given_enumerator Collection%collection, %each do + _given_collection Collection%collection, %to_a do {{collection}} end + %collection = Collection%collection.new.%to_a - Collection%collection.new.%each do |%item| - # TODO - end + ::Spectator::Definitions::GROUPS[\{{@type.stringify}}] = + GivenExampleGroup(typeof(%collection.first)).new( + {{collection.stringify}}, + %collection, + ::Spectator::Definitions::GROUPS[{{@type.stringify}}] + ) + + {{block.body}} 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}} include {{@type.id}} @@ -52,9 +58,15 @@ module Spectator {{block.body}} end - def {{each_method_name.id}} - %collection.each do |%item| - yield %item + def %first + %collection.first + end + + def {{to_a_method_name.id}} + Array(typeof(%first)).new.tap do |%array| + %collection.each do |%item| + %array << %item + end end end end diff --git a/src/spectator/given_example_group.cr b/src/spectator/given_example_group.cr new file mode 100644 index 0000000..424b2f8 --- /dev/null +++ b/src/spectator/given_example_group.cr @@ -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