/
case_match_node_spec.rb
176 lines (151 loc) · 3.8 KB
/
case_match_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
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
# frozen_string_literal: true
RSpec.describe RuboCop::AST::CaseMatchNode do
subject(:case_match_node) { parse_source(source).ast }
context 'when using Ruby 2.7 or newer', :ruby27 do
describe '.new' do
let(:source) do
<<~RUBY
case expr
in pattern
end
RUBY
end
it { is_expected.to be_a(described_class) }
end
describe '#keyword' do
let(:source) do
<<~RUBY
case expr
in pattern
end
RUBY
end
it { expect(case_match_node.keyword).to eq('case') }
end
describe '#in_pattern_branches' do
let(:source) do
<<~RUBY
case expr
in pattern
in pattern
in pattern
end
RUBY
end
it { expect(case_match_node.in_pattern_branches.size).to eq(3) }
it {
expect(case_match_node.in_pattern_branches).to all(be_in_pattern_type)
}
end
describe '#each_in_pattern' do
let(:source) do
<<~RUBY
case expr
in pattern
in pattern
in pattern
end
RUBY
end
context 'when not passed a block' do
it {
expect(case_match_node.each_in_pattern).to be_a(Enumerator)
}
end
context 'when passed a block' do
it 'yields all the conditions' do
expect { |b| case_match_node.each_in_pattern(&b) }
.to yield_successive_args(*case_match_node.in_pattern_branches)
end
end
end
describe '#else?' do
context 'without an else statement' do
let(:source) do
<<~RUBY
case expr
in pattern
end
RUBY
end
it { is_expected.not_to be_else }
end
context 'with an else statement' do
let(:source) do
<<~RUBY
case expr
in pattern
else
end
RUBY
end
it { is_expected.to be_else }
end
end
describe '#else_branch' do
describe '#else?' do
context 'without an else statement' do
let(:source) do
<<~RUBY
case expr
in pattern
end
RUBY
end
it { expect(case_match_node.else_branch).to be_nil }
end
context 'with an else statement' do
let(:source) do
<<~RUBY
case expr
in pattern
else
:foo
end
RUBY
end
it { expect(case_match_node.else_branch).to be_sym_type }
end
end
end
describe '#branches' do
context 'when there is an else' do
context 'with else body' do
let(:source) { <<~RUBY }
case pattern
in :foo then # do nothing
in :bar then 42
else 'hello'
end
RUBY
it 'returns all the bodies' do
expect(case_match_node.branches).to match [nil, be_int_type, be_str_type]
end
end
context 'with empty else' do
let(:source) { <<~RUBY }
case pattern
in :foo then # do nothing
in :bar then 42
else # do nothing
end
RUBY
it 'returns all the bodies' do
expect(case_match_node.branches).to match [nil, be_int_type, nil]
end
end
end
context 'when there is no else keyword' do
let(:source) { <<~RUBY }
case pattern
in :foo then # do nothing
in :bar then 42
end
RUBY
it 'returns only then when bodies' do
expect(case_match_node.branches).to match [nil, be_int_type]
end
end
end
end
end