Browse Source

LibJS: Add tests for strict mode and strict mode propagation

davidot 4 years ago
parent
commit
295192bf15

+ 61 - 0
Userland/Libraries/LibJS/Tests/program-non-strict.js

@@ -0,0 +1,61 @@
+"do not use strict";
+"no really";
+// /\ Valid directives which should not trigger strict mode
+
+test("basic functionality", () => {
+    expect(isStrictMode()).toBeFalse();
+
+    (function () {
+        expect(isStrictMode()).toBeFalse();
+    })();
+
+    (() => {
+        expect(isStrictMode()).toBeFalse();
+    })();
+
+    (() => {
+        expect(isStrictMode()).toBeFalse();
+    })();
+
+    function a() {
+        expect(isStrictMode()).toBeFalse();
+    }
+
+    a();
+
+    eval("expect(isStrictMode()).toBeFalse()");
+});
+
+test("functions with strict mode", () => {
+    expect(isStrictMode()).toBeFalse();
+
+    function a() {
+        "this is allowed trust me";
+        "use strict";
+        expect(isStrictMode()).toBeTrue();
+    }
+
+    a();
+
+    expect(isStrictMode()).toBeFalse();
+
+    (() => {
+        "use strict";
+        expect(isStrictMode()).toBeTrue();
+    })();
+
+    function b() {
+        eval("expect(isStrictMode()).toBeFalse()");
+
+        function nested() {
+            "use strict";
+            eval("expect(isStrictMode()).toBeTrue()");
+        }
+
+        nested();
+
+        eval("expect(isStrictMode()).toBeFalse()");
+    }
+
+    b();
+});

+ 8 - 0
Userland/Libraries/LibJS/Tests/program-strict-mode.js

@@ -15,4 +15,12 @@ test("basic functionality", () => {
         "use strict";
         expect(isStrictMode()).toBeTrue();
     })();
+
+    function a() {
+        expect(isStrictMode()).toBeTrue();
+    }
+
+    a();
+
+    eval("expect(isStrictMode()).toBeTrue()");
 });