|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe RuboCop::Cop::Style::MinMax, :config do |
| 4 | + subject(:cop) { described_class.new(config) } |
| 5 | + |
| 6 | + context 'with an array literal containing calls to `#min` and `#max`' do |
| 7 | + context 'when the expression stands alone' do |
| 8 | + it 'registers an offense if the receivers match' do |
| 9 | + expect_offense(<<-RUBY.strip_indent) |
| 10 | + [foo.min, foo.max] |
| 11 | + ^^^^^^^^^^^^^^^^^^ Use `foo.minmax` instead of `[foo.min, foo.max]`. |
| 12 | + RUBY |
| 13 | + end |
| 14 | + |
| 15 | + it 'does not register an offense if the receivers do not match' do |
| 16 | + expect_no_offenses(<<-RUBY.strip_indent) |
| 17 | + [foo.min, bar.max] |
| 18 | + RUBY |
| 19 | + end |
| 20 | + |
| 21 | + it 'does not register an offense if there are additional elements' do |
| 22 | + expect_no_offenses(<<-RUBY.strip_indent) |
| 23 | + [foo.min, foo.baz, foo.max] |
| 24 | + RUBY |
| 25 | + end |
| 26 | + |
| 27 | + it 'auto-corrects an offense to use `#minmax`' do |
| 28 | + corrected = autocorrect_source(<<-RUBY.strip_indent) |
| 29 | + [foo.bar.min, foo.bar.max] |
| 30 | + RUBY |
| 31 | + |
| 32 | + expect(corrected).to eq(<<-RUBY.strip_indent) |
| 33 | + foo.bar.minmax |
| 34 | + RUBY |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'when the expression is used in a parallel assignment' do |
| 39 | + it 'registers an offense if the receivers match' do |
| 40 | + expect_offense(<<-RUBY.strip_indent) |
| 41 | + bar = foo.min, foo.max |
| 42 | + ^^^^^^^^^^^^^^^^ Use `foo.minmax` instead of `foo.min, foo.max`. |
| 43 | + RUBY |
| 44 | + end |
| 45 | + |
| 46 | + it 'does not register an offense if the receivers do not match' do |
| 47 | + expect_no_offenses(<<-RUBY.strip_indent) |
| 48 | + baz = foo.min, bar.max |
| 49 | + RUBY |
| 50 | + end |
| 51 | + |
| 52 | + it 'does not register an offense if there are additional elements' do |
| 53 | + expect_no_offenses(<<-RUBY.strip_indent) |
| 54 | + bar = foo.min, foo.baz, foo.max |
| 55 | + RUBY |
| 56 | + end |
| 57 | + |
| 58 | + it 'auto-corrects an offense to use `#minmax`' do |
| 59 | + corrected = autocorrect_source(<<-RUBY.strip_indent) |
| 60 | + baz = foo.bar.min, foo.bar.max |
| 61 | + RUBY |
| 62 | + |
| 63 | + expect(corrected).to eq(<<-RUBY.strip_indent) |
| 64 | + baz = foo.bar.minmax |
| 65 | + RUBY |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context 'when the expression is used as a return value' do |
| 70 | + it 'registers an offense if the receivers match' do |
| 71 | + expect_offense(<<-RUBY.strip_indent) |
| 72 | + return foo.min, foo.max |
| 73 | + ^^^^^^^^^^^^^^^^ Use `foo.minmax` instead of `foo.min, foo.max`. |
| 74 | + RUBY |
| 75 | + end |
| 76 | + |
| 77 | + it 'does not register an offense if the receivers do not match' do |
| 78 | + expect_no_offenses(<<-RUBY.strip_indent) |
| 79 | + return foo.min, bar.max |
| 80 | + RUBY |
| 81 | + end |
| 82 | + |
| 83 | + it 'does not register an offense if there are additional elements' do |
| 84 | + expect_no_offenses(<<-RUBY.strip_indent) |
| 85 | + return foo.min, foo.baz, foo.max |
| 86 | + RUBY |
| 87 | + end |
| 88 | + |
| 89 | + it 'auto-corrects an offense to use `#minmax`' do |
| 90 | + corrected = autocorrect_source(<<-RUBY.strip_indent) |
| 91 | + return foo.bar.min, foo.bar.max |
| 92 | + RUBY |
| 93 | + |
| 94 | + expect(corrected).to eq(<<-RUBY.strip_indent) |
| 95 | + return foo.bar.minmax |
| 96 | + RUBY |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments