class Expected::Matchers::HaveAttrWriterMatcher

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_writer for

# File lib/expected/matchers/have_attr_writer.rb, line 25
def initialize(attribute)
  unless attribute.is_a?(String) || attribute.is_a?(Symbol)
    raise 'HaveAttrWriterMatcher 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_writer.rb, line 53
def description
  "have_attr_writer: `#{attribute}`"
end
failure_message() click to toggle source

@return [String]

# File lib/expected/matchers/have_attr_writer.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_writer.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_writer.rb, line 36
def matches?(subject)
  self.subject = subject
  method? &&
    sets_correct_value?
end

Private Instance Methods

attribute_ivar() click to toggle source

@return [Symbol]

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

@return String

# File lib/expected/matchers/have_attr_writer.rb, line 101
def expectation
  "<#{@original_subject}> to have attr_writer `#{attribute}`"
end
method?() click to toggle source
# File lib/expected/matchers/have_attr_writer.rb, line 76
def method?
  if subject.respond_to? method_name
    true
  else
    @failure = "no method `#{method_name}`"
    false
  end
end
method_name() click to toggle source

@return [Symbol]

# File lib/expected/matchers/have_attr_writer.rb, line 72
def method_name
  @method_name ||= :"#{attribute}="
end
sets_correct_value?() click to toggle source
# File lib/expected/matchers/have_attr_writer.rb, line 85
def sets_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.send(method_name, test_val)
  ret = if subject.instance_variable_get(attribute_ivar) == test_val
          true
        else
          @failure = "method `#{method_name}` did not set 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_writer.rb, line 61
def subject=(subject)
  @original_subject = subject
  @subject = subject.instance_of?(Class) ? subject.allocate : subject
end