class RSpec::SleepingKingStudios::Matchers::Core::HaveConstantMatcher
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
Public Class Methods
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 18 def initialize expected @expected = expected.intern end
@param [String, Symbol] expected The name of the constant to check for on
the actual object.
Public Instance Methods
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 23 def description message = "have #{@immutable ? 'immutable ' : ''}constant #{@expected.inspect}" message << ' with value ' << value_to_string if @value_set message end
(see BaseMatcher#description
)
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 33 def does_not_match? actual super @errors = {} !has_constant? end
(see BaseMatcher#does_not_match?
)
RSpec::SleepingKingStudios::Matchers::BaseMatcher#does_not_match?
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 42 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
(see BaseMatcher#failure_message
)
RSpec::SleepingKingStudios::Matchers::BaseMatcher#failure_message
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 81 def failure_message_when_negated message = super message << ", but #{@actual.inspect} defines constant #{@expected.inspect} with "\ "value #{actual.const_get(@expected).inspect}" message end
(see BaseMatcher#failure_message_when_negated
)
RSpec::SleepingKingStudios::Matchers::BaseMatcher#failure_message_when_negated
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 98 def immutable @immutable = true self end
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.
@return [HaveConstantMatcher] self
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 107 def immutable? !!@immutable end
@return [Boolean] True if a mutability expectation is set, otherwise
false.
Source
# 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
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.
@param [Object] actual The object to check.
@return [Boolean] true If the object has a constant :expected and matches
the value and mutability expectations (if any); otherwise false.
RSpec::SleepingKingStudios::Matchers::BaseMatcher#matches?
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 136 def with value @value = value @value_set = true self end
Sets a value expectation. The matcher will compare the value of the constant with the specified value.
@param [Object] value The value to compare.
@return [HaveConstantMatcher] self
Private Instance Methods
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 145 def has_constant? unless actual.respond_to?(:const_defined?) @errors[:does_not_define_constants] = true return false end # unless unless actual.const_defined?(@expected) @errors[:constant_is_not_defined] = true return false end # unless true end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 161 def immutable_value? return true unless @immutable actual_value = actual.const_get(@expected) if mutable?(actual_value) @errors[:value_is_mutable] = true return false end # if true end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 175 def matches_constant? filter [ matches_constant_value?, immutable_value? ].send(filter) { |bool| bool } end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 181 def matches_constant_value? return true unless @value_set actual_value = actual.const_get(@expected) match = (@value.respond_to?(:matches?) && @value.respond_to?(:description)) ? @value.matches?(actual_value) : @value == actual_value unless match @errors[:value_does_not_match] = { :expected => @value, :received => actual_value } # end hash return false end # unless true end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 201 def mutable? value case value when nil, false, true, Numeric, Symbol false when Array return true unless value.frozen? value.reduce(false) { |memo, item| memo || mutable?(item) } when Hash return true unless value.frozen? (value.keys + value.values).reduce(false) { |memo, item| memo || mutable?(item) } else !value.frozen? end # case end
Source
# File lib/rspec/sleeping_king_studios/matchers/core/have_constant_matcher.rb, line 223 def value_to_string return @value.description if @value.respond_to?(:matches?) && @value.respond_to?(:description) @value.inspect end
Formats the expected value as a human-readable string. If the value looks like an RSpec
matcher (it responds to :matches? and :description), calls value#description; otherwise calls value#inspect.
@return [String] the value as a human-readable string.