/
const_node_spec.rb
45 lines (34 loc) · 1.17 KB
/
const_node_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
# frozen_string_literal: true
RSpec.describe RuboCop::AST::ConstNode do
let(:ast) { parse_source(source).ast }
let(:const_node) { ast }
let(:source) { '::Foo::Bar::BAZ' }
describe '#namespace' do
it { expect(const_node.namespace.source).to eq '::Foo::Bar' }
end
describe '#short_name' do
it { expect(const_node.short_name).to eq :BAZ }
end
describe '#module_name?' do
it { expect(const_node.module_name?).to eq false }
context 'with a constant with a lowercase letter' do
let(:source) { '::Foo::Bar' }
it { expect(const_node.module_name?).to eq true }
end
end
describe '#absolute?' do
it { expect(const_node.absolute?).to eq true }
context 'with a constant not starting with ::' do
let(:source) { 'Foo::Bar::BAZ' }
it { expect(const_node.absolute?).to eq false }
end
end
describe '#each_path' do
let(:source) { 'var = ::Foo::Bar::BAZ' }
let(:const_node) { ast.children.last }
it 'yields all parts of the namespace' do
expect(const_node.each_path.map(&:type)).to eq %i[cbase const const]
expect(const_node.each_path.to_a.last(2).map(&:short_name)).to eq %i[Foo Bar]
end
end
end