Repository navigation
Expand file tree
/
Copy pathblock_comment_test.go
More file actions
73 lines (67 loc) · 1.87 KB
/
Copy pathblock_comment_test.go
File metadata and controls
73 lines (67 loc) · 1.87 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
package parser
import (
"testing"
"github.com/go-ruby-parser/parser/ast"
)
// `=begin` … `=end` block comments (at column 0) are skipped wholesale, with the
// surrounding code parsing normally.
func TestBlockComment(t *testing.T) {
cases := []string{
"=begin\nthis is a comment\nmore\n=end\nputs 1",
"x = 1\n=begin\ncomment\n=end\ny = 2",
"=begin doc\nfoo\n=end",
"=begin\nunterminated comment to EOF",
}
for _, src := range cases {
if _, err := Parse(src); err != nil {
t.Fatalf("Parse(%q) error: %v", src, err)
}
}
}
// A `=begin` not at column 0 is not a block comment: `x = begin … end` is an
// ordinary begin expression assigned to x.
func TestBlockCommentNotAtLineStart(t *testing.T) {
prog, err := Parse("x = begin\n 1\nend")
if err != nil {
t.Fatalf("Parse error: %v", err)
}
asn, ok := prog.Body[0].(*ast.Assign)
if !ok {
t.Fatalf("expected *ast.Assign, got %T", prog.Body[0])
}
if _, ok := asn.Value.(*ast.Begin); !ok {
t.Fatalf("expected RHS *ast.Begin, got %T", asn.Value)
}
}
// An identifier that merely starts with "begin" is unaffected.
func TestBlockCommentIdentifierPrefix(t *testing.T) {
if _, err := Parse("result = beginning"); err != nil {
t.Fatalf("Parse error: %v", err)
}
}
// The bitwise op-assignments |=, &=, ^=, >>= now lex (they were previously
// missing, so `mode |= x` failed). Confirm each produces an OpAssign.
func TestBitwiseOpAssign(t *testing.T) {
for _, c := range []struct {
src string
op string
}{
{"mode |= x", "|"},
{"a &= b", "&"},
{"c ^= d", "^"},
{"x >>= 2", ">>"},
{"y <<= 1", "<<"},
} {
prog, err := Parse(c.src)
if err != nil {
t.Fatalf("Parse(%q) error: %v", c.src, err)
}
oa, ok := prog.Body[0].(*ast.OpAssign)
if !ok {
t.Fatalf("%q: expected *ast.OpAssign, got %T", c.src, prog.Body[0])
}
if oa.Op != c.op {
t.Fatalf("%q: op=%q, want %q", c.src, oa.Op, c.op)
}
}
}