/
in_pattern_node_spec.rb
151 lines (123 loc) · 3.62 KB
/
in_pattern_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
# frozen_string_literal: true
RSpec.describe RuboCop::AST::InPatternNode do
context 'when using Ruby 2.7 or newer', :ruby27 do
let(:in_pattern_node) { parse_source(source).ast.children[1] }
describe '.new' do
let(:source) do
['case condition',
'in [42] then foo',
'end'].join("\n")
end
it { expect(in_pattern_node).to be_a(described_class) }
end
describe '#pattern' do
context 'with a value pattern' do
let(:source) do
['case condition',
'in 42 then foo',
'end'].join("\n")
end
it { expect(in_pattern_node.pattern).to be_int_type }
end
context 'with a variable pattern' do
let(:source) do
['case condition',
'in var then foo',
'end'].join("\n")
end
it { expect(in_pattern_node.pattern).to be_match_var_type }
end
context 'with an alternative pattern' do
let(:source) do
['case condition',
'in :foo | :bar | :baz then foo',
'end'].join("\n")
end
it { expect(in_pattern_node.pattern).to be_match_alt_type }
end
context 'with an as pattern' do
let(:source) do
['case condition',
'in Integer => var then foo',
'end'].join("\n")
end
it { expect(in_pattern_node.pattern).to be_match_as_type }
end
context 'with an array pattern' do
let(:source) do
['case condition',
'in :foo, :bar, :baz then foo',
'end'].join("\n")
end
it { expect(in_pattern_node.pattern).to be_array_pattern_type }
end
context 'with a hash pattern' do
let(:source) do
['case condition',
'in foo:, bar:, baz: then foo',
'end'].join("\n")
end
it { expect(in_pattern_node.pattern).to be_hash_pattern_type }
end
context 'with a pin operator', :ruby31 do
let(:source) do
['case condition',
'in ^(2 + 2) then foo',
'end'].join("\n")
end
it { expect(in_pattern_node.pattern).to be_pin_type }
end
end
describe '#then?' do
context 'with a then keyword' do
let(:source) do
['case condition',
'in [42] then foo',
'end'].join("\n")
end
it { expect(in_pattern_node).to be_then }
end
context 'without a then keyword' do
let(:source) do
['case condition',
'in [42]',
' foo',
'end'].join("\n")
end
it { expect(in_pattern_node).not_to be_then }
end
end
describe '#body' do
context 'with a then keyword' do
let(:source) do
['case condition',
'in [42] then :foo',
'end'].join("\n")
end
it { expect(in_pattern_node.body).to be_sym_type }
end
context 'without a then keyword' do
let(:source) do
['case condition',
'in [42]',
' [:foo, :bar]',
'end'].join("\n")
end
it { expect(in_pattern_node.body).to be_array_type }
end
end
describe '#branch_index' do
let(:source) do
['case condition',
'in [42] then 1',
'in [43] then 2',
'in [44] then 3',
'end'].join("\n")
end
let(:in_patterns) { parse_source(source).ast.children[1...-1] }
it { expect(in_patterns[0].branch_index).to eq(0) }
it { expect(in_patterns[1].branch_index).to eq(1) }
it { expect(in_patterns[2].branch_index).to eq(2) }
end
end
end