-
Notifications
You must be signed in to change notification settings - Fork 48
/
render_test.go
125 lines (105 loc) · 2.97 KB
/
render_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
package render
import (
"log"
"strings"
"testing"
"github.com/matryer/is"
"github.com/pacedotdev/oto/parser"
)
func TestRender(t *testing.T) {
is := is.New(t)
def := parser.Definition{
PackageName: "services",
}
params := map[string]interface{}{
"Description": "Package services contains services.",
}
template := `// <%= params["Description"] %>
package <%= def.PackageName %>`
s, err := Render(template, def, params)
is.NoErr(err)
for _, should := range []string{
"// Package services contains services.",
"package services",
} {
if !strings.Contains(s, should) {
t.Errorf("missing: %s", should)
is.Fail()
}
}
}
// TestRenderCommentsWithQuotes addresses https://github.com/pacedotdev/oto/issues/17.
func TestRenderCommentsWithQuotes(t *testing.T) {
is := is.New(t)
def := parser.Definition{
PackageName: "services",
Services: []parser.Service{
{
Comment: `This comment contains "quotes"`,
Name: "MyService",
},
},
}
template := `
<%= for (service) in def.Services { %>
<%= format_comment_text(service.Comment) %>type <%= service.Name %> struct
<% } %>
`
s, err := Render(template, def, nil)
is.NoErr(err)
log.Println(s)
for _, should := range []string{
`// This comment contains "quotes"`,
} {
if !strings.Contains(s, should) {
t.Errorf("missing: %s", should)
is.Fail()
}
}
}
func TestCamelizeDown(t *testing.T) {
for in, expected := range map[string]string{
"CamelsAreGreat": "camelsAreGreat",
"ID": "id",
"HTML": "html",
"PreviewHTML": "previewHTML",
} {
actual := camelizeDown(in)
if actual != expected {
t.Errorf("%s expected: %q but got %q", in, expected, actual)
}
}
}
func TestFormatTags(t *testing.T) {
is := is.New(t)
trimBackticks := func(s string) string {
is.True(strings.HasPrefix(s, "`"))
is.True(strings.HasSuffix(s, "`"))
return strings.Trim(s, "`")
}
tagStr, err := formatTags(`json:"field,omitempty"`)
is.NoErr(err)
is.Equal(trimBackticks(string(tagStr)), `json:"field,omitempty"`)
tagStr, err = formatTags(`json:"field,omitempty" monkey:"true"`)
is.NoErr(err)
is.Equal(trimBackticks(string(tagStr)), `json:"field,omitempty" monkey:"true"`)
tagStr, err = formatTags(`json:"field,omitempty"`, `monkey:"true"`)
is.NoErr(err)
is.Equal(trimBackticks(string(tagStr)), `json:"field,omitempty" monkey:"true"`)
}
func TestFormatCommentText(t *testing.T) {
is := is.New(t)
actual := strings.TrimSpace(string(formatCommentText("card's")))
is.Equal(actual, "// card's")
actual = strings.TrimSpace(string(formatCommentText(`What happens if I use "quotes"?`)))
is.Equal(actual, `// What happens if I use "quotes"?`)
actual = strings.TrimSpace(string(formatCommentText("What about\nnew lines?")))
is.Equal(actual, `// What about new lines?`)
}
func TestSmartPrefix(t *testing.T) {
is := is.New(t)
actual := smartPrefix("public", "*Object")
is.Equal(actual, "*publicObject")
actual = smartPrefix("public", "Object")
is.Equal(actual, "publicObject")
}