Class: Minicrest::HasSize

Inherits:
Matcher
  • Object
show all
Defined in:
lib/minicrest/has_size.rb

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.

Examples:

Basic usage with integer

has_size(5).matches?('hello')  # => true
has_size(3).matches?([1, 2, 3])  # => true
has_size(2).matches?({a: 1, b: 2})  # => true

With matcher argument

has_size(equals(5)).matches?('hello')  # => true
has_size(never(equals(0))).matches?('hello')  # => true

Instance Method Summary collapse

Methods inherited from Matcher

#&, #|

Constructor Details

#initialize(expected) ⇒ HasSize

Creates a new size matcher.

Parameters:

  • expected (Integer, Matcher)

    the expected size or a matcher for the size



27
28
29
30
31
# File 'lib/minicrest/has_size.rb', line 27

def initialize(expected)
  super()
  @expected = expected
  @is_matcher = expected.is_a?(Matcher)
end

Instance Method Details

#descriptionString

Returns a description of what this matcher expects.

Returns:

  • (String)

    description



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.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    failure message



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/minicrest/has_size.rb', line 60

def failure_message(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.

Parameters:

  • actual (Object)

    the value to check

Returns:

  • (Boolean)

    true if size matches



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.

Parameters:

  • actual (Object)

    the value that was checked

Returns:

  • (String)

    message indicating unexpected size match



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/minicrest/has_size.rb', line 76

def negated_failure_message(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