forked from rubocop/rubocop-ast
/
rescue_node_spec.rb
128 lines (107 loc) · 2.75 KB
/
rescue_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
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
# frozen_string_literal: true
RSpec.describe RuboCop::AST::RescueNode do
let(:ast) { parse_source(source).ast }
let(:rescue_node) { ast.children.first }
describe '.new' do
let(:source) { <<~RUBY }
begin
rescue => e
end
RUBY
it { expect(rescue_node.is_a?(described_class)).to be(true) }
end
describe '#body' do
let(:source) { <<~RUBY }
begin
foo
rescue => e
end
RUBY
it { expect(rescue_node.body.send_type?).to be(true) }
end
describe '#resbody_branches' do
let(:source) { <<~RUBY }
begin
rescue FooError then foo
rescue BarError, BazError then bar_and_baz
end
RUBY
it { expect(rescue_node.resbody_branches.size).to eq(2) }
it { expect(rescue_node.resbody_branches).to all(be_resbody_type) }
end
describe '#branches' do
context 'when there is an else' do
let(:source) { <<~RUBY }
begin
rescue FooError then foo
rescue BarError then # do nothing
else 'bar'
end
RUBY
it 'returns all the bodies' do
expect(rescue_node.branches).to match [be_send_type, nil, be_str_type]
end
context 'with an empty else' do
let(:source) { <<~RUBY }
begin
rescue FooError then foo
rescue BarError then # do nothing
else # do nothing
end
RUBY
it 'returns all the bodies' do
expect(rescue_node.branches).to match [be_send_type, nil, nil]
end
end
end
context 'when there is no else keyword' do
let(:source) { <<~RUBY }
begin
rescue FooError then foo
rescue BarError then # do nothing
end
RUBY
it 'returns only then rescue bodies' do
expect(rescue_node.branches).to match [be_send_type, nil]
end
end
end
describe '#else_branch' do
context 'without an else statement' do
let(:source) { <<~RUBY }
begin
rescue FooError then foo
end
RUBY
it { expect(rescue_node.else_branch.nil?).to be(true) }
end
context 'with an else statement' do
let(:source) { <<~RUBY }
begin
rescue FooError then foo
else bar
end
RUBY
it { expect(rescue_node.else_branch.send_type?).to be(true) }
end
end
describe '#else?' do
context 'without an else statement' do
let(:source) { <<~RUBY }
begin
rescue FooError then foo
end
RUBY
it { expect(rescue_node.else?).to be_falsey }
end
context 'with an else statement' do
let(:source) { <<~RUBY }
begin
rescue FooError then foo
else bar
end
RUBY
it { expect(rescue_node.else?).to be_truthy }
end
end
end