forked from open2b/scriggo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates_test.go
189 lines (171 loc) · 4.72 KB
/
templates_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
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
// Copyright 2019 The Scriggo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package scriggo
import (
"fmt"
"reflect"
"testing"
"github.com/open2b/scriggo/ast"
"github.com/open2b/scriggo/internal/compiler"
"github.com/open2b/scriggo/internal/fstest"
)
func TestInitGlobals(t *testing.T) {
// Test no globals.
globals := initGlobalVariables([]compiler.Global{}, nil)
if globals != nil {
t.Fatalf("expected nil, got %v", globals)
}
// Test zero value.
global := compiler.Global{
Pkg: "p",
Name: "a",
Type: reflect.TypeOf(0),
}
globals = initGlobalVariables([]compiler.Global{global}, nil)
g := globals[0]
if g.Kind() != reflect.Int {
t.Fatalf("unexpected kind %v", g.Kind())
}
if g.Interface() != 0 {
t.Fatalf("unexpected %v, expecting 0", g.Interface())
}
// Test pointer value in globals.
n := 1
global = compiler.Global{
Pkg: "p",
Name: "a",
Type: reflect.TypeOf(n),
Value: reflect.ValueOf(&n).Elem(),
}
globals = initGlobalVariables([]compiler.Global{global}, nil)
n = 2
g = globals[0]
if g.Kind() != reflect.Int {
t.Fatalf("unexpected kind %v", g.Kind())
}
iface := g.Interface()
if iface != n {
t.Fatalf("unexpected %v (type %T), expecting %d (type %T)", iface, iface, n, n)
}
// Test pointer value in init.
global = compiler.Global{
Pkg: "main",
Name: "a",
Type: reflect.TypeOf(n),
}
init := map[string]interface{}{"a": &n}
globals = initGlobalVariables([]compiler.Global{global}, init)
if globals == nil {
t.Fatalf("unexpected %v, expecting nil", globals)
}
n = 3
g = globals[0]
if g.Kind() != reflect.Int {
t.Fatalf("unexpected kind %v", g.Kind())
}
iface = g.Interface()
if iface != n {
t.Fatalf("unexpected %v (type %T), expecting %d (type %T)", iface, iface, n, n)
}
// Test non pointer value in init.
global = compiler.Global{
Pkg: "main",
Name: "a",
Type: reflect.TypeOf(n),
}
init = map[string]interface{}{"a": n}
globals = initGlobalVariables([]compiler.Global{global}, init)
if globals == nil {
t.Fatalf("unexpected %v, expecting nil", globals)
}
g = globals[0]
if g.Kind() != reflect.Int {
t.Fatalf("unexpected kind %v", g.Kind())
}
iface = g.Interface()
if iface != n {
t.Fatalf("unexpected %v (type %T), expecting %d (type %T)", iface, iface, n, n)
}
}
func recoverInitGlobalsPanic(t *testing.T, expected string) {
got := recover()
if got == nil {
t.Fatalf("expecting panic")
}
if _, ok := got.(string); !ok {
panic(got)
}
if got.(string) != expected {
t.Fatalf("unexpected panic %q, expecting panic %q", got, expected)
}
}
func TestInitGlobalsAlreadyInitializedError(t *testing.T) {
defer recoverInitGlobalsPanic(t, "variable \"a\" already initialized")
n := 2
global := compiler.Global{
Pkg: "main",
Name: "a",
Type: reflect.TypeOf(n),
Value: reflect.ValueOf(&n).Elem(),
}
init := map[string]interface{}{"a": 5}
_ = initGlobalVariables([]compiler.Global{global}, init)
}
func TestInitGlobalsNilError(t *testing.T) {
defer recoverInitGlobalsPanic(t, "variable initializer \"a\" cannot be nil")
global := compiler.Global{
Pkg: "main",
Name: "a",
Type: reflect.TypeOf(0),
}
init := map[string]interface{}{"a": nil}
_ = initGlobalVariables([]compiler.Global{global}, init)
}
func TestInitGlobalsInvalidTypeError(t *testing.T) {
defer recoverInitGlobalsPanic(t, "variable initializer \"a\" must have type int or *int, but have bool")
global := compiler.Global{
Pkg: "main",
Name: "a",
Type: reflect.TypeOf(0),
}
init := map[string]interface{}{"a": true}
_ = initGlobalVariables([]compiler.Global{global}, init)
}
func TestInitGlobalsNilPointerError(t *testing.T) {
defer recoverInitGlobalsPanic(t, "variable initializer \"a\" cannot be a nil pointer")
global := compiler.Global{
Pkg: "main",
Name: "a",
Type: reflect.TypeOf(0),
}
init := map[string]interface{}{"a": (*int)(nil)}
_ = initGlobalVariables([]compiler.Global{global}, init)
}
type testFormatFS struct {
fstest.Files
format Format
}
func (fsys testFormatFS) Format(_ string) (Format, error) {
return fsys.format, nil
}
// TestFormatFS tests that BuildTemplate gets the file format by calling the
// Format method of FormatFS.
func TestFormatFS(t *testing.T) {
fsys := testFormatFS{Files: fstest.Files{"index": ""}}
for _, format := range []Format{FormatText, FormatHTML, FormatCSS, FormatJS, FormatJSON, FormatMarkdown} {
fsys.format = format
options := BuildOptions{
TreeTransformer: func(tree *ast.Tree) error {
if tree.Format != ast.Format(format) {
return fmt.Errorf("expected format %s, got %s", format, tree.Format)
}
return nil
},
}
_, err := BuildTemplate(fsys, "index", &options)
if err != nil {
t.Fatal(err)
}
}
}