-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplit_test.go
90 lines (83 loc) · 2.13 KB
/
templit_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
package templit_test
import (
"testing"
"text/template"
"github.com/euforic/templit"
"github.com/google/go-cmp/cmp"
)
// TestNewExecutor tests the NewExecutor function.
func TestNewExecutor(t *testing.T) {
tests := []struct {
name string
input string
funcMap template.FuncMap
expected string
err bool
}{
{
name: "valid template file",
input: "test_data/templates/basic_test/greeting.txt",
funcMap: nil,
err: false,
},
// ... add more test cases here
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executor := templit.NewExecutor(nil)
executor.Funcs(tt.funcMap)
err := executor.ParsePath(tt.input)
if (err != nil) != tt.err {
t.Fatalf("expected error: %v, got: %v", tt.err, err)
}
if executor == nil {
t.Fatalf("expected executor to not be nil")
}
})
}
}
// TestRender tests the Render function.
func TestRender(t *testing.T) {
tests := []struct {
name string
inputPath string
templateName string
data interface{}
expected string
err bool
}{
{
name: "valid template",
inputPath: "test_data/templates/basic_test",
templateName: "test_data/templates/basic_test/greeting.txt",
data: map[string]string{"Name": "John"},
expected: "Hello, John!\n",
err: false,
},
{
name: "valid template block",
inputPath: "test_data/templates/basic_test",
templateName: "example_block",
data: map[string]string{"greeting": "Hey"},
expected: "Hey, this is an example block.",
err: false,
},
// ... add more test cases here
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executor := templit.NewExecutor(nil)
err := executor.ParsePath(tt.inputPath)
if err != nil {
t.Fatalf("failed to create executor: %v", err)
}
result, err := executor.Render(tt.templateName, tt.data)
if (err != nil) != tt.err {
t.Fatalf("expected error: %v, got: %v", tt.err, err)
}
if diff := cmp.Diff(tt.expected, result); diff != "" {
t.Fatalf("unexpected result (-want +got):\n%s", diff)
}
})
}
}