From 4d81031274d3e4706d5f49e67e190b788a33c11a Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 13 Dec 2021 02:42:02 -0700 Subject: [PATCH] Fix usage of 'expect' outside test block Previously gave weird runtime erorr about mismatched groups. Now correctly produces a compilation error. --- CHANGELOG.md | 3 +++ src/spectator/dsl/expectations.cr | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6676749..bc4010b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). ## [Unreleased] +### Fixed +- Fixed runtime error with `expect` outside of test block - now gives compilation error. + ### Added - Description of a `provided` example can be set by using `it` as an argument. [#69](https://gitlab.com/arctic-fox/spectator/-/issues/69) diff --git a/src/spectator/dsl/expectations.cr b/src/spectator/dsl/expectations.cr index 0878dda..a35a15c 100644 --- a/src/spectator/dsl/expectations.cr +++ b/src/spectator/dsl/expectations.cr @@ -39,6 +39,8 @@ module Spectator::DSL # 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. macro expect(actual) + {% raise "Cannot use 'expect' outside of a test block" unless @def %} + %actual = begin {{actual}} end @@ -85,6 +87,8 @@ module Spectator::DSL # { |__arg0| __arg0.foo } # ``` 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+\./ %} {% method_name = block.body.id.split('.')[1..-1].join('.') %} %block = ::Spectator::Block.new({{"#" + method_name}}) do @@ -108,6 +112,8 @@ module Spectator::DSL # is_expected.to eq("foo") # ``` macro is_expected + {% raise "Cannot use 'is_expected' outside of a test block" unless @def %} + expect(subject) end @@ -134,6 +140,8 @@ module Spectator::DSL # # See also: `#is_not` macro is(expected) + {% raise "Cannot use 'is' outside of a test block" unless @def %} + expect(subject).to(eq({{expected}})) end @@ -160,6 +168,8 @@ module Spectator::DSL # # See also: `#is` macro is_not(expected) + {% raise "Cannot use 'is_not' outside of a test block" unless @def %} + expect(subject).not_to(eq({{expected}})) end