|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Style::SuperArguments, :config do |
| 4 | + shared_examples 'offense' do |description, args, forwarded_args = args| |
| 5 | + it "registers and corrects an offense when using def`#{description} (#{args}) => (#{forwarded_args})`" do |
| 6 | + expect_offense(<<~RUBY, forwarded_args: forwarded_args) |
| 7 | + def method(#{args}) |
| 8 | + super(#{forwarded_args}) |
| 9 | + ^^^^^^^{forwarded_args}^ Call `super` without arguments and parentheses when the signature is identical. |
| 10 | + end |
| 11 | + RUBY |
| 12 | + |
| 13 | + expect_correction(<<~RUBY) |
| 14 | + def method(#{args}) |
| 15 | + super |
| 16 | + end |
| 17 | + RUBY |
| 18 | + end |
| 19 | + |
| 20 | + it "registers and corrects an offense when using defs`#{description} (#{args}) => (#{forwarded_args})`" do |
| 21 | + expect_offense(<<~RUBY, forwarded_args: forwarded_args) |
| 22 | + def self.method(#{args}) |
| 23 | + super(#{forwarded_args}) |
| 24 | + ^^^^^^^{forwarded_args}^ Call `super` without arguments and parentheses when the signature is identical. |
| 25 | + end |
| 26 | + RUBY |
| 27 | + |
| 28 | + expect_correction(<<~RUBY) |
| 29 | + def self.method(#{args}) |
| 30 | + super |
| 31 | + end |
| 32 | + RUBY |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + shared_examples 'no offense' do |description, args, forwarded_args = args| |
| 37 | + it "registers no offense when using def `#{description} (#{args}) => (#{forwarded_args})`" do |
| 38 | + expect_no_offenses(<<~RUBY) |
| 39 | + def method(#{args}) |
| 40 | + super(#{forwarded_args}) |
| 41 | + end |
| 42 | + RUBY |
| 43 | + end |
| 44 | + |
| 45 | + it "registers no offense when using defs `#{description} (#{args}) => (#{forwarded_args})`" do |
| 46 | + expect_no_offenses(<<~RUBY) |
| 47 | + def self.method(#{args}) |
| 48 | + super(#{forwarded_args}) |
| 49 | + end |
| 50 | + RUBY |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + it_behaves_like 'offense', 'no arguments', '' |
| 55 | + it_behaves_like 'offense', 'single positional argument', 'a' |
| 56 | + it_behaves_like 'offense', 'multiple positional arguments', 'a, b' |
| 57 | + it_behaves_like 'offense', 'multiple positional arguments with default', 'a, b, c = 1', 'a, b, c' |
| 58 | + it_behaves_like 'offense', 'positional/keyword argument', 'a, b:', 'a, b: b' |
| 59 | + it_behaves_like 'offense', 'positional/keyword argument with default', 'a, b: 1', 'a, b: b' |
| 60 | + it_behaves_like 'offense', 'positional/keyword argument both with default', 'a = 1, b: 2', 'a, b: b' |
| 61 | + it_behaves_like 'offense', 'named block argument', '&blk' |
| 62 | + it_behaves_like 'offense', 'positional splat arguments', '*args' |
| 63 | + it_behaves_like 'offense', 'keyword splat arguments', '**kwargs' |
| 64 | + it_behaves_like 'offense', 'positional/keyword splat arguments', '*args, **kwargs' |
| 65 | + it_behaves_like 'offense', 'positionalkeyword splat arguments with block', '*args, **kwargs, &blk' |
| 66 | + it_behaves_like 'offense', 'keyword arguments mixed with forwarding', 'a:, **kwargs', 'a: a, **kwargs' |
| 67 | + it_behaves_like 'offense', 'tripple dot forwarding', '...' |
| 68 | + it_behaves_like 'offense', 'tripple dot forwarding with extra arg', 'a, ...' |
| 69 | + |
| 70 | + it_behaves_like 'no offense', 'different amount of positional arguments', 'a, b', 'a' |
| 71 | + it_behaves_like 'no offense', 'positional arguments in different order', 'a, b', 'b, a' |
| 72 | + it_behaves_like 'no offense', 'keyword arguments in different order', 'a:, b:', 'b: b, a: a' |
| 73 | + it_behaves_like 'no offense', 'positional/keyword argument mixing', 'a, b', 'a, b: b' |
| 74 | + it_behaves_like 'no offense', 'positional/keyword argument mixing reversed', 'a, b:', 'a, b' |
| 75 | + it_behaves_like 'no offense', 'block argument with different name', '&blk', '&other_blk' |
| 76 | + it_behaves_like 'no offense', 'keyword arguments and hash', 'a:', '{ a: a }' |
| 77 | + it_behaves_like 'no offense', 'keyword arguments with send node', 'a:, b:', 'a: a, b: c' |
| 78 | + it_behaves_like 'no offense', 'tripple dot forwarding with extra param', '...', 'a, ...' |
| 79 | + it_behaves_like 'no offense', 'tripple dot forwarding with different param', 'a, ...', 'b, ...' |
| 80 | + it_behaves_like 'no offense', 'keyword forwarding with extra keyword', 'a, **kwargs', 'a: a, **kwargs' |
| 81 | + |
| 82 | + context 'Ruby >= 3.1', :ruby31 do |
| 83 | + it_behaves_like 'offense', 'hash value omission', 'a:' |
| 84 | + it_behaves_like 'offense', 'anonymous block forwarding', '&' |
| 85 | + end |
| 86 | + |
| 87 | + context 'Ruby >= 3.2', :ruby32 do |
| 88 | + it_behaves_like 'offense', 'anonymous positional forwarding', '*' |
| 89 | + it_behaves_like 'offense', 'anonymous keyword forwarding', '**' |
| 90 | + |
| 91 | + it_behaves_like 'no offense', 'mixed anonymous forwarding', '*, **', '*' |
| 92 | + it_behaves_like 'no offense', 'mixed anonymous forwarding', '*, **', '**' |
| 93 | + end |
| 94 | + |
| 95 | + it 'registers no offense when explicitly passing no arguments' do |
| 96 | + expect_no_offenses(<<~RUBY) |
| 97 | + def foo(a) |
| 98 | + super() |
| 99 | + end |
| 100 | + RUBY |
| 101 | + end |
| 102 | + |
| 103 | + it 'registers an offense when passign along no arguments' do |
| 104 | + expect_offense(<<~RUBY) |
| 105 | + def foo |
| 106 | + super() |
| 107 | + ^^^^^^^ Call `super` without arguments and parentheses when the signature is identical. |
| 108 | + end |
| 109 | + RUBY |
| 110 | + end |
| 111 | + |
| 112 | + it 'registers an offense for nested declarations' do |
| 113 | + expect_offense(<<~RUBY) |
| 114 | + def foo(a) |
| 115 | + def bar(b:) |
| 116 | + super(b: b) |
| 117 | + ^^^^^^^^^^^ Call `super` without arguments and parentheses when the signature is identical. |
| 118 | + end |
| 119 | + super(a) |
| 120 | + ^^^^^^^^ Call `super` without arguments and parentheses when the signature is identical. |
| 121 | + end |
| 122 | + RUBY |
| 123 | + end |
| 124 | + |
| 125 | + it 'registers no offense when calling super in a dsl method' do |
| 126 | + expect_no_offenses(<<~RUBY) |
| 127 | + describe 'example' do |
| 128 | + subject { super() } |
| 129 | + end |
| 130 | + RUBY |
| 131 | + end |
| 132 | + |
| 133 | + context 'when calling super with an extra block argument' do |
| 134 | + it 'registers no offense when calling super with no arguments' do |
| 135 | + expect_no_offenses(<<~RUBY) |
| 136 | + def test |
| 137 | + super { x } |
| 138 | + end |
| 139 | + RUBY |
| 140 | + end |
| 141 | + |
| 142 | + it 'registers no offense when calling super with implicit positional arguments' do |
| 143 | + expect_no_offenses(<<~RUBY) |
| 144 | + def test(a) |
| 145 | + super { x } |
| 146 | + end |
| 147 | + RUBY |
| 148 | + end |
| 149 | + |
| 150 | + it 'registers no offense for a method with block when calling super with positional argument' do |
| 151 | + expect_no_offenses(<<~RUBY) |
| 152 | + def test(a, &blk) |
| 153 | + super(a) { x } |
| 154 | + end |
| 155 | + RUBY |
| 156 | + end |
| 157 | + end |
| 158 | + |
| 159 | + context 'scope changes' do |
| 160 | + it 'registers no offense when the scope changes because of a class definition with block' do |
| 161 | + expect_no_offenses(<<~RUBY) |
| 162 | + def foo(a) |
| 163 | + Class.new do |
| 164 | + def foo(a, b) |
| 165 | + super(a) |
| 166 | + end |
| 167 | + end |
| 168 | + end |
| 169 | + RUBY |
| 170 | + end |
| 171 | + end |
| 172 | + |
| 173 | + it 'registers an offense when the scope changes because of a block' do |
| 174 | + expect_offense(<<~RUBY) |
| 175 | + def foo(a) |
| 176 | + bar do |
| 177 | + super(a) |
| 178 | + ^^^^^^^^ Call `super` without arguments and parentheses when the signature is identical. |
| 179 | + end |
| 180 | + end |
| 181 | + RUBY |
| 182 | + end |
| 183 | + |
| 184 | + it 'registers an offense when the scope changes because of a numblock' do |
| 185 | + expect_offense(<<~RUBY) |
| 186 | + def foo(a) |
| 187 | + bar do |
| 188 | + baz(_1) |
| 189 | + super(a) |
| 190 | + ^^^^^^^^ Call `super` without arguments and parentheses when the signature is identical. |
| 191 | + end |
| 192 | + end |
| 193 | + RUBY |
| 194 | + end |
| 195 | + |
| 196 | + it 'registers no offense when the scope changes because of sclass' do |
| 197 | + expect_no_offenses(<<~RUBY) |
| 198 | + def foo(a) |
| 199 | + class << self |
| 200 | + def foo(b) |
| 201 | + super(a) |
| 202 | + end |
| 203 | + end |
| 204 | + end |
| 205 | + RUBY |
| 206 | + end |
| 207 | + |
| 208 | + it 'registers no offense when calling super in define_singleton_method' do |
| 209 | + expect_no_offenses(<<~RUBY) |
| 210 | + def test(a) |
| 211 | + define_singleton_method(:test2) do |a| |
| 212 | + super(a) |
| 213 | + end |
| 214 | + b.define_singleton_method(:test2) do |a| |
| 215 | + super(a) |
| 216 | + end |
| 217 | + end |
| 218 | + RUBY |
| 219 | + end |
| 220 | +end |
0 commit comments