Class: RSpec::SleepingKingStudios::Matchers::Core::HaveConstantMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb

Overview

Matcher for testing whether an object has a specific constant. Includes functionality for testing the value of the constant and whether the constant is immutable.

Since:

  • 2.2.0

Constant Summary

Constants included from Description

Description::DEFAULT_EXPECTED_ITEMS

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ HaveConstantMatcher

Returns a new instance of HaveConstantMatcher.

Parameters:

  • expected (String, Symbol)

    The name of the constant to check for on the actual object.

Since:

  • 2.2.0



16
17
18
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 16

def initialize expected
  @expected = expected.intern
end

Instance Method Details

#descriptionObject

Since:

  • 2.2.0



21
22
23
24
25
26
27
28
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 21

def description
  message =
    "have #{@immutable ? 'immutable ' : ''}constant #{@expected.inspect}"

  message << ' with value ' << value_to_string if @value_set

  message
end

#does_not_match?(actual) ⇒ Boolean

Inverse of #matches? method.

Parameters:

  • actual (Object)

    the object to test against the matcher

Returns:

  • (Boolean)

    false if the object matches, otherwise true

See Also:

Since:

  • 2.2.0



99
100
101
102
103
104
105
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 99

def does_not_match? actual
  super

  @errors = {}

  !has_constant?
end

#failure_messageObject

Message for when the object does not match, but was expected to. Make sure to always call #matches? first to set up the matcher state.

Since:

  • 2.2.0



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 31

def failure_message
  message = super

  messages = []

  if @errors[:does_not_define_constants]
    message <<
      ", but #{@actual.inspect} does not respond to ::const_defined?"

    return message
  end # if

  if @errors[:constant_is_not_defined]
    message <<
      ", but #{@actual.inspect} does not define constant #{@expected.inspect}"

    return message
  end # if

  if hsh = @errors[:value_does_not_match]
    messages <<
      "constant #{@expected.inspect} has value #{hsh[:received].inspect}"
  end # if

  if hsh = @errors[:value_is_mutable]
    messages <<
      "the value of #{@expected.inspect} was mutable"
  end # if

  unless messages.empty?
    tools = ::SleepingKingStudios::Tools::ArrayTools

    message << ', but ' << tools.humanize_list(messages)
  end # unless

  message
end

#failure_message_when_negatedObject

Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.

Since:

  • 2.2.0



70
71
72
73
74
75
76
77
78
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 70

def failure_message_when_negated
  message = super

  message <<
    ", but #{@actual.inspect} defines constant #{@expected.inspect} with "\
    "value #{actual.const_get(@expected).inspect}"

  message
end

#immutableHaveConstantMatcher

Sets a mutability expectation. The matcher will determine whether the value of the constant is mutable. Values of ‘nil`, `false`, `true` are always immutable, as are `Numeric` and `Symbol` primitives. `Array` values must be frozen and all array items must be immutable. `Hash` values must be frozen and all hash keys and values must be immutable.

Returns:

Since:

  • 2.2.0



114
115
116
117
118
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 114

def immutable
  @immutable = true

  self
end

#immutable?Boolean

Returns True if a mutability expectation is set, otherwise false.

Returns:

  • (Boolean)

    True if a mutability expectation is set, otherwise false.

Since:

  • 2.2.0



122
123
124
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 122

def immutable?
  !!@immutable
end

#matches?(actual) ⇒ Boolean

Checks if the object has a constant :expected. Additionally, if a value expectation is set, compares the value of #expected to the specified value and checks the mutability of the constant.

Parameters:

  • actual (Object)

    The object to check.

Returns:

  • (Boolean)

    true If the object has a constant :expected and matches the value and mutability expectations (if any); otherwise false.

Since:

  • 2.2.0



88
89
90
91
92
93
94
95
96
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 88

def matches? actual
  super

  @errors = {}

  return false unless has_constant?

  matches_constant? :all?
end

#with(value) ⇒ HaveConstantMatcher Also known as: with_value

Sets a value expectation. The matcher will compare the value of the constant with the specified value.

Parameters:

  • value (Object)

    The value to compare.

Returns:

Since:

  • 2.2.0



132
133
134
135
136
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 132

def with value
  @value     = value
  @value_set = true
  self
end