Skip to content

Commit df566da

Browse files
authored
Merge pull request #331 from koic/support_itblock_in_minitest_cops
Support `it` block parameter in `Minitest` cops
2 parents c226e17 + 2778b15 commit df566da

File tree

9 files changed

+81
-5
lines changed

9 files changed

+81
-5
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ jobs:
9191
run: |
9292
sed -e "/gem 'rubocop', github: 'rubocop\/rubocop'/d" -i Gemfile
9393
cat << EOF > Gemfile.local
94-
gem 'rubocop', '1.72.1' # Specify the oldest supported RuboCop version
94+
gem 'rubocop', '1.75.0' # Specify the oldest supported RuboCop version
9595
EOF
9696
- name: set up Ruby
9797
uses: ruby/setup-ruby@v1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#331](https://github.com/rubocop/rubocop-minitest/pull/331): Support `it` block parameter in `Rails` cops. ([@koic][])

lib/rubocop/cop/minitest/multiple_assertions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def assertions_count_based_on_type(node)
6565
assertions_count_in_branches(node.branches)
6666
when :rescue
6767
assertions_count(node.body) + assertions_count_in_branches(node.branches)
68-
when :block, :numblock
68+
when :block, :numblock, :itblock
6969
assertions_count(node.body)
7070
when *RuboCop::AST::Node::ASSIGNMENTS
7171
assertions_count_in_assignment(node)

lib/rubocop/minitest/assert_offense.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ def registry
230230
end
231231

232232
def ruby_version
233-
# Prism supports parsing Ruby 3.3+.
234-
ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.3 : RuboCop::TargetRuby::DEFAULT_VERSION
233+
# Prism is the default backend parser for Ruby 3.4+.
234+
ENV['PARSER_ENGINE'] == 'parser_prism' ? 3.4 : RuboCop::TargetRuby::DEFAULT_VERSION
235235
end
236236

237237
def parser_engine

rubocop-minitest.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ Gem::Specification.new do |spec|
3434
spec.require_paths = ['lib']
3535

3636
spec.add_dependency 'lint_roller', '~> 1.1'
37-
spec.add_dependency 'rubocop', '>= 1.72.1', '< 2.0'
37+
spec.add_dependency 'rubocop', '>= 1.75.0', '< 2.0'
3838
spec.add_dependency 'rubocop-ast', '>= 1.38.0', '< 2.0'
3939
end

test/rubocop/cop/minitest/assert_predicate_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ def test_do_something
145145
RUBY
146146
end
147147

148+
def test_does_not_register_offense_when_using_assert_with_predicate_method_and_it_parameter
149+
assert_no_offenses(<<~RUBY)
150+
class FooTest < Minitest::Test
151+
def test_do_something
152+
assert([1, 2, 3].any? { some_filter_function it })
153+
end
154+
end
155+
RUBY
156+
end
157+
148158
def test_does_not_raise_error_using_assert_with_block
149159
assert_no_offenses(<<~RUBY)
150160
class FooTest < Minitest::Test

test/rubocop/cop/minitest/multiple_assertions_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ def test_asserts_two_times
4545
RUBY
4646
end
4747

48+
def test_registers_offense_when_multiple_expectations_with_itblock
49+
assert_offense(<<~RUBY)
50+
class FooTest < Minitest::Test
51+
def test_asserts_two_times
52+
^^^^^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [2/1].
53+
assert_something do
54+
assert_equal(it, bar)
55+
end
56+
end
57+
end
58+
RUBY
59+
end
60+
4861
def test_checks_when_inheriting_some_class_and_class_name_ending_with_test
4962
assert_offense(<<~RUBY)
5063
class FooTest < ActiveSupport::TestCase
@@ -268,6 +281,32 @@ class FooTest < ActiveSupport::TestCase
268281
RUBY
269282
end
270283

284+
def test_assignments_with_itblocks_are_counted_correctly
285+
assert_offense(<<~RUBY)
286+
class FooTest < ActiveSupport::TestCase
287+
test "#render errors include stack traces" do
288+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [9/1].
289+
err = assert_raises React::ServerRendering::PrerenderError do
290+
assert_equal it, 1
291+
292+
assert_raises React::ServerRendering::PrerenderError do
293+
assert_equal it, 1
294+
end
295+
296+
_ = assert_raises React::ServerRendering::PrerenderError do
297+
assert_equal it, 1
298+
assert_equal it, 1
299+
end
300+
end
301+
302+
assert_match(/NonExistentComponent/, err.to_s, "it names the component")
303+
304+
assert_match(/\n/, err.to_s, "it includes the multi-line backtrace")
305+
end
306+
end
307+
RUBY
308+
end
309+
271310
def test_does_not_register_offense_when_using_or_assigning_a_value_to_an_object_attribute
272311
assert_no_offenses(<<~RUBY)
273312
class FooTest < Minitest::Test

test/rubocop/cop/minitest/refute_predicate_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,14 @@ def test_do_something
144144
end
145145
RUBY
146146
end
147+
148+
def test_does_not_register_offense_when_using_refute_with_predicate_method_and_it_parameter
149+
assert_no_offenses(<<~RUBY)
150+
class FooTest < Minitest::Test
151+
def test_do_something
152+
refute([1, 2, 3].any? { some_filter_function it })
153+
end
154+
end
155+
RUBY
156+
end
147157
end

test/rubocop/cop/minitest/return_in_test_case_method_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,20 @@ def foo
100100
end
101101
RUBY
102102
end
103+
104+
def test_does_not_register_offense_when_using_return_inside_itblock
105+
assert_no_offenses(<<~RUBY)
106+
class FooTest < Minitest::Test
107+
def test_foo
108+
Foo.class_eval do
109+
it.extend(ClassMethods)
110+
def foo
111+
return 100
112+
end
113+
end
114+
assert_equal 100, Foo.new.foo
115+
end
116+
end
117+
RUBY
118+
end
103119
end

0 commit comments

Comments
 (0)