Allow named arguments in provided block

This commit is contained in:
Michael Miller 2021-07-31 10:15:16 -06:00
parent ef1832721c
commit 9a97596b84
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 62 additions and 3 deletions

View file

@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support defining hooks in `Spectator.configure` block. [#21](https://gitlab.com/arctic-fox/spectator/-/issues/21)
- Examples with failures or skipped during execution will report the location of that result. [#57](https://gitlab.com/arctic-fox/spectator/-/issues/57)
- Support custom messages for failed expectations. [#28](https://gitlab.com/arctic-fox/spectator/-/issues/28)
- Allow named arguments and assignments for `provided` (`given`) block.
### Changed
- `around_each` hooks wrap `before_all` and `after_all` hooks. [#12](https://github.com/icy-arctic-fox/spectator/issues/12)

View file

@ -0,0 +1,55 @@
require "../spec_helper"
Spectator.describe Spectator do
context "consice syntax" do
describe "provided group with a single assignment" do
provided x = 42 do
expect(x).to eq(42)
end
end
describe "provided group with multiple assignments" do
provided x = 42, y = 123 do
expect(x).to eq(42)
expect(y).to eq(123)
end
end
describe "provided group with a single named argument" do
provided x: 42 do
expect(x).to eq(42)
end
end
describe "provided group with multiple named arguments" do
provided x: 42, y: 123 do
expect(x).to eq(42)
expect(y).to eq(123)
end
end
describe "provided group with mix of assignments and named arguments" do
provided x = 42, y: 123 do
expect(x).to eq(42)
expect(y).to eq(123)
end
provided x = 42, y = 123, z: 0, foo: "bar" do
expect(x).to eq(42)
expect(y).to eq(123)
expect(z).to eq(0)
expect(foo).to eq("bar")
end
end
describe "provided group with references to other arguments" do
let(foo) { "bar" }
provided x = 3, y: x * 5, baz: foo.sub('r', 'z') do
expect(x).to eq(3)
expect(y).to eq(15)
expect(baz).to eq("baz")
end
end
end
end

View file

@ -18,13 +18,16 @@ module Spectator::DSL
# expect(x).to eq(42)
# end
# ```
macro provided(*assignments, &block)
macro provided(*assignments, **kwargs, &block)
{% raise "Cannot use 'provided' inside of a test block" if @def %}
class Given%given < {{@type.id}}
{% for assignment in assignments %}
let({{assignment.target}}) { {{assignment.value}} }
{% end %}
{% for name, value in kwargs %}
let({{name}}) { {{value}} }
{% end %}
{% if block %}
example {{block}}
@ -36,9 +39,9 @@ module Spectator::DSL
# :ditto:
@[Deprecated("Use `provided` instead.")]
macro given(*assignments, &block)
macro given(*assignments, **kwargs, &block)
{% raise "Cannot use 'given' inside of a test block" if @def %}
provided({{assignments.splat}}) {{block}}
provided({{assignments.splat(",")}} {{kwargs.double_splat}}) {{block}}
end
end
end