Handle sample count

This commit is contained in:
Michael Miller 2021-07-17 12:49:11 -06:00
parent 76a23131cb
commit 9d72d26630
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
2 changed files with 11 additions and 1 deletions

View file

@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Cleanup and simplify DSL implementation.
- Better error messages and detection when DSL methods are used when they shouldn't (i.e. `describe` inside `it`).
- Prevent usage of reserved keywords in DSL (such as `initialize`).
- The count argument for `sample` and `random_sample` groups must be named (use `count: 5` instead of just `5`).
- Other minor internal improvements and cleanup.
### Deprecated

View file

@ -125,8 +125,11 @@ module Spectator::DSL
# Key-value pairs can also be specified.
# Any falsey items will remove a previously defined tag.
#
# The number of items iterated can be restricted by specifying a *count* argument.
# The first *count* items will be used if specified, otherwise all items will be used.
#
# TODO: Handle string interpolation in example and group names.
macro sample(collection, *tags, **metadata, &block)
macro sample(collection, *tags, count = nil, **metadata, &block)
{% raise "Cannot use 'sample' inside of a test block" if @def %}
class Group%group < {{@type.id}}
@ -136,6 +139,12 @@ module Spectator::DSL
{{collection}}
end
{% if count %}
def self.%collection
previous_def.first({{count}})
end
{% end %}
::Spectator::DSL::Builder.start_iterative_group(
%collection,
{{collection.stringify}},