Fix usage of 'expect' outside test block

Previously gave weird runtime erorr about mismatched groups.
Now correctly produces a compilation error.
This commit is contained in:
Michael Miller 2021-12-13 02:42:02 -07:00
parent d9269e17a8
commit 4d81031274
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
2 changed files with 13 additions and 0 deletions

View file

@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
### Fixed
- Fixed runtime error with `expect` outside of test block - now gives compilation error.
### Added ### Added
- Description of a `provided` example can be set by using `it` as an argument. [#69](https://gitlab.com/arctic-fox/spectator/-/issues/69) - Description of a `provided` example can be set by using `it` as an argument. [#69](https://gitlab.com/arctic-fox/spectator/-/issues/69)

View file

@ -39,6 +39,8 @@ module Spectator::DSL
# Where the actual value is returned by the system under test, # Where the actual value is returned by the system under test,
# and the expected value is what the actual value should be to satisfy the condition. # and the expected value is what the actual value should be to satisfy the condition.
macro expect(actual) macro expect(actual)
{% raise "Cannot use 'expect' outside of a test block" unless @def %}
%actual = begin %actual = begin
{{actual}} {{actual}}
end end
@ -85,6 +87,8 @@ module Spectator::DSL
# { |__arg0| __arg0.foo } # { |__arg0| __arg0.foo }
# ``` # ```
macro expect(&block) macro expect(&block)
{% raise "Cannot use 'expect' outside of a test block" unless @def %}
{% if block.args.size == 1 && block.args[0] =~ /^__arg\d+$/ && block.body.is_a?(Call) && block.body.id =~ /^__arg\d+\./ %} {% if block.args.size == 1 && block.args[0] =~ /^__arg\d+$/ && block.body.is_a?(Call) && block.body.id =~ /^__arg\d+\./ %}
{% method_name = block.body.id.split('.')[1..-1].join('.') %} {% method_name = block.body.id.split('.')[1..-1].join('.') %}
%block = ::Spectator::Block.new({{"#" + method_name}}) do %block = ::Spectator::Block.new({{"#" + method_name}}) do
@ -108,6 +112,8 @@ module Spectator::DSL
# is_expected.to eq("foo") # is_expected.to eq("foo")
# ``` # ```
macro is_expected macro is_expected
{% raise "Cannot use 'is_expected' outside of a test block" unless @def %}
expect(subject) expect(subject)
end end
@ -134,6 +140,8 @@ module Spectator::DSL
# #
# See also: `#is_not` # See also: `#is_not`
macro is(expected) macro is(expected)
{% raise "Cannot use 'is' outside of a test block" unless @def %}
expect(subject).to(eq({{expected}})) expect(subject).to(eq({{expected}}))
end end
@ -160,6 +168,8 @@ module Spectator::DSL
# #
# See also: `#is` # See also: `#is`
macro is_not(expected) macro is_not(expected)
{% raise "Cannot use 'is_not' outside of a test block" unless @def %}
expect(subject).not_to(eq({{expected}})) expect(subject).not_to(eq({{expected}}))
end end