Class: Minicrest::Matcher
- Inherits:
-
Object
- Object
- Minicrest::Matcher
- Defined in:
- lib/minicrest/matcher.rb
Overview
Base class for all matchers.
Matchers encapsulate a matching condition and provide descriptive messages for both successful and failed matches.
To create a custom matcher, subclass this and implement: - #matches?(actual) - returns true if the actual value matches - #description - returns a description of what the matcher expects - #failure_message(actual) - returns message when match fails - #negated_failure_message(actual) - returns message when negated match fails
Direct Known Subclasses
All, And, Anything, Between, Blank, CollectionItemMatcher, ComparisonMatcher, Contains, ContainsExactly, DescendsFrom, Empty, Equals, Falsy, HasAttribute, HasKey, HasSize, HasValue, HashEntryMatcher, Includes, InstanceOf, Is, IsCloseTo, IsIn, IsLessThanOrEqualTo, MatchesPattern, NilValue, None, Not, Or, RespondsTo, Some, StringMatcher, Truthy
Instance Method Summary collapse
-
#&(other) ⇒ And
Combines this matcher with another using logical AND.
-
#description ⇒ String
Returns a description of what this matcher expects.
-
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
-
#matches?(actual) ⇒ Boolean
Checks if the actual value matches this matcher’s condition.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
-
#|(other) ⇒ Or
Combines this matcher with another using logical OR.
Instance Method Details
#&(other) ⇒ And
Combines this matcher with another using logical AND.
79 80 81 |
# File 'lib/minicrest/matcher.rb', line 79 def &(other) And.new(self, other) end |
#description ⇒ String
Returns a description of what this matcher expects.
49 50 51 |
# File 'lib/minicrest/matcher.rb', line 49 def description raise NotImplementedError, "#{self.class} must implement #description" end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
57 58 59 60 61 62 |
# File 'lib/minicrest/matcher.rb', line 57 def (actual) <<~MSG.chomp expected #{actual.inspect} to be #{description} MSG end |
#matches?(actual) ⇒ Boolean
Checks if the actual value matches this matcher’s condition.
42 43 44 |
# File 'lib/minicrest/matcher.rb', line 42 def matches?(actual) raise NotImplementedError, "#{self.class} must implement #matches?" end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
68 69 70 71 72 73 |
# File 'lib/minicrest/matcher.rb', line 68 def (actual) <<~MSG.chomp expected #{actual.inspect} not to be #{description} MSG end |