|
| 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 | + end |
| 12 | + |
| 13 | + it "registers an offense for `::JSON.#{method_name}` followed by `deep_symbolize_keys`" do |
| 14 | + expect_offense(<<~RUBY, method_name: method_name) |
| 15 | + ::JSON.#{method_name}(json).deep_symbolize_keys |
| 16 | + ^^^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option. |
| 17 | + RUBY |
| 18 | + end |
| 19 | + |
| 20 | + it "registers an offense for `::JSON.#{method_name}` followed by `deep_symbolize_keys` with safe navigation" do |
| 21 | + expect_offense(<<~RUBY, method_name: method_name) |
| 22 | + ::JSON.#{method_name}("null")&.deep_symbolize_keys |
| 23 | + ^^^^^^^^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `symbolize_names` option. |
| 24 | + RUBY |
| 25 | + end |
| 26 | + |
| 27 | + it "does not register for `JSON.#{method_name}` with `symbolize_names` option" do |
| 28 | + expect_no_offenses(<<~RUBY) |
| 29 | + JSON.#{method_name}(json, symbolize_names: true) |
| 30 | + RUBY |
| 31 | + end |
| 32 | + |
| 33 | + it "does not register for single `JSON.#{method_name}`" do |
| 34 | + expect_no_offenses(<<~RUBY) |
| 35 | + JSON.#{method_name}(json) |
| 36 | + RUBY |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | +end |
0 commit comments