浏览代码

LibWeb: Prevent calling test() twice

Calling test() multiple times in the same test file is not actually
valid, and can cause the following test to hang forever. So let's stop
doing that in the one test that did so, and also prevent the same
mistake happening again. :^)

Throwing an exception on subsequent test() calls means that we don't
hang, the test will fail with missing output, and we get a log message
explaining why.
Sam Atkins 1 年之前
父节点
当前提交
1e6cd19b28
共有 2 个文件被更改,包括 16 次插入7 次删除
  1. 4 6
      Tests/LibWeb/Text/input/get-bounding-client-rect.html
  2. 12 1
      Tests/LibWeb/Text/input/include.js

+ 4 - 6
Tests/LibWeb/Text/input/get-bounding-client-rect.html

@@ -13,12 +13,10 @@
 <script src="include.js"></script>
 <script>
     test(() => {
-        const rect = document.getElementById("box").getBoundingClientRect();
-        println(JSON.stringify(rect));
-    });
+        const box_rect = document.getElementById("box").getBoundingClientRect();
+        println(JSON.stringify(box_rect));
 
-    test(() => {
-        const rect = document.getElementById("inline").getBoundingClientRect();
-        println(JSON.stringify(rect));
+        const inline_rect = document.getElementById("inline").getBoundingClientRect();
+        println(JSON.stringify(inline_rect));
     });
 </script>

+ 12 - 1
Tests/LibWeb/Text/input/include.js

@@ -1,4 +1,11 @@
 var __outputElement = null;
+let __alreadyCalledTest = false;
+function __preventMultipleTestFunctions() {
+    if (__alreadyCalledTest) {
+        throw new Error("You must only call test() or asyncTest() once per page");
+    }
+    __alreadyCalledTest = true;
+}
 
 if (globalThis.internals === undefined) {
     internals = {
@@ -17,6 +24,7 @@ document.addEventListener("DOMContentLoaded", function () {
 });
 
 function test(f) {
+    __preventMultipleTestFunctions();
     document.addEventListener("DOMContentLoaded", f);
     window.addEventListener("load", () => {
         internals.signalTextTestIsDone();
@@ -24,7 +32,10 @@ function test(f) {
 }
 
 function asyncTest(f) {
-    const done = () => internals.signalTextTestIsDone();
+    const done = () => {
+        __preventMultipleTestFunctions();
+        internals.signalTextTestIsDone();
+    };
     document.addEventListener("DOMContentLoaded", () => {
         f(done);
     });