-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtemplate_test.go
63 lines (59 loc) · 1.56 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
// Copyright 2013 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"go/ast"
"html/template"
"testing"
)
func TestTemplateFuncs(t *testing.T) {
data := map[string]any{
"File": &ast.File{},
"FileName": "/usr/local/go/src/pkg/fmt/format.go",
"StdPkg": "io/fs",
"NonStdPkg": "golang.org/x/tools/cmd/guru",
}
tests := []struct {
tmpl, want string
}{
{"<p>test</p>", "<p>test</p>"},
{"{{base .FileName}}", "format.go"},
{"{{if stdpkg .StdPkg}}ok{{else}}fail{{end}}", "ok"},
{"{{if stdpkg .NonStdPkg}}fail{{else}}ok{{end}}", "ok"},
}
for _, tt := range tests {
tmpl := template.Must(template.New("").Funcs(funcs).Parse(tt.tmpl))
var out bytes.Buffer
if err := tmpl.Execute(&out, data); err != nil {
t.Errorf("Did not expect error for template %q, but got error: %q", tt.tmpl, err)
continue
}
if x := out.String(); x != tt.want {
t.Errorf("Executing template %q with data %#v resulted in %q, want %q",
tt.tmpl, 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)
}
}
}