/
resbody_node_spec.rb
91 lines (71 loc) · 2.35 KB
/
resbody_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
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
# frozen_string_literal: true
RSpec.describe RuboCop::AST::ResbodyNode do
let(:resbody_node) do
begin_node = parse_source(source).ast
rescue_node, = *begin_node
rescue_node.children[1]
end
describe '.new' do
let(:source) { 'begin; beginbody; rescue; rescuebody; end' }
it { expect(resbody_node.is_a?(described_class)).to be(true) }
end
describe '#exceptions' do
context 'without exception' do
let(:source) { <<~RUBY }
begin
rescue
end
RUBY
it { expect(resbody_node.exceptions.size).to eq(0) }
end
context 'with a single exception' do
let(:source) { <<~RUBY }
begin
rescue FooError
end
RUBY
it { expect(resbody_node.exceptions.size).to eq(1) }
it { expect(resbody_node.exceptions).to all(be_const_type) }
end
context 'with multiple exceptions' do
let(:source) { <<~RUBY }
begin
rescue FooError, BarError
end
RUBY
it { expect(resbody_node.exceptions.size).to eq(2) }
it { expect(resbody_node.exceptions).to all(be_const_type) }
end
end
describe '#exception_variable' do
context 'for an explicit rescue' do
let(:source) { 'begin; beginbody; rescue Error => ex; rescuebody; end' }
it { expect(resbody_node.exception_variable.source).to eq('ex') }
end
context 'for an implicit rescue' do
let(:source) { 'begin; beginbody; rescue => ex; rescuebody; end' }
it { expect(resbody_node.exception_variable.source).to eq('ex') }
end
context 'when an exception variable is not given' do
let(:source) { 'begin; beginbody; rescue; rescuebody; end' }
it { expect(resbody_node.exception_variable).to be(nil) }
end
end
describe '#body' do
let(:source) { 'begin; beginbody; rescue Error => ex; :rescuebody; end' }
it { expect(resbody_node.body.sym_type?).to be(true) }
end
describe '#branch_index' do
let(:source) { <<~RUBY }
begin
rescue FooError then foo
rescue BarError, BazError then bar_and_baz
rescue QuuxError => e then quux
end
RUBY
let(:resbodies) { parse_source(source).ast.children.first.resbody_branches }
it { expect(resbodies[0].branch_index).to eq(0) }
it { expect(resbodies[1].branch_index).to eq(1) }
it { expect(resbodies[2].branch_index).to eq(2) }
end
end