-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastree_test.go
91 lines (76 loc) · 1.56 KB
/
astree_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package astree
import (
"bytes"
"go/parser"
"go/token"
"strings"
"testing"
)
func TestInternalTree(t *testing.T) {
src := `package main
import "fmt"
var foo int
func main() {
foo = 1
fmt.Println(foo)
}
`
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "src.go", src, 0)
if err != nil {
t.Fatal(err)
}
b := &bytes.Buffer{}
tree(b, fset, "", []string{"", ""}, f)
// its ok if no panic by invalid memory address error
}
func TestTree_Generics(t *testing.T) {
src := `package main
func main() {
ints := map[string]int64{
"first": 34,
"second": 12,
}
v := SumIntsOrFloats[string, int64](ints)
println(v)
}
func SumIntsOrFloats[K comparable, V int64 | float64](m map[K]V) V {
var s V
for _, v := range m {
s += v
}
return s
}
`
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "src.go", src, 0)
if err != nil {
t.Fatal(err)
}
b := &bytes.Buffer{}
tree(b, fset, "", []string{"", ""}, f)
// its ok if no panic by invalid memory address error
}
func TestFile_NilFileSet(t *testing.T) {
src := `package main
import "fmt"
var foo int
func main() {
foo = 1
fmt.Println(foo)
}
`
fset := token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, "src.go", src, 0)
if err != nil {
t.Fatal(err)
}
b := &bytes.Buffer{}
err = File(b, nil, f)
if err == nil {
t.Error("expected to cause error")
}
if !strings.Contains(err.Error(), "*token.FileSet is nil") {
t.Errorf("error message is not expected. actual=%s", err.Error())
}
}