aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-03-18 02:33:51 +0800
committerGitHub <noreply@github.com>2017-03-18 02:33:51 +0800
commit0489d6de6499154c758a6464387546ec5a060f67 (patch)
tree71d718c699e59b222e2a3d4d9ba74f5a4f7d59d0
parentfb092839c26ddaa0614df5295be85ea207bda9f7 (diff)
downloadtracifyjs-0489d6de6499154c758a6464387546ec5a060f67.tar.gz
tracifyjs-0489d6de6499154c758a6464387546ec5a060f67.zip
handle runtime errors in `expect_stdout` (#1618)
allow test to pass if both `input` and `expect` throws the same kind of error
-rw-r--r--test/compress/issue-1588.js12
-rwxr-xr-xtest/run-tests.js61
2 files changed, 38 insertions, 35 deletions
diff --git a/test/compress/issue-1588.js b/test/compress/issue-1588.js
index ff25635c..fce9ba54 100644
--- a/test/compress/issue-1588.js
+++ b/test/compress/issue-1588.js
@@ -85,3 +85,15 @@ unsafe_undefined: {
}
expect_stdout: true
}
+
+runtime_error: {
+ input: {
+ const a = 1;
+ console.log(a++);
+ }
+ expect: {
+ const a = 1;
+ console.log(a++);
+ }
+ expect_stdout: true
+}
diff --git a/test/run-tests.js b/test/run-tests.js
index c3c142d1..7f3256e0 100755
--- a/test/run-tests.js
+++ b/test/run-tests.js
@@ -172,48 +172,33 @@ function run_compress_tests() {
}
}
if (test.expect_stdout) {
- try {
- var stdout = run_code(input_code);
- if (test.expect_stdout === true) {
- test.expect_stdout = stdout;
- }
- if (test.expect_stdout != stdout) {
- log("!!! Invalid input or expected stdout\n---INPUT---\n{input}\n---EXPECTED STDOUT---\n{expected}\n---ACTUAL STDOUT---\n{actual}\n\n", {
+ var stdout = run_code(input_code);
+ if (test.expect_stdout === true) {
+ test.expect_stdout = stdout;
+ }
+ if (!same_stdout(test.expect_stdout, stdout)) {
+ log("!!! Invalid input or expected stdout\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", {
+ input: input_code,
+ expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR",
+ expected: test.expect_stdout,
+ actual_type: typeof stdout == "string" ? "STDOUT" : "ERROR",
+ actual: stdout,
+ });
+ failures++;
+ failed_files[file] = 1;
+ } else {
+ stdout = run_code(output);
+ if (!same_stdout(test.expect_stdout, stdout)) {
+ log("!!! failed\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", {
input: input_code,
+ expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR",
expected: test.expect_stdout,
+ actual_type: typeof stdout == "string" ? "STDOUT" : "ERROR",
actual: stdout,
});
failures++;
failed_files[file] = 1;
- } else {
- try {
- stdout = run_code(output);
- if (test.expect_stdout != stdout) {
- log("!!! failed\n---INPUT---\n{input}\n---EXPECTED STDOUT---\n{expected}\n---ACTUAL STDOUT---\n{actual}\n\n", {
- input: input_code,
- expected: test.expect_stdout,
- actual: stdout,
- });
- failures++;
- failed_files[file] = 1;
- }
- } catch (ex) {
- log("!!! Execution of output failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n--ERROR--\n{error}\n\n", {
- input: input_code,
- output: output,
- error: ex.toString(),
- });
- failures++;
- failed_files[file] = 1;
- }
}
- } catch (ex) {
- log("!!! Execution of input failed\n---INPUT---\n{input}\n--ERROR--\n{error}\n\n", {
- input: input_code,
- error: ex.toString(),
- });
- failures++;
- failed_files[file] = 1;
}
}
}
@@ -345,7 +330,13 @@ function run_code(code) {
try {
new vm.Script(code).runInNewContext({ console: console }, { timeout: 5000 });
return stdout;
+ } catch (ex) {
+ return ex;
} finally {
process.stdout.write = original_write;
}
}
+
+function same_stdout(expected, actual) {
+ return typeof expected == typeof actual && expected.toString() == actual.toString();
+}