blob: 6170f3ce4774d601feb170cb4c44899509767a2c (
plain)
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
|
import vweb.assets
import os
// clean_cache_dir used before and after tests that write to a cache directory.
// Because of parallel compilation and therefore test running,
// unique cache dirs are needed per test function.
fn clean_cache_dir(dir string) {
if os.is_dir(dir) {
os.rmdir_all(dir) or { panic(err) }
}
}
fn base_cache_dir() string {
return os.join_path(os.temp_dir(), 'assets_test_cache')
}
fn cache_dir(test_name string) string {
return os.join_path(base_cache_dir(), test_name)
}
fn get_test_file_path(file string) string {
path := os.join_path(base_cache_dir(), file)
if !os.is_dir(base_cache_dir()) {
os.mkdir_all(base_cache_dir()) or { panic(err) }
}
if !os.exists(path) {
os.write_file(path, get_test_file_contents(file)) or { panic(err) }
}
return path
}
fn get_test_file_contents(file string) string {
contents := match file {
'test1.js' { '{"one": 1}\n' }
'test2.js' { '{"two": 2}\n' }
'test1.css' { '.one {\n\tcolor: #336699;\n}\n' }
'test2.css' { '.two {\n\tcolor: #996633;\n}\n' }
else { 'wibble\n' }
}
return contents
}
fn test_set_cache() {
mut am := assets.new_manager()
am.cache_dir = 'cache'
}
fn test_set_minify() {
mut am := assets.new_manager()
am.minify = true
}
fn test_add() {
mut am := assets.new_manager()
assert am.add('css', 'testx.css') == false
assert am.add('css', get_test_file_path('test1.css')) == true
assert am.add('js', get_test_file_path('test1.js')) == true
// assert am.add('css', get_test_file_path('test2.js')) == false // TODO: test extension on add
}
fn test_add_css() {
mut am := assets.new_manager()
assert am.add_css('testx.css') == false
assert am.add_css(get_test_file_path('test1.css')) == true
// assert am.add_css(get_test_file_path('test1.js')) == false // TODO: test extension on add
}
fn test_add_js() {
mut am := assets.new_manager()
assert am.add_js('testx.js') == false
assert am.add_css(get_test_file_path('test1.js')) == true
// assert am.add_css(get_test_file_path('test1.css')) == false // TODO: test extension on add
}
fn test_combine_css() {
mut am := assets.new_manager()
am.cache_dir = cache_dir('test_combine_css')
clean_cache_dir(am.cache_dir)
am.add_css(get_test_file_path('test1.css'))
am.add_css(get_test_file_path('test2.css'))
// TODO: How do I test non-minified, is there a "here doc" format that keeps formatting?
am.minify = true
expected := '.one { color: #336699; } .two { color: #996633; } '
actual := am.combine_css(false)
assert actual == expected
assert actual.contains(expected)
// Test cache path doesn't change when input files and minify setting do not.
path1 := am.combine_css(true)
clean_cache_dir(am.cache_dir)
path2 := am.combine_css(true)
assert path1 == path2
clean_cache_dir(am.cache_dir)
}
fn test_combine_js() {
mut am := assets.new_manager()
am.cache_dir = cache_dir('test_combine_js')
clean_cache_dir(am.cache_dir)
am.add_js(get_test_file_path('test1.js'))
am.add_js(get_test_file_path('test2.js'))
expected1 := '{"one": 1}'
expected2 := '{"two": 2}'
expected := expected1 + '\n' + expected2 + '\n'
actual := am.combine_js(false)
assert actual == expected
assert actual.contains(expected)
assert actual.contains(expected1)
assert actual.contains(expected2)
am.minify = true
clean_cache_dir(am.cache_dir)
expected3 := expected1 + ' ' + expected2 + ' '
actual2 := am.combine_js(false)
assert actual2 == expected3
assert actual2.contains(expected3)
// Test cache path doesn't change when input files and minify setting do not.
path1 := am.combine_js(true)
clean_cache_dir(am.cache_dir)
path2 := am.combine_js(true)
assert path1 == path2
clean_cache_dir(am.cache_dir)
}
fn test_include_css() {
mut am := assets.new_manager()
file1 := get_test_file_path('test1.css')
am.add_css(file1)
expected := '<link rel="stylesheet" href="$file1">\n'
actual := am.include_css(false)
assert actual == expected
assert actual.contains(expected)
// Two lines of output.
file2 := get_test_file_path('test2.css')
am.add_css(file2)
am.cache_dir = cache_dir('test_include_css')
clean_cache_dir(am.cache_dir)
expected2 := expected + '<link rel="stylesheet" href="$file2">\n'
actual2 := am.include_css(false)
assert actual2 == expected2
assert actual2.contains(expected2)
// Combined output.
clean_cache_dir(am.cache_dir)
actual3 := am.include_css(true)
assert actual3.contains(expected2) == false
assert actual3.starts_with('<link rel="stylesheet" href="$am.cache_dir/') == true
// Test cache path doesn't change when input files and minify setting do not.
clean_cache_dir(am.cache_dir)
actual4 := am.include_css(true)
assert actual4 == actual3
clean_cache_dir(am.cache_dir)
}
fn test_include_js() {
mut am := assets.new_manager()
file1 := get_test_file_path('test1.js')
am.add_js(file1)
expected := '<script type="text/javascript" src="$file1"></script>\n'
actual := am.include_js(false)
assert actual == expected
assert actual.contains(expected)
// Two lines of output.
file2 := get_test_file_path('test2.js')
am.add_js(file2)
am.cache_dir = cache_dir('test_include_js')
clean_cache_dir(am.cache_dir)
expected2 := expected + '<script type="text/javascript" src="$file2"></script>\n'
actual2 := am.include_js(false)
assert actual2 == expected2
assert actual2.contains(expected2)
// Combined output.
clean_cache_dir(am.cache_dir)
actual3 := am.include_js(true)
assert actual3.contains(expected2) == false
assert actual3.starts_with('<script type="text/javascript" src="$am.cache_dir/')
// Test cache path doesn't change when input files and minify setting do not.
clean_cache_dir(am.cache_dir)
actual4 := am.include_js(true)
assert actual4 == actual3
clean_cache_dir(am.cache_dir)
}
|