Class: RSpec::SleepingKingStudios::Matchers::Core::ConstructMatcher

Inherits:
BaseMatcher
  • Object
show all
Includes:
Shared::MatchParameters
Defined in:
lib/rspec/sleeping_king_studios/matchers/core/construct_matcher.rb

Overview

Matcher for checking whether an object can be constructed via #new and #initialize, and the parameters accepted by #initialize.

Since:

  • 1.0.0

Constant Summary

Constants included from Description

Description::DEFAULT_EXPECTED_ITEMS

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual

Instance Method Summary collapse

Methods included from Shared::MatchParameters

#argument, #with, #with_a_block, #with_arbitrary_keywords, #with_keywords, #with_unlimited_arguments

Methods inherited from BaseMatcher

#does_not_match?

Instance Method Details

#descriptionObject

Since:

  • 1.0.0



16
17
18
19
20
21
22
23
24
# File 'lib/rspec/sleeping_king_studios/matchers/core/construct_matcher.rb', line 16

def description
  message = 'construct'

  if method_signature_expectation?
    message << ' ' << method_signature_expectation.description
  end # if

  message
end

#failure_messageObject

Message for when the object does not match, but was expected to. Make sure to always call #matches? first to set up the matcher state.

Since:

  • 1.0.0



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rspec/sleeping_king_studios/matchers/core/construct_matcher.rb', line 81

def failure_message
  message = "expected #{@actual.inspect} to be constructible"

  if @failing_method_reasons.key?(:does_not_respond_to_new)
    message << ", but #{@actual.inspect} does not respond to ::new"
  elsif @failing_method_reasons.key?(:unable_to_create_instance)
    message << ", but was unable to allocate an instance of #{@actual.inspect} with ::allocate or ::new"
  elsif @failing_method_reasons.key?(:constructor_is_not_a_method)
    message <<
      ", but was unable to reflect on constructor because :initialize is not a method on #{@actual.inspect}"
  else
    errors = @failing_method_reasons

    # TODO: Replace this with ", but received arguments did not match "\
    # " method signature:"
    message << " with arguments:\n" << format_errors(errors)
  end # if-elsif-else

  message
end

#failure_message_when_negatedObject

Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.

Since:

  • 1.0.0



103
104
105
106
107
108
109
110
111
# File 'lib/rspec/sleeping_king_studios/matchers/core/construct_matcher.rb', line 103

def failure_message_when_negated
  message = "expected #{@actual.inspect} not to be constructible"

  if method_signature_expectation?
    message << ' ' << method_signature_expectation.description
  end # if

  message
end

#matches?(actual) ⇒ Boolean

Checks if the object responds to :new. If so, allocates an instance and checks the parameters expected by #initialize against the expected parameters, if any.

Parameters:

  • actual (Object)

    The object to check.

Returns:

  • (Boolean)

    True if the object responds to :new and accepts the specified parameters for #initialize; otherwise false.

Since:

  • 1.0.0



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rspec/sleeping_king_studios/matchers/core/construct_matcher.rb', line 34

def matches? actual
  @actual = actual
  @failing_method_reasons = {}

  unless @actual.respond_to?(:new, @include_all)
    @failing_method_reasons = {
      :does_not_respond_to_new => true
    } # end hash

    return false
  end # unless

  instance =
    begin; actual.allocate; rescue NoMethodError; nil; end ||
    begin; actual.new;      rescue StandardError; nil; end

  if instance.nil?
    @failing_method_reasons = {
      :unable_to_create_instance => true
    } # end hash

    return false
  end # unless

  constructor =
    begin; instance.method(:initialize); rescue NameError; nil; end

  unless constructor.is_a?(Method)
    @failing_method_reasons = {
      :constructor_is_not_a_method => true
    } # end hash

    return false
  end # unless

  return true unless method_signature_expectation?

  unless check_method_signature(constructor)
    @failing_method_reasons = method_signature_expectation.errors

    return false
  end # unless

  true
end