-
Notifications
You must be signed in to change notification settings - Fork 1
/
walk_test.go
115 lines (107 loc) · 2.95 KB
/
walk_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
package templit_test
import (
"fmt"
"os"
"strings"
"testing"
"text/template"
"github.com/euforic/templit"
)
// TestExecutor_RenderTemplate tests the StringRender function.
func TestExecutor_StringRender(t *testing.T) {
var tests = []struct {
name string
tmpl string
data interface{}
funcMap template.FuncMap
expected string
wantErr bool
}{
{
name: "Simple template without data",
tmpl: "Hello, World!",
expected: "Hello, World!",
},
{
name: "Template with data",
tmpl: "Hello, {{.Name}}!",
data: map[string]string{"Name": "John"},
expected: "Hello, John!",
},
{
name: "Template with function",
tmpl: "Hello, {{lower .Name}}!",
data: map[string]string{"Name": "JOHN"},
funcMap: template.FuncMap{"lower": func(s string) string { return strings.ToLower(s) }},
expected: "Hello, john!",
},
{
name: "Malformed template",
tmpl: "Hello, {{.Name!",
data: map[string]string{"Name": "John"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := templit.NewExecutor(nil)
got, err := e.StringRender(tt.tmpl, tt.data)
if (err != nil) != tt.wantErr {
t.Fatalf("RenderTemplate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.expected {
t.Errorf("RenderTemplate() = %v, expected %v", got, tt.expected)
}
})
}
}
// TestWalkAndProcessDir tests the WalkAndProcessDir function.
func TestWalkAndProcessDir(t *testing.T) {
tests := []struct {
name string
inputDir string
data interface{}
funcMap template.FuncMap
expectedOutput string
expectedError error
}{
{
name: "Simple directory processing",
inputDir: "test_data/templates/basic_test",
data: map[string]interface{}{
"Name": "John",
"Title": "Project",
"Description": "This is a test project.",
"Detail": "more info here.",
},
funcMap: templit.DefaultFuncMap,
expectedOutput: "test_data/outputs/basic_test/",
},
// ... (other test cases)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create temp directory for output
tempOutputDir, err := os.MkdirTemp("", "test_output_"+strings.ReplaceAll(strings.ToLower(tt.name), " ", "_"))
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempOutputDir) // Cleanup
executor := templit.NewExecutor(nil)
err = executor.WalkAndProcessDir(tt.inputDir, tempOutputDir, tt.data)
if err != nil {
if tt.expectedError == nil || err.Error() != tt.expectedError.Error() {
t.Fatalf("expected error %v, got %v", tt.expectedError, err)
}
return
}
// Compare generated output directory with expected directory
fmt.Println(tt.expectedOutput, tempOutputDir)
err = compareDirs(tt.expectedOutput, tempOutputDir)
if err != nil {
t.Fatalf("output directory mismatch: %v", err)
}
})
}
}