|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::HashLiteralKeysConversion, :config do |
| 4 | + it 'registers an offense and corrects when using `symbolize_keys` with only symbol keys' do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + { a: 1, b: 2 }.symbolize_keys |
| 7 | + ^^^^^^^^^^^^^^ Redundant hash keys conversion, all the keys have the required type. |
| 8 | + RUBY |
| 9 | + |
| 10 | + expect_correction(<<~RUBY) |
| 11 | + { a: 1, b: 2 } |
| 12 | + RUBY |
| 13 | + end |
| 14 | + |
| 15 | + it 'registers an offense and corrects when using `symbolize_keys` with only symbol and string keys' do |
| 16 | + expect_offense(<<~RUBY) |
| 17 | + { a: 1, 'b' => 2 }.symbolize_keys |
| 18 | + ^^^^^^^^^^^^^^ Convert hash keys explicitly to the required type. |
| 19 | + RUBY |
| 20 | + |
| 21 | + expect_correction(<<~RUBY) |
| 22 | + { a: 1, :b => 2 } |
| 23 | + RUBY |
| 24 | + end |
| 25 | + |
| 26 | + it 'does not register an offense when using `symbolize_keys` with integer keys' do |
| 27 | + expect_no_offenses(<<~RUBY) |
| 28 | + { a: 1, 2 => 3 }.symbolize_keys |
| 29 | + RUBY |
| 30 | + end |
| 31 | + |
| 32 | + it 'does not register an offense when using `symbolize_keys` with non hash literal receiver' do |
| 33 | + expect_no_offenses(<<~RUBY) |
| 34 | + options.symbolize_keys |
| 35 | + RUBY |
| 36 | + end |
| 37 | + |
| 38 | + it 'registers an offense and corrects when using `stringify_keys` with only string keys' do |
| 39 | + expect_offense(<<~RUBY) |
| 40 | + { 'a' => 1, 'b' => 2 }.stringify_keys |
| 41 | + ^^^^^^^^^^^^^^ Redundant hash keys conversion, all the keys have the required type. |
| 42 | + RUBY |
| 43 | + |
| 44 | + expect_correction(<<~RUBY) |
| 45 | + { 'a' => 1, 'b' => 2 } |
| 46 | + RUBY |
| 47 | + end |
| 48 | + |
| 49 | + it 'registers an offense and corrects when using `stringify_keys` with only symbol and string keys' do |
| 50 | + expect_offense(<<~RUBY) |
| 51 | + { a: 1, 'b' => 2 }.stringify_keys |
| 52 | + ^^^^^^^^^^^^^^ Convert hash keys explicitly to the required type. |
| 53 | + RUBY |
| 54 | + |
| 55 | + expect_correction(<<~RUBY) |
| 56 | + { 'a'=> 1, 'b' => 2 } |
| 57 | + RUBY |
| 58 | + end |
| 59 | + |
| 60 | + it 'does not register an offense when using `stringify_keys` with integer keys' do |
| 61 | + expect_no_offenses(<<~RUBY) |
| 62 | + { 'a' => 1, 2 => 3 }.stringify_keys |
| 63 | + RUBY |
| 64 | + end |
| 65 | + |
| 66 | + it 'does not register an offense when using `stringify_keys` with non hash literal receiver' do |
| 67 | + expect_no_offenses(<<~RUBY) |
| 68 | + options.stringify_keys |
| 69 | + RUBY |
| 70 | + end |
| 71 | + |
| 72 | + it 'registers an offense and corrects when using `deep_symbolize_keys` with symbol keys' do |
| 73 | + expect_offense(<<~RUBY) |
| 74 | + { |
| 75 | + a: 1, |
| 76 | + b: { |
| 77 | + c: 1 |
| 78 | + } |
| 79 | + }.deep_symbolize_keys |
| 80 | + ^^^^^^^^^^^^^^^^^^^ Redundant hash keys conversion, all the keys have the required type. |
| 81 | + RUBY |
| 82 | + |
| 83 | + expect_correction(<<~RUBY) |
| 84 | + { |
| 85 | + a: 1, |
| 86 | + b: { |
| 87 | + c: 1 |
| 88 | + } |
| 89 | + } |
| 90 | + RUBY |
| 91 | + end |
| 92 | + |
| 93 | + it 'registers an offense and corrects when using `deep_symbolize_keys` with symbol and string keys' do |
| 94 | + expect_offense(<<~RUBY) |
| 95 | + { |
| 96 | + 'a' => 1, |
| 97 | + b: { |
| 98 | + c: 1 |
| 99 | + } |
| 100 | + }.deep_symbolize_keys |
| 101 | + ^^^^^^^^^^^^^^^^^^^ Convert hash keys explicitly to the required type. |
| 102 | + RUBY |
| 103 | + |
| 104 | + expect_correction(<<~RUBY) |
| 105 | + { |
| 106 | + :a => 1, |
| 107 | + b: { |
| 108 | + c: 1 |
| 109 | + } |
| 110 | + } |
| 111 | + RUBY |
| 112 | + end |
| 113 | + |
| 114 | + it 'registers an offense and corrects when using `deep_symbolize_keys` with flat and only symbol and string keys' do |
| 115 | + expect_offense(<<~RUBY) |
| 116 | + { |
| 117 | + 'a' => 1, |
| 118 | + b: 2 |
| 119 | + }.deep_symbolize_keys |
| 120 | + ^^^^^^^^^^^^^^^^^^^ Convert hash keys explicitly to the required type. |
| 121 | + RUBY |
| 122 | + |
| 123 | + expect_correction(<<~RUBY) |
| 124 | + { |
| 125 | + :a => 1, |
| 126 | + b: 2 |
| 127 | + } |
| 128 | + RUBY |
| 129 | + end |
| 130 | + |
| 131 | + it 'does not register an offense when using `deep_symbolize_keys` with integer keys' do |
| 132 | + expect_no_offenses(<<~RUBY) |
| 133 | + { |
| 134 | + 'a' => 1, |
| 135 | + b: { |
| 136 | + 2 => 3 |
| 137 | + } |
| 138 | + }.deep_symbolize_keys |
| 139 | + RUBY |
| 140 | + end |
| 141 | + |
| 142 | + it 'does not register an offense when using `deep_symbolize_keys` with non hash literal receiver' do |
| 143 | + expect_no_offenses(<<~RUBY) |
| 144 | + options.deep_symbolize_keys |
| 145 | + RUBY |
| 146 | + end |
| 147 | + |
| 148 | + it 'registers an offense and corrects when using `deep_stringify_keys` with only string keys' do |
| 149 | + expect_offense(<<~RUBY) |
| 150 | + { |
| 151 | + 'a' => 1, |
| 152 | + 'b' => { |
| 153 | + 'c' => 1 |
| 154 | + } |
| 155 | + }.deep_stringify_keys |
| 156 | + ^^^^^^^^^^^^^^^^^^^ Redundant hash keys conversion, all the keys have the required type. |
| 157 | + RUBY |
| 158 | + |
| 159 | + expect_correction(<<~RUBY) |
| 160 | + { |
| 161 | + 'a' => 1, |
| 162 | + 'b' => { |
| 163 | + 'c' => 1 |
| 164 | + } |
| 165 | + } |
| 166 | + RUBY |
| 167 | + end |
| 168 | + |
| 169 | + it 'registers an offense and corrects when using `deep_stringify_keys` with only symbol and string keys' do |
| 170 | + expect_offense(<<~RUBY) |
| 171 | + { |
| 172 | + 'a' => 1, |
| 173 | + b: { |
| 174 | + c: 1 |
| 175 | + } |
| 176 | + }.deep_stringify_keys |
| 177 | + ^^^^^^^^^^^^^^^^^^^ Convert hash keys explicitly to the required type. |
| 178 | + RUBY |
| 179 | + |
| 180 | + expect_correction(<<~RUBY) |
| 181 | + { |
| 182 | + 'a' => 1, |
| 183 | + 'b'=> { |
| 184 | + 'c'=> 1 |
| 185 | + } |
| 186 | + } |
| 187 | + RUBY |
| 188 | + end |
| 189 | + |
| 190 | + it 'does not register an offense when using `deep_stringify_keys` with integer keys' do |
| 191 | + expect_no_offenses(<<~RUBY) |
| 192 | + { |
| 193 | + 'a' => 1, |
| 194 | + b: { |
| 195 | + 2 => 3 |
| 196 | + } |
| 197 | + }.deep_stringify_keys |
| 198 | + RUBY |
| 199 | + end |
| 200 | + |
| 201 | + it 'does not register an offense when using `deep_stringify_keys` with non hash literal receiver' do |
| 202 | + expect_no_offenses(<<~RUBY) |
| 203 | + options.deep_stringify_keys |
| 204 | + RUBY |
| 205 | + end |
| 206 | + |
| 207 | + it 'registers an offense and autocorrects when using `symbolize_keys` with empty hash literal' do |
| 208 | + expect_offense(<<~RUBY) |
| 209 | + {}.symbolize_keys |
| 210 | + ^^^^^^^^^^^^^^ Redundant hash keys conversion, all the keys have the required type. |
| 211 | + RUBY |
| 212 | + |
| 213 | + expect_correction(<<~RUBY) |
| 214 | + {} |
| 215 | + RUBY |
| 216 | + end |
| 217 | + |
| 218 | + it 'does not register an offense when using `symbolize_keys` with non alphanumeric keys' do |
| 219 | + expect_no_offenses(<<~RUBY) |
| 220 | + { 'hello world' => 1 }.symbolize_keys |
| 221 | + RUBY |
| 222 | + end |
| 223 | + |
| 224 | + context 'Ruby >= 3.1', :ruby31 do |
| 225 | + it 'does not register an offense when using hash value omission' do |
| 226 | + expect_no_offenses(<<~RUBY) |
| 227 | + { a:, b: 2 }.stringify_keys |
| 228 | + RUBY |
| 229 | + end |
| 230 | + end |
| 231 | +end |
0 commit comments