Skip to content

Commit be80f7e

Browse files
authored
support multi-line string in tests (#1590)
`expect_exact` sometimes have multiple lines and `\n` are hard to read. Use array of strings to emulate line breaks and improve readability.
1 parent cf45e2f commit be80f7e

File tree

3 files changed

+77
-12
lines changed

3 files changed

+77
-12
lines changed

test/compress/loops.js

+46-4
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,15 @@ issue_186_beautify: {
295295
else
296296
bar();
297297
}
298-
expect_exact: 'var x = 3;\n\nif (foo()) do {\n do {\n alert(x);\n } while (--x);\n} while (x); else bar();'
298+
expect_exact: [
299+
'var x = 3;',
300+
'',
301+
'if (foo()) do {',
302+
' do {',
303+
' alert(x);',
304+
' } while (--x);',
305+
'} while (x); else bar();',
306+
]
299307
}
300308

301309
issue_186_beautify_ie8: {
@@ -314,7 +322,17 @@ issue_186_beautify_ie8: {
314322
else
315323
bar();
316324
}
317-
expect_exact: 'var x = 3;\n\nif (foo()) {\n do {\n do {\n alert(x);\n } while (--x);\n } while (x);\n} else bar();'
325+
expect_exact: [
326+
'var x = 3;',
327+
'',
328+
'if (foo()) {',
329+
' do {',
330+
' do {',
331+
' alert(x);',
332+
' } while (--x);',
333+
' } while (x);',
334+
'} else bar();',
335+
]
318336
}
319337

320338
issue_186_bracketize: {
@@ -374,7 +392,19 @@ issue_186_beautify_bracketize: {
374392
else
375393
bar();
376394
}
377-
expect_exact: 'var x = 3;\n\nif (foo()) {\n do {\n do {\n alert(x);\n } while (--x);\n } while (x);\n} else {\n bar();\n}'
395+
expect_exact: [
396+
'var x = 3;',
397+
'',
398+
'if (foo()) {',
399+
' do {',
400+
' do {',
401+
' alert(x);',
402+
' } while (--x);',
403+
' } while (x);',
404+
'} else {',
405+
' bar();',
406+
'}',
407+
]
378408
}
379409

380410
issue_186_beautify_bracketize_ie8: {
@@ -394,5 +424,17 @@ issue_186_beautify_bracketize_ie8: {
394424
else
395425
bar();
396426
}
397-
expect_exact: 'var x = 3;\n\nif (foo()) {\n do {\n do {\n alert(x);\n } while (--x);\n } while (x);\n} else {\n bar();\n}'
427+
expect_exact: [
428+
'var x = 3;',
429+
'',
430+
'if (foo()) {',
431+
' do {',
432+
' do {',
433+
' alert(x);',
434+
' } while (--x);',
435+
' } while (x);',
436+
'} else {',
437+
' bar();',
438+
'}',
439+
]
398440
}

test/compress/max_line_len.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ too_short: {
77
return { c: 42, d: a(), e: "foo"};
88
}
99
}
10-
expect_exact: 'function f(a){\nreturn{\nc:42,\nd:a(),\ne:"foo"}}'
10+
expect_exact: [
11+
'function f(a){',
12+
'return{',
13+
'c:42,',
14+
'd:a(),',
15+
'e:"foo"}}',
16+
]
1117
expect_warnings: [
1218
"WARN: Output exceeds 10 characters"
1319
]
@@ -22,7 +28,12 @@ just_enough: {
2228
return { c: 42, d: a(), e: "foo"};
2329
}
2430
}
25-
expect_exact: 'function f(a){\nreturn{c:42,\nd:a(),e:"foo"}\n}'
31+
expect_exact: [
32+
'function f(a){',
33+
'return{c:42,',
34+
'd:a(),e:"foo"}',
35+
'}',
36+
]
2637
expect_warnings: [
2738
]
2839
}

test/run-tests.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,23 @@ function parse_test(file) {
214214
}));
215215
}
216216

217+
function read_string(stat) {
218+
if (stat.TYPE === "SimpleStatement") {
219+
var body = stat.body;
220+
out: switch(body.TYPE) {
221+
case "String":
222+
return body.value;
223+
case "Array":
224+
return body.elements.map(function(element) {
225+
if (element.TYPE !== "String")
226+
throw new Error("Should be array of strings");
227+
return element.value;
228+
}).join("\n");
229+
}
230+
}
231+
throw new Error("Should be string or array of strings");
232+
}
233+
217234
function get_one_test(name, block) {
218235
var test = { name: name, options: {} };
219236
var tw = new U.TreeWalker(function(node, descend){
@@ -240,12 +257,7 @@ function parse_test(file) {
240257
else if (stat.body.length == 0) stat = new U.AST_EmptyStatement();
241258
}
242259
if (node.label.name === "expect_exact") {
243-
if (!(stat.TYPE === "SimpleStatement" && stat.body.TYPE === "String")) {
244-
throw new Error(
245-
"The value of the expect_exact clause should be a string, " +
246-
"like `expect_exact: \"some.exact.javascript;\"`");
247-
}
248-
test[node.label.name] = stat.body.start.value
260+
test[node.label.name] = read_string(stat);
249261
} else {
250262
test[node.label.name] = stat;
251263
}

0 commit comments

Comments
 (0)