/
missing_super_spec.rb
138 lines (124 loc) · 3.52 KB
/
missing_super_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Lint::MissingSuper do
subject(:cop) { described_class.new }
context 'constructor' do
it 'registers an offense when no `super` call' do
expect_offense(<<~RUBY)
class Child < Parent
def initialize
^^^^^^^^^^^^^^ Call `super` to initialize state of the parent class.
end
end
RUBY
end
it 'does not register an offense for the class without parent class' do
expect_no_offenses(<<~RUBY)
class Child
def initialize
end
end
RUBY
end
it 'does not register an offense for the class with stateless parent class' do
expect_no_offenses(<<~RUBY)
class Child < Object
def initialize
end
end
RUBY
end
it 'does not register an offense for the constructor-like method defined outside of a class' do
expect_no_offenses(<<~RUBY)
module M
def initialize
end
end
RUBY
end
it 'does not register an offense when there is a `super` call' do
expect_no_offenses(<<~RUBY)
class Child < Parent
def initialize
super
end
end
RUBY
end
end
context 'callbacks' do
it 'registers no offense when module callback without `super` call' do
expect_no_offenses(<<~RUBY)
module M
def self.included(base)
end
end
RUBY
end
it 'registers an offense when class callback without `super` call' do
expect_offense(<<~RUBY)
class Foo
def self.inherited(base)
^^^^^^^^^^^^^^^^^^^^^^^^ Call `super` to invoke callback defined in the parent class.
end
end
RUBY
end
it 'registers an offense when class callback within `self << class` and without `super` call' do
expect_offense(<<~RUBY)
class Foo
class << self
def inherited(base)
^^^^^^^^^^^^^^^^^^^ Call `super` to invoke callback defined in the parent class.
end
end
end
RUBY
end
it 'registers an offense when method callback is without `super` call' do
expect_offense(<<~RUBY)
class Foo
def method_added(*)
^^^^^^^^^^^^^^^^^^^ Call `super` to invoke callback defined in the parent class.
end
end
RUBY
end
it 'registers an offense for instance level `method_missing?` with no `super` call' do
expect_offense(<<~RUBY)
class Foo
def method_missing(*args)
^^^^^^^^^^^^^^^^^^^^^^^^^ Call `super` to invoke callback defined in the parent class.
end
end
RUBY
end
it 'registers an offense for class level `method_missing?` with no `super` call' do
expect_offense(<<~RUBY)
class Foo
def self.method_missing(*args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Call `super` to invoke callback defined in the parent class.
end
end
RUBY
end
it 'does not register an offense when `method_missing?` contains `super` call' do
expect_no_offenses(<<~RUBY)
class Foo
def method_missing(*args)
super
do_something
end
end
RUBY
end
it 'does not register an offense when callback has a `super` call' do
expect_no_offenses(<<~RUBY)
class Foo
def self.inherited(base)
super
end
end
RUBY
end
end
end