Repository navigation
Expand file tree
/
Copy pathsingleton_class_test.go
More file actions
132 lines (121 loc) · 4.51 KB
/
Copy pathsingleton_class_test.go
File metadata and controls
132 lines (121 loc) · 4.51 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
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
package parser_test
import (
"testing"
"github.com/go-ruby-parser/parser"
"github.com/go-ruby-parser/parser/ast"
)
// topSingleton parses src and returns the single top-level SingletonClassDef.
func topSingleton(t *testing.T, src string) *ast.SingletonClassDef {
t.Helper()
prog, err := parser.Parse(src)
if err != nil {
t.Fatalf("Parse(%q): %v", src, err)
}
if len(prog.Body) != 1 {
t.Fatalf("Parse(%q): want 1 top-level node, got %d", src, len(prog.Body))
}
sc, ok := prog.Body[0].(*ast.SingletonClassDef)
if !ok {
t.Fatalf("Parse(%q): top node is %T, want *ast.SingletonClassDef", src, prog.Body[0])
}
return sc
}
// TestSingletonClassSelf covers `class << self` — the idiomatic class-method
// form. The target is *SelfLit and the body holds the defined methods.
func TestSingletonClassSelf(t *testing.T) {
sc := topSingleton(t, "class << self; def x; 1; end; end")
if _, ok := sc.Target.(*ast.SelfLit); !ok {
t.Fatalf("Target is %T, want *ast.SelfLit", sc.Target)
}
if len(sc.Body) != 1 {
t.Fatalf("Body has %d nodes, want 1", len(sc.Body))
}
md, ok := sc.Body[0].(*ast.MethodDef)
if !ok || md.Name != "x" {
t.Fatalf("Body[0] = %T (%+v), want method def x", sc.Body[0], sc.Body[0])
}
}
// TestSingletonClassSelfNewlines covers the multi-line layout (no `;`).
func TestSingletonClassSelfNewlines(t *testing.T) {
sc := topSingleton(t, "class << self\n def x\n 1\n end\nend")
if _, ok := sc.Target.(*ast.SelfLit); !ok {
t.Fatalf("Target is %T, want *ast.SelfLit", sc.Target)
}
if len(sc.Body) != 1 {
t.Fatalf("Body has %d nodes, want 1", len(sc.Body))
}
}
// TestSingletonClassLocalTarget covers `class << obj` where obj is a local
// variable — the target resolves to a VarRef read.
func TestSingletonClassLocalTarget(t *testing.T) {
prog, err := parser.Parse("o = Object.new\nclass << o\n def y; 2; end\nend")
if err != nil {
t.Fatalf("Parse: %v", err)
}
if len(prog.Body) != 2 {
t.Fatalf("want 2 top-level nodes, got %d", len(prog.Body))
}
sc, ok := prog.Body[1].(*ast.SingletonClassDef)
if !ok {
t.Fatalf("Body[1] is %T, want *ast.SingletonClassDef", prog.Body[1])
}
vr, ok := sc.Target.(*ast.VarRef)
if !ok || vr.Name != "o" {
t.Fatalf("Target = %T (%+v), want VarRef o", sc.Target, sc.Target)
}
}
// TestSingletonClassConstTarget covers `class << SomeConst` (e.g. a module's
// metaclass) — the target is a constant call/ref expression.
func TestSingletonClassConstTarget(t *testing.T) {
sc := topSingleton(t, "class << Foo\n def bar; end\nend")
if _, ok := sc.Target.(*ast.ConstRef); !ok {
t.Fatalf("Target is %T, want *ast.ConstRef", sc.Target)
}
}
// TestSingletonClassExprTarget covers an arbitrary expression target such as a
// method-call result: `class << obj.thing`.
func TestSingletonClassExprTarget(t *testing.T) {
sc := topSingleton(t, "class << obj.thing\n def z; end\nend")
call, ok := sc.Target.(*ast.Call)
if !ok || call.Name != "thing" || call.Recv == nil {
t.Fatalf("Target = %T (%+v), want call to thing on a receiver", sc.Target, sc.Target)
}
}
// TestSingletonClassAssignTarget covers `class << @x = expr` — MRI allows the
// singleton target to be an assignment (assign, then open the result's singleton
// class), which needs a full expression, not just a ternary, after the `<<`.
func TestSingletonClassAssignTarget(t *testing.T) {
sc := topSingleton(t, "class << @receiver = Object.new\n def m; end\nend")
asn, ok := sc.Target.(*ast.IvarAssign)
if !ok || asn.Name != "@receiver" {
t.Fatalf("Target = %T (%+v), want *ast.IvarAssign @receiver", sc.Target, sc.Target)
}
if len(sc.Body) != 1 {
t.Fatalf("Body has %d nodes, want 1", len(sc.Body))
}
}
// TestSingletonClassEmptyBody covers a singleton class with no body statements.
func TestSingletonClassEmptyBody(t *testing.T) {
sc := topSingleton(t, "class << self\nend")
if len(sc.Body) != 0 {
t.Fatalf("Body has %d nodes, want 0", len(sc.Body))
}
}
// TestSingletonClassNested covers the common `class Foo; class << self; ...`
// nesting used to declare class methods inside a class body.
func TestSingletonClassNested(t *testing.T) {
prog, err := parser.Parse("class Foo\n class << self\n def bar; end\n end\nend")
if err != nil {
t.Fatalf("Parse: %v", err)
}
cd, ok := prog.Body[0].(*ast.ClassDef)
if !ok || cd.Name != "Foo" {
t.Fatalf("top is %T, want ClassDef Foo", prog.Body[0])
}
if len(cd.Body) != 1 {
t.Fatalf("class body has %d nodes, want 1", len(cd.Body))
}
if _, ok := cd.Body[0].(*ast.SingletonClassDef); !ok {
t.Fatalf("class body[0] is %T, want *ast.SingletonClassDef", cd.Body[0])
}
}