/
active_support_on_load_spec.rb
114 lines (91 loc) · 3.5 KB
/
active_support_on_load_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
# frozen_string_literal: true
RSpec.describe(RuboCop::Cop::Rails::ActiveSupportOnLoad, :config) do
it 'adds offense when trying to extend a framework class with include' do
expect_offense(<<~RUBY)
ActiveRecord::Base.include(MyClass)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { include MyClass }` instead of `ActiveRecord::Base.include(MyClass)`.
RUBY
end
it 'adds offense when trying to extend a framework class with prepend' do
expect_offense(<<~RUBY)
ActiveRecord::Base.prepend(MyClass)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { prepend MyClass }` instead of `ActiveRecord::Base.prepend(MyClass)`.
RUBY
end
it 'adds offense when trying to extend a framework class with extend' do
expect_offense(<<~RUBY)
ActiveRecord::Base.extend(MyClass)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { extend MyClass }` instead of `ActiveRecord::Base.extend(MyClass)`.
RUBY
end
it 'adds offense when trying to extend a framework class with absolute name' do
expect_offense(<<~RUBY)
::ActiveRecord::Base.extend(MyClass)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { extend MyClass }` instead of `::ActiveRecord::Base.extend(MyClass)`.
RUBY
end
it 'adds offense when trying to extend a framework class with a variable' do
expect_offense(<<~RUBY)
ActiveRecord::Base.extend(my_class)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `ActiveSupport.on_load(:active_record) { extend my_class }` instead of `ActiveRecord::Base.extend(my_class)`.
RUBY
end
it 'does not add offense when extending a variable' do
expect_no_offenses(<<~RUBY)
foo.extend(MyClass)
RUBY
end
it 'does not add offense when extending the framework using on_load and include' do
expect_no_offenses(<<~RUBY)
ActiveSupport.on_load(:active_record) { include MyClass }
RUBY
end
it 'does not add offense when extending the framework using on_load and include in a multi-line block' do
expect_no_offenses(<<~RUBY)
ActiveSupport.on_load(:active_record) do
include MyClass
end
RUBY
end
it 'does not add offense when not including a class' do
expect_no_offenses(<<~RUBY)
ActiveRecord::Base.include_root_in_json = false
RUBY
end
it 'does not add offense when using include?' do
expect_no_offenses(<<~RUBY)
name.include?('bob')
RUBY
end
context 'autocorrect' do
it 'autocorrects extension on supported classes' do
source = <<~RUBY
ActiveRecord::Base.prepend(MyClass)
RUBY
corrected_source = <<~RUBY
ActiveSupport.on_load(:active_record) { prepend MyClass }
RUBY
corrected = autocorrect_source(source)
expect(corrected).to(eq(corrected_source))
end
it 'does not autocorrect extension on unsupported classes' do
source = <<~RUBY
MyClass1.prepend(MyClass)
RUBY
corrected = autocorrect_source(source)
expect(corrected).to(eq(source))
source = <<~RUBY
MyClass1.include(MyClass)
RUBY
corrected = autocorrect_source(source)
expect(corrected).to(eq(source))
end
it 'does not autocorrect when there is no extension on the supported classes' do
source = <<~RUBY
ActiveRecord::Base.include_root_in_json = false
RUBY
corrected = autocorrect_source(source)
expect(corrected).to(eq(source))
end
end
end