Repository navigation
Expand file tree
/
Copy pathgroup_test.go
More file actions
185 lines (155 loc) · 3.08 KB
/
Copy pathgroup_test.go
File metadata and controls
185 lines (155 loc) · 3.08 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package gg
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"log"
"os"
"testing"
"text/template"
)
func ExampleNewGroup() {
f := NewGroup()
f.AddPackage("main")
f.NewImport().AddPath("fmt")
f.NewFunction("main").AddBody(
String(`fmt.Println("%s")`, "Hello, World!"),
)
fmt.Println(f.String())
// Output:
// package main
//
// import "fmt"
// func main(){
// fmt.Println("Hello, World!")}
}
func TestParseAST(t *testing.T) {
content := `package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", content, parser.AllErrors)
if err != nil {
t.Error(err)
}
ast.Print(fset, f)
}
func TestViaGolangAST(t *testing.T) {
fset := token.NewFileSet()
f := &ast.File{
Name: ast.NewIdent("main"),
Scope: ast.NewScope(nil),
}
f.Decls = append(f.Decls, &ast.GenDecl{
Tok: token.IMPORT,
Specs: []ast.Spec{
&ast.ImportSpec{
Path: &ast.BasicLit{
Kind: token.STRING,
Value: `"fmt"`,
},
},
},
})
f.Decls = append(f.Decls, &ast.FuncDecl{
Name: ast.NewIdent("main"),
Type: &ast.FuncType{},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ExprStmt{X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: ast.NewIdent("fmt"),
Sel: ast.NewIdent("Println"),
},
Args: []ast.Expr{
&ast.BasicLit{
Kind: token.STRING,
Value: `"Hello, World!"`,
},
},
}},
},
},
})
err := format.Node(os.Stdout, fset, f)
if err != nil {
log.Fatalf("ast is incorrect")
}
}
func TestViaString(t *testing.T) {
b := &bytes.Buffer{}
fmt.Fprintf(b, "package %s\n\n", "main")
fmt.Fprintf(b, "import %s\n\n", `"fmt"`)
fmt.Fprintf(b, "func main() {\n")
fmt.Fprintf(b, "\tfmt.Println(%s)\n", `"Hello, World!"`)
fmt.Fprint(b, "}\n")
fmt.Println(b.String())
}
func TestViaGolangTemplate(t *testing.T) {
b := &bytes.Buffer{}
data := struct {
Package string
Import []string
Content string
}{
Package: "main",
Import: []string{"fmt"},
Content: "Hello, World!",
}
tmpl := `package {{ .Package }}
import (
{{ range $_, $v := .Import -}}
"{{ $v }}"
{{ end -}}
)
func main() {
fmt.Println("{{ .Content }}")
}`
err := template.Must(template.New("test").Parse(tmpl)).Execute(b, data)
if err != nil {
t.Error(err)
}
fmt.Println(b.String())
}
func TestGroup(t *testing.T) {
buf := pool.Get()
defer buf.Free()
expected := `import "math"
// test line comment
// test add string
type Test string
type Alias = string
if true {}
for true {}
switch true {}
var name = 123
const name = 123
func test() {
}
type test struct {}
type test interface {}
`
g := NewGroup()
g.NewImport().AddPath("math")
g.AddLineComment("test line comment")
g.AddLine()
g.AddString("// test add string")
g.AddType("Test", "string")
g.AddTypeAlias("Alias", "string")
g.NewIf("true")
g.NewFor("true")
g.NewSwitch("true")
g.NewVar().AddField("name", "123")
g.NewConst().AddField("name", "123")
g.NewFunction("test").AddBody(Line())
g.NewStruct("test")
g.NewInterface("test")
g.render(buf)
compareAST(t, expected, buf.String())
}