class Expected::Matchers::HaveAttrReaderMatcher

Class used by {#have_constant}

Attributes

attribute[R]
subject[R]

Public Class Methods

new(attribute) click to toggle source

@param attribute [String, Symbol] The attribute the {#subject} is expected to have an attr_reader for

# File lib/expected/matchers/have_attr_reader.rb, line 25
def initialize(attribute)
  unless attribute.is_a?(String) || attribute.is_a?(Symbol)
    raise 'HaveAttrReaderMatcher attribute must be a String or Symbol'
  end
  @attribute = attribute.to_sym
end

Public Instance Methods

description() click to toggle source

@return [String]

# File lib/expected/matchers/have_attr_reader.rb, line 53
def description
  "have_attr_reader: `#{attribute}`"
end
failure_message() click to toggle source

@return [String]

# File lib/expected/matchers/have_attr_reader.rb, line 43
def failure_message
  "Expected #{expectation} (#{@failure})"
end
failure_message_when_negated() click to toggle source

@return [String]

# File lib/expected/matchers/have_attr_reader.rb, line 48
def failure_message_when_negated
  "Did not expect #{expectation}"
end
matches?(subject) click to toggle source

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_attr_reader.rb, line 36
def matches?(subject)
  self.subject = subject
  method? &&
    returns_correct_value?
end

Private Instance Methods

attribute_ivar() click to toggle source

@return [Symbol]

# File lib/expected/matchers/have_attr_reader.rb, line 67
def attribute_ivar
  @attribute_ivar ||= :"@#{attribute}"
end
expectation() click to toggle source

@return String

# File lib/expected/matchers/have_attr_reader.rb, line 96
def expectation
  "<#{@original_subject}> to have attr_reader `#{attribute}`"
end
method?() click to toggle source
# File lib/expected/matchers/have_attr_reader.rb, line 71
def method?
  if subject.respond_to? attribute
    true
  else
    @failure = "no method `#{attribute}`"
    false
  end
end
returns_correct_value?() click to toggle source
# File lib/expected/matchers/have_attr_reader.rb, line 80
def returns_correct_value? # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  has_original_val = subject.instance_variable_defined?(attribute_ivar)
  original_val = subject.instance_variable_get(attribute_ivar)
  test_val = SecureRandom.hex
  subject.instance_variable_set(attribute_ivar, test_val)
  ret = if subject.send(attribute) == test_val
          true
        else
          @failure = "method `#{attribute}` did not return the value of #{attribute_ivar}"
          false
        end
  subject.instance_variable_set(attribute_ivar, original_val) if has_original_val
  ret
end
subject=(subject) click to toggle source

The thing to test against @return [Class, Module]

# File lib/expected/matchers/have_attr_reader.rb, line 61
def subject=(subject)
  @original_subject = subject
  @subject = subject.instance_of?(Class) ? subject.allocate : subject
end