Repository navigation
Expand file tree
/
Copy pathconst_opassign_test.go
More file actions
93 lines (86 loc) · 3.1 KB
/
Copy pathconst_opassign_test.go
File metadata and controls
93 lines (86 loc) · 3.1 KB
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
package parser
import (
"testing"
"github.com/go-ruby-parser/parser/ast"
)
// TestConstOpAssign covers compound assignment to a constant (`C += 1`,
// `C ||= x`) and to a scope-resolved constant (`A::B += 1`, `A::B ||= x`), which
// the parser previously rejected. Arithmetic and `&&=` desugar to `C = C OP x`;
// `||=` is guarded behind `defined?` so it defines an undefined constant instead
// of raising, matching MRI 4.0.6.
func TestConstOpAssign(t *testing.T) {
for _, src := range []string{
`C += 1`, `C -= 1`, `C *= 2`, `C **= 2`, `C &&= 1`, `C ||= 1`,
`A::B += 1`, `A::B ||= 1`, `Foo::Bar::BAZ *= 3`,
`(x; ConstantSpecs)::OpAssign += 2`,
`defined?(A += 1)`,
} {
if _, err := Parse(src); err != nil {
t.Errorf("Parse(%q) error: %v", src, err)
}
}
// `C += 1` desugars to `C = C + 1` (a ConstAssign whose value adds a read).
prog, err := Parse(`C += 1`)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
ca, ok := prog.Body[0].(*ast.ConstAssign)
if !ok {
t.Fatalf("C += 1: expected *ast.ConstAssign, got %T", prog.Body[0])
}
bin, ok := ca.Value.(*ast.BinaryExpr)
if !ok || bin.Op != "+" {
t.Fatalf("C += 1: expected value `C + 1`, got %#v", ca.Value)
}
if _, ok := bin.Left.(*ast.ConstRef); !ok {
t.Fatalf("C += 1: expected a ConstRef read on the left, got %T", bin.Left)
}
// `C ||= 1` desugars to `(defined?(C) && C) || (C = 1)` so an undefined
// constant is defined rather than read.
prog, err = Parse(`C ||= 1`)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
or, ok := prog.Body[0].(*ast.BinaryExpr)
if !ok || or.Op != "||" {
t.Fatalf("C ||= 1: expected a top-level `||`, got %#v", prog.Body[0])
}
guard, ok := or.Left.(*ast.BinaryExpr)
if !ok || guard.Op != "&&" {
t.Fatalf("C ||= 1: expected a `defined?(C) && C` guard, got %#v", or.Left)
}
call, ok := guard.Left.(*ast.Call)
if !ok || call.Name != "defined?" {
t.Fatalf("C ||= 1: expected `defined?(C)` in the guard, got %#v", guard.Left)
}
if _, ok := or.Right.(*ast.ConstAssign); !ok {
t.Fatalf("C ||= 1: expected a ConstAssign on the right, got %T", or.Right)
}
// The scoped `||=` form is guarded the same way and targets a ScopedConst.
prog, err = Parse(`A::B ||= 1`)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
or = prog.Body[0].(*ast.BinaryExpr)
if _, ok := or.Right.(*ast.ScopedConstAssign); !ok {
t.Fatalf("A::B ||= 1: expected a ScopedConstAssign on the right, got %T", or.Right)
}
}
// TestPowerAssign covers the `**=` power-assignment operator, which the lexer
// previously split into `**` and `=` (a parse error), for local, instance, and
// constant targets and right-associatively chained.
func TestPowerAssign(t *testing.T) {
for _, src := range []string{`x **= 2`, `@i **= 2`, `C **= 2`, `a **= b **= 2`} {
if _, err := Parse(src); err != nil {
t.Errorf("Parse(%q) error: %v", src, err)
}
}
// `**` and `**=` remain distinct: `x ** 2` is the power operator.
prog, err := Parse(`x ** 2`)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
if bin, ok := prog.Body[0].(*ast.BinaryExpr); !ok || bin.Op != "**" {
t.Fatalf("x ** 2: expected a `**` BinaryExpr, got %#v", prog.Body[0])
}
}