Quellcode durchsuchen

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 vor 1 Jahr
Ursprung
Commit
1e6cd19b28

+ 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);
     });