Skip to content

Commit ce260d4

Browse files
authored
Merge pull request #1802 from G-Rath/support-kind-of
feat: support "kind_of" assertions in `RSpec/Rails/MinitestAssertions`
2 parents a5d367b + 029e655 commit ce260d4

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Support asserts with messages in `Rspec/BeEmpty`. ([@G-Rath])
77
- Add support for `assert_empty`, `assert_not_empty` and `refute_empty` to `RSpec/Rails/MinitestAssertions`. ([@ydah])
88
- Support correcting some `*_predicate` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
9+
- Support correcting `*_kind_of` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
910
- Support correcting `*_in_delta` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
1011
- Support correcting `*_match` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])
1112
- Support correcting `*_instance_of` assertions in `RSpec/Rails/MinitestAssertions`. ([@G-Rath])

lib/rubocop/cop/rspec_rails/minitest_assertions.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,28 @@ def assertion
8989
end
9090
end
9191

92+
# :nodoc:
93+
class KindOfAssertion < BasicAssertion
94+
MATCHERS = %i[
95+
assert_kind_of
96+
assert_not_kind_of
97+
refute_kind_of
98+
].freeze
99+
100+
# @!method self.minitest_assertion(node)
101+
def_node_matcher 'self.minitest_assertion', <<~PATTERN # rubocop:disable InternalAffairs/NodeMatcherDirective
102+
(send nil? {:assert_kind_of :assert_not_kind_of :refute_kind_of} $_ $_ $_?)
103+
PATTERN
104+
105+
def self.match(expected, actual, failure_message)
106+
new(expected, actual, failure_message.first)
107+
end
108+
109+
def assertion
110+
"be_a_kind_of(#{expected})"
111+
end
112+
end
113+
92114
# :nodoc:
93115
class InstanceOfAssertion < BasicAssertion
94116
MATCHERS = %i[

spec/rubocop/cop/rspec_rails/minitest_assertions_spec.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,107 @@
8484
end
8585
end
8686

87+
context 'with kind_of assertions' do
88+
it 'registers an offense when using `assert_kind_of`' do
89+
expect_offense(<<~RUBY)
90+
assert_kind_of(a, b)
91+
^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to be_a_kind_of(a)`.
92+
RUBY
93+
94+
expect_correction(<<~RUBY)
95+
expect(b).to be_a_kind_of(a)
96+
RUBY
97+
end
98+
99+
it 'registers an offense when using `assert_kind_of` with ' \
100+
'no parentheses' do
101+
expect_offense(<<~RUBY)
102+
assert_kind_of a, b
103+
^^^^^^^^^^^^^^^^^^^ Use `expect(b).to be_a_kind_of(a)`.
104+
RUBY
105+
106+
expect_correction(<<~RUBY)
107+
expect(b).to be_a_kind_of(a)
108+
RUBY
109+
end
110+
111+
it 'registers an offense when using `assert_kind_of` with ' \
112+
'failure message' do
113+
expect_offense(<<~RUBY)
114+
assert_kind_of a, b, "must be kind of"
115+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).to(be_a_kind_of(a), "must be kind of")`.
116+
RUBY
117+
118+
expect_correction(<<~RUBY)
119+
expect(b).to(be_a_kind_of(a), "must be kind of")
120+
RUBY
121+
end
122+
123+
it 'registers an offense when using `assert_kind_of` with ' \
124+
'multi-line arguments' do
125+
expect_offense(<<~RUBY)
126+
assert_kind_of(a,
127+
^^^^^^^^^^^^^^^^^ Use `expect(b).to(be_a_kind_of(a), "must be kind of")`.
128+
b,
129+
"must be kind of")
130+
RUBY
131+
132+
expect_correction(<<~RUBY)
133+
expect(b).to(be_a_kind_of(a), "must be kind of")
134+
RUBY
135+
end
136+
137+
it 'registers an offense when using `assert_not_kind_of`' do
138+
expect_offense(<<~RUBY)
139+
assert_not_kind_of a, b
140+
^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(b).not_to be_a_kind_of(a)`.
141+
RUBY
142+
143+
expect_correction(<<~RUBY)
144+
expect(b).not_to be_a_kind_of(a)
145+
RUBY
146+
end
147+
148+
it 'registers an offense when using `refute_kind_of`' do
149+
expect_offense(<<~RUBY)
150+
refute_kind_of a, b
151+
^^^^^^^^^^^^^^^^^^^ Use `expect(b).not_to be_a_kind_of(a)`.
152+
RUBY
153+
154+
expect_correction(<<~RUBY)
155+
expect(b).not_to be_a_kind_of(a)
156+
RUBY
157+
end
158+
159+
it 'does not register an offense when ' \
160+
'using `expect(b).to be_a_kind_of(a)`' do
161+
expect_no_offenses(<<~RUBY)
162+
expect(b).to be_a_kind_of(a)
163+
RUBY
164+
end
165+
166+
it 'does not register an offense when ' \
167+
'using `expect(b).not_to be_a_kind_of(a)`' do
168+
expect_no_offenses(<<~RUBY)
169+
expect(b).not_to be_a_kind_of(a)
170+
RUBY
171+
end
172+
173+
it 'does not register an offense when ' \
174+
'using `expect(b).to be_kind_of(a)`' do
175+
expect_no_offenses(<<~RUBY)
176+
expect(b).to be_kind_of(a)
177+
RUBY
178+
end
179+
180+
it 'does not register an offense when ' \
181+
'using `expect(b).not_to be_kind_of(a)`' do
182+
expect_no_offenses(<<~RUBY)
183+
expect(b).not_to be_kind_of(a)
184+
RUBY
185+
end
186+
end
187+
87188
context 'with instance_of assertions' do
88189
it 'registers an offense when using `assert_instance_of`' do
89190
expect_offense(<<~RUBY)

0 commit comments

Comments
 (0)