forked from fzipp/pythia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_test.go
63 lines (59 loc) · 1.45 KB
/
template_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
package main
import (
"bytes"
"go/ast"
"testing"
"github.com/fzipp/pythia/static"
)
func TestTemplateFuncs(t *testing.T) {
static.Files = map[string]string{
"plain.html": "<p>test</p>",
"base.html": "{{base .FileName}}",
"stdpkg1.html": "{{if stdpkg .StdPkg}}ok{{else}}fail{{end}}",
"stdpkg2.html": "{{if stdpkg .NonStdPkg}}fail{{else}}ok{{end}}",
}
data := map[string]interface{}{
"File": &ast.File{},
"FileName": "/usr/local/go/src/pkg/fmt/format.go",
"StdPkg": "io/ioutil",
"NonStdPkg": "golang.org/x/tools/cmd/guru",
}
tests := []struct {
file, want string
}{
{"plain.html", "<p>test</p>"},
{"base.html", "format.go"},
{"stdpkg1.html", "ok"},
{"stdpkg2.html", "ok"},
}
for _, tt := range tests {
template := parseTemplate(tt.file)
out := new(bytes.Buffer)
template.Execute(out, data)
if x := out.String(); x != tt.want {
t.Errorf("Executing template %q with data %q resulted in %q, want %q",
tt.file, data, x, tt.want)
}
}
}
func TestIsStandardPackage(t *testing.T) {
tests := []struct {
path string
want bool
}{
{"fmt", true},
{"net/http", true},
{"image/color/palette", true},
{"github.com/fzipp/pythia", false},
{"golang.org/x/tools/cmd/guru", false},
{"main", false},
{"foo", false},
{"foo/bar", false},
{"", false},
}
for _, tt := range tests {
if x := isStandardPackage(tt.path); x != tt.want {
t.Errorf("isStandardPackage(%q) = %v, want %v", tt.path, x, tt.want)
}
}
}