summaryrefslogtreecommitdiff
path: root/spec/rubocop/code_reuse_helpers_spec.rb
blob: 2720141aad2e16d8f2c3d428256268b4bc189282 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# frozen_string_literal: true

require 'spec_helper'
require 'rubocop'
require 'parser/current'
require_relative '../../rubocop/code_reuse_helpers'

describe RuboCop::CodeReuseHelpers do
  def parse_source(source, path = 'foo.rb')
    buffer = Parser::Source::Buffer.new(path)
    buffer.source = source

    builder = RuboCop::AST::Builder.new
    parser = Parser::CurrentRuby.new(builder)

    parser.parse(buffer)
  end

  let(:cop) do
    Class.new do
      include RuboCop::CodeReuseHelpers
    end.new
  end

  describe '#send_to_constant?' do
    it 'returns true when sending to a constant' do
      node = parse_source('Foo.bar')

      expect(cop.send_to_constant?(node)).to eq(true)
    end

    it 'returns false when sending to something other than a constant' do
      node = parse_source('10')

      expect(cop.send_to_constant?(node)).to eq(false)
    end
  end

  describe '#send_receiver_name_ends_with?' do
    it 'returns true when the receiver ends with a suffix' do
      node = parse_source('FooFinder.new')

      expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(true)
    end

    it 'returns false when the receiver is the same as a suffix' do
      node = parse_source('Finder.new')

      expect(cop.send_receiver_name_ends_with?(node, 'Finder')).to eq(false)
    end
  end

  describe '#file_path_for_node' do
    it 'returns the file path of a node' do
      node = parse_source('10')
      path = cop.file_path_for_node(node)

      expect(path).to eq('foo.rb')
    end
  end

  describe '#name_of_constant' do
    it 'returns the name of a constant' do
      node = parse_source('Foo')

      expect(cop.name_of_constant(node)).to eq(:Foo)
    end
  end

  describe '#in_finder?' do
    it 'returns true for a node in the finders directory' do
      node = parse_source('10', Rails.root.join('app', 'finders', 'foo.rb'))

      expect(cop.in_finder?(node)).to eq(true)
    end

    it 'returns false for a node outside the finders directory' do
      node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))

      expect(cop.in_finder?(node)).to eq(false)
    end
  end

  describe '#in_model?' do
    it 'returns true for a node in the models directory' do
      node = parse_source('10', Rails.root.join('app', 'models', 'foo.rb'))

      expect(cop.in_model?(node)).to eq(true)
    end

    it 'returns false for a node outside the models directory' do
      node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))

      expect(cop.in_model?(node)).to eq(false)
    end
  end

  describe '#in_service_class?' do
    it 'returns true for a node in the services directory' do
      node = parse_source('10', Rails.root.join('app', 'services', 'foo.rb'))

      expect(cop.in_service_class?(node)).to eq(true)
    end

    it 'returns false for a node outside the services directory' do
      node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))

      expect(cop.in_service_class?(node)).to eq(false)
    end
  end

  describe '#in_presenter?' do
    it 'returns true for a node in the presenters directory' do
      node = parse_source('10', Rails.root.join('app', 'presenters', 'foo.rb'))

      expect(cop.in_presenter?(node)).to eq(true)
    end

    it 'returns false for a node outside the presenters directory' do
      node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))

      expect(cop.in_presenter?(node)).to eq(false)
    end
  end

  describe '#in_serializer?' do
    it 'returns true for a node in the serializers directory' do
      node = parse_source('10', Rails.root.join('app', 'serializers', 'foo.rb'))

      expect(cop.in_serializer?(node)).to eq(true)
    end

    it 'returns false for a node outside the serializers directory' do
      node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))

      expect(cop.in_serializer?(node)).to eq(false)
    end
  end

  describe '#in_worker?' do
    it 'returns true for a node in the workers directory' do
      node = parse_source('10', Rails.root.join('app', 'workers', 'foo.rb'))

      expect(cop.in_worker?(node)).to eq(true)
    end

    it 'returns false for a node outside the workers directory' do
      node = parse_source('10', Rails.root.join('app', 'foo', 'foo.rb'))

      expect(cop.in_worker?(node)).to eq(false)
    end
  end

  describe '#in_api?' do
    it 'returns true for a node in the API directory' do
      node = parse_source('10', Rails.root.join('lib', 'api', 'foo.rb'))

      expect(cop.in_api?(node)).to eq(true)
    end

    it 'returns false for a node outside the API directory' do
      node = parse_source('10', Rails.root.join('lib', 'foo', 'foo.rb'))

      expect(cop.in_api?(node)).to eq(false)
    end
  end

  describe '#in_directory?' do
    it 'returns true for a directory in the CE app/ directory' do
      node = parse_source('10', Rails.root.join('app', 'models', 'foo.rb'))

      expect(cop.in_directory?(node, 'models')).to eq(true)
    end

    it 'returns true for a directory in the EE app/ directory' do
      node =
        parse_source('10', Rails.root.join('ee', 'app', 'models', 'foo.rb'))

      expect(cop.in_directory?(node, 'models')).to eq(true)
    end

    it 'returns false for a directory in the lib/ directory' do
      node =
        parse_source('10', Rails.root.join('lib', 'models', 'foo.rb'))

      expect(cop.in_directory?(node, 'models')).to eq(false)
    end
  end

  describe '#name_of_receiver' do
    it 'returns the name of a send receiver' do
      node = parse_source('Foo.bar')

      expect(cop.name_of_receiver(node)).to eq('Foo')
    end
  end

  describe '#each_class_method' do
    it 'yields every class method to the supplied block' do
      node = parse_source(<<~RUBY)
        class Foo
          class << self
            def first
            end
          end

          def self.second
          end
        end
      RUBY

      nodes = cop.each_class_method(node).to_a

      expect(nodes.length).to eq(2)

      expect(nodes[0].children[0]).to eq(:first)
      expect(nodes[1].children[1]).to eq(:second)
    end
  end

  describe '#each_send_node' do
    it 'yields every send node to the supplied block' do
      node = parse_source("foo\nbar")
      nodes = cop.each_send_node(node).to_a

      expect(nodes.length).to eq(2)
      expect(nodes[0].children[1]).to eq(:foo)
      expect(nodes[1].children[1]).to eq(:bar)
    end
  end

  describe '#disallow_send_to' do
    it 'disallows sending a message to a constant' do
      def_node = parse_source(<<~RUBY)
        def foo
          FooFinder.new
        end
      RUBY

      send_node = def_node.each_child_node(:send).first

      expect(cop)
        .to receive(:add_offense)
        .with(send_node, location: :expression, message: 'oops')

      cop.disallow_send_to(def_node, 'Finder', 'oops')
    end
  end
end