Browse Source

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 year ago
parent
commit
1e6cd19b28

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

@@ -13,12 +13,10 @@
 <script src="include.js"></script>
 <script src="include.js"></script>
 <script>
 <script>
     test(() => {
     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>
 </script>

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

@@ -1,4 +1,11 @@
 var __outputElement = null;
 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) {
 if (globalThis.internals === undefined) {
     internals = {
     internals = {
@@ -17,6 +24,7 @@ document.addEventListener("DOMContentLoaded", function () {
 });
 });
 
 
 function test(f) {
 function test(f) {
+    __preventMultipleTestFunctions();
     document.addEventListener("DOMContentLoaded", f);
     document.addEventListener("DOMContentLoaded", f);
     window.addEventListener("load", () => {
     window.addEventListener("load", () => {
         internals.signalTextTestIsDone();
         internals.signalTextTestIsDone();
@@ -24,7 +32,10 @@ function test(f) {
 }
 }
 
 
 function asyncTest(f) {
 function asyncTest(f) {
-    const done = () => internals.signalTextTestIsDone();
+    const done = () => {
+        __preventMultipleTestFunctions();
+        internals.signalTextTestIsDone();
+    };
     document.addEventListener("DOMContentLoaded", () => {
     document.addEventListener("DOMContentLoaded", () => {
         f(done);
         f(done);
     });
     });