forked from jabley/mustache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.go
190 lines (157 loc) · 3.95 KB
/
gen.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
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
186
187
188
189
190
// +build ignore
package main
// This program generates mustache_spec_test.go. Invoke it as
// go run gen.go -output mustache_spec_test.go
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
type JSONSpecDoc struct {
Tests []JSONSpecTest
}
type JSONSpecTest struct {
Name string `json:"name"`
Data map[string]interface{} `json:"data"`
Expected string `json:"expected"`
Template string `json:"template"`
Description string `json:"desc"`
Partials *map[string]string `json:"partials"`
}
var filename = flag.String("output", "mustache_spec_test.go", "output file name")
var supportedSpecNames = []string{
"comments",
"delimiters",
"interpolation",
"inverted",
"partials",
"sections",
"lambdas",
}
func main() {
flag.Parse()
var buf bytes.Buffer
writeHeader(&buf)
r := strings.NewReplacer(" ", "", "-", "", "(", "", ")", "", "~", "")
testSpec := func(scope string, test JSONSpecTest) {
fmt.Fprint(&buf, `func `)
fmt.Fprint(&buf, "Test"+r.Replace(strings.Title(scope))+r.Replace(test.Name))
fmt.Fprintln(&buf, `(t *testing.T) {`)
if test.Partials != nil {
for k, v := range *test.Partials {
name := k + ".mustache"
fmt.Fprintf(&buf, "\tgeneratePartial(%#v, %#v)\n", name, v)
fmt.Fprintf(&buf, "\tdefer os.Remove(%#v)\n", name)
}
}
lambda, hasLambda := test.Data["lambda"]
if hasLambda {
if fns, ok := lambda.(map[string]interface{}); ok {
if fn, ok := fns["go"]; ok {
fmt.Fprintf(&buf, "\tlambda := %v\n", fn)
}
}
delete(test.Data, "lambda")
}
fmt.Fprintf(&buf, "\ttemplate := %#v\n", test.Template)
fmt.Fprintf(&buf, "\texpected := %#v\n", test.Expected)
fmt.Fprintf(&buf, "\tcontext := %#v\n", test.Data)
if hasLambda {
fmt.Fprintf(&buf, "\tcontext[\"lambda\"] = lambda\n")
}
fmt.Fprintf(&buf, "\ttestSpec(t, template, expected, context)\n")
fmt.Fprintf(&buf, "}\n\n")
}
generateTests := func(pathName string) {
data, err := ioutil.ReadFile(pathName)
if err != nil {
log.Fatal(err)
}
var jsonDocSpec JSONSpecDoc
err = json.Unmarshal(data, &jsonDocSpec)
if err != nil {
log.Fatal(err)
}
base := filepath.Base(pathName)
ext := filepath.Ext(base)
scope := base[0 : len(base)-len(ext)]
for _, test := range jsonDocSpec.Tests {
testSpec(scope, test)
}
}
filepath.Walk("ext/spec/specs/", func(path string, info os.FileInfo, err error) error {
if !strings.HasSuffix(path, ".json") {
return nil
}
if !isSupportedSpec(path) {
return nil
}
generateTests(path)
return nil
})
data, err := format.Source(buf.Bytes())
if err != nil {
log.Fatalf("%v\n%s", err, buf.Bytes())
}
err = ioutil.WriteFile(*filename, data, 0644)
if err != nil {
log.Fatalf("%v\n%v", err, data)
}
}
func isSupportedSpec(path string) bool {
for _, specName := range supportedSpecNames {
if strings.Contains(path, specName) {
return true
}
}
return false
}
func writeHeader(buf *bytes.Buffer) {
fmt.Fprintln(buf)
fmt.Fprintf(buf, "// generated by go run gen.go -output %v; DO NOT EDIT\n", *filename)
fmt.Fprintln(buf)
fmt.Fprintln(buf, "package mustache")
fmt.Fprintln(buf)
fmt.Fprintln(buf)
fmt.Fprintln(buf, `import (
"os"
"strings"
"strconv"
"testing"
)
func convertHTMLCharsToExpectedFormat(s string) string {
return strings.Replace(s, """, """, -1)
}
func generatePartial(name string, content string) {
fo, err := os.Create(name)
if err != nil {
panic(err)
}
defer func() {
if err := fo.Close(); err != nil {
panic(err)
}
}()
if _, err := fo.Write([]byte(content)); err != nil {
panic(err)
}
}
func testSpec(t *testing.T,
template string,
expected string,
context interface{}) {
output := convertHTMLCharsToExpectedFormat(Render(template, context))
if output != expected {
t.Errorf("%q\nexpected: %q\nbut got: %q",
template, expected, output)
}
}
`)
}