From e3576c8924de126b1dc46a97f93a1cef4aab3921 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 19 May 2021 19:46:46 -0600 Subject: [PATCH 1/2] Add support for `be ===` and `be =~` Addresses https://github.com/icy-arctic-fox/spectator/issues/26 --- src/spectator/matchers/pattern_matcher.cr | 41 +++++++++++++++++++++++ src/spectator/matchers/truthy_matcher.cr | 20 +++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/spectator/matchers/pattern_matcher.cr diff --git a/src/spectator/matchers/pattern_matcher.cr b/src/spectator/matchers/pattern_matcher.cr new file mode 100644 index 0000000..c83df1d --- /dev/null +++ b/src/spectator/matchers/pattern_matcher.cr @@ -0,0 +1,41 @@ +require "./value_matcher" + +module Spectator::Matchers + # Common matcher that tests whether a value matches another. + # The values are compared with the === operator. + # This is the same as `CaseMatcher`, but the operands are flipped. + struct PatternMatcher(ExpectedType) < ValueMatcher(ExpectedType) + # Short text about the matcher's purpose. + # This explains what condition satisfies the matcher. + # The description is used when the one-liner syntax is used. + def description : String + "matches #{expected.label}" + end + + # Checks whether the matcher is satisifed with the expression given to it. + private def match?(actual : TestExpression(T)) : Bool forall T + actual.value === expected.value + end + + # Message displayed when the matcher isn't satisifed. + # + # This is only called when `#match?` returns false. + # + # The message should typically only contain the test expression labels. + # Actual values should be returned by `#values`. + private def failure_message(actual) : String + "#{actual.label} does not match #{expected.label}" + end + + # Message displayed when the matcher isn't satisifed and is negated. + # This is essentially what would satisfy the matcher if it wasn't negated. + # + # This is only called when `#does_not_match?` returns false. + # + # The message should typically only contain the test expression labels. + # Actual values should be returned by `#values`. + private def failure_message_when_negated(actual) : String + "#{actual.label} matched #{expected.label}" + end + end +end diff --git a/src/spectator/matchers/truthy_matcher.cr b/src/spectator/matchers/truthy_matcher.cr index bc26a6e..eeecfb9 100644 --- a/src/spectator/matchers/truthy_matcher.cr +++ b/src/spectator/matchers/truthy_matcher.cr @@ -82,6 +82,26 @@ module Spectator::Matchers InequalityMatcher.new(expected) end + # Creates a matcher that checks if a value is semantically equal to an expected value. + # The spec would look like: + # ``` + # expect("foobar").to be === /foo/ + # ``` + def ===(value) + expected = TestValue.new(value) + PatternMatcher.new(expected) + end + + # Creates a matcher that checks if a value matches the pattern of an expected value. + # The spec would look like: + # ``` + # expect("foobar").to be =~ /foo/ + # ``` + def =~(value) + expected = TestValue.new(value) + PatternMatcher.new(expected) + end + # Checks whether the matcher is satisifed with the expression given to it. private def match?(actual : TestExpression(T)) : Bool forall T @truthy == !!actual.value From 55398709def41b2dbf2aecaf7e413422f47364d1 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 19 May 2021 19:51:08 -0600 Subject: [PATCH 2/2] Bump version to 0.9.37 --- CHANGELOG.md | 7 ++++++- README.md | 2 +- shard.yml | 2 +- src/spectator.cr | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd81cf4..7d62b6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. 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). +## [0.9.37] - 2021-05-19 +### Added +- Added support for `be ===` and `be =~`. [#26](https://github.com/icy-arctic-fox/spectator/issues/26) + ## [0.9.36] - 2021-04-22 ### Fixed - Remove old workaround that prevented compilation on Windows. [#58](https://gitlab.com/arctic-fox/spectator/-/issues/58) @@ -257,7 +261,8 @@ This has been changed so that it compiles and raises an error at runtime with a First version ready for public use. -[Unreleased]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.36...HEAD +[Unreleased]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.37...release%2F0.10 +[0.9.36]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.36...v0.9.37 [0.9.35]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.35...v0.9.36 [0.9.35]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.34...v0.9.35 [0.9.34]: https://gitlab.com/arctic-fox/spectator/-/compare/v0.9.33...v0.9.34 diff --git a/README.md b/README.md index 082c951..11ba066 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Add this to your application's `shard.yml`: development_dependencies: spectator: gitlab: arctic-fox/spectator - version: ~> 0.9.36 + version: ~> 0.9.37 ``` Usage diff --git a/shard.yml b/shard.yml index c545790..e0ab280 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: spectator -version: 0.9.36 +version: 0.9.37 description: | A feature-rich spec testing framework for Crystal with similarities to RSpec. diff --git a/src/spectator.cr b/src/spectator.cr index 3f3619b..73a56d5 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -6,7 +6,7 @@ module Spectator extend self # Current version of the Spectator library. - VERSION = "0.9.36" + VERSION = "0.9.37" # Top-level describe method. # All specs in a file must be wrapped in this call.