|  | 
|  | 1 | +# frozen_string_literal: true | 
|  | 2 | + | 
|  | 3 | +RSpec.describe RuboCop::Cop::Rails::JSONSymbolizeNames, :config do | 
|  | 4 | +  %i[load_file load_file! parse parse!].each do |method_name| | 
|  | 5 | +    context "with `#{method_name}` method" do | 
|  | 6 | +      it "registers an offense for `JSON.#{method_name}` followed by `deep_symbolize_keys`" do | 
|  | 7 | +        expect_offense(<<~RUBY, method_name: method_name) | 
|  | 8 | +          JSON.#{method_name}(json).deep_symbolize_keys | 
|  | 9 | +          ^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option. | 
|  | 10 | +        RUBY | 
|  | 11 | + | 
|  | 12 | +        expect_correction(<<~RUBY) | 
|  | 13 | +          JSON.#{method_name}(json, symbolize_names: true) | 
|  | 14 | +        RUBY | 
|  | 15 | +      end | 
|  | 16 | + | 
|  | 17 | +      it "registers an offense for `JSON.#{method_name}` followed by `deep_symbolize_keys` with string literal" do | 
|  | 18 | +        expect_offense(<<~RUBY, method_name: method_name) | 
|  | 19 | +          JSON.#{method_name}('{"foo": "bar"}').deep_symbolize_keys | 
|  | 20 | +          ^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option. | 
|  | 21 | +        RUBY | 
|  | 22 | + | 
|  | 23 | +        expect_correction(<<~RUBY) | 
|  | 24 | +          JSON.#{method_name}('{"foo": "bar"}', symbolize_names: true) | 
|  | 25 | +        RUBY | 
|  | 26 | +      end | 
|  | 27 | + | 
|  | 28 | +      it "registers an offense for `::JSON.#{method_name}` followed by `deep_symbolize_keys`" do | 
|  | 29 | +        expect_offense(<<~RUBY, method_name: method_name) | 
|  | 30 | +          ::JSON.#{method_name}(json).deep_symbolize_keys | 
|  | 31 | +          ^^^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option. | 
|  | 32 | +        RUBY | 
|  | 33 | + | 
|  | 34 | +        expect_correction(<<~RUBY) | 
|  | 35 | +          ::JSON.#{method_name}(json, symbolize_names: true) | 
|  | 36 | +        RUBY | 
|  | 37 | +      end | 
|  | 38 | + | 
|  | 39 | +      it "registers an offense for `::JSON.#{method_name}` followed by `deep_symbolize_keys` with safe navigation" do | 
|  | 40 | +        expect_offense(<<~RUBY, method_name: method_name) | 
|  | 41 | +          ::JSON.#{method_name}(json_null)&.deep_symbolize_keys | 
|  | 42 | +          ^^^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option. | 
|  | 43 | +        RUBY | 
|  | 44 | + | 
|  | 45 | +        expect_correction(<<~RUBY) | 
|  | 46 | +          ::JSON.#{method_name}(json_null, symbolize_names: true) | 
|  | 47 | +        RUBY | 
|  | 48 | +      end | 
|  | 49 | + | 
|  | 50 | +      it "registers an offense for `JSON.#{method_name}` followed by `deep_symbolize_keys` with non-literal option" do | 
|  | 51 | +        expect_offense(<<~RUBY, method_name: method_name) | 
|  | 52 | +          ::JSON.#{method_name}(json, options)&.deep_symbolize_keys | 
|  | 53 | +          ^^^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option. | 
|  | 54 | +        RUBY | 
|  | 55 | + | 
|  | 56 | +        expect_correction(<<~RUBY) | 
|  | 57 | +          ::JSON.#{method_name}(json, options, symbolize_names: true) | 
|  | 58 | +        RUBY | 
|  | 59 | +      end | 
|  | 60 | + | 
|  | 61 | +      it "does not register an offense for `JSON.#{method_name}` with `symbolize_names` option" do | 
|  | 62 | +        expect_no_offenses(<<~RUBY) | 
|  | 63 | +          JSON.#{method_name}(json, symbolize_names: true) | 
|  | 64 | +        RUBY | 
|  | 65 | +      end | 
|  | 66 | + | 
|  | 67 | +      it "does not register an offense for single `JSON.#{method_name}`" do | 
|  | 68 | +        expect_no_offenses(<<~RUBY) | 
|  | 69 | +          JSON.#{method_name}(json) | 
|  | 70 | +        RUBY | 
|  | 71 | +      end | 
|  | 72 | + | 
|  | 73 | +      context 'with `create_additions` option' do | 
|  | 74 | +        it "registers an offense for `JSON.#{method_name}` with `create_additions` option set to `false`" do | 
|  | 75 | +          expect_offense(<<~RUBY, method_name: method_name) | 
|  | 76 | +            JSON.#{method_name}(json, create_additions: false).deep_symbolize_keys | 
|  | 77 | +            ^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option. | 
|  | 78 | +          RUBY | 
|  | 79 | +        end | 
|  | 80 | + | 
|  | 81 | +        it "does not register for `JSON.#{method_name}` with `create_additions` option set to `true`" do | 
|  | 82 | +          expect_no_offenses(<<~RUBY) | 
|  | 83 | +            JSON.#{method_name}(json, create_additions: true).deep_symbolize_keys | 
|  | 84 | +          RUBY | 
|  | 85 | +        end | 
|  | 86 | +      end | 
|  | 87 | +    end | 
|  | 88 | +  end | 
|  | 89 | +end | 
0 commit comments