Class: Minicrest::HasSize
Overview
Matcher that checks if a value has a specific size.
Works with any object that responds to size: - Strings (character count) - Arrays (element count) - Hashes (key-value pair count) - Sets (element count)
Can be used with an integer for exact size matching, or with another matcher for flexible size checking.
Instance Method Summary collapse
-
#description ⇒ String
Returns a description of what this matcher expects.
-
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
-
#initialize(expected) ⇒ HasSize
constructor
Creates a new size matcher.
-
#matches?(actual) ⇒ Boolean
Checks if actual has the expected size.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
Methods inherited from Matcher
Constructor Details
Instance Method Details
#description ⇒ String
Returns a description of what this matcher expects.
48 49 50 51 52 53 54 |
# File 'lib/minicrest/has_size.rb', line 48 def description if @is_matcher "has size #{@expected.description}" else "has size #{@expected}" end end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/minicrest/has_size.rb', line 60 def (actual) if @is_matcher <<~MSG.chomp expected #{actual.inspect} to have size #{@expected.description}, but had size #{actual.size} MSG else <<~MSG.chomp expected #{actual.inspect} to have size #{@expected}, but had size #{actual.size} MSG end end |
#matches?(actual) ⇒ Boolean
Checks if actual has the expected size.
37 38 39 40 41 42 43 |
# File 'lib/minicrest/has_size.rb', line 37 def matches?(actual) if @is_matcher @expected.matches?(actual.size) else actual.size == @expected end end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/minicrest/has_size.rb', line 76 def (actual) if @is_matcher <<~MSG.chomp expected #{actual.inspect} not to have size #{@expected.description}, but it did MSG else <<~MSG.chomp expected #{actual.inspect} not to have size #{@expected}, but it did MSG end end |