|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::InternalAffairs::NodeTypeMultiplePredicates, :config do |
| 4 | + context 'in an `or` node with multiple node type predicate branches' do |
| 5 | + it 'does not register an offense for type predicates called without a receiver' do |
| 6 | + expect_no_offenses(<<~RUBY) |
| 7 | + str_type? || sym_type? |
| 8 | + RUBY |
| 9 | + end |
| 10 | + |
| 11 | + it 'does not register an offense for type predicates called with different receivers' do |
| 12 | + expect_no_offenses(<<~RUBY) |
| 13 | + foo.str_type? || bar.sym_type? |
| 14 | + RUBY |
| 15 | + end |
| 16 | + |
| 17 | + it 'does not register an offense when all method calls are not type predicates' do |
| 18 | + expect_no_offenses(<<~RUBY) |
| 19 | + foo.bar? || foo.sym_type? |
| 20 | + RUBY |
| 21 | + end |
| 22 | + |
| 23 | + it 'does not register an offense for negated predicates' do |
| 24 | + expect_no_offenses(<<~RUBY) |
| 25 | + !node.str_type? || !node.sym_type? |
| 26 | + RUBY |
| 27 | + end |
| 28 | + |
| 29 | + it 'registers an offense and corrects' do |
| 30 | + expect_offense(<<~RUBY) |
| 31 | + node.str_type? || node.sym_type? |
| 32 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.type?(:str, :sym)` instead of checking for multiple node types. |
| 33 | + RUBY |
| 34 | + |
| 35 | + expect_correction(<<~RUBY) |
| 36 | + node.type?(:str, :sym) |
| 37 | + RUBY |
| 38 | + end |
| 39 | + |
| 40 | + it 'registers an offense and corrects with `defined_type?`' do |
| 41 | + expect_offense(<<~RUBY) |
| 42 | + node.call_type? || node.defined_type? |
| 43 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.type?(:call, :defined?)` instead of checking for multiple node types. |
| 44 | + RUBY |
| 45 | + |
| 46 | + expect_correction(<<~RUBY) |
| 47 | + node.type?(:call, :defined?) |
| 48 | + RUBY |
| 49 | + end |
| 50 | + |
| 51 | + it 'registers an offense and corrects for nested `or` nodes' do |
| 52 | + expect_offense(<<~RUBY) |
| 53 | + node.str_type? || node.sym_type? || node.boolean_type? |
| 54 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.type?(:str, :sym)` instead of checking for multiple node types. |
| 55 | + RUBY |
| 56 | + |
| 57 | + expect_correction(<<~RUBY) |
| 58 | + node.type?(:str, :sym, :boolean) |
| 59 | + RUBY |
| 60 | + end |
| 61 | + |
| 62 | + it 'registers an offense and corrects when the LHS is a `type?` call' do |
| 63 | + expect_offense(<<~RUBY) |
| 64 | + node.type?(:str, :sym) || node.boolean_type? |
| 65 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.type?(:str, :sym, :boolean)` instead of checking for multiple node types. |
| 66 | + RUBY |
| 67 | + |
| 68 | + expect_correction(<<~RUBY) |
| 69 | + node.type?(:str, :sym, :boolean) |
| 70 | + RUBY |
| 71 | + end |
| 72 | + |
| 73 | + it 'registers an offense and corrects when the RHS is a `type?` call' do |
| 74 | + expect_offense(<<~RUBY) |
| 75 | + node.boolean_type? || node.type?(:str, :sym) |
| 76 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.type?(:boolean, :str, :sym)` instead of checking for multiple node types. |
| 77 | + RUBY |
| 78 | + |
| 79 | + expect_correction(<<~RUBY) |
| 80 | + node.type?(:boolean, :str, :sym) |
| 81 | + RUBY |
| 82 | + end |
| 83 | + |
| 84 | + it 'registers an offense and corrects with safe navigation' do |
| 85 | + expect_offense(<<~RUBY) |
| 86 | + node&.str_type? || node&.sym_type? |
| 87 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node&.type?(:str, :sym)` instead of checking for multiple node types. |
| 88 | + RUBY |
| 89 | + |
| 90 | + expect_correction(<<~RUBY) |
| 91 | + node&.type?(:str, :sym) |
| 92 | + RUBY |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context 'in an `and` node with multiple negated type predicate branches' do |
| 97 | + it 'does not register an offense for type predicates called without a receiver' do |
| 98 | + expect_no_offenses(<<~RUBY) |
| 99 | + !str_type? && !sym_type? |
| 100 | + RUBY |
| 101 | + end |
| 102 | + |
| 103 | + it 'does not register an offense for type predicates called with different receivers' do |
| 104 | + expect_no_offenses(<<~RUBY) |
| 105 | + !foo.str_type? && !bar.sym_type? |
| 106 | + RUBY |
| 107 | + end |
| 108 | + |
| 109 | + it 'does not register an offense when all method calls are not type predicates' do |
| 110 | + expect_no_offenses(<<~RUBY) |
| 111 | + !foo.bar? && !foo.sym_type? |
| 112 | + RUBY |
| 113 | + end |
| 114 | + |
| 115 | + it 'does not register an offense for type predicates called without negation' do |
| 116 | + expect_no_offenses(<<~RUBY) |
| 117 | + node.str_type? && node.sym_type? |
| 118 | + RUBY |
| 119 | + end |
| 120 | + |
| 121 | + it 'registers an offense and corrects' do |
| 122 | + expect_offense(<<~RUBY) |
| 123 | + !node.str_type? && !node.sym_type? |
| 124 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node.type?(:str, :sym)` instead of checking against multiple node types. |
| 125 | + RUBY |
| 126 | + |
| 127 | + expect_correction(<<~RUBY) |
| 128 | + !node.type?(:str, :sym) |
| 129 | + RUBY |
| 130 | + end |
| 131 | + |
| 132 | + it 'registers an offense and corrects with `defined_type?`' do |
| 133 | + expect_offense(<<~RUBY) |
| 134 | + !node.call_type? && !node.defined_type? |
| 135 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node.type?(:call, :defined?)` instead of checking against multiple node types. |
| 136 | + RUBY |
| 137 | + |
| 138 | + expect_correction(<<~RUBY) |
| 139 | + !node.type?(:call, :defined?) |
| 140 | + RUBY |
| 141 | + end |
| 142 | + |
| 143 | + it 'registers an offense and corrects for nested `and` nodes' do |
| 144 | + expect_offense(<<~RUBY) |
| 145 | + !node.str_type? && !node.sym_type? && !node.boolean_type? |
| 146 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node.type?(:str, :sym)` instead of checking against multiple node types. |
| 147 | + RUBY |
| 148 | + |
| 149 | + expect_correction(<<~RUBY) |
| 150 | + !node.type?(:str, :sym, :boolean) |
| 151 | + RUBY |
| 152 | + end |
| 153 | + |
| 154 | + it 'registers an offense and corrects when the LHS is a `type?` call' do |
| 155 | + expect_offense(<<~RUBY) |
| 156 | + !node.type?(:str, :sym) && !node.boolean_type? |
| 157 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node.type?(:str, :sym, :boolean)` instead of checking against multiple node types. |
| 158 | + RUBY |
| 159 | + |
| 160 | + expect_correction(<<~RUBY) |
| 161 | + !node.type?(:str, :sym, :boolean) |
| 162 | + RUBY |
| 163 | + end |
| 164 | + |
| 165 | + it 'registers an offense and corrects when the RHS is a `type?` call' do |
| 166 | + expect_offense(<<~RUBY) |
| 167 | + !node.boolean_type? && !node.type?(:str, :sym) |
| 168 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node.type?(:boolean, :str, :sym)` instead of checking against multiple node types. |
| 169 | + RUBY |
| 170 | + |
| 171 | + expect_correction(<<~RUBY) |
| 172 | + !node.type?(:boolean, :str, :sym) |
| 173 | + RUBY |
| 174 | + end |
| 175 | + |
| 176 | + it 'registers an offense and corrects with safe navigation' do |
| 177 | + expect_offense(<<~RUBY) |
| 178 | + !node&.str_type? && !node&.sym_type? |
| 179 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node&.type?(:str, :sym)` instead of checking against multiple node types. |
| 180 | + RUBY |
| 181 | + |
| 182 | + expect_correction(<<~RUBY) |
| 183 | + !node&.type?(:str, :sym) |
| 184 | + RUBY |
| 185 | + end |
| 186 | + end |
| 187 | +end |
0 commit comments