|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Style::HashExcept, :config do |
| 4 | + context 'Ruby 3.0 or higher', :ruby30 do |
| 5 | + it 'registers and corrects an offense when using `reject` and comparing with `lvar == :sym`' do |
| 6 | + expect_offense(<<~RUBY) |
| 7 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| k == :bar } |
| 8 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(:bar)` instead. |
| 9 | + RUBY |
| 10 | + |
| 11 | + expect_correction(<<~RUBY) |
| 12 | + {foo: 1, bar: 2, baz: 3}.except(:bar) |
| 13 | + RUBY |
| 14 | + end |
| 15 | + |
| 16 | + it 'registers and corrects an offense when using `reject` and comparing with `:sym == lvar`' do |
| 17 | + expect_offense(<<~RUBY) |
| 18 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| :bar == k } |
| 19 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(:bar)` instead. |
| 20 | + RUBY |
| 21 | + |
| 22 | + expect_correction(<<~RUBY) |
| 23 | + {foo: 1, bar: 2, baz: 3}.except(:bar) |
| 24 | + RUBY |
| 25 | + end |
| 26 | + |
| 27 | + it 'registers and corrects an offense when using `select` and comparing with `lvar != :sym`' do |
| 28 | + expect_offense(<<~RUBY) |
| 29 | + {foo: 1, bar: 2, baz: 3}.select { |k, v| k != :bar } |
| 30 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(:bar)` instead. |
| 31 | + RUBY |
| 32 | + |
| 33 | + expect_correction(<<~RUBY) |
| 34 | + {foo: 1, bar: 2, baz: 3}.except(:bar) |
| 35 | + RUBY |
| 36 | + end |
| 37 | + |
| 38 | + it 'registers and corrects an offense when using `select` and comparing with `:sym != lvar`' do |
| 39 | + expect_offense(<<~RUBY) |
| 40 | + {foo: 1, bar: 2, baz: 3}.select { |k, v| :bar != k } |
| 41 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(:bar)` instead. |
| 42 | + RUBY |
| 43 | + |
| 44 | + expect_correction(<<~RUBY) |
| 45 | + {foo: 1, bar: 2, baz: 3}.except(:bar) |
| 46 | + RUBY |
| 47 | + end |
| 48 | + |
| 49 | + it "registers and corrects an offense when using `reject` and comparing with `lvar == 'str'`" do |
| 50 | + expect_offense(<<~RUBY) |
| 51 | + hash.reject { |k, v| k == 'str' } |
| 52 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except('str')` instead. |
| 53 | + RUBY |
| 54 | + |
| 55 | + expect_correction(<<~RUBY) |
| 56 | + hash.except('str') |
| 57 | + RUBY |
| 58 | + end |
| 59 | + |
| 60 | + it 'registers and corrects an offense when using `reject` and other than comparison by string and symbol using `eql?`' do |
| 61 | + expect_offense(<<~RUBY) |
| 62 | + hash.reject { |k, v| k.eql?(0.0) } |
| 63 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(0.0)` instead. |
| 64 | + RUBY |
| 65 | + |
| 66 | + expect_correction(<<~RUBY) |
| 67 | + hash.except(0.0) |
| 68 | + RUBY |
| 69 | + end |
| 70 | + |
| 71 | + it 'registers and corrects an offense when using `filter` and comparing with `lvar != :sym`' do |
| 72 | + expect_offense(<<~RUBY) |
| 73 | + {foo: 1, bar: 2, baz: 3}.filter { |k, v| k != :bar } |
| 74 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `except(:bar)` instead. |
| 75 | + RUBY |
| 76 | + |
| 77 | + expect_correction(<<~RUBY) |
| 78 | + {foo: 1, bar: 2, baz: 3}.except(:bar) |
| 79 | + RUBY |
| 80 | + end |
| 81 | + |
| 82 | + it 'does not register an offense when using `reject` and other than comparison by string and symbol using `==`' do |
| 83 | + expect_no_offenses(<<~RUBY) |
| 84 | + hash.reject { |k, v| k == 0.0 } |
| 85 | + RUBY |
| 86 | + end |
| 87 | + |
| 88 | + it 'does not register an offense when using `delete_if` and comparing with `lvar == :sym`' do |
| 89 | + expect_no_offenses(<<~RUBY) |
| 90 | + {foo: 1, bar: 2, baz: 3}.delete_if { |k, v| k == :bar } |
| 91 | + RUBY |
| 92 | + end |
| 93 | + |
| 94 | + it 'does not register an offense when using `keep_if` and comparing with `lvar != :sym`' do |
| 95 | + expect_no_offenses(<<~RUBY) |
| 96 | + {foo: 1, bar: 2, baz: 3}.keep_if { |k, v| k != :bar } |
| 97 | + RUBY |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context 'Ruby 2.7 or lower', :ruby27 do |
| 102 | + it 'does not register an offense when using `reject` and comparing with `lvar == :key`' do |
| 103 | + expect_no_offenses(<<~RUBY) |
| 104 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| k == :bar } |
| 105 | + RUBY |
| 106 | + end |
| 107 | + |
| 108 | + it 'does not register an offense when using `reject` and comparing with `:key == lvar`' do |
| 109 | + expect_no_offenses(<<~RUBY) |
| 110 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| :bar == k } |
| 111 | + RUBY |
| 112 | + end |
| 113 | + |
| 114 | + it 'does not register an offense when using `select` and comparing with `lvar != :key`' do |
| 115 | + expect_no_offenses(<<~RUBY) |
| 116 | + {foo: 1, bar: 2, baz: 3}.select { |k, v| k != :bar } |
| 117 | + RUBY |
| 118 | + end |
| 119 | + |
| 120 | + it 'does not register an offense when using `select` and comparing with `:key != lvar`' do |
| 121 | + expect_no_offenses(<<~RUBY) |
| 122 | + {foo: 1, bar: 2, baz: 3}.select { |k, v| :bar != k } |
| 123 | + RUBY |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + it 'does not register an offense when using `reject` and comparing with `lvar != :key`' do |
| 128 | + expect_no_offenses(<<~RUBY) |
| 129 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| k != :bar } |
| 130 | + RUBY |
| 131 | + end |
| 132 | + |
| 133 | + it 'does not register an offense when using `reject` and comparing with `:key != lvar`' do |
| 134 | + expect_no_offenses(<<~RUBY) |
| 135 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| :bar != key } |
| 136 | + RUBY |
| 137 | + end |
| 138 | + |
| 139 | + it 'does not register an offense when using `select` and comparing with `lvar == :key`' do |
| 140 | + expect_no_offenses(<<~RUBY) |
| 141 | + {foo: 1, bar: 2, baz: 3}.select { |k, v| k == :bar } |
| 142 | + RUBY |
| 143 | + end |
| 144 | + |
| 145 | + it 'does not register an offense when using `select` and comparing with `:key == lvar`' do |
| 146 | + expect_no_offenses(<<~RUBY) |
| 147 | + {foo: 1, bar: 2, baz: 3}.select { |k, v| :bar == key } |
| 148 | + RUBY |
| 149 | + end |
| 150 | + |
| 151 | + it 'does not register an offense when not using key block argument`' do |
| 152 | + expect_no_offenses(<<~RUBY) |
| 153 | + {foo: 1, bar: 2, baz: 3}.reject { |k, v| do_something != :bar } |
| 154 | + RUBY |
| 155 | + end |
| 156 | + |
| 157 | + it 'does not register an offense when not using block`' do |
| 158 | + expect_no_offenses(<<~RUBY) |
| 159 | + {foo: 1, bar: 2, baz: 3}.reject |
| 160 | + RUBY |
| 161 | + end |
| 162 | + |
| 163 | + it 'does not register an offense when using `Hash#except`' do |
| 164 | + expect_no_offenses(<<~RUBY) |
| 165 | + {foo: 1, bar: 2, baz: 3}.except(:bar) |
| 166 | + RUBY |
| 167 | + end |
| 168 | +end |
0 commit comments