From 6ae817d4bc61255bb627cd68f22dc99674e6fff9 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 26 Sep 2019 15:49:06 -0600 Subject: [PATCH] Add random sample --- src/spectator/dsl/groups.cr | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/spectator/dsl/groups.cr b/src/spectator/dsl/groups.cr index 8caa779..5c9e2d1 100644 --- a/src/spectator/dsl/groups.cr +++ b/src/spectator/dsl/groups.cr @@ -56,5 +56,41 @@ module Spectator ::Spectator::SpecBuilder.end_group end end + + macro random_sample(collection, count = nil, &block) + {% name = block.args.empty? ? :value.id : block.args.first.id %} + + def %collection + {{collection}} + end + + def %to_a + {% if count %} + %collection.first({{count}}) + {% else %} + %collection.to_a + {% end %} + end + + class Context%sample < {{@type.id}} + ::Spectator::SpecBuilder.start_sample_group({{collection.stringify}}, :%sample, {{name.stringify}}) do |values| + sample = {{@type.id}}.new(values) + collection = sample.%to_a + {% if count %} + collection.sample({{count}}, ::Spectator.random) + {% else %} + collection.shuffle(::Spectator.random) + {% end %} + end + + def {{name}} + @spectator_test_values.get_value(:%sample, typeof(%to_a.first)) + end + + {{block.body}} + + ::Spectator::SpecBuilder.end_group + end + end end end