-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsers_test.go
62 lines (54 loc) · 1.21 KB
/
parsers_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
package toast
import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestGoFileFromAST(t *testing.T) {
path := "./mock"
loadFiles(t, path, "go")
}
func TestCUEFileFromAST(t *testing.T) {
path := "./mock"
loadFiles(t, path, "cue")
}
func loadFiles(t *testing.T, dirPath, output string) {
entries, err := os.ReadDir(dirPath)
if err != nil {
t.Fatal(err)
}
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".go") {
continue
}
filePath := fmt.Sprintf("%s/%s", dirPath, entry.Name())
load(t, filePath, output)
}
}
func load(t *testing.T, path, output string) {
fset := token.NewFileSet()
astFile, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
f, _ := NewFile(astFile, WithCUEPackageName("mockcue"))
// f.debug = true
outputPath := path[strings.LastIndex(path, "/")+1:]
var out string
if output == "go" {
out = f.Go()
} else {
out = f.CUE()
outputPath = outputPath[:len(outputPath)-3] + ".cue"
}
if err := os.MkdirAll("./.testdata/", 0755); err != nil {
panic(err)
}
if err := ioutil.WriteFile("./.testdata/"+outputPath, []byte(out), 0644); err != nil {
panic(err)
}
}