Fix sample iteration with single block arg (not tuple)

This commit is contained in:
Michael Miller 2022-01-26 13:14:30 -07:00
parent 4e7318b964
commit 92e839415d
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
3 changed files with 43 additions and 3 deletions

View file

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
###
- Fixed usage of `sample` with single block argument. [#41](https://github.com/icy-arctic-fox/spectator/issues/41#issuecomment-1022525702)
## [0.10.4] - 2022-01-11
### Added
- Support string interpolation for example name/description. [#41](https://github.com/icy-arctic-fox/spectator/issues/41)

View file

@ -0,0 +1,30 @@
require "../spec_helper"
Spectator.describe "GitHub Issue #41" do
sample [1, 2, 3] do |i|
it "is itself" do
expect(i).to eq i
end
end
def self.an_array
[1, 2, 3]
end
sample an_array do |i|
it "is itself" do
expect(i).to eq(i)
end
end
# NOTE: NamedTuple does not work, must be Enumerable(T) for `sample`.
def self.a_hash
{:a => "a", :b => "b", :c => "c"}
end
sample a_hash do |k, v|
it "works on hashes" do
expect(v).to eq(k.to_s)
end
end
end

View file

@ -101,9 +101,15 @@ module Spectator::DSL
)
\{% if block %}
\{% for arg, i in block.args %}
let(\{{arg}}) do |example|
example.group.as(::Spectator::ExampleGroupIteration(typeof(Group\%group.\%collection.first))).item[\{{i}}]
\{% if block.args.size > 1 %}
\{% for arg, i in block.args %}
let(\{{arg}}) do |example|
example.group.as(::Spectator::ExampleGroupIteration(typeof(Group\%group.\%collection.first))).item[\{{i}}]
end
\{% end %}
\{% else %}
let(\{{block.args[0]}}) do |example|
example.group.as(::Spectator::ExampleGroupIteration(typeof(Group\%group.\%collection.first))).item
end
\{% end %}