2021-06-12 22:23:38 +00:00
|
|
|
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.
|
2021-11-28 22:45:17 +00:00
|
|
|
# The names in the assignments will be available in the example code.
|
2021-06-12 22:23:38 +00:00
|
|
|
#
|
|
|
|
# If the code block is omitted, then the example is skipped (marked as not implemented).
|
|
|
|
#
|
|
|
|
# Tags and metadata cannot be used with this macro.
|
|
|
|
#
|
|
|
|
# ```
|
2021-12-02 02:11:36 +00:00
|
|
|
# provided x = 42, y: 123 do
|
2021-06-12 22:23:38 +00:00
|
|
|
# expect(x).to eq(42)
|
2021-12-02 02:11:36 +00:00
|
|
|
# expect(y).to eq(123)
|
2021-06-12 22:23:38 +00:00
|
|
|
# end
|
|
|
|
# ```
|
2021-12-02 08:57:34 +00:00
|
|
|
macro provided(*assignments, it description = nil, **kwargs, &block)
|
2021-06-12 23:06:43 +00:00
|
|
|
{% raise "Cannot use 'provided' inside of a test block" if @def %}
|
|
|
|
|
2021-06-12 22:23:38 +00:00
|
|
|
class Given%given < {{@type.id}}
|
|
|
|
{% for assignment in assignments %}
|
|
|
|
let({{assignment.target}}) { {{assignment.value}} }
|
|
|
|
{% end %}
|
2021-07-31 16:15:16 +00:00
|
|
|
{% for name, value in kwargs %}
|
2021-12-02 08:57:34 +00:00
|
|
|
let({{name}}) { {{value}} }
|
2021-07-31 16:15:16 +00:00
|
|
|
{% end %}
|
2021-06-12 22:23:38 +00:00
|
|
|
|
|
|
|
{% if block %}
|
2021-12-02 08:57:34 +00:00
|
|
|
{% if description %}
|
2021-12-02 02:11:36 +00:00
|
|
|
example {{description}} {{block}}
|
|
|
|
{% else %}
|
|
|
|
example {{block}}
|
|
|
|
{% end %}
|
2021-06-12 22:23:38 +00:00
|
|
|
{% else %}
|
2021-12-02 08:57:34 +00:00
|
|
|
{% if description %}
|
2021-12-02 02:11:36 +00:00
|
|
|
example {{description}} {{block}}
|
|
|
|
{% else %}
|
|
|
|
example {{assignments.splat.stringify}}
|
|
|
|
{% end %}
|
2021-06-12 22:23:38 +00:00
|
|
|
{% end %}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# :ditto:
|
|
|
|
@[Deprecated("Use `provided` instead.")]
|
2021-07-31 16:15:16 +00:00
|
|
|
macro given(*assignments, **kwargs, &block)
|
2021-06-12 23:06:43 +00:00
|
|
|
{% raise "Cannot use 'given' inside of a test block" if @def %}
|
2021-07-31 16:15:16 +00:00
|
|
|
provided({{assignments.splat(",")}} {{kwargs.double_splat}}) {{block}}
|
2021-06-12 22:23:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|