-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrite_test.go
93 lines (74 loc) · 1.3 KB
/
write_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
92
93
// Copyright 2020 murosan. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gollect
import (
"bytes"
"path/filepath"
"testing"
"github.com/murosan/gollect/testdata"
dmp "github.com/sergi/go-diff/diffmatchpatch"
)
func TestWrite(t *testing.T) {
cases := []struct {
path,
want string
}{
{
path: testdata.FilePaths.Write1,
want: `package main
import (
"fmt"
f "fmt"
)
func main() {
fmt.Println(NonFormatted1)
Abc()
Def()
}
var NonFormatted1 = func() int {
return 100
}()
// comment
func Abc() {
fmt.Println("abc")
}
func Def() { f.Println("def") }
`,
},
{
path: testdata.FilePaths.Write2,
want: `package main
func main() {
FuncA()
FuncB()
}
func FuncA() {}
func FuncB() {
}
`,
},
}
for i, c := range cases {
var buf bytes.Buffer
program := NewProgram()
paths, _ := filepath.Glob(c.path)
ParseAll(program, "main", paths)
AnalyzeForeach(program, "main", "main")
pkg := NewPackage("main")
d, _ := program.DeclSet().Get(pkg, "main")
d.Use()
err := Write(&buf, program)
if err != nil {
t.Fatal(err)
}
if buf.String() != c.want {
diff := dmp.New().DiffMain(c.want, buf.String(), true)
t.Errorf(
"\n[at] %d\n[diff]\n%s",
i,
colorDiff(diff),
)
}
}
}