-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.lua
292 lines (215 loc) · 5.55 KB
/
tests.lua
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
--[[============================================================
--=
--= LuaCss Testsuite
--=
--============================================================]]
local cssLib = require"css"
local utf8 = require"utf8"
local function printFirstDiff(a, b)
print("Byte diff: "..(#b-#a))
a = a:gsub("\n", "\\n")
b = b:gsub("\n", "\\n")
local testFrom = 1
local testTo = math.min(#a, #b)
while testFrom < testTo do
local mid = math.floor(testFrom+(testTo-testFrom)/2)
if
a:sub(mid, mid) == b:sub(mid, mid)
and a:sub(math.max(mid-20, 1), mid) == b:sub(math.max(mid-20, 1), mid)
and a:sub(1, mid) == b:sub(1, mid)
then
testFrom = mid+1
else
testTo = mid
end
end
local similarUntilPos = testFrom
local from = utf8.offset(a, 0, math.max(similarUntilPos-50, 1))
local before = a:sub(from, utf8.offsetend(a, 0, similarUntilPos))
print(("First diff at: %d/%d (%.1f%%)"):format(similarUntilPos, #a, 100*similarUntilPos/#a))
print(">", a:sub(
from,
utf8.offsetend(a, 0, similarUntilPos+50) or #a
))
print(">", b:sub(
from,
utf8.offsetend(b, 0, similarUntilPos+50) or #b
))
print(">", (" "):rep(utf8.len(before)-1).."^")
end
local function runTest(css, printCss)
if printCss then
print("================================")
print(css)
print("================================")
end
local clock = os.clock()
local tokens = cssLib.tokenize(css)
local tokenizeTime = os.clock()-clock
if printCss then
--[[ Print classes.
local classSet = {}
for i, token in ipairs(tokens) do
if i > 1 and token.type == "ident" and tokens[i-1].value == "." then
classSet[token.value] = true
end
end
local classes = {}
for k in pairs(classSet) do
table.insert(classes, k)
end
table.sort(classes)
for i, class in ipairs(classes) do
print("class", i, class)
end
--]]
-- [[ Print tokens.
for i, token in ipairs(tokens) do
print(
i,
token.type,
token.value and "'"..tostring(token.value):gsub("\n", "\\n").."'" or "",
token.unit and "'"..tostring(token.unit).."'" or ""
)
end
--]]
end
css = cssLib.serializeAndMinimize(tokens)
if printCss then
print("----------------")
print(css)
end
-- [[ Round-trip test.
local cssAgain = cssLib.minimize(css)
if printCss then
print("----------------")
print(cssAgain)
end
if css ~= cssAgain then
print("Parsing round-trip failed!")
printFirstDiff(css, cssAgain)
error()
end
--]]
if printCss then
print("----------------")
end
print("Tokenization time: "..tokenizeTime.." seconds")
return css
end
local function runTestOnFile(path, printCss, skipLargeFiles)
local file = assert(io.open(path, "rb"))
local css = file:read"*a"
file:close()
if skipLargeFiles and #css >= 10000 then return nil end
print("Running test: "..path:gsub("^tests/", ""))
return runTest(css, printCss), css
end
local function runTestsuite(skipLargeFiles)
print("Running testsuite")
local ok, lfs = pcall(require, "lfs")
if not ok then
error("Testsuite requires LuaFileSystem.")
end
lfs.mkdir("temp")
local out = assert(io.open("temp/testsuiteMinified.css", "wb"))
local function testDir(dir)
for name in lfs.dir(dir) do
if not (name == "." or name == "..") then
local path = dir.."/"..name
local mode = assert(lfs.attributes(path, "mode"))
if mode == "file" and name:find"%.css$" then
out:write("/* ", path:gsub("^tests/", ""), " */\n")
local cssMinified, cssOriginal = runTestOnFile(path, false, skipLargeFiles)
if not cssMinified then
out:write("/* Skipping large file. */\n")
else
out:write(cssMinified, "\n")
local file = assert(io.open(path..".min", "rb"))
local cssExpected = file:read"*a"
file:close()
if cssMinified == cssExpected then
out:write("/* OK! */\n")
else
print()
print("Unexpected result!")
printFirstDiff(cssMinified, cssExpected)
print()
out:write("/* Expected: */\n", cssExpected, "\n")
end
end
out:write("\n")
elseif mode == "directory" then
testDir(path)
end
end
end
end
testDir("tests")
out:close()
end
local function runSmallTest()
print("Running small test")
local css = [[
div {
background: red;
}
html >/* <=IE7 */ body small { font-size: 0.6rem; }
.sink .cls {;
;;/**/ ;;color:#8e8e8e;;
color: green;
background-color: rgba(100, 150,200.0, .9);
background: transparent url(/images/dog.png) 5.30e+0% 0px;
background-image: url( "/images/Cat's fur (2)]].."\1"..[[.png" );
border:1px solid #999;; ;
}
body main>span { font-family: "Arial"; }
body main > span { border-radius: 5.0px; }
#id {
font-size: 3rem;
}
/* comment asdf */
@media screen and (min-width: 1000px) {
a::after {
text-decoration: underline;
content: 'A cat\'s what?';
}
}
a[href]:hover {
font-weight: 200;
}
a[href$=".png"] {}
a[href*='/images/'] {}
a[href^="https://"] {}
div[class|=top] {}
article[data-tags~=news] {}
section
{
margin: 0;
/*! important comment wthin a rule */
padding: 5px;
}
body > * {
}
<!--
#wrapper{width:50%!important;}
-->
a.\#hash\1 tag\1 span { height: .8125e+2px; }
heading { margin-left:calc(20px + 1em) ; /*-xxx-what: 5.2e0e4m;*/ }
@font-face {
font-family: "Ampersand";
src: local('Times New Roman');
unicode-range: U+26, u+123-abcd, U+01a??, u+012300-123fF;
}
.badStuff {
background-image: url(/dog
house.png);
font-family: "Arial
}
]]
runTest(css, true)
end
-- runSmallTest()
-- runTestOnFile("test.css")
-- runTestOnFile("tests/yui/issue205.css", true)
runTestsuite(true)