From a95c5bcab7fc3f03fa73ac62baf0f6a9b3dde94d Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 28 Sep 2021 18:22:41 -0600 Subject: [PATCH] Better handling with `be` match against Value types and nil Releates to https://github.com/icy-arctic-fox/spectator/issues/37 --- CHANGELOG.md | 1 + src/spectator/matchers/reference_matcher.cr | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb9c608..174142b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed - Fix usage of `be ===` and `be =~` [#34](https://github.com/icy-arctic-fox/spectator/issues/34) +- Better handling of the `be(nil)` when used with value types. [#37](https://github.com/icy-arctic-fox/spectator/issues/37) ### Changed - Elegantly handle missing/undefined methods with `have_attributes` matcher. diff --git a/src/spectator/matchers/reference_matcher.cr b/src/spectator/matchers/reference_matcher.cr index 8385eb6..2344527 100644 --- a/src/spectator/matchers/reference_matcher.cr +++ b/src/spectator/matchers/reference_matcher.cr @@ -14,7 +14,9 @@ module Spectator::Matchers # Checks whether the matcher is satisifed with the expression given to it. private def match?(actual : Expression(T)) : Bool forall T value = expected.value - if value && value.responds_to?(:same?) + if value.nil? + actual.value.nil? + elsif value.responds_to?(:same?) value.same?(actual.value) else # Value type (struct) comparison.