-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcodegen_test.go
117 lines (109 loc) · 2.5 KB
/
codegen_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
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestCodeGenFromNode(t *testing.T) {
tests := []struct {
node node
want string
}{
{
node: &nodeElement{
tag: tag{name: "div", attrs: []*attr{{name: stringPos{string: "id"}, value: stringPos{string: "foo"}}}},
startTagNodes: []node{
&nodeLiteral{str: "<div "},
&nodeLiteral{str: "id=\""},
&nodeLiteral{str: "foo"},
&nodeLiteral{str: "\">"},
},
children: []node{&nodeLiteral{str: "bar"}},
},
want: `io.WriteString(w, "<div ")
io.WriteString(w, "id=\"")
io.WriteString(w, "foo")
io.WriteString(w, "\">")
io.WriteString(w, "bar")
io.WriteString(w, "</div>")
`,
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
page, err := newPageFromTree(&syntaxTree{nodes: []node{test.node}})
if err != nil {
t.Fatalf("new page from tree: %v", err)
}
g := newPageCodeGen(page, projectFile{}, "")
g.lineDirectivesEnabled = false
g.genNode(test.node)
got := g.bodyb.String()
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("expected code gen diff (-want +got):\n%s", diff)
}
})
}
}
func TestRouteForPage(t *testing.T) {
tests := []struct {
path string
want string
}{
{
"index.up",
"/",
},
{
"about.up",
"/about",
},
{
"x/sub.up",
"/x/sub",
},
{
"testdata/foo.up",
"/testdata/foo",
},
{
"x/$name.up",
"/x/:name",
},
{
"$projectId/$productId",
"/:projectId/:productId",
},
{
"blah/index.up",
"/blah/",
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
if got := routeForPage(test.path); test.want != got {
t.Errorf("want %q, got %q", test.want, got)
}
})
}
}
func TestGeneratedTypename(t *testing.T) {
tests := []struct {
pfile projectFile
strategy upFileType
want string
}{
{projectFile{path: "index.up", projectFilesSubdir: "."}, upFilePage, "IndexPage"},
{projectFile{path: "foo-bar.up", projectFilesSubdir: "."}, upFilePage, "FooBarPage"},
{projectFile{path: "foo_bar.up", projectFilesSubdir: "."}, upFilePage, "FooBarPage"},
{projectFile{path: "a/b/c.up", projectFilesSubdir: "."}, upFilePage, "ABCPage"},
{projectFile{path: "a/b/$c.up", projectFilesSubdir: "."}, upFilePage, "ABDollarSignCPage"},
}
for _, test := range tests {
t.Run(test.pfile.path, func(t *testing.T) {
got := generatedTypename(test.pfile, test.strategy)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("(-want, +got)\n%s", diff)
}
})
}
}