-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple_test.go
56 lines (45 loc) · 1.19 KB
/
simple_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
package formatgo
import (
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestFormatCode(t *testing.T) {
const code = `
package main
import (
"fmt"
"strconv"
)
import "time"
//这是main函数的注释
func main() {
fmt.Println("abc") //随便打印个字符串
fmt.Println(time.Now()) //随便打印当前时间
fmt.Println(strconv.Itoa(1))
}
`
t.Log(code)
newCode, err := FormatCode(code)
require.NoError(t, err)
t.Log(newCode)
}
func TestFormatFile(t *testing.T) {
_, path, _, ok := runtime.Caller(0)
require.True(t, ok)
t.Log(path)
require.True(t, strings.HasSuffix(path, "/formatgo/simple_test.go")) //需要确保就是这个文件
require.NoError(t, FormatFile(path)) //把该文件格式化
}
func TestFormatProject(t *testing.T) {
_, path, _, ok := runtime.Caller(0)
require.True(t, ok)
t.Log(path)
require.True(t, strings.HasSuffix(path, "/formatgo/simple_test.go"))
root := filepath.Dir(path)
t.Log(root)
require.True(t, strings.HasSuffix(root, "/formatgo")) //这样99.99%能够确保目录路径是正确的
require.NoError(t, FormatRoot(root)) //把本项目格式化
}