Skip to content

Commit cbbc487

Browse files
gsneddersjgraham
authored andcommitted
Use func.toString as the test name as a first fallback
1 parent c5d9edf commit cbbc487

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta name="variant" content="">
5+
<meta name="variant" content="?keep-promise">
6+
<title>Tests with no title</title>
7+
<script src="../../variants.js"></script>
8+
</head>
9+
<script src="/resources/testharness.js"></script>
10+
11+
<body>
12+
<h1>Tests with no title</h1>
13+
<div id="log"></div>
14+
<script>
15+
test(function(){assert_true(true, '1')});
16+
test(()=>assert_true(true, '2'));
17+
test(() => assert_true(true, '3'));
18+
test(() => {
19+
assert_true(true, '4');
20+
});
21+
test(() => { assert_true(true, '5') });
22+
test(() => { assert_true(true, '6') } );
23+
test(() => { assert_true(true, '7'); });
24+
</script>
25+
<script type="text/json" id="expected">
26+
{
27+
"summarized_status": {
28+
"status_string": "OK",
29+
"message": null
30+
},
31+
"summarized_tests": [
32+
{
33+
"status_string": "PASS",
34+
"name": "Tests with no title",
35+
"properties": {},
36+
"message": null
37+
},
38+
{
39+
"status_string": "PASS",
40+
"name": "assert_true(true, '2')",
41+
"properties": {},
42+
"message": null
43+
},
44+
{
45+
"status_string": "PASS",
46+
"name": "assert_true(true, '3')",
47+
"properties": {},
48+
"message": null
49+
},
50+
{
51+
"status_string": "PASS",
52+
"name": "Tests with no title 1",
53+
"properties": {},
54+
"message": null
55+
},
56+
{
57+
"status_string": "PASS",
58+
"name": "assert_true(true, '5')",
59+
"properties": {},
60+
"message": null
61+
},
62+
{
63+
"status_string": "PASS",
64+
"name": "assert_true(true, '6')",
65+
"properties": {},
66+
"message": null
67+
},
68+
{
69+
"status_string": "PASS",
70+
"name": "assert_true(true, '7')",
71+
"properties": {},
72+
"message": null
73+
}
74+
],
75+
"type": "complete"
76+
}
77+
</script>
78+
</body>
79+
</html>

resources/testharness.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,30 @@ policies and contribution forms [3].
520520
Object.prototype.toString.call(worker) == '[object ServiceWorker]';
521521
}
522522

523+
function get_test_name(func, name)
524+
{
525+
if (name) {
526+
return name;
527+
}
528+
529+
if (func) {
530+
var func_code = func.toString();
531+
532+
// Try and match with brackets, but fallback to matching without
533+
var arrow = func_code.match(/^\(\)\s*=>\s*(?:{(.*)}\s*|(.*))$/);
534+
535+
// Check for JS line separators
536+
if (arrow !== null && !/[\u000A\u000D\u2028\u2029]/.test(func_code)) {
537+
var trimmed = (arrow[1] || arrow[2]).trim();
538+
// drop a trailing ; if it's the only one
539+
if (/^[^;]*;$/.test(trimmed)) trimmed = trimmed.substring(0, trimmed.length - 1);
540+
return trimmed;
541+
}
542+
}
543+
544+
return test_environment.next_default_test_name();
545+
}
546+
523547
/*
524548
* API functions
525549
*/
@@ -530,7 +554,7 @@ policies and contribution forms [3].
530554
tests.status.message = '`test` invoked after `promise_setup`';
531555
tests.complete();
532556
}
533-
var test_name = name ? name : test_environment.next_default_test_name();
557+
var test_name = get_test_name(func, name);
534558
var test_obj = new Test(test_name, properties);
535559
var value = test_obj.step(func, test_obj, test_obj);
536560

@@ -566,7 +590,7 @@ policies and contribution forms [3].
566590
name = func;
567591
func = null;
568592
}
569-
var test_name = name ? name : test_environment.next_default_test_name();
593+
var test_name = get_test_name(func, name);
570594
var test_obj = new Test(test_name, properties);
571595
if (func) {
572596
var value = test_obj.step(func, test_obj, test_obj);
@@ -603,7 +627,7 @@ policies and contribution forms [3].
603627
name = func;
604628
func = null;
605629
}
606-
var test_name = name ? name : test_environment.next_default_test_name();
630+
var test_name = get_test_name(func, name);
607631
var test = new Test(test_name, properties);
608632
test._is_promise_test = true;
609633

0 commit comments

Comments
 (0)