-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetmanager_test.go
executable file
·294 lines (249 loc) · 6.82 KB
/
assetmanager_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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package assetmanager_test
import (
"fmt"
"io/ioutil"
"strings"
"testing"
"github.com/s4l1h/assetmanager"
)
func ExampleAssetManager_File() {
asset := assetmanager.New()
asset.AddDir("./test")
buffer, err := asset.File("test/index.html")
if err != nil {
fmt.Println(err)
}
r, _ := ioutil.ReadAll(buffer)
fmt.Println(string(r))
// Output: index.html
}
func ExampleAssetManager_AddReplacer() {
asset := assetmanager.New()
// Add Replacer
asset.AddReplacer("testReplacer", func(name string) string {
return fmt.Sprintf("main:%s", name)
})
// Add Another Replacer.
asset.AddReplacer("convergohtml", func(name string) string {
return strings.Replace(name, ".html", ".gohtml", -1)
})
// add file from string
asset.AddFileString("index.html", "test content")
// get file content
c, _ := asset.GetString("main:index.gohtml")
fmt.Println(c)
// Output: test content
}
func replaceMe(name string) string {
return name
}
func ExampleAssetManager_Merge() {
asset := assetmanager.New()
asset.AddFileString("index.html", "index.html content")
asset.AddFileString("asset1.html", "asset1.html content")
asset2 := assetmanager.New()
asset2.AddFileString("asset2.html", "asset2.html content")
asset.Merge(asset2)
c, _ := asset.GetString("asset2.html")
fmt.Println(c)
// Output: asset2.html content
}
func ExampleAssetManager_MergeAndRunReplacer() {
asset := assetmanager.New()
asset.AddReplacer("ToUpper", func(name string) string {
return strings.ToUpper(name)
})
asset.AddFileString("index.html", "index.html content")
asset.AddFileString("asset1.html", "asset1.html content")
asset2 := assetmanager.New()
asset2.AddFileString("asset2.html", "asset2.html content")
asset.MergeAndRunReplacer(asset2)
c, _ := asset.GetString("ASSET2.HTML")
fmt.Println(c)
// Output: asset2.html content
}
func TestReplacer(t *testing.T) {
fileName := "test/index.html"
fileContent := "index.html content"
newFileName := "main::test/index.html"
asset := assetmanager.New()
// Add Replace Function
asset.AddReplacer("replacerTestName", replaceMe)
if asset.ExistsReplacer("replacerTestName") == false {
t.Error("AddReplacer Error")
}
asset.RemoveReplacer("replacerTestName")
if asset.ExistsReplacer("replacerTestName") == true {
t.Error("RemoveReplacer Error")
}
// Add another Replacer Function
asset.AddReplacer("testToMain", func(name string) string {
name = strings.Replace(name, "test/", "main::test/", -1)
return name
})
// Add File
asset.AddFileString(fileName, fileContent)
// Get it with the new name.
r, e := asset.GetString(newFileName)
if e != nil {
t.Error(e)
}
if r != fileContent {
t.Errorf("File content not equal %s", fileContent)
}
}
func TestExt(t *testing.T) {
asset := assetmanager.New()
asset.AddAllowedExt(".html", ".tpl")
asset.AddDisallowExt(".php", ".jpg")
ext1 := asset.GetExt("index.html")
ext2 := asset.GetExt("index.php")
if ext1 != ".html" {
t.Errorf("getExt Error ")
}
if !asset.CheckAllowed(ext1) {
t.Errorf("CheckAllowed Error ")
}
if asset.CheckAllowed(ext2) {
t.Errorf("CheckAllowed Error ")
}
if !asset.CheckDisallowed(ext1) {
t.Errorf("CheckDisallowed Error: CheckDisallowed ")
}
if asset.CheckDisallowed(ext2) {
t.Errorf("CheckDisallowed Error: CheckDisallowed ")
}
if asset.CheckAddFile("index.php") {
t.Errorf("canAddFile Error: canAddFile index.php")
}
if !asset.CheckAddFile("index.html") {
t.Errorf("canAddFile Error: canAddFile index.html")
}
}
func TestAddRemoveAllowed(t *testing.T) {
asset := assetmanager.New()
asset.AddAllowedExt(".html", ".tpl", ".php")
if asset.CheckAllowed(".html") != true {
t.Errorf("RemoveAllowedExt Error: CheckAllowed ")
}
asset.RemoveAllowedExt(".html")
if asset.CheckAllowed(".html") != false {
t.Errorf("RemoveAllowedExt Error: CheckAllowed ")
}
}
func TestAddRemoveDisallowed(t *testing.T) {
asset := assetmanager.New()
asset.AddDisallowExt(".html", ".tpl", ".php")
//t.Error(asset.DisallowedExt)
if asset.CheckDisallowed(".html") != false {
t.Errorf("RemoveDisallowExt Error: CheckAllowed ")
}
asset.RemoveDisallowExt(".html")
//t.Error(asset.DisallowedExt)
if asset.CheckDisallowed(".html") != true {
t.Errorf("RemoveDisallowExt Error: CheckAllowed ")
}
}
func TestAddFile(t *testing.T) {
asset := assetmanager.New()
file := "test/index.html"
asset.AddFile(file)
if !asset.Exists(file) {
t.Errorf("File Not Found %s", file)
}
content, err := asset.GetString(file)
if err != nil {
t.Error("Get File Error", err)
}
if content != "index.html" {
t.Error("Get File content not equal index.html")
}
}
func TestAddFileString(t *testing.T) {
asset := assetmanager.New()
file := "test/index.html"
content := "index.html"
asset.AddFileString(file, content)
if !asset.Exists(file) {
t.Errorf("File Not Found %s", file)
}
content, err := asset.GetString(file)
if err != nil {
t.Error("GetString File Error", err)
}
if content != "index.html" {
t.Error("GetString File content not equal index.html")
}
}
func TestAddDir(t *testing.T) {
asset := assetmanager.New()
asset.AddAllowedExt(".html", ".tpl")
asset.AddDisallowExt(".php", ".tpl")
//asset.AddDir(helpers.DirName())
asset.AddDir("./test")
//t.Error(asset.Files)
content, err := asset.Get("test/index.html")
if err != nil {
t.Error("Get File Error test/index.html ", err)
}
if string(content) != string([]byte("index.html")) {
t.Error("Get File content not equal index.html")
}
contentString, errString := asset.GetString("test/index.html")
if errString != nil {
t.Error("GetString Error test/index.html ", errString)
}
if contentString != "index.html" {
t.Error("Get File content not equal index.html")
}
r, e := asset.GetString("test/randomFile.html")
if e == nil {
t.Error("GetString need return error ", e)
}
if r != "" {
t.Error("GetString need return empty")
}
}
func TestDelete(t *testing.T) {
asset := assetmanager.New()
file := "index.html"
content := "index.html file content"
asset.AddFileString(file, content)
if !asset.Exists(file) {
t.Errorf("File Not Found %s", file)
}
asset.Delete(file)
if asset.Exists(file) {
t.Errorf("Delete error %s", file)
}
}
func TestCopy(t *testing.T) {
asset := assetmanager.New()
file := "index.html"
content := "index.html file content"
asset.AddFileString(file, content)
assetCopy := assetmanager.New()
assetCopy.Copy(asset)
if !asset.Exists(file) {
t.Errorf("File Not Found %s", file)
}
if !assetCopy.Exists(file) {
t.Errorf("asset copy error File Not Found %s", file)
}
}
func TestEmptyNameReplacer(t *testing.T) {
file := "index.html"
content := "index.html file content"
asset := assetmanager.New()
// Add Replacer
asset.AddReplacer("testReplacer", func(name string) string {
if name == file {
return "" // empt
}
return name
})
asset.AddFileString(file, content)
if asset.Exists(file) {
t.Errorf("Empty Name Replacer Error File Found %s", file)
}
}