Repository navigation
Expand file tree
/
Copy pathblock_param_test.go
More file actions
100 lines (94 loc) · 3.25 KB
/
Copy pathblock_param_test.go
File metadata and controls
100 lines (94 loc) · 3.25 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
package parser_test
import (
"testing"
"github.com/go-ruby-parser/parser"
"github.com/go-ruby-parser/parser/ast"
)
// TestBlockBlockParam covers the `&block` parameter in brace, do, and stabby
// lambda parameter lists. It is recorded on ast.Block.BlockParam, parallel to
// MethodDef.BlockParam — and is always the last parameter.
func TestBlockBlockParam(t *testing.T) {
cases := []struct {
src string
wantParams int
wantSplat int
wantBlock string
}{
{"foo { |*a, &b| b }", 1, 0, "b"},
{"foo do |x, &b| b end", 1, -1, "b"},
{"->(x, &b){ b }", 1, -1, "b"},
{"foo { |&b| b }", 0, -1, "b"},
{"foo { |x, y, &blk| blk }", 2, -1, "blk"},
{"foo { |x| x }", 1, -1, ""}, // no &block: BlockParam empty
{"->(x){ x }", 1, -1, ""},
}
for _, c := range cases {
blk := blockOf(t, c.src)
if len(blk.Params) != c.wantParams {
t.Errorf("%q: params=%d, want %d", c.src, len(blk.Params), c.wantParams)
}
if blk.SplatIndex != c.wantSplat {
t.Errorf("%q: splat=%d, want %d", c.src, blk.SplatIndex, c.wantSplat)
}
if blk.BlockParam != c.wantBlock {
t.Errorf("%q: BlockParam=%q, want %q", c.src, blk.BlockParam, c.wantBlock)
}
}
}
// TestBlockBlockParamDeclaredLocal: the &block name is in scope inside the body
// (it resolves as a local, not a bare method call).
func TestBlockBlockParamDeclaredLocal(t *testing.T) {
blk := blockOf(t, "foo { |&b| b }")
if _, ok := blk.Body[0].(*ast.VarRef); !ok {
t.Errorf("body[0]=%T, want *ast.VarRef (b is a declared local)", blk.Body[0])
}
}
// TestDefBlockParamUnchanged: `def f(x, &b)` still records the method block param
// on MethodDef, untouched by the block-literal change.
func TestDefBlockParamUnchanged(t *testing.T) {
prog, err := parser.Parse("def f(x, &b)\n b\nend")
if err != nil {
t.Fatalf("Parse: %v", err)
}
def := prog.Body[0].(*ast.MethodDef)
if def.BlockParam != "b" {
t.Errorf("MethodDef.BlockParam=%q, want %q", def.BlockParam, "b")
}
}
// TestBlockLocalsAreCarriedOnTheBlock pins that the block-local list after the
// `;` reaches the AST. MRI's `opt_bv_decl: '\n'? ';' bv_decls '\n'?` with
// `bvar: tIDENTIFIER { new_bv(p, $1); }` (parse.y v3_4_0) adds each name to the
// block's own local table, where it is nil-valued and shadows any enclosing
// binding — so a consumer needs the names to give them slots.
func TestBlockLocalsAreCarriedOnTheBlock(t *testing.T) {
for _, tc := range []struct {
src string
params []string
locals []string
}{
{`f { |one; bl| bl }`, []string{"one"}, []string{"bl"}},
{`f { |one; bl, bl2| bl }`, []string{"one"}, []string{"bl", "bl2"}},
{`f { |one| one }`, []string{"one"}, nil},
} {
prog, err := parser.Parse(tc.src)
if err != nil {
t.Fatalf("Parse(%q) error: %v", tc.src, err)
}
call, ok := prog.Body[0].(*ast.Call)
if !ok || call.Block == nil {
t.Fatalf("Parse(%q): want a call with a block", tc.src)
}
if got := call.Block.Locals; len(got) != len(tc.locals) {
t.Errorf("Parse(%q): locals = %q, want %q", tc.src, got, tc.locals)
} else {
for i, w := range tc.locals {
if got[i] != w {
t.Errorf("Parse(%q): local %d = %q, want %q", tc.src, i, got[i], w)
}
}
}
if len(call.Block.Params) != len(tc.params) {
t.Errorf("Parse(%q): params = %q, want %q", tc.src, call.Block.Params, tc.params)
}
}
}