-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
tpl_test.go
79 lines (67 loc) · 2.04 KB
/
tpl_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
// Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com
package goatcounter_test
import (
"io/fs"
"os"
"strings"
"testing"
"zgo.at/errors"
. "zgo.at/goatcounter/v2"
"zgo.at/goatcounter/v2/gctest"
"zgo.at/zstd/zgo"
"zgo.at/ztpl"
)
func TestTpl(t *testing.T) {
sp := func(s string) *string { return &s }
ip := func(i int) *int { return &i }
i64p := func(i int64) *int64 { return &i }
ctx := gctest.Context(nil)
site := Site{Code: "example"}
user := User{Email: "[email protected]", EmailToken: sp("T-EMAIL"), LoginRequest: sp("T-LOGIN-REQ")}
files, _ := fs.Sub(os.DirFS(zgo.ModuleRoot()), "tpl")
err := ztpl.Init(files)
if err != nil {
t.Fatal(err)
}
errs := errors.NewGroup(4)
errs.Append(errors.New("err: <1>"))
errs.Append(errors.New("err: <2>"))
errs.Append(errors.New("err: <3>"))
errs.Append(errors.New("err: <4>"))
errs.Append(errors.New("err: <5>"))
tests := []struct {
t interface{ Render() ([]byte, error) }
}{
{TplEmailWelcome{ctx, site, user, "count.example.com"}},
{TplEmailForgotSite{ctx, []Site{site}, "[email protected]"}},
{TplEmailForgotSite{ctx, []Site{}, "[email protected]"}},
{TplEmailPasswordReset{ctx, site, user}},
{TplEmailVerify{ctx, site, user}},
{TplEmailImportError{ctx, errors.Unwrap(errors.New("oh noes"))}},
{TplEmailImportDone{ctx, site, 42, errors.NewGroup(10)}},
{TplEmailImportDone{ctx, site, 42, errs}},
{TplEmailAddUser{ctx, site, user, "[email protected]"}},
{TplEmailExportDone{ctx, site, user, Export{
ID: 2,
NumRows: ip(42),
Size: sp("42"),
LastHitID: i64p(642051),
Hash: sp("sha256-AAA"),
}}},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
got, err := tt.t.Render()
if err != nil {
t.Fatal(err)
}
want := "Cheers,\nMartin\n"
if !strings.Contains(string(got), want) {
t.Errorf("didn't contain %q", want)
}
t.Log("\n" + string(got))
})
}
}