-
-
Notifications
You must be signed in to change notification settings - Fork 277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support *_includes
assertions in RSpec/Rails/MinitestAssertions
#1779
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,13 +10,15 @@ module Rails | |
# # bad | ||
# assert_equal(a, b) | ||
# assert_equal a, b, "must be equal" | ||
# assert_not_includes a, b | ||
# refute_equal(a, b) | ||
# assert_nil a | ||
# refute_empty(b) | ||
# | ||
# # good | ||
# expect(b).to eq(a) | ||
# expect(b).to(eq(a), "must be equal") | ||
# expect(a).not_to include(b) | ||
# expect(b).not_to eq(a) | ||
# expect(a).to eq(nil) | ||
# expect(a).not_to be_empty | ||
|
@@ -28,11 +30,14 @@ class MinitestAssertions < Base | |
RESTRICT_ON_SEND = %i[ | ||
assert_equal | ||
assert_not_equal | ||
assert_includes | ||
assert_not_includes | ||
assert_nil | ||
assert_not_nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There’s an “a-ha” moment, since all those assert_no_* are from test-unit, not minitest. But I’d say we don’t care much if there’s a corresponding native RSpec expectation/matcher we can suggest using instead. test-unit assertions https://www.rubydoc.info/gems/test-unit/2.3.2/Test/Unit/Assertions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea though also they're defined by Rails which is actually why I was including them: https://github.com/rails/rails/blob/main/activesupport/lib/active_support/test_case.rb#L206 |
||
assert_empty | ||
assert_not_empty | ||
refute_equal | ||
refute_includes | ||
refute_nil | ||
refute_empty | ||
].freeze | ||
|
@@ -42,6 +47,11 @@ class MinitestAssertions < Base | |
(send nil? {:assert_equal :assert_not_equal :refute_equal} $_ $_ $_?) | ||
PATTERN | ||
|
||
# @!method minitest_includes(node) | ||
def_node_matcher :minitest_includes, <<~PATTERN | ||
(send nil? {:assert_includes :assert_not_includes :refute_includes} $_ $_ $_?) | ||
PATTERN | ||
|
||
# @!method minitest_nil(node) | ||
def_node_matcher :minitest_nil, <<~PATTERN | ||
(send nil? {:assert_nil :assert_not_nil :refute_nil} $_ $_?) | ||
|
@@ -52,12 +62,17 @@ class MinitestAssertions < Base | |
(send nil? {:assert_empty :assert_not_empty :refute_empty} $_ $_?) | ||
PATTERN | ||
|
||
def on_send(node) # rubocop:disable Metrics/MethodLength | ||
def on_send(node) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just disabled
(which you could probably take further by having an ASSERTION_CHECKERS array, and then the rest of this rule just iterates over that in a few places to pull through node matchers, restrict sends, |
||
minitest_equal(node) do |expected, actual, failure_message| | ||
on_assertion(node, EqualAssertion.new(expected, actual, | ||
failure_message.first)) | ||
end | ||
|
||
minitest_includes(node) do |collection, expected, failure_message| | ||
on_assertion(node, IncludesAssertion.new(collection, expected, | ||
failure_message.first)) | ||
end | ||
|
||
minitest_nil(node) do |actual, failure_message| | ||
on_assertion(node, NilAssertion.new(actual, | ||
failure_message.first)) | ||
|
@@ -99,6 +114,28 @@ def replaced(node) | |
end | ||
end | ||
|
||
# :nodoc: | ||
class IncludesAssertion | ||
def initialize(collection, expected, fail_message) | ||
@collection = collection | ||
@expected = expected | ||
@fail_message = fail_message | ||
end | ||
|
||
def replaced(node) | ||
a_source = @collection.source | ||
b_source = @expected.source | ||
|
||
runner = node.method?(:assert_includes) ? 'to' : 'not_to' | ||
if @fail_message.nil? | ||
"expect(#{a_source}).#{runner} include(#{b_source})" | ||
else | ||
"expect(#{a_source}).#{runner}(include(#{b_source})," \ | ||
" #{@fail_message.source})" | ||
end | ||
end | ||
end | ||
|
||
# :nodoc: | ||
class NilAssertion | ||
def initialize(actual, fail_message) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it exist?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's defined by Rails: https://github.com/rails/rails/blob/main/activesupport/lib/active_support/test_case.rb#L206