Class: RSpec::SleepingKingStudios::Matchers::Core::HaveConstantMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::SleepingKingStudios::Matchers::Core::HaveConstantMatcher
- Includes:
- Matchers::Composable
- 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.
Constant Summary
Constants included from Description
Description::DEFAULT_EXPECTED_ITEMS
Instance Attribute Summary
Attributes inherited from BaseMatcher
Instance Method Summary collapse
- #description ⇒ Object
-
#does_not_match?(actual) ⇒ Boolean
Inverse of #matches? method.
-
#failure_message ⇒ Object
Message for when the object does not match, but was expected to.
-
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to.
-
#immutable ⇒ HaveConstantMatcher
(also: #frozen)
Sets a mutability expectation.
-
#immutable? ⇒ Boolean
(also: #frozen?)
True if a mutability expectation is set, otherwise false.
-
#initialize(expected) ⇒ HaveConstantMatcher
constructor
A new instance of HaveConstantMatcher.
-
#matches?(actual) ⇒ Boolean
Checks if the object has a constant :expected.
-
#with(value) ⇒ HaveConstantMatcher
(also: #with_value)
Sets a value expectation.
Constructor Details
#initialize(expected) ⇒ HaveConstantMatcher
Returns a new instance of HaveConstantMatcher.
18 19 20 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 18 def initialize expected @expected = expected.intern end |
Instance Method Details
#description ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 23 def description = "have #{@immutable ? 'immutable ' : ''}constant #{@expected.inspect}" << ' with value ' << value_to_string if @value_set end |
#does_not_match?(actual) ⇒ Boolean
Inverse of #matches? method.
33 34 35 36 37 38 39 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 33 def does_not_match? actual super @errors = {} !has_constant? end |
#failure_message ⇒ Object
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.
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 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 42 def = super = [] if @errors[:does_not_define_constants] << ", but #{@actual.inspect} does not respond to ::const_defined?" return end if @errors[:constant_is_not_defined] << ", but #{@actual.inspect} does not define constant #{@expected.inspect}" return end if hsh = @errors[:value_does_not_match] << "constant #{@expected.inspect} has value #{hsh[:received].inspect}" end if hsh = @errors[:value_is_mutable] << "the value of #{@expected.inspect} was mutable" end unless .empty? tools = SleepingKingStudios::Tools::Toolbelt.instance << ', but ' << tools.array_tools.humanize_list() end end |
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.
81 82 83 84 85 86 87 88 89 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 81 def = super << ", but #{@actual.inspect} defines constant #{@expected.inspect} with "\ "value #{actual.const_get(@expected).inspect}" end |
#immutable ⇒ HaveConstantMatcher Also known as: frozen
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.
98 99 100 101 102 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 98 def immutable @immutable = true self end |
#immutable? ⇒ Boolean Also known as: frozen?
Returns True if a mutability expectation is set, otherwise false.
107 108 109 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 107 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.
120 121 122 123 124 125 126 127 128 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 120 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.
136 137 138 139 140 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb', line 136 def with value @value = value @value_set = true self end |