Reimplement given as provided and deprecate

The behavior is slightly different now.
Nested example blocks aren't allowed in `provided`.
The block produces one example, not multiple.
This commit is contained in:
Michael Miller 2021-06-12 16:23:38 -06:00
parent 71a5c39f6c
commit 704c28e822
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
4 changed files with 46 additions and 2 deletions

View file

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Simplify and reduce defined types and generics. Should speed up compilation times.
- `around_each` hooks wrap `before_all` and `after_all` hooks. [#12](https://github.com/icy-arctic-fox/spectator/issues/12)
- `given` (now `provided`) blocks changed to produce a single example. `it` can no longer be nested in a `provided` block.
- The "should" syntax no longer reports the source as inside Spectator.
- Short-hand "should" syntax must be included by using `require "spectator/should"` - `it { should eq("foo") }`
- Overhaul example creation and handling.
@ -31,9 +32,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated
- `pending` blocks will behave differently in v0.11.0. They will mimic RSpec in that they _compile and run_ the block expecting it to fail. Use a `skip` (or `xit`) block instead to prevent compiling the example.
- `given` has been renamed to `provided`. The `given` keyword may be reused later for memoization.
### Removed
- Removed one-liner it syntax without braces (block).
- Removed one-liner `it`-syntax without braces (block).
## [0.9.38] - 2021-05-27
### Fixed

View file

@ -0,0 +1,41 @@
require "./examples"
require "./groups"
require "./memoize"
module Spectator::DSL
# DSL methods and macros for shorter syntax.
module Concise
# Defines an example and input values in a shorter syntax.
# The only arguments given to this macro are one or more assignments.
# The names in the assigments will be available in the example code.
#
# If the code block is omitted, then the example is skipped (marked as not implemented).
#
# Tags and metadata cannot be used with this macro.
#
# ```
# given x = 42 do
# expect(x).to eq(42)
# end
# ```
macro provided(*assignments, &block)
class Given%given < {{@type.id}}
{% for assignment in assignments %}
let({{assignment.target}}) { {{assignment.value}} }
{% end %}
{% if block %}
example {{block}}
{% else %}
example {{assignments.splat.stringify}}
{% end %}
end
end
# :ditto:
@[Deprecated("Use `provided` instead.")]
macro given(*assignments, &block)
provided({{assignments.splat}}) {{block}}
end
end
end

View file

@ -116,6 +116,6 @@ module Spectator::DSL
define_example_group :xcontext, skip: "Temporarily skipped with xcontext"
# TODO: sample, random_sample, and given
# TODO: sample, random_sample
end
end

View file

@ -8,6 +8,7 @@ require "./tags"
# This type is intentionally outside the `Spectator` module.
# The reason for this is to prevent name collision when using the DSL to define a spec.
class SpectatorTestContext < SpectatorContext
include ::Spectator::DSL::Concise
include ::Spectator::DSL::Examples
include ::Spectator::DSL::Expectations
include ::Spectator::DSL::Groups