Class: Minicrest::IsIn
Overview
Matcher that checks if a value is present in a collection.
Works with: - Arrays: checks if element is in array - Hashes: checks if key is in hash - Strings: checks if substring is in string - Ranges: checks if value is in range
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(collection) ⇒ IsIn
constructor
Creates a new is_in matcher.
-
#matches?(actual) ⇒ Boolean
Checks if actual is in the collection.
-
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
Methods inherited from Matcher
Constructor Details
#initialize(collection) ⇒ IsIn
Creates a new is_in matcher.
27 28 29 30 |
# File 'lib/minicrest/is_in.rb', line 27 def initialize(collection) super() @collection = collection end |
Instance Method Details
#description ⇒ String
Returns a description of what this matcher expects.
47 48 49 |
# File 'lib/minicrest/is_in.rb', line 47 def description "in #{format_collection(@collection)}" end |
#failure_message(actual) ⇒ String
Returns the failure message when the match fails.
55 56 57 58 59 |
# File 'lib/minicrest/is_in.rb', line 55 def (actual) <<~MSG.chomp expected #{actual.inspect} to be in #{format_collection(@collection)} MSG end |
#matches?(actual) ⇒ Boolean
Checks if actual is in the collection.
36 37 38 39 40 41 42 |
# File 'lib/minicrest/is_in.rb', line 36 def matches?(actual) if @collection.is_a?(Hash) @collection.key?(actual) else @collection.include?(actual) end end |
#negated_failure_message(actual) ⇒ String
Returns the failure message when a negated match fails.
65 66 67 68 69 |
# File 'lib/minicrest/is_in.rb', line 65 def (actual) <<~MSG.chomp expected #{actual.inspect} not to be in #{format_collection(@collection)}, but it was MSG end |