-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_test.go
135 lines (124 loc) · 3.15 KB
/
test_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
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
package main
import (
"strings"
"testing"
)
func TestExtractRangeVar(t *testing.T) {
want := "Searchers"
s := `//# {{ range .Searchers }}
_TemplateSearcherKey _TemplateSearcherKeyType
//# {{ end }}`
got := extractRangeIterVariable(s)
if got != want {
t.Errorf("want: %s;\n got: %s", want, got)
}
}
func TestPreprocessRangeStatements(t *testing.T) {
s := `package searcher_templates
type Query struct {
Count int
//# {{ range $key, $value := .Searchers }}
_TemplateSearcherKey _TemplateSearcherKeyType
//# {{ end }}
}
`
want := `package searcher_templates
type Query struct {
Count int
//# {{ range $key, $value := .Searchers }}
{{ .Key }} {{ .KeyType }}
//# {{ end }}
}`
got := preprocessRangeStatements(s)
if strings.TrimSpace(want) != strings.TrimSpace(got) {
t.Errorf("want: %s;\n got: %s", want, got)
}
}
//func TestPreprocessCodeTemplate(t *testing.T) {
// s := `package searcher_templates
//
// //# {{ range .Searchers }}
// type _TemplateSearcherName struct {
// data map[_TemplateSearcherKeyType][]*_TemplateModelName
// sortSliceFn SliceSortFN
// searchFn SliceElementSearchFN
// }
//
// func (sm *_TemplateSearcherName) init(data []_TemplateModelName,
// sortSliceFn SliceSortFN,
// searchFn SliceElementSearchFN) {
//
// sm.data = map[_TemplateSearcherKeyType][]*_TemplateModelName{}
// sm.sortSliceFn = sortSliceFn
// sm.searchFn = searchFn
//
// for i, e := range data {
// sm.data[e._TemplateSearcherKey] = append(sm.data[e._TemplateSearcherKey], &data[i])
// }
//
// for k := range sm.data {
// sortSliceFn(sm.data[k])
// }
// }
//
// func (sm *_TemplateSearcherName) find(q *Query) searchResult {
// return newSimpleResult(
// sm.data[q._TemplateSearcherKey],
// sm.searchFn)
// }
//
// //# {{ end }}
// `
//
// want := `package searcher_templates
//
// {{ range .Searchers }}
// type {{ .Name }} struct {
// data map[{{ .KeyType }}][]*{{ $.ModelName }}
// sortSliceFn SliceSortFN
// searchFn SliceElementSearchFN
// }
//
// func (sm *{{ .Name }}) init(data []{{ $.ModelName }},
// sortSliceFn SliceSortFN,
// searchFn SliceElementSearchFN) {
//
// sm.data = map[{{ .KeyType }}][]*{{ $.ModelName }}{}
// sm.sortSliceFn = sortSliceFn
// sm.searchFn = searchFn
//
// for i, e := range data {
// sm.data[e.{{ .Key }}] = append(sm.data[e.{{ .Key }}], &data[i])
// }
//
// for k := range sm.data {
// sortSliceFn(sm.data[k])
// }
// }
//
// func (sm *{{ .Name }}) find(q *Query) searchResult {
// return newSimpleResult(
// sm.data[q.{{ .Key }}],
// sm.searchFn)
// }
//
// {{ end }}`
//
// got := preprocessCodeTemplate(s)
// if strings.TrimSpace(want) != strings.TrimSpace(got) {
// t.Errorf("want: %s;\n got: %s", want, got)
// }
//}
//func TestPrerpocessRangeBodyTemplate(t *testing.T) {
// s := `//# {{ range .Searchers }}
// _TemplateSearcherKey _TemplateSearcherKeyType
// //# {{ end }}`
//
// want := `//# {{ range .Searchers }}
// {{ $.SearcherKey }} {{ $.SearcherKeyType }}
// //# {{ end }}`
// got := prerpocessTemplateVars(s, "")
// if strings.TrimSpace(want) != strings.TrimSpace(got) {
// t.Errorf("want: %s\n got: %s", want, got)
// }
//}