-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_test.go
More file actions
200 lines (164 loc) · 5.04 KB
/
Copy pathtemplate_test.go
File metadata and controls
200 lines (164 loc) · 5.04 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"html/template"
"testing"
"github.com/bbondy/go-brianbondy/data" // Import the data package where BlogPost is defined
)
// Mock data structures for testing
type TestPost struct {
Title string
Content string
Empty string
}
func TestInitializeTemplates(t *testing.T) {
if err := initializeTemplates(); err != nil {
t.Fatalf("initializeTemplates() error = %v", err)
}
if len(compiledTemplates) != len(templateFiles) {
t.Fatalf("loaded %d templates, want %d", len(compiledTemplates), len(templateFiles))
}
firstHomeTemplate := compiledTemplates["home"]
if err := initializeTemplates(); err != nil {
t.Fatalf("second initializeTemplates() error = %v", err)
}
if compiledTemplates["home"] != firstHomeTemplate {
t.Error("initializeTemplates() reparsed the home template")
}
}
func TestAvail(t *testing.T) {
// Test with struct
post := TestPost{
Title: "Test Title",
Content: "Test Content",
Empty: "",
}
// Test valid non-empty field
if !avail("Title", post) {
t.Error("avail should return true for Title field")
}
// Test valid non-empty field with pointer
if !avail("Title", &post) {
t.Error("avail should return true for Title field with pointer")
}
// Test valid empty string field
if avail("Empty", post) {
t.Error("avail should return false for empty string field")
}
// Test non-existent field
if avail("NonExistent", post) {
t.Error("avail should return false for non-existent field")
}
// Test with non-struct
if avail("Title", "string") {
t.Error("avail should return false with non-struct")
}
// Test with nil
if avail("Title", nil) {
t.Error("avail should return false with nil")
}
}
func TestHTMLSafe(t *testing.T) {
htmlString := "<p>Test</p>"
htmlSafeFunc := funcMap["htmlSafe"].(func(string) template.HTML)
result := htmlSafeFunc(htmlString)
expected := template.HTML(htmlString)
if result != expected {
t.Errorf("htmlSafe should return template.HTML, got: %v, want: %v", result, expected)
}
}
func TestGetTagCount(t *testing.T) {
// Setup test data
oldTagCountMap := tagCountMap
tagCountMap = map[string]int{
"golang": 5,
"test": 2,
}
defer func() { tagCountMap = oldTagCountMap }() // Restore after test
getTagCountFunc := funcMap["getTagCount"].(func(string) int)
// Test existing tag
if count := getTagCountFunc("golang"); count != 5 {
t.Errorf("getTagCount for 'golang' should return 5, got: %d", count)
}
// Test another existing tag
if count := getTagCountFunc("test"); count != 2 {
t.Errorf("getTagCount for 'test' should return 2, got: %d", count)
}
// Test non-existent tag
if count := getTagCountFunc("nonexistent"); count != 0 {
t.Errorf("getTagCount for non-existent tag should return 0, got: %d", count)
}
}
func TestTruncateTitle(t *testing.T) {
truncateFunc := funcMap["truncateTitle"].(func(string) string)
tests := []struct {
input string
expected string
}{
{"Short title", "Short title"},
{"This is exactly four words", "This is exactly four..."},
{"This is a longer title with many words", "This is a longer..."},
{"This, is. a! title? with: punctuation;", "This, is. a! title..."},
}
for _, test := range tests {
result := truncateFunc(test.input)
if result != test.expected {
t.Errorf("truncateTitle(%q) = %q, want %q", test.input, result, test.expected)
}
}
}
func TestTagUrl(t *testing.T) {
tagUrlFunc := funcMap["tagUrl"].(func(string) string)
tests := []struct {
input string
expected string
}{
{"golang", "/all?tag=golang"},
{"web development", "/all?tag=web+development"},
{"c++", "/all?tag=c%2B%2B"},
}
for _, test := range tests {
result := tagUrlFunc(test.input)
if result != test.expected {
t.Errorf("tagUrl(%q) = %q, want %q", test.input, result, test.expected)
}
}
}
func TestYearUrl(t *testing.T) {
yearUrlFunc := funcMap["yearUrl"].(func(int) string)
tests := []struct {
input int
expected string
}{
{2020, "/all?year=2020"},
{2021, "/all?year=2021"},
{1999, "/all?year=1999"},
}
for _, test := range tests {
result := yearUrlFunc(test.input)
if result != test.expected {
t.Errorf("yearUrl(%d) = %q, want %q", test.input, result, test.expected)
}
}
}
func TestGetYearCount(t *testing.T) {
// Setup test data
oldBlogPostYearMap := blogPostYearMap
// Create a mock map with the correct type
blogPostYearMap = make(map[int][]data.BlogPost)
blogPostYearMap[2020] = make([]data.BlogPost, 2) // 2 posts
blogPostYearMap[2021] = make([]data.BlogPost, 3) // 3 posts
defer func() { blogPostYearMap = oldBlogPostYearMap }() // Restore after test
getYearCountFunc := funcMap["getYearCount"].(func(int) int)
// Test existing year
if count := getYearCountFunc(2020); count != 2 {
t.Errorf("getYearCount for 2020 should return 2, got: %d", count)
}
// Test another existing year
if count := getYearCountFunc(2021); count != 3 {
t.Errorf("getYearCount for 2021 should return 3, got: %d", count)
}
// Test non-existent year
if count := getYearCountFunc(1999); count != 0 {
t.Errorf("getYearCount for non-existent year should return 0, got: %d", count)
}
}