Repository navigation
Expand file tree
/
Copy patharg_forwarding_test.go
More file actions
112 lines (101 loc) · 2.78 KB
/
Copy patharg_forwarding_test.go
File metadata and controls
112 lines (101 loc) · 2.78 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
package parser_test
import (
"testing"
"github.com/go-ruby-parser/parser"
"github.com/go-ruby-parser/parser/ast"
)
func methodDef(t *testing.T, src string) *ast.MethodDef {
t.Helper()
m, ok := parseOne(t, src).(*ast.MethodDef)
if !ok {
t.Fatalf("Parse(%q): node = %T, want *ast.MethodDef", src, parseOne(t, src))
}
return m
}
// firstCall returns the first *ast.Call found in a method body.
func firstCall(t *testing.T, m *ast.MethodDef) *ast.Call {
t.Helper()
for _, n := range m.Body {
if c, ok := n.(*ast.Call); ok {
return c
}
}
t.Fatalf("no call in method body")
return nil
}
func TestForwardDefAndCall(t *testing.T) {
m := methodDef(t, "def f(...); g(...); end")
if !m.Forward {
t.Error("MethodDef.Forward = false, want true")
}
if len(m.Params) != 0 {
t.Errorf("Params = %v, want empty", m.Params)
}
call := firstCall(t, m)
if len(call.Args) != 1 {
t.Fatalf("call args = %d, want 1", len(call.Args))
}
if _, ok := call.Args[0].(*ast.ForwardArgs); !ok {
t.Errorf("call arg = %T, want *ast.ForwardArgs", call.Args[0])
}
}
func TestForwardWithLeadingParam(t *testing.T) {
m := methodDef(t, "def f(a, ...); g(a, ...); end")
if !m.Forward {
t.Error("Forward = false, want true")
}
if len(m.Params) != 1 || m.Params[0] != "a" {
t.Errorf("Params = %v, want [a]", m.Params)
}
call := firstCall(t, m)
if len(call.Args) != 2 {
t.Fatalf("call args = %d, want 2", len(call.Args))
}
if _, ok := call.Args[1].(*ast.ForwardArgs); !ok {
t.Errorf("last arg = %T, want *ast.ForwardArgs", call.Args[1])
}
}
func TestForwardWithPositionalThenForward(t *testing.T) {
m := methodDef(t, "def f(...); g(1, ...); end")
call := firstCall(t, m)
if len(call.Args) != 2 {
t.Fatalf("call args = %d, want 2", len(call.Args))
}
if _, ok := call.Args[0].(*ast.IntLit); !ok {
t.Errorf("arg0 = %T, want *ast.IntLit", call.Args[0])
}
if _, ok := call.Args[1].(*ast.ForwardArgs); !ok {
t.Errorf("arg1 = %T, want *ast.ForwardArgs", call.Args[1])
}
}
func TestForwardDefOnly(t *testing.T) {
m := methodDef(t, "def f(...); end")
if !m.Forward {
t.Error("Forward = false, want true")
}
}
func TestForwardParenlessDef(t *testing.T) {
m := methodDef(t, "def f ...\nend")
if !m.Forward {
t.Error("paren-less Forward = false, want true")
}
}
func TestForwardDoesNotBreakRange(t *testing.T) {
for _, src := range []string{"(1...5)", "g(1...5)", "a = 1...10", "x[1...3]"} {
if _, err := parser.Parse(src); err != nil {
t.Errorf("Parse(%q) returned error: %v", src, err)
}
}
}
func TestForwardValidPrograms(t *testing.T) {
for _, src := range []string{
"def f(...); g(...); end",
"def f(a, ...); g(a, ...); end",
"def f(...); g(1, ...); end",
"def f(...); end",
} {
if _, err := parser.Parse(src); err != nil {
t.Errorf("Parse(%q) returned error: %v", src, err)
}
}
}