Skip to content

Commit 9ded22d

Browse files
authored
Add basic unittests (#94)
Prepare code to run basic unit tests: - Add separate shell-config.js that can be shared between cli and unit tests - Add basic first unit tests to check that all tags are strings
1 parent 9f5b161 commit 9ded22d

File tree

4 files changed

+76
-35
lines changed

4 files changed

+76
-35
lines changed

cli.js

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,7 @@
2323
* THE POSSIBILITY OF SUCH DAMAGE.
2424
*/
2525

26-
const isInBrowser = false;
27-
console = {
28-
log: globalThis?.console?.log ?? print,
29-
error: globalThis?.console?.error ?? print,
30-
}
31-
32-
const isD8 = typeof Realm !== "undefined";
33-
if (isD8)
34-
globalThis.readFile = read;
35-
const isSpiderMonkey = typeof newGlobal !== "undefined";
36-
if (isSpiderMonkey) {
37-
globalThis.readFile = readRelativeToScript;
38-
globalThis.arguments = scriptArgs;
39-
}
40-
41-
if (typeof arguments !== "undefined" && arguments.length > 0)
42-
testList = arguments.slice();
43-
if (typeof testList === "undefined")
44-
testList = undefined;
45-
46-
if (typeof testIterationCount === "undefined")
47-
testIterationCount = undefined;
48-
49-
if (typeof runMode !== "undefined" && runMode == "RAMification")
50-
RAMification = true;
51-
else
52-
RAMification = false;
53-
26+
load("./shell-config.js")
5427
load("./JetStreamDriver.js");
5528

5629
async function runJetStream() {

shell-config.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2018 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
* THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
const isInBrowser = false;
27+
console = {
28+
log: globalThis?.console?.log ?? print,
29+
error: globalThis?.console?.error ?? print,
30+
}
31+
32+
const isD8 = typeof Realm !== "undefined";
33+
if (isD8)
34+
globalThis.readFile = read;
35+
const isSpiderMonkey = typeof newGlobal !== "undefined";
36+
if (isSpiderMonkey) {
37+
globalThis.readFile = readRelativeToScript;
38+
globalThis.arguments = scriptArgs;
39+
}
40+
41+
if (typeof arguments !== "undefined" && arguments.length > 0)
42+
testList = arguments.slice();
43+
if (typeof testList === "undefined")
44+
testList = undefined;
45+
46+
if (typeof testIterationCount === "undefined")
47+
testIterationCount = undefined;
48+
49+
if (typeof runMode !== "undefined" && runMode == "RAMification")
50+
RAMification = true;
51+
else
52+
RAMification = false;

tests/run-shell.mjs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ const SHELL_NAME = (function() {
6666
const FILE_PATH = fileURLToPath(import.meta.url);
6767
const SRC_DIR = path.dirname(path.dirname(FILE_PATH));
6868
const CLI_PATH = path.join(SRC_DIR, "cli.js");
69+
const UNIT_TEST_PATH = path.join(SRC_DIR, "tests", "unit-tests.js");
6970

70-
const BASE_CLI_ARGS_WITH_OPTIONS = [CLI_PATH];
71-
if (SHELL_NAME != "spidermonkey")
72-
BASE_CLI_ARGS_WITH_OPTIONS.push("--");
73-
Object.freeze(BASE_CLI_ARGS_WITH_OPTIONS);
71+
function convertCliArgs(cli, ...cliArgs) {
72+
if (SHELL_NAME == "spidermonkey")
73+
return [cli, ...cliArgs]
74+
return [cli, "--", ...cliArgs];
75+
}
7476

7577
const GITHUB_ACTIONS_OUTPUT = "GITHUB_ACTIONS_OUTPUT" in process.env;
7678

@@ -133,10 +135,10 @@ function sh(binary, args) {
133135
async function runTests() {
134136
const shellBinary = logGroup(`Installing JavaScript Shell: ${SHELL_NAME}`, testSetup);
135137
let success = true;
136-
success &&= runTest("Run Complete Suite", () => sh(shellBinary, [CLI_PATH]));
138+
success &&= runTest("Run UnitTests", () => sh(shellBinary, [UNIT_TEST_PATH]));
139+
success &&= runTest("Run Complete Suite", () => sh(shellBinary, convertCliArgs(CLI_PATH)));
137140
success &&= runTest("Run Single Suite", () => {
138-
const singleTestArgs = [...BASE_CLI_ARGS_WITH_OPTIONS, "proxy-mobx"];
139-
sh(shellBinary, singleTestArgs);
141+
sh(shellBinary, convertCliArgs(CLI_PATH, "proxy-mobx"));
140142
});
141143
if (!success) {
142144
process.exit(1)

tests/unit-tests.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("shell-config.js")
2+
load("JetStreamDriver.js");
3+
4+
function assertTrue(condition, message) {
5+
if (!condition) {
6+
throw new Error(message || "Assertion failed");
7+
}
8+
}
9+
10+
(function testTagsAreStrings() {
11+
for (const benchmark of BENCHMARKS) {
12+
benchmark.tags.forEach(tag => assertTrue(typeof(tag) == "string"))
13+
}
14+
})();

0 commit comments

Comments
 (0)