Repository navigation
Expand file tree
/
Copy pathclass_expr_test.go
More file actions
64 lines (60 loc) · 2.12 KB
/
Copy pathclass_expr_test.go
File metadata and controls
64 lines (60 loc) · 2.12 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
package parser_test
import (
"fmt"
"testing"
"github.com/go-ruby-parser/parser"
"github.com/go-ruby-parser/parser/ast"
)
// TestDefinitionIsAPrimary pins MRI's grammar: `k_class cpath superclass
// bodystmt k_end`, `k_class tLSHFT expr_value term bodystmt k_end`,
// `k_module cpath bodystmt k_end` and `defn_head f_arglist bodystmt k_end` are
// all alternatives of `primary` (parse.y v3_4_0), so a definition composes with
// everything a primary composes with — not only a trailing `.method` and a
// statement modifier, but binary operators too. `class C; end.should == nil` is
// the shape ruby/spec uses to pin a class body's value.
func TestDefinitionIsAPrimary(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
src string
}{
{"class compared", `class C; end.should == nil`},
{"class with a body compared", `class C; 20; end.value == 20`},
{"singleton class compared", `class << self; :s; end.value == :s`},
{"module compared", `module M; end.to_s == "M"`},
{"def compared", `def f; end.to_s == "f"`},
{"class in a boolean chain", `class C; end.nil? && true`},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
n := parseOne(t, tc.src)
if _, ok := n.(*ast.BinaryExpr); !ok {
t.Fatalf("Parse(%q): node = %T, want *ast.BinaryExpr", tc.src, n)
}
})
}
}
// TestDefinitionKeepsStatementForms checks the plain statement shapes still
// parse the same way once definitions go through the expression path.
func TestDefinitionKeepsStatementForms(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
src string
want any
}{
{`class C; end`, (*ast.ClassDef)(nil)},
{`module M; end`, (*ast.ModuleDef)(nil)},
{`def f; end`, (*ast.MethodDef)(nil)},
{`class C; end if true`, (*ast.If)(nil)},
{`def f; end if true`, (*ast.If)(nil)},
{`def f; end.tap { |x| x }`, (*ast.Call)(nil)},
} {
n := parseOne(t, tc.src)
if got, want := fmt.Sprintf("%T", n), fmt.Sprintf("%T", tc.want); got != want {
t.Errorf("Parse(%q): node = %s, want %s", tc.src, got, want)
}
}
if _, err := parser.Parse("class C; def m; end; end"); err != nil {
t.Errorf("nested definition: %v", err)
}
}