class Expected::Matchers::HaveConstantMatcher
Class used by {#have_constant}
Attributes
Public Class Methods
@raise If the provided name is not a {String} or {Symbol} @param name [String, Symbol] The name of the constant
# File lib/expected/matchers/have_constant.rb, line 30 def initialize(name) unless name.is_a?(String) || name.is_a?(Symbol) raise 'HaveConstantMatcher constant name must be a String or Symbol' end @name = name end
Public Instance Methods
@return [String]
# File lib/expected/matchers/have_constant.rb, line 75 def description description = "have_constant: #{name}" description += " of_type => #{options[:type].inspect}" if options.key? :type description += " with_value => #{options[:value].inspect}" if options.key? :value description end
@return [String]
# File lib/expected/matchers/have_constant.rb, line 65 def failure_message "Expected #{expectation} (#{@failure})" end
@return [String]
# File lib/expected/matchers/have_constant.rb, line 70 def failure_message_when_negated "Did not expect #{expectation}" end
Run the test @param subject The thing to test against @return [True] If the test passes @return [False] if the test fails
# File lib/expected/matchers/have_constant.rb, line 57 def matches?(subject) self.subject = subject constant_exists? && correct_type? && correct_value? end
Sets the expected type of the constant's value @param type [Module, Class] The expected type of the constant's value @return [self]
# File lib/expected/matchers/have_constant.rb, line 48 def of_type(type) options[:type] = type self end
Sets the expected value of the constant @param value The expected value of the constant @return [self]
# File lib/expected/matchers/have_constant.rb, line 40 def with_value(value) options[:value] = value self end
Private Instance Methods
Check if the {#subject} has the constant @return Boolean
# File lib/expected/matchers/have_constant.rb, line 97 def constant_exists? if subject.const_defined? name true else @failure = 'missing constant' false end end
Check if the constants value is the correct type, specified from {#of_type} @return Boolean
# File lib/expected/matchers/have_constant.rb, line 108 def correct_type? return true unless options.key? :type value = subject.const_get(name) if value.is_a? options[:type] true else @failure = "type was <#{value.class}>" false end end
Check if the constants value is correct, specified from {#with_value} @return Boolean
# File lib/expected/matchers/have_constant.rb, line 121 def correct_value? return true unless options.key? :value value = subject.const_get(name) if value == options[:value] true else @failure = "value was #{value.inspect}" false end end
@return String
# File lib/expected/matchers/have_constant.rb, line 133 def expectation expectation = "<#{subject}> to have a constant named #{name}" expectation += " with a type of <#{options[:type]}>" if options[:type] expectation += " with a value of #{options[:value].inspect}" if options[:value] expectation end
@return [Hash]
# File lib/expected/matchers/have_constant.rb, line 85 def options @options ||= {}.with_indifferent_access end
The thing to test against @return [Class, Module]
# File lib/expected/matchers/have_constant.rb, line 91 def subject=(subject) @subject = subject.instance_of?(Class) || subject.is_a?(Module) ? subject : subject.class end