invoke_matcher 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/invoke_matcher/version.rb +5 -0
- data/lib/invoke_matcher.rb +115 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bf226105ac69330c4904343286ffd39734097e4ea744de55653eb90923619742
|
4
|
+
data.tar.gz: 28f11ae446664b5e4bee2dacd4d6cf69ebf72d5f350651e0c4f35685dc77cccf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a5b84b12fef7d3b9dad5b9a830396a72a1b5a1bb9eef45d6421c1dfe92f1fcff735e9b043f4a419aee489d74a7417489fd964088a104425a4403dc36d7a7e355
|
7
|
+
data.tar.gz: be4132ca6edce95552a85f46bb102c289f265cc3477eee70799a634e22d25476adcd7ad5a278939c4e816b951d39835090d2138a580e5f376bd6033b71f6280c
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
|
5
|
+
module InvokeMatcher
|
6
|
+
class Matcher
|
7
|
+
extend Forwardable
|
8
|
+
|
9
|
+
include RSpec::Mocks::ExampleMethods
|
10
|
+
include RSpec::Matchers::Composable
|
11
|
+
|
12
|
+
def_delegators :have_received_matcher, :failure_message, :failure_message_when_negated
|
13
|
+
|
14
|
+
attr_reader :have_received_matcher, :expected_recipient
|
15
|
+
|
16
|
+
def initialize(expected_method)
|
17
|
+
@expected_method = expected_method
|
18
|
+
@have_received_matcher = RSpec::Mocks::Matchers::HaveReceived.new(@expected_method)
|
19
|
+
end
|
20
|
+
|
21
|
+
def description
|
22
|
+
"invoke #{@expected_method} on #{expected_recipient.inspect}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def and_call_original
|
26
|
+
@call_original = true
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def method_missing(name, ...)
|
31
|
+
ensure_recipient_is_defined!
|
32
|
+
return super unless respond_to_missing?(name)
|
33
|
+
|
34
|
+
@have_received_matcher = have_received_matcher.public_send(name, ...)
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def respond_to_missing?(name, include_private = false)
|
39
|
+
@have_received_matcher.respond_to?(name, include_private)
|
40
|
+
end
|
41
|
+
|
42
|
+
def matches?(event_proc)
|
43
|
+
ensure_recipient_is_defined!
|
44
|
+
set_method_expectation
|
45
|
+
|
46
|
+
event_proc.call
|
47
|
+
|
48
|
+
have_received_matcher.matches?(expected_recipient) && matches_result?
|
49
|
+
end
|
50
|
+
|
51
|
+
def set_method_expectation
|
52
|
+
return if testing_double?
|
53
|
+
|
54
|
+
@actual_result = expected_recipient.send(@expected_method)
|
55
|
+
expectation = allow(expected_recipient).to receive(@expected_method)
|
56
|
+
expectation.and_call_original if @call_original
|
57
|
+
end
|
58
|
+
|
59
|
+
def testing_double?
|
60
|
+
@expected_recipient.is_a?(RSpec::Mocks::Double) ||
|
61
|
+
@expected_recipient.is_a?(RSpec::Mocks::InstanceVerifyingDouble) ||
|
62
|
+
@expected_recipient.is_a?(RSpec::Mocks::ObjectVerifyingDouble)
|
63
|
+
end
|
64
|
+
|
65
|
+
def does_not_match?(event_proc)
|
66
|
+
ensure_recipient_is_defined!
|
67
|
+
set_method_expectation
|
68
|
+
|
69
|
+
event_proc.call
|
70
|
+
|
71
|
+
have_received_matcher.does_not_match?(expected_recipient)
|
72
|
+
end
|
73
|
+
|
74
|
+
def on(expected_recipient)
|
75
|
+
@expected_recipient = expected_recipient
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
def with(*args, **kwargs)
|
80
|
+
have_received_matcher.with(*args, **kwargs)
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
84
|
+
def supports_block_expectations?
|
85
|
+
true
|
86
|
+
end
|
87
|
+
|
88
|
+
def matches_result?
|
89
|
+
return true unless defined?(@expected_return_value)
|
90
|
+
|
91
|
+
values_match?(@expected_return_value, @actual_result)
|
92
|
+
end
|
93
|
+
|
94
|
+
def and_expect_return(expected_return_value)
|
95
|
+
@expected_return_value = expected_return_value
|
96
|
+
self
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def ensure_recipient_is_defined!
|
102
|
+
raise ArgumentError, "missing `on`" unless defined?(expected_recipient)
|
103
|
+
end
|
104
|
+
|
105
|
+
def call_original(matcher)
|
106
|
+
@call_original ? matcher.and_call_original : matcher
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def invoke(expected_method)
|
111
|
+
InvokeMatcher::Matcher.new(expected_method)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
RSpec::Matchers.define_negated_matcher :not_invoke, :invoke
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: invoke_matcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler Rhodes
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-01-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- rhodetyl000@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/invoke_matcher.rb
|
21
|
+
- lib/invoke_matcher/version.rb
|
22
|
+
homepage: https://github.com/tylerCaineRhodes/invoke_matcher
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata:
|
26
|
+
homepage_uri: https://github.com/tylerCaineRhodes/invoke_matcher
|
27
|
+
source_code_uri: https://github.com/tylerCaineRhodes/invoke_matcher
|
28
|
+
rubygems_mfa_required: 'true'
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.0.0
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubygems_version: 3.0.3.1
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: RSpec matcher for testing method invocations
|
48
|
+
test_files: []
|