Skip to content

Commit d62ddf4

Browse files
author
Doug Edey
committed
Updates for review
1 parent 16f51d0 commit d62ddf4

File tree

58 files changed

+117
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+117
-90
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ Here are the Markdown snippets for the two badges:
9797

9898
## Using this Gem for testing custom cops
9999

100-
You can use this gem to test your own cops, by using the `Rubocop::Minitest::Test` test class, you'll get `assert_offense`, `assert_correction`, and `assert_no_offense` helpers
100+
You can use this gem to test your own cops, by using the `RuboCop::Minitest::Test` test class, you'll get `assert_offense`, `assert_correction`, and `assert_no_offense` helpers
101101

102102
```ruby
103103
104104
require "rubocop/minitest/support"
105105
require "custom_cops/my_cop"
106106
107107
module CustomCops
108-
class MyCopTest < Rubocop::Minitest::Test
108+
class MyCopTest < RuboCop::Minitest::Test
109109
test "registers offense" do
110110
assert_offense(<<~RUBY)
111111
class FooTest < Minitest::Test
@@ -158,9 +158,24 @@ module CustomCops
158158
end
159159
RUBY
160160
end
161+
162+
# You can set the `@cop` attribute to override the auto-detected cop and provide configuration options
163+
test "override cop configuration" do
164+
cop_config = RuboCop::Config.new('Minitest/MultipleAssertions' => { 'Max' => 1 })
165+
@cop = RuboCop::Cop::Minitest::MultipleAssertions.new(cop_config)
166+
167+
assert_offense(<<~RUBY)
168+
class FooTest < Minitest::Test
169+
def test_asserts_twice
170+
^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [2/1].
171+
assert_equal(foo, bar)
172+
assert_empty(array)
173+
end
174+
end
175+
RUBY
176+
end
161177
end
162178
end
163-
164179
```
165180

166181
## Contributing
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* Allow `Rubocop::Minitest::Test` to be used by consumers of the gem to test custom cops.
1+
* Allow `RuboCop::Minitest::Test` to be used by consumers of the gem to test custom cops.

lib/rubocop/minitest/support.rb

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
require 'rubocop/rspec/expect_offense'
1010
require 'rubocop/cop/legacy/corrector'
1111

12-
module Rubocop
12+
module RuboCop
1313
module Minitest
1414
# Minitest base test class that adds `assert_offense` and `assert_no_offenses`
1515
#
@@ -23,36 +23,44 @@ module Minitest
2323
#
2424
# @example Usage
2525
#
26-
# assert_offense(<<~RUBY)
27-
# class FooTest < Minitest::Test
28-
# def test_do_something
29-
# assert_equal(nil, somestuff)
30-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
31-
# end
26+
# class AssertNilTest < RuboCop::Minitest::Test
27+
# test "fails when using assert_equal(nil, ...)" do
28+
# assert_offense(<<~RUBY)
29+
# class FooTest < Minitest::Test
30+
# def test_do_something
31+
# assert_equal(nil, somestuff)
32+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
33+
# end
34+
# end
35+
# RUBY
3236
# end
33-
# RUBY
37+
# end
3438
#
3539
# Autocorrection can be tested using `assert_correction` after
3640
# `assert_offense`.
3741
#
3842
# @example `assert_offense` and `assert_correction`
3943
#
40-
# assert_offense(<<~RUBY)
41-
# class FooTest < Minitest::Test
42-
# def test_do_something
43-
# assert_equal(nil, somestuff)
44-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
45-
# end
46-
# end
47-
# RUBY
44+
# class AssertNilTest < RuboCop::Minitest::Test
45+
# test "autocorrects when using assert_equal(nil, ...)" do
46+
# assert_offense(<<~RUBY)
47+
# class FooTest < Minitest::Test
48+
# def test_do_something
49+
# assert_equal(nil, somestuff)
50+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
51+
# end
52+
# RUBY
4853
#
49-
# assert_correction(<<~RUBY)
50-
# class FooTest < Minitest::Test
51-
# def test_do_something
52-
# assert_nil(somestuff)
54+
# assert_correction(<<~RUBY)
55+
# class FooTest < Minitest::Test
56+
# def test_do_something
57+
# assert_nil(somestuff)
58+
# end
59+
# end
60+
# RUBY
5361
# end
5462
# end
55-
# RUBY
63+
# end
5664
#
5765
# If you do not want to specify an offense then use the
5866
# companion method `assert_no_offenses`. This method is a much
@@ -65,17 +73,20 @@ module Minitest
6573
#
6674
# @example `assert_offense` and `assert_no_corrections`
6775
#
68-
# assert_offense(<<~RUBY)
69-
# class FooTest < Minitest::Test
70-
# def test_do_something
71-
# assert_equal(nil, somestuff)
72-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
73-
# end
74-
# end
75-
# RUBY
76-
#
77-
# assert_no_corrections
76+
# class AssertNilTest < RuboCop::Minitest::Test
77+
# test "no autocorrections"
78+
# assert_offense(<<~RUBY)
79+
# class FooTest < Minitest::Test
80+
# def test_do_something
81+
# assert_equal(nil, somestuff)
82+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
83+
# end
84+
# end
85+
# RUBY
7886
#
87+
# assert_no_corrections
88+
# end
89+
# end
7990
class Test < ::Minitest::Test
8091
class << self
8192
# Helper to define a test method using a String. Under the hood, it replaces
@@ -193,6 +204,7 @@ def assert_correction(correction, loop: true)
193204
def setup_assertion
194205
RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
195206
RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
207+
raise('Could not autodetect cop.') if @cop.nil?
196208
end
197209

198210
def inspect_source(source, cop, file = nil)

test/project_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative 'test_helper'
44

5-
class ProjectTest < Rubocop::Minitest::Test
5+
class ProjectTest < RuboCop::Minitest::Test
66
def setup
77
@issues = []
88
@bodies = []

test/rubocop/cop/minitest/assert_empty_literal_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative '../../../test_helper'
44

5-
class AssertEmptyLiteralTest < Rubocop::Minitest::Test
5+
class AssertEmptyLiteralTest < RuboCop::Minitest::Test
66
def test_registers_offense_when_asserting_empty_array
77
assert_offense(<<~RUBY)
88
class FooTest < Minitest::Test

test/rubocop/cop/minitest/assert_empty_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative '../../../test_helper'
44

5-
class AssertEmptyTest < Rubocop::Minitest::Test
5+
class AssertEmptyTest < RuboCop::Minitest::Test
66
def test_registers_offense_when_using_assert_with_empty
77
assert_offense(<<~RUBY)
88
class FooTest < Minitest::Test

test/rubocop/cop/minitest/assert_equal_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative '../../../test_helper'
44

5-
class AssertEqualTest < Rubocop::Minitest::Test
5+
class AssertEqualTest < RuboCop::Minitest::Test
66
def test_registers_offense_when_using_assert_equal_operator_with_string
77
assert_offense(<<~RUBY)
88
class FooTest < Minitest::Test

test/rubocop/cop/minitest/assert_in_delta_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative '../../../test_helper'
44

5-
class AssertInDeltaTest < Rubocop::Minitest::Test
5+
class AssertInDeltaTest < RuboCop::Minitest::Test
66
def test_registers_offense_when_using_assert_equal_with_float_as_expected_value
77
assert_offense(<<~RUBY)
88
class FooTest < Minitest::Test

test/rubocop/cop/minitest/assert_includes_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative '../../../test_helper'
44

5-
class AssertIncludesTest < Rubocop::Minitest::Test
5+
class AssertIncludesTest < RuboCop::Minitest::Test
66
def test_registers_offense_when_using_assert_with_include
77
assert_offense(<<~RUBY)
88
class FooTest < Minitest::Test

test/rubocop/cop/minitest/assert_instance_of_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative '../../../test_helper'
44

5-
class AssertInstanceOfTest < Rubocop::Minitest::Test
5+
class AssertInstanceOfTest < RuboCop::Minitest::Test
66
def test_registers_offense_when_using_assert_with_instance_of
77
assert_offense(<<~RUBY)
88
class FooTest < Minitest::Test

0 commit comments

Comments
 (0)