Skip to content
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

Add new AssertMatch and RefuteMatch matchers #49

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

* New cops `AssertMatch` and `RefuteMatch` check for use of `assert_match`/`refute_match` instead of `assert(foo.match(bar))`/`refute(foo.match(bar))`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


## 0.5.1 (2019-12-25)

### Bug fixes
Expand Down
12 changes: 12 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Minitest/AssertEqual:
Enabled: true
VersionAdded: '0.4'

Minitest/AssertMatch:
Description: 'This cop enforces the test to use `assert_match` instead of using `assert(matcher.match(object))`.'
StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#assert-match'
Enabled: true
VersionAdded: '0.6'

Minitest/AssertIncludes:
Description: 'This cop enforces the test to use `assert_includes` instead of using `assert(collection.include?(object))`.'
StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#assert-includes'
Expand Down Expand Up @@ -74,6 +80,12 @@ Minitest/RefuteIncludes:
Enabled: true
VersionAdded: '0.3'

Minitest/RefuteMatch:
Description: 'This cop enforces the test to use `refute_match` instead of using `refute(matcher.match(object))`.'
StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#refute-match'
Enabled: true
VersionAdded: '0.6'

Minitest/RefuteInstanceOf:
Description: 'This cop enforces the test to use `refute_instance_of(Class, object)` over `refute(object.instance_of?(Class))`.'
StyleGuide: 'https://github.com/rubocop-hq/minitest-style-guide#refute-instance-of'
Expand Down
60 changes: 60 additions & 0 deletions lib/rubocop/cop/minitest/assert_match.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Minitest
# This cop enforces the test to use `assert_match`
# instead of using `assert(matcher.match(string))`.
#
# @example
# # bad
# assert(matcher.match(string))
# assert(matcher.match(string), 'the message')
#
# # good
# assert_match(regex, string)
# assert_match(matcher, string, 'the message')
#
class AssertMatch < Cop
include ArgumentRangeHelper

MSG = 'Prefer using `assert_match(%<arguments>s)` over ' \
'`assert(%<receiver>s)`.'

def_node_matcher :assert_with_match, <<~PATTERN
(send nil? :assert $(send $_ :match $_) $...)
PATTERN

def on_send(node)
assert_with_match(node) do
|first_receiver_arg, matcher, actual, rest_receiver_arg|
message = rest_receiver_arg.first
arguments = node_arguments(matcher, actual, message)
receiver = [first_receiver_arg.source, message&.source].compact.join(', ')

offense_message = format(MSG, arguments: arguments, receiver: receiver)

add_offense(node, message: offense_message)
end
end

def autocorrect(node)
lambda do |corrector|
assert_with_match(node) do |_, matcher, actual|
corrector.replace(node.loc.selector, 'assert_match')

replacement = [matcher, actual].map(&:source).join(', ')
corrector.replace(first_argument_range(node), replacement)
end
end
end

private

def node_arguments(matcher, actual, message)
[matcher.source, actual.source, message&.source].compact.join(', ')
end
end
end
end
end
60 changes: 60 additions & 0 deletions lib/rubocop/cop/minitest/refute_match.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Minitest
# This cop enforces the test to use `refute_match`
# instead of using `refute(matcher.match(string))`.
#
# @example
# # bad
# refute(matcher.match(string))
# refute(matcher.match(string), 'the message')
#
# # good
# refute_match(matcher, string)
# refute_match(matcher, string, 'the message')
#
class RefuteMatch < Cop
include ArgumentRangeHelper

MSG = 'Prefer using `refute_match(%<arguments>s)` over ' \
'`refute(%<receiver>s)`.'

def_node_matcher :refute_with_match, <<~PATTERN
(send nil? :refute $(send $_ :match $_) $...)
PATTERN

def on_send(node)
refute_with_match(node) do
|first_receiver_arg, matcher, actual, rest_receiver_arg|
message = rest_receiver_arg.first
arguments = node_arguments(matcher, actual, message)
receiver = [first_receiver_arg.source, message&.source].compact.join(', ')

offense_message = format(MSG, arguments: arguments, receiver: receiver)

add_offense(node, message: offense_message)
end
end

def autocorrect(node)
lambda do |corrector|
refute_with_match(node) do |_, matcher, actual|
corrector.replace(node.loc.selector, 'refute_match')

replacement = [matcher, actual].map(&:source).join(', ')
corrector.replace(first_argument_range(node), replacement)
end
end
end

private

def node_arguments(matcher, actual, message)
[matcher.source, actual.source, message&.source].compact.join(', ')
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/rubocop/cop/minitest_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
require_relative 'minitest/assert_nil'
require_relative 'minitest/assert_includes'
require_relative 'minitest/assert_instance_of'
require_relative 'minitest/assert_match'
require_relative 'minitest/assert_respond_to'
require_relative 'minitest/assert_truthy'
require_relative 'minitest/refute_empty'
require_relative 'minitest/refute_false'
require_relative 'minitest/refute_equal'
require_relative 'minitest/refute_nil'
require_relative 'minitest/refute_includes'
require_relative 'minitest/refute_match'
require_relative 'minitest/refute_instance_of'
require_relative 'minitest/refute_respond_to'
2 changes: 2 additions & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [Minitest/AssertEqual](cops_minitest.md#minitestassertequal)
* [Minitest/AssertIncludes](cops_minitest.md#minitestassertincludes)
* [Minitest/AssertInstanceOf](cops_minitest.md#minitestassertinstanceof)
* [Minitest/AssertMatch](cops_minitest.md#minitestassertmatch)
* [Minitest/AssertNil](cops_minitest.md#minitestassertnil)
* [Minitest/AssertRespondTo](cops_minitest.md#minitestassertrespondto)
* [Minitest/AssertTruthy](cops_minitest.md#minitestasserttruthy)
Expand All @@ -14,6 +15,7 @@
* [Minitest/RefuteFalse](cops_minitest.md#minitestrefutefalse)
* [Minitest/RefuteIncludes](cops_minitest.md#minitestrefuteincludes)
* [Minitest/RefuteInstanceOf](cops_minitest.md#minitestrefuteinstanceof)
* [Minitest/RefuteMatch](cops_minitest.md#minitestrefutematch)
* [Minitest/RefuteNil](cops_minitest.md#minitestrefutenil)
* [Minitest/RefuteRespondTo](cops_minitest.md#minitestrefuterespondto)

Expand Down
50 changes: 50 additions & 0 deletions manual/cops_minitest.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ assert_instance_of(Class, object, 'the message')

* [https://github.com/rubocop-hq/minitest-style-guide#assert-instance-of](https://github.com/rubocop-hq/minitest-style-guide#assert-instance-of)

## Minitest/AssertMatch

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.6 | -

This cop enforces the test to use `assert_match`
instead of using `assert(matcher.match(string))`.

### Examples

```ruby
# bad
assert(matcher.match(string))
assert(matcher.match(string), 'the message')

# good
assert_match(regex, string)
assert_match(matcher, string, 'the message')
```

### References

* [https://github.com/rubocop-hq/minitest-style-guide#assert-match](https://github.com/rubocop-hq/minitest-style-guide#assert-match)

## Minitest/AssertNil

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
Expand Down Expand Up @@ -319,6 +344,31 @@ refute_instance_of(Class, object, 'the message')

* [https://github.com/rubocop-hq/minitest-style-guide#refute-instance-of](https://github.com/rubocop-hq/minitest-style-guide#refute-instance-of)

## Minitest/RefuteMatch

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | Yes | 0.6 | -

This cop enforces the test to use `refute_match`
instead of using `refute(matcher.match(string))`.

### Examples

```ruby
# bad
refute(matcher.match(string))
refute(matcher.match(string), 'the message')

# good
refute_match(matcher, string)
refute_match(matcher, string, 'the message')
```

### References

* [https://github.com/rubocop-hq/minitest-style-guide#refute-match](https://github.com/rubocop-hq/minitest-style-guide#refute-match)

## Minitest/RefuteNil

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
Expand Down
82 changes: 82 additions & 0 deletions test/rubocop/cop/minitest/assert_match_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

require 'test_helper'

class AssertMatchTest < Minitest::Test
def setup
@cop = RuboCop::Cop::Minitest::AssertMatch.new
end

def test_registers_offense_when_using_assert_with_match
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
assert(matcher.match(object))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_match(matcher, object)` over `assert(matcher.match(object))`.
end
end
RUBY

assert_correction(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
assert_match(matcher, object)
end
end
RUBY
end

def test_registers_offense_when_using_assert_with_match_and_message
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
assert(matcher.match(object), 'the message')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_match(matcher, object, 'the message')` over `assert(matcher.match(object), 'the message')`.
end
end
RUBY

assert_correction(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
assert_match(matcher, object, 'the message')
end
end
RUBY
end

def test_registers_offense_when_using_assert_with_match_and_heredoc_message
assert_offense(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
assert(matcher.match(object), <<~MESSAGE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_match(matcher, object, <<~MESSAGE)` over `assert(matcher.match(object), <<~MESSAGE)`.
the message
MESSAGE
)
end
end
RUBY

assert_correction(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
assert_match(matcher, object, <<~MESSAGE
the message
MESSAGE
)
end
end
RUBY
end

def test_does_not_register_offense_when_using_assert_match
assert_no_offenses(<<~RUBY, @cop)
class FooTest < Minitest::Test
def test_do_something
assert_match(matcher, object)
end
end
RUBY
end
end
Loading