Class: RSpec::SleepingKingStudios::Matchers::Core::ConstructMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- RSpec::SleepingKingStudios::Matchers::Core::ConstructMatcher
- 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.
Constant Summary
Constants included from Description
Description::DEFAULT_EXPECTED_ITEMS
Instance Attribute Summary
Attributes inherited from BaseMatcher
Instance Method Summary collapse
- #description ⇒ Object
-
#failure_message ⇒ Object
Message for when the object does not match, but was expected to.
-
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to.
-
#matches?(actual) ⇒ Boolean
Checks if the object responds to :new.
Methods included from Shared::MatchParameters
#argument, #with, #with_a_block, #with_arbitrary_keywords, #with_keywords, #with_unlimited_arguments
Methods inherited from BaseMatcher
Instance Method Details
#description ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/construct_matcher.rb', line 16 def description = 'construct' if method_signature_expectation? << ' ' << method_signature_expectation.description end # if end |
#failure_message ⇒ Object
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.
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 = "expected #{@actual.inspect} to be constructible" if @failing_method_reasons.key?(:does_not_respond_to_new) << ", but #{@actual.inspect} does not respond to ::new" elsif @failing_method_reasons.key?(:unable_to_create_instance) << ", but was unable to allocate an instance of #{@actual.inspect} with ::allocate or ::new" elsif @failing_method_reasons.key?(:constructor_is_not_a_method) << ", 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:" << " with arguments:\n" << format_errors(errors) end # if-elsif-else end |
#failure_message_when_negated ⇒ Object
Message for when the object matches, but was expected not to. Make sure to always call #matches? first to set up the matcher state.
103 104 105 106 107 108 109 110 111 |
# File 'lib/rspec/sleeping_king_studios/matchers/core/construct_matcher.rb', line 103 def = "expected #{@actual.inspect} not to be constructible" if method_signature_expectation? << ' ' << method_signature_expectation.description end # if 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.
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 |