Class: Expected::Matchers::HaveConstantMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/expected/matchers/have_constant.rb

Overview

Class used by #have_constant

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ HaveConstantMatcher

Returns a new instance of HaveConstantMatcher.

Parameters:

  • name (String, Symbol)

    The name of the constant

Raises:

  • If the provided name is not a String or Symbol



30
31
32
33
34
35
# 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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/expected/matchers/have_constant.rb', line 26

def name
  @name
end

#subjectObject

Returns the value of attribute subject.



26
27
28
# File 'lib/expected/matchers/have_constant.rb', line 26

def subject
  @subject
end

Instance Method Details

#descriptionString

Returns:

  • (String)


75
76
77
78
79
80
# 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

#failure_messageString

Returns:

  • (String)


65
66
67
# File 'lib/expected/matchers/have_constant.rb', line 65

def failure_message
  "Expected #{expectation} (#{@failure})"
end

#failure_message_when_negatedString

Returns:

  • (String)


70
71
72
# File 'lib/expected/matchers/have_constant.rb', line 70

def failure_message_when_negated
  "Did not expect #{expectation}"
end

#matches?(subject) ⇒ True, False

Run the test

Parameters:

  • subject

    The thing to test against

Returns:

  • (True)

    If the test passes

  • (False)

    if the test fails



57
58
59
60
61
62
# File 'lib/expected/matchers/have_constant.rb', line 57

def matches?(subject)
  self.subject = subject
  constant_exists? &&
    correct_type? &&
    correct_value?
end

#of_type(type) ⇒ self

Sets the expected type of the constant’s value

Parameters:

  • type (Module, Class)

    The expected type of the constant’s value

Returns:

  • (self)


48
49
50
51
# File 'lib/expected/matchers/have_constant.rb', line 48

def of_type(type)
  options[:type] = type
  self
end

#with_value(value) ⇒ self

Sets the expected value of the constant

Parameters:

  • value

    The expected value of the constant

Returns:

  • (self)


40
41
42
43
# File 'lib/expected/matchers/have_constant.rb', line 40

def with_value(value)
  options[:value] = value
  self
end