-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcssfs_test.go
305 lines (284 loc) · 7.17 KB
/
cssfs_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
295
296
297
298
299
300
301
302
303
304
305
/*
Copyright © 2020, 2022 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <support@mwat.de>
*/
package cssfs
import (
"net/http"
"os"
"path"
"path/filepath"
"strings"
"testing"
)
// `removeCSSwhitespace()` removes unneeded whitespace from `aCSS`.
//
// `aCSS` The raw CSS data.
func removeCSSwhitespace(aCSS []byte) []byte {
for _, re := range cssREs {
aCSS = re.regEx.ReplaceAll(aCSS, []byte(re.replace))
}
return aCSS
} // removeCSSwhitespace()
func Test_gzName(t *testing.T) {
dir, _ := filepath.Abs(`./`)
s1 := `./stylesheet`
w1, _ := filepath.Abs(s1)
w1 += cssGZnameSuffix
s2 := `./stylesheetcss`
w2, _ := filepath.Abs(s2)
w2 += cssGZnameSuffix
s3 := `./stylesheet.css`
w3, _ := filepath.Abs(s3)
w3 += cssGZnameSuffix
fs := newFS(dir, true)
type args struct {
aFilename string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
{" 0", args{``}, `/dev/null`},
{" 1", args{s1}, w1},
{" 2", args{s2}, w2},
{" 3", args{s3}, w3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := fs.gzName(tt.args.aFilename); got != tt.want {
t.Errorf("gzName() = %v,\nwant %v", got, tt.want)
}
})
}
} // Test_gzName()
func Test_minName(t *testing.T) {
dir, _ := filepath.Abs(`./`)
s1 := `./stylesheet`
w1, _ := filepath.Abs(s1)
w1 += cssMinNameSuffix
s2 := `./stylesheetcss`
w2, _ := filepath.Abs(s2)
w2 += cssMinNameSuffix
s3 := `./stylesheet.css`
w3, _ := filepath.Abs(s3)
w3 = w3[:len(w3)-4] + cssMinNameSuffix
fs := newFS(dir, false)
type args struct {
aFilename string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
{" 0", args{``}, `/dev/null`},
{" 1", args{s1}, w1},
{" 2", args{s2}, w2},
{" 3", args{s3}, w3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := fs.minName(tt.args.aFilename); got != tt.want {
t.Errorf("minName() = '%v',\nwant `%v`", got, tt.want)
}
})
}
} // Test_minName()
func Test_removeCSSwhitespace(t *testing.T) {
c0, w0 := []byte(``), []byte(``)
c1 := []byte(`/*
this are my css rules
*/
`)
w1 := []byte(``)
c2 := []byte(`
body { /* default background colour */
background : #f9f9f3;
}
`)
w2 := []byte(`body{background:#f9f9f3}`)
c3 := []byte(`
@media screen {
body {
background : #f9f9f3; /* default background colour */
display :block;
}
}
@media print {
body {
background: #fff;
}
}
`)
w3 := []byte(`@media screen{body{background:#f9f9f3;display:block}}@media print{body{background:#fff}}`)
c4 := []byte(`p :link { display: inline; } a: hover { display :none; }`)
w4 := []byte(`p :link{display:inline}a:hover{display:none}`)
c5 := []byte(`div.empty { height: 0em; width: 0ex; } #main { max-width: 99%; }`)
w5 := []byte(`div.empty{height:0;width:0}#main{max-width:99%}`)
type args struct {
aCSS []byte
}
tests := []struct {
name string
args args
want []byte
}{
// TODO: Add test cases.
{" 0", args{c0}, w0},
{" 1", args{c1}, w1},
{" 2", args{c2}, w2},
{" 3", args{c3}, w3},
{" 4", args{c4}, w4},
{" 5", args{c5}, w5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeCSSwhitespace(tt.args.aCSS); string(got) != string(tt.want) {
t.Errorf("removeCSSwhitespace() = '%s',\nwant '%s'", got, tt.want)
}
})
}
} // Test_removeCSSwhitespace()
func Test_tCSSfilesFilesystem_createGZfile(t *testing.T) {
dir, _ := filepath.Abs(`./`)
c1 := `./css/stylesheet.css`
c2 := `.././css/dark.css`
c3 := `css/light.css`
c4 := dir + `/css/fonts.css`
fs := newFS(dir, true)
defer func() {
_ = os.Remove(fs.gzName(c1))
_ = os.Remove(fs.gzName(c2))
_ = os.Remove(fs.gzName(c3))
_ = os.Remove(fs.gzName(c4))
}()
type args struct {
aFilename string
}
tests := []struct {
name string
fields tCSSfilesFilesystem
args args
wantErr bool
}{
// TODO: Add test cases.
{" 0", fs, args{`does not exist`}, true},
{" 1", fs, args{c1}, false},
{" 2", fs, args{c2}, false},
{" 3", fs, args{c3}, false},
{" 4", fs, args{c4}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cf := tt.fields
if !strings.HasPrefix(tt.args.aFilename, cf.root) {
tt.args.aFilename = filepath.Join(cf.root,
filepath.FromSlash(path.Clean(`/`+tt.args.aFilename)))
}
if err := cf.createGZfile(tt.args.aFilename); (err != nil) != tt.wantErr {
t.Errorf("tCSSfilesFilesystem.createGZfile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
} // Test_tCSSfilesFilesystem_createGZfile()
func Test_tCSSFilesFilesystem_createMinFile(t *testing.T) {
dir, _ := filepath.Abs(`./`)
c1 := `./css/stylesheet.css`
c2 := `/css/dark.css`
c3 := `css/light.css`
c4 := dir + `/css/fonts.css`
fs := newFS(dir, false)
defer func() {
_ = os.Remove(fs.minName(c1))
_ = os.Remove(fs.minName(c2))
_ = os.Remove(fs.minName(c3))
_ = os.Remove(fs.minName(c4))
}()
type args struct {
aFilename string
}
tests := []struct {
name string
fields tCSSfilesFilesystem
args args
wantErr bool
}{
// TODO: Add test cases.
{" 0", fs, args{`does not exist`}, true},
{" 1", fs, args{c1}, false},
{" 2", fs, args{c2}, false},
{" 3", fs, args{c3}, false},
{" 4", fs, args{c4}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cf := tt.fields
if !strings.HasPrefix(tt.args.aFilename, cf.root) {
tt.args.aFilename = filepath.Join(cf.root,
filepath.FromSlash(path.Clean(`/`+tt.args.aFilename)))
}
if err := cf.createMinFile(tt.args.aFilename); (err != nil) != tt.wantErr {
t.Errorf("tCSSFilesFilesystem.createMinFile() error = %v,\nwantErr %v", err, tt.wantErr)
}
})
}
} // Test_tCSSFilesFilesystem_createMinFile()
func Test_tCSSFilesFilesystem_Open(t *testing.T) {
dir, _ := filepath.Abs(`./`)
c1 := `./css/stylesheet.css`
c2 := `/css/dark.css`
c3 := `css/light.css`
c4 := `/css/fonts.css`
fs1 := newFS(dir, false)
fs2 := newFS(dir, true)
defer func() {
_ = os.Remove(fs1.minName(c1))
_ = os.Remove(fs1.minName(c2))
_ = os.Remove(fs1.minName(c3))
_ = os.Remove(fs1.minName(c4))
_ = os.Remove(fs2.gzName(c1))
_ = os.Remove(fs2.gzName(c2))
_ = os.Remove(fs2.gzName(c3))
_ = os.Remove(fs2.gzName(c4))
}()
var hf http.File
type args struct {
aName string
}
tests := []struct {
name string
cf tCSSfilesFilesystem
args args
want http.File
wantErr bool
}{
// TODO: Add test cases.
{" 0", fs1, args{`doesn't exist`}, hf, true},
{" 1", fs1, args{c1}, hf, false},
{" 2", fs1, args{c2}, hf, false},
{" 3", fs1, args{c3}, hf, false},
{" 4", fs1, args{c4}, hf, false},
{"10", fs2, args{`doesn't exist`}, hf, true},
{"11", fs2, args{c1}, hf, false},
{"12", fs2, args{c2}, hf, false},
{"13", fs2, args{c3}, hf, false},
{"14", fs2, args{c4}, hf, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.cf.Open(tt.args.aName)
if (nil != err) != tt.wantErr {
t.Errorf("tCSSFilesFilesystem.Open() error = %v, wantErr %v", err, tt.wantErr)
return
}
if (nil == got) && (!tt.wantErr) {
t.Errorf("tCSSFilesFilesystem.Open() = %v, want %v", got, tt.want)
}
})
}
} // Test_tCSSFilesFilesystem_Open()