From 007572f0b85cc29b1e279d77cf21585f08d2832e Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 19 Sep 2018 23:13:43 -0600 Subject: [PATCH] Get rid of #add_examples Added #example_count method to help with array sizing. --- src/spectator/example_group.cr | 26 ++++++++++++++------------ src/spectator/given_example_group.cr | 10 +++++++--- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr index a97542a..d48be08 100644 --- a/src/spectator/example_group.cr +++ b/src/spectator/example_group.cr @@ -30,9 +30,21 @@ module Spectator @children.select { |child| child.is_a?(ExampleGroup) }.map { |child| child.unsafe_as(ExampleGroup) } end + def example_count + @children.sum do |child| + child.is_a?(ExampleFactory) ? 1 : child.example_count + end + end + def all_examples - Array(Example).new.tap do |array| - add_examples(array) + Array(Example).new(example_count).tap do |array| + @children.each do |child| + if child.is_a?(ExampleFactory) + array << child.build + else + array.concat(child.all_examples) + end + end end end @@ -94,15 +106,5 @@ module Spectator private def wrap_proc(inner : Proc(Nil) ->, wrapper : ->) -> { inner.call(wrapper) } end - - def add_examples(array : Array(Example)) - @children.each do |child| - if child.is_a?(ExampleFactory) - array << child.build - else - array.concat(child.all_examples) - end - end - end end end diff --git a/src/spectator/given_example_group.cr b/src/spectator/given_example_group.cr index fb61b5d..f7f8c86 100644 --- a/src/spectator/given_example_group.cr +++ b/src/spectator/given_example_group.cr @@ -8,9 +8,13 @@ module Spectator super(what, parent) end - protected def add_examples(array : Array(Example)) - @collection.each do |value| - examples = super.all_examples + def example_count + super.example_count * @collection.size + end + + def all_examples + Array(Example).new(example_count).tap do |array| + examples = super examples.each do |example| @mapping[example] = value end