@@ -113,6 +113,49 @@ def test_asserts_once
113
113
RUBY
114
114
end
115
115
116
+ def test_does_not_crash_with_mass_assignments
117
+ assert_no_offenses ( <<~RUBY )
118
+ class FooTest < Minitest::Test
119
+ def test_does_not_crash
120
+ _, _ = foo
121
+ _, _ = []
122
+ _, _ = {}
123
+ _, _ = ''
124
+ _, _ = 1
125
+ end
126
+ end
127
+ RUBY
128
+ end
129
+
130
+ def test_all_types_of_assignments_are_understood
131
+ assert_offense ( <<~RUBY )
132
+ class FooTest < Minitest::Test
133
+ def test_all_types_of_assignment
134
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [8/1].
135
+ # lvasgn
136
+ foo = assert_equal(1, 1)
137
+ # ivasgn
138
+ @instance_variable = assert_equal(1, 1)
139
+ # cvasgn
140
+ @@class_variable = assert_equal(1, 1)
141
+ # gvasgn
142
+ $global_variable = assert_equal(1, 1)
143
+ # casgn
144
+ # MyClass::CONSTANT_VALUE = assert_equal(1, 1)
145
+ # masgn
146
+ a, b, c = assert_equal(1, 1)
147
+ a, b, c = [assert_equal(1, 1), assert_equal(1, 1), assert_equal(1, 1)]
148
+ # op_asgn
149
+ counter += assert_equal(1, 1)
150
+ # or_asgn
151
+ result ||= assert_equal(1, 1)
152
+ # and_asgn
153
+ flag &&= assert_equal(1, 1)
154
+ end
155
+ end
156
+ RUBY
157
+ end
158
+
116
159
def test_assignments_are_not_counted_twice
117
160
assert_no_offenses ( <<~RUBY )
118
161
class FooTest < Minitest::Test
@@ -295,6 +338,22 @@ class FooTest < ActiveSupport::TestCase
295
338
RUBY
296
339
end
297
340
341
+ def test_registers_offense_when_multiple_assertions_inside_assigned_conditional
342
+ assert_offense ( <<~RUBY )
343
+ class FooTest < ActiveSupport::TestCase
344
+ test 'something' do
345
+ ^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [2/1].
346
+ _ = if condition
347
+ assert_equal(foo, bar) # 1
348
+ assert_empty(array) # 2
349
+ else
350
+ assert_equal(foo, baz)
351
+ end
352
+ end
353
+ end
354
+ RUBY
355
+ end
356
+
298
357
def test_does_not_register_offense_when_single_assertion_inside_conditional
299
358
assert_no_offenses ( <<~RUBY )
300
359
class FooTest < ActiveSupport::TestCase
@@ -330,6 +389,27 @@ class FooTest < ActiveSupport::TestCase
330
389
RUBY
331
390
end
332
391
392
+ def test_registers_offense_when_multiple_expectations_inside_assigned_case
393
+ assert_offense ( <<~RUBY )
394
+ class FooTest < ActiveSupport::TestCase
395
+ test 'something' do
396
+ ^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [3/1].
397
+ _ = case
398
+ when condition1
399
+ assert_equal(foo, bar)
400
+ assert_empty(array)
401
+ when condition2
402
+ assert_equal(foo, bar) # 1
403
+ assert_empty(array) # 2
404
+ assert_equal(foo, baz) # 3
405
+ else
406
+ assert_equal(foo, zoo)
407
+ end
408
+ end
409
+ end
410
+ RUBY
411
+ end
412
+
333
413
def test_does_not_register_offense_when_single_assertion_inside_case
334
414
assert_no_offenses ( <<~RUBY )
335
415
class FooTest < ActiveSupport::TestCase
@@ -366,6 +446,27 @@ class FooTest < ActiveSupport::TestCase
366
446
RUBY
367
447
end
368
448
449
+ def test_registers_offense_when_multiple_expectations_inside_assigned_pattern_matching
450
+ assert_offense ( <<~RUBY )
451
+ class FooTest < ActiveSupport::TestCase
452
+ test 'something' do
453
+ ^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [3/1].
454
+ _ = case variable
455
+ in pattern1
456
+ assert_equal(foo, bar)
457
+ assert_empty(array)
458
+ in pattern2
459
+ assert_equal(foo, bar) # 1
460
+ assert_empty(array) # 2
461
+ assert_equal(foo, baz) # 3
462
+ else
463
+ assert_equal(foo, zoo)
464
+ end
465
+ end
466
+ end
467
+ RUBY
468
+ end
469
+
369
470
def test_does_not_register_offense_when_single_assertion_inside_pattern_matching
370
471
assert_no_offenses ( <<~RUBY )
371
472
class FooTest < ActiveSupport::TestCase
@@ -399,6 +500,26 @@ class FooTest < ActiveSupport::TestCase
399
500
RUBY
400
501
end
401
502
503
+ def test_registers_offense_when_multiple_expectations_inside_assigned_rescue
504
+ assert_offense ( <<~RUBY )
505
+ class FooTest < ActiveSupport::TestCase
506
+ test 'something' do
507
+ ^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [3/1].
508
+ _ = begin
509
+ do_something
510
+ rescue Foo
511
+ assert_equal(foo, bar)
512
+ assert_empty(array)
513
+ rescue Bar
514
+ assert_equal(foo, bar) # 1
515
+ assert_empty(array) # 2
516
+ assert_equal(foo, baz) # 3
517
+ end
518
+ end
519
+ end
520
+ RUBY
521
+ end
522
+
402
523
def test_does_not_register_offense_when_single_assertion_inside_rescue
403
524
assert_no_offenses ( <<~RUBY )
404
525
class FooTest < ActiveSupport::TestCase
@@ -450,6 +571,86 @@ class FooTest < ActiveSupport::TestCase
450
571
RUBY
451
572
end
452
573
574
+ def test_registers_offense_when_complex_structure_with_assignments_and_multiple_assertions
575
+ configure_max_assertions ( 2 )
576
+
577
+ assert_offense ( <<~RUBY )
578
+ class FooTest < ActiveSupport::TestCase
579
+ test 'something' do
580
+ ^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [4/2].
581
+ _= if condition1
582
+ assert foo
583
+ elsif condition2
584
+ assert foo
585
+ else
586
+ begin
587
+ do_something
588
+ assert foo # 1
589
+ rescue Foo
590
+ assert foo
591
+ assert foo
592
+ rescue Bar
593
+ # noop
594
+ rescue Zoo
595
+ _ = case
596
+ when condition
597
+ assert foo # 2
598
+ assert foo # 3
599
+ assert foo # 4
600
+ else
601
+ assert foo
602
+ assert foo
603
+ end
604
+ else
605
+ assert foo
606
+ end
607
+ end
608
+ end
609
+ end
610
+ RUBY
611
+ end
612
+
613
+ def test_registers_offense_when_complex_multiple_assignment_structure_and_multiple_assertions
614
+ configure_max_assertions ( 2 )
615
+
616
+ assert_offense ( <<~RUBY )
617
+ class FooTest < ActiveSupport::TestCase
618
+ test 'something' do
619
+ ^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [5/2].
620
+ _, _, _ = [
621
+ if condition1
622
+ assert foo
623
+ else
624
+ assert foo
625
+ end,
626
+ begin
627
+ do_something
628
+ assert foo # 1
629
+ rescue Foo
630
+ assert foo
631
+ assert foo
632
+ rescue Bar
633
+ # noop
634
+ rescue Zoo
635
+ _ = case
636
+ when condition
637
+ assert foo # 2
638
+ assert foo # 3
639
+ assert foo # 4
640
+ else
641
+ assert foo
642
+ assert foo
643
+ end
644
+ else
645
+ assert foo
646
+ end,
647
+ assert(foo)
648
+ ]
649
+ end
650
+ end
651
+ RUBY
652
+ end
653
+
453
654
def test_does_not_register_offense_when_complex_structure_with_single_assertion
454
655
assert_no_offenses ( <<~RUBY )
455
656
class FooTest < ActiveSupport::TestCase
0 commit comments