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

@ -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