/
struct_inheritance_spec.rb
61 lines (52 loc) · 1.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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::StructInheritance do
subject(:cop) { described_class.new }
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 '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 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