/
struct_inheritance_spec.rb
120 lines (103 loc) · 3.6 KB
/
struct_inheritance_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::StructInheritance, :config do
it 'registers an offense when extending instance of Struct' do
expect_offense(<<~RUBY)
class Person < Struct.new(:first_name, :last_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct.
def foo; end
end
RUBY
expect_correction(<<~RUBY)
Person = Struct.new(:first_name, :last_name) do
def foo; end
end
RUBY
end
it 'registers an offense when extending instance of ::Struct' do
expect_offense(<<~RUBY)
class Person < ::Struct.new(:first_name, :last_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct.
def foo; end
end
RUBY
expect_correction(<<~RUBY)
Person = ::Struct.new(:first_name, :last_name) do
def foo; end
end
RUBY
end
it 'registers an offense when extending instance of Struct with do ... end' do
expect_offense(<<~RUBY)
class Person < Struct.new(:first_name, :last_name) do end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct.
end
RUBY
expect_correction(<<~RUBY)
Person = Struct.new(:first_name, :last_name) do
end
RUBY
end
it 'registers an offense when extending instance of Struct without `do` ... `end` and class body is empty' do
expect_offense(<<~RUBY)
class Person < Struct.new(:first_name, :last_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct.
end
RUBY
expect_correction(<<~RUBY)
Person = Struct.new(:first_name, :last_name)
RUBY
end
it 'registers an offense when extending instance of ::Struct with ' \
'do ... end' do
expect_offense(<<~RUBY)
class Person < ::Struct.new(:first_name, :last_name) do end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct.
end
RUBY
expect_correction(<<~RUBY)
Person = ::Struct.new(:first_name, :last_name) do
end
RUBY
end
it 'registers an offense when extending instance of `Struct` when there is a comment ' \
'before class declaration' do
expect_offense(<<~RUBY)
# comment
class Person < Struct.new(:first_name, :last_name) do end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't extend an instance initialized by `Struct.new`. Use a block to customize the struct.
end
RUBY
expect_correction(<<~RUBY)
# comment
Person = Struct.new(:first_name, :last_name) do
end
RUBY
end
it 'accepts plain class' do
expect_no_offenses(<<~RUBY)
class Person
end
RUBY
end
it 'accepts extending DelegateClass' do
expect_no_offenses(<<~RUBY)
class Person < DelegateClass(Animal)
end
RUBY
end
it 'accepts assignment to Struct.new' do
expect_no_offenses('Person = Struct.new(:first_name, :last_name)')
end
it 'accepts assignment to ::Struct.new' do
expect_no_offenses('Person = ::Struct.new(:first_name, :last_name)')
end
it 'accepts assignment to block form of Struct.new' do
expect_no_offenses(<<~RUBY)
Person = Struct.new(:first_name, :last_name) do
def age
42
end
end
RUBY
end
end