Skip to content

Commit 949aa1d

Browse files
DougEdeyDoug Edey
authored andcommitted
Make the Rubocop::Minitest::Test suite re-usable by gem consumers
1 parent b446022 commit 949aa1d

File tree

60 files changed

+382
-283
lines changed

Some content is hidden

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

60 files changed

+382
-283
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,89 @@ Here are the Markdown snippets for the two badges:
9595
[![Minitest Style Guide](https://img.shields.io/badge/code_style-community-brightgreen.svg)](https://minitest.rubystyle.guide)
9696
```
9797

98+
## Using this Gem for testing custom cops
99+
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
101+
102+
```ruby
103+
104+
require "rubocop/minitest/support"
105+
require "custom_cops/my_cop"
106+
107+
module CustomCops
108+
class MyCopTest < RuboCop::Minitest::Test
109+
def test_registers_offense
110+
assert_offense(<<~RUBY)
111+
class FooTest < Minitest::Test
112+
def test_do_something
113+
assert_equal(nil, somestuff)
114+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
115+
end
116+
end
117+
RUBY
118+
end
119+
120+
def test_assert_offense_and_correction
121+
assert_offense(<<~RUBY)
122+
class FooTest < Minitest::Test
123+
def test_do_something
124+
assert_equal(nil, somestuff)
125+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
126+
end
127+
end
128+
RUBY
129+
130+
assert_correction(<<~RUBY)
131+
class FooTest < Minitest::Test
132+
def test_do_something
133+
assert_nil(somestuff)
134+
end
135+
end
136+
RUBY
137+
end
138+
139+
def test_assert_offense_and_no_corrections
140+
assert_offense(<<~RUBY)
141+
class FooTest < Minitest::Test
142+
def test_do_something
143+
assert_equal(nil, somestuff)
144+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
145+
end
146+
end
147+
RUBY
148+
149+
assert_no_corrections
150+
end
151+
152+
def test_assert_no_offense
153+
assert_no_offenses(<<~RUBY)
154+
class FooTest < Minitest::Test
155+
def test_do_something
156+
assert_nil(somestuff)
157+
end
158+
end
159+
RUBY
160+
end
161+
162+
# You can set the `@cop` attribute to override the auto-detected cop and provide configuration options
163+
def test_override_cop_configuration
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
177+
end
178+
end
179+
```
180+
98181
## Contributing
99182

100183
Checkout the [contribution guidelines](CONTRIBUTING.md).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#278](https://github.com/rubocop/rubocop-minitest/pull/278): Allow `RuboCop::Minitest::Test` to be used by consumers of the gem to test custom cops. ([@dougedey][])

lib/rubocop/minitest/assert_offense.rb

Lines changed: 0 additions & 224 deletions
This file was deleted.

0 commit comments

Comments
 (0)