diff --git a/CHANGELOG.md b/CHANGELOG.md index 63ed0f7..6541b7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/spec/spectator/concise_spec.cr b/spec/spectator/concise_spec.cr new file mode 100644 index 0000000..c472a6e --- /dev/null +++ b/spec/spectator/concise_spec.cr @@ -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 diff --git a/src/spectator/dsl/concise.cr b/src/spectator/dsl/concise.cr index bba29f2..a7222ae 100644 --- a/src/spectator/dsl/concise.cr +++ b/src/spectator/dsl/concise.cr @@ -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