Add random sample

This commit is contained in:
Michael Miller 2019-09-26 15:49:06 -06:00
parent 3d37a94aaf
commit 6ae817d4bc
1 changed files with 36 additions and 0 deletions

View File

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