Bug 1831042: store the index of an added assertion and use it to update the corresponding assertion's status. r=jgraham

This allows nested assertions in `assert_dom_throws` which currently
some tests use, e.g. <popover-attribute-basic.html>.

Differential Revision: https://phabricator.services.mozilla.com/D177006
This commit is contained in:
Mirko Brodesser 2023-05-04 13:59:48 +00:00
parent 952ba710e6
commit 98ab729dd1
2 changed files with 62 additions and 3 deletions

View file

@ -0,0 +1,55 @@
<!DOCTYPE HTML>
<title>assert_throws_dom</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
test(() => {
function f() {
assert_true(false, "Trivial assertion.");
// Would lead to throwing a SyntaxError.
document.createElement("div").contentEditable = "invalid";
}
assert_throws_dom("SyntaxError", () => { f(); });
}, "Violated assertion nested in `assert_throws_dom`.");
</script>
<script type="text/json" id="expected">
{
"type": "complete",
"summarized_status": {
"message": null,
"status_string": "OK"
},
"summarized_tests": [
{
"message": "assert_true: Trivial assertion. expected true got false",
"name": "Violated assertion nested in `assert_throws_dom`.",
"properties": {},
"status_string": "FAIL"
}
],
"summarized_asserts": [
{
"assert_name": "assert_throws_dom",
"test": "Violated assertion nested in `assert_throws_dom`.",
"args": [
"\"SyntaxError\"",
"function \"() => { f(); }\""
],
"status": 1
},
{
"assert_name": "assert_true",
"test": "Violated assertion nested in `assert_throws_dom`.",
"args": [
"false",
"\"Trivial assertion.\""
],
"status": 1
}
]
}
</script>

View file

@ -1426,12 +1426,16 @@
function assert_wrapper(...args) {
let status = Test.statuses.TIMEOUT;
let stack = null;
let new_assert_index = null;
try {
if (settings.debug) {
console.debug("ASSERT", name, tests.current_test && tests.current_test.name, args);
}
if (tests.output) {
tests.set_assert(name, args);
// Remember the newly pushed assert's index, because `apply`
// below might push new asserts.
new_assert_index = tests.asserts_run.length - 1;
}
const rv = f.apply(undefined, args);
status = Test.statuses.PASS;
@ -1445,7 +1449,7 @@
stack = get_stack();
}
if (tests.output) {
tests.set_assert_status(status, stack);
tests.set_assert_status(new_assert_index, status, stack);
}
}
}
@ -3673,8 +3677,8 @@
this.asserts_run.push(new AssertRecord(this.current_test, assert_name, args))
}
Tests.prototype.set_assert_status = function(status, stack) {
let assert_record = this.asserts_run[this.asserts_run.length - 1];
Tests.prototype.set_assert_status = function(index, status, stack) {
let assert_record = this.asserts_run[index];
assert_record.status = status;
assert_record.stack = stack;
}