|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::DeprecatedActiveModelErrorsMethods, :config do |
| 4 | + shared_examples 'errors call with explicit receiver' do |
| 5 | + context 'when modifying errors' do |
| 6 | + it 'registers an offense' do |
| 7 | + expect_offense(<<~RUBY, file_path) |
| 8 | + user.errors[:name] << 'msg' |
| 9 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 10 | + RUBY |
| 11 | + end |
| 12 | + |
| 13 | + context 'when assigning' do |
| 14 | + it 'registers an offense' do |
| 15 | + expect_offense(<<~RUBY, file_path) |
| 16 | + user.errors[:name] = [] |
| 17 | + ^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 18 | + RUBY |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + context 'when modifying errors.messages' do |
| 24 | + it 'registers an offense' do |
| 25 | + expect_offense(<<~RUBY, file_path) |
| 26 | + user.errors.messages[:name] << 'msg' |
| 27 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 28 | + RUBY |
| 29 | + end |
| 30 | + |
| 31 | + context 'when assigning' do |
| 32 | + it 'registers an offense' do |
| 33 | + expect_offense(<<~RUBY, file_path) |
| 34 | + user.errors.messages[:name] = [] |
| 35 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 36 | + RUBY |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context 'when modifying errors.details' do |
| 42 | + it 'registers an offense' do |
| 43 | + expect_offense(<<~RUBY, file_path) |
| 44 | + user.errors.details[:name] << {} |
| 45 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 46 | + RUBY |
| 47 | + end |
| 48 | + |
| 49 | + context 'when assigning' do |
| 50 | + it 'registers an offense' do |
| 51 | + expect_offense(<<~RUBY, file_path) |
| 52 | + user.errors.details[:name] = [] |
| 53 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 54 | + RUBY |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + shared_examples 'errors call without explicit receiver' do |
| 61 | + def expect_offense_if_model_file(code, file_path) |
| 62 | + if file_path.include?('/models/') |
| 63 | + expect_offense(code, file_path) |
| 64 | + else |
| 65 | + code = code.gsub(/^\^+ .+$/, '') |
| 66 | + expect_no_offenses(code, file_path) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + context 'when modifying errors' do |
| 71 | + it 'registers an offense for model file' do |
| 72 | + expect_offense_if_model_file(<<~RUBY, file_path) |
| 73 | + errors[:name] << 'msg' |
| 74 | + ^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 75 | + RUBY |
| 76 | + end |
| 77 | + |
| 78 | + context 'when assigning' do |
| 79 | + it 'registers an offense' do |
| 80 | + expect_offense_if_model_file(<<~RUBY, file_path) |
| 81 | + errors[:name] = [] |
| 82 | + ^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 83 | + RUBY |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + context 'when calling non-manipulative methods' do |
| 88 | + it 'does not register an offense' do |
| 89 | + expect_no_offenses(<<~RUBY, file_path) |
| 90 | + errors[:name].present? |
| 91 | + RUBY |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context 'when modifying errors.messages' do |
| 97 | + it 'registers an offense' do |
| 98 | + expect_offense_if_model_file(<<~RUBY, file_path) |
| 99 | + errors.messages[:name] << 'msg' |
| 100 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 101 | + RUBY |
| 102 | + end |
| 103 | + |
| 104 | + context 'when assigning' do |
| 105 | + it 'registers an offense' do |
| 106 | + expect_offense_if_model_file(<<~RUBY, file_path) |
| 107 | + errors.messages[:name] = [] |
| 108 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 109 | + RUBY |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + context 'when calling non-manipulative methods' do |
| 114 | + it 'does not register an offense' do |
| 115 | + expect_no_offenses(<<~RUBY, file_path) |
| 116 | + errors.messages[:name].present? |
| 117 | + RUBY |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + context 'when modifying errors.details' do |
| 123 | + it 'registers an offense' do |
| 124 | + expect_offense_if_model_file(<<~RUBY, file_path) |
| 125 | + errors.details[:name] << {} |
| 126 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 127 | + RUBY |
| 128 | + end |
| 129 | + |
| 130 | + context 'when assigning' do |
| 131 | + it 'registers an offense' do |
| 132 | + expect_offense_if_model_file(<<~RUBY, file_path) |
| 133 | + errors.details[:name] = [] |
| 134 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. |
| 135 | + RUBY |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + context 'when calling non-manipulative methods' do |
| 140 | + it 'does not register an offense' do |
| 141 | + expect_no_offenses(<<~RUBY, file_path) |
| 142 | + errors.details[:name].present? |
| 143 | + RUBY |
| 144 | + end |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + context 'when file is model file' do |
| 150 | + let(:file_path) { '/foo/app/models/bar.rb' } |
| 151 | + |
| 152 | + it_behaves_like 'errors call with explicit receiver' |
| 153 | + it_behaves_like 'errors call without explicit receiver' |
| 154 | + end |
| 155 | + |
| 156 | + context 'when file is generic' do |
| 157 | + let(:file_path) { '/foo/app/lib/bar.rb' } |
| 158 | + |
| 159 | + it_behaves_like 'errors call with explicit receiver' |
| 160 | + it_behaves_like 'errors call without explicit receiver' |
| 161 | + end |
| 162 | +end |
0 commit comments