-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes_test.go
73 lines (67 loc) · 1.53 KB
/
nodes_test.go
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
// Copyright (c) 2016, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package syntax
import (
"fmt"
"strings"
"testing"
)
func TestPosition(t *testing.T) {
t.Parallel()
parserBash := NewParser(KeepComments)
parserPosix := NewParser(KeepComments, Variant(LangPOSIX))
parserMirBSD := NewParser(KeepComments, Variant(LangMirBSDKorn))
for i, c := range fileTests {
for j, in := range c.Strs {
t.Run(fmt.Sprintf("%03d-%d", i, j), func(t *testing.T) {
parser := parserPosix
if c.Bash != nil {
parser = parserBash
} else if c.MirBSDKorn != nil {
parser = parserMirBSD
}
prog, err := parser.Parse(strings.NewReader(in), "")
if err != nil {
t.Fatalf("Unexpected error in %q: %v", in, err)
}
v := &posWalker{
t: t,
f: prog,
lines: strings.Split(in, "\n"),
}
Walk(prog, v.Visit)
})
}
}
}
type posWalker struct {
t *testing.T
f *File
lines []string
}
func (v *posWalker) Visit(n Node) bool {
if n == nil {
return true
}
p := n.Pos()
if !p.IsValid() && len(v.f.Stmts) > 0 {
v.t.Fatalf("Invalid Pos")
}
if c, ok := n.(*Comment); ok {
if v.f.Pos().After(c.Pos()) {
v.t.Fatalf("A Comment is before its File")
}
if c.End().After(v.f.End()) {
v.t.Fatalf("A Comment is after its File")
}
}
return true
}
func TestWeirdOperatorString(t *testing.T) {
t.Parallel()
op := RedirOperator(1000)
want := "token(1000)"
if got := op.String(); got != want {
t.Fatalf("token.String() mismatch: want %s, got %s", want, got)
}
}