-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
masgn_node_spec.rb
72 lines (50 loc) · 1.63 KB
/
masgn_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
# frozen_string_literal: true
RSpec.describe RuboCop::AST::MasgnNode do
let(:masgn_node) { parse_source(source).ast }
let(:source) { 'x, y = z' }
describe '.new' do
context 'with a `masgn` node' do
it { expect(masgn_node).to be_a(described_class) }
end
end
describe '#names' do
subject { masgn_node.names }
let(:source) { 'a, @b, @@c, $d, E, *f = z' }
it { is_expected.to eq(%i[a @b @@c $d E f]) }
context 'with nested `mlhs` nodes' do
let(:source) { 'a, (b, c) = z' }
it { is_expected.to eq(%i[a b c]) }
end
context 'with nested assignment on LHS' do
let(:source) { 'a, b[c+=1] = z' }
it { is_expected.to eq([:a, 'b[c+=1]']) }
end
context 'with a method chain on LHS' do
let(:source) { 'a, b.c = z' }
it { is_expected.to eq([:a, 'b.c']) }
end
end
describe '#expression' do
include AST::Sexp
subject { masgn_node.expression }
context 'with variables' do
it { is_expected.to eq(s(:send, nil, :z)) }
end
context 'with a LHS splat' do
let(:source) { 'x, *y = z' }
it { is_expected.to eq(s(:send, nil, :z)) }
end
context 'with multiple RHS values' do
let(:source) { 'x, y = 1, 2' }
it { is_expected.to eq(s(:array, s(:int, 1), s(:int, 2))) }
end
context 'with an RHS splat' do
let(:source) { 'x, y = *z' }
it { is_expected.to eq(s(:array, s(:splat, s(:send, nil, :z)))) }
end
context 'with assignment on RHS' do
let(:source) { 'x, y = 1, z+=2' }
it { is_expected.to eq(s(:array, s(:int, 1), s(:op_asgn, s(:lvasgn, :z), :+, s(:int, 2)))) }
end
end
end