From 295192bf153b207702c7df0f993daf8a5f58e5ea Mon Sep 17 00:00:00 2001 From: davidot Date: Mon, 12 Jul 2021 01:39:03 +0200 Subject: [PATCH] LibJS: Add tests for strict mode and strict mode propagation --- .../LibJS/Tests/program-non-strict.js | 61 +++++++++++++++++++ .../LibJS/Tests/program-strict-mode.js | 8 +++ 2 files changed, 69 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/program-non-strict.js diff --git a/Userland/Libraries/LibJS/Tests/program-non-strict.js b/Userland/Libraries/LibJS/Tests/program-non-strict.js new file mode 100644 index 00000000000..c66e298d731 --- /dev/null +++ b/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(); +}); diff --git a/Userland/Libraries/LibJS/Tests/program-strict-mode.js b/Userland/Libraries/LibJS/Tests/program-strict-mode.js index d8f8941d0f2..3bb885a49bb 100644 --- a/Userland/Libraries/LibJS/Tests/program-strict-mode.js +++ b/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()"); });