Commit 150a8aba authored by Frederik Gossen's avatar Frederik Gossen Committed by Commit Bot

[test] Prevent Unintentionally Undefined Error Types

Ignore the error type in {assertThrows} only if it was not passed as an
argument. If users do not care about the error type they can user the
generic type {Error}. Before this change, an undefined error type would
simply be ignored. A simple typo could therefore disable the error type
assertion without being recognized.

Change-Id: I9becfd0bf14dcaa511854e65ff94f94481cc79b0
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1585855
Commit-Queue: Frederik Gossen <frgossen@google.com>
Reviewed-by: 's avatarMathias Bynens <mathias@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61105}
parent ed319e84
......@@ -118,19 +118,19 @@ TestLocalDoesNotThrow("switch (true) { case true: class x { }; }");
TestLocalDoesNotThrow("switch (true) { default: class x { }; }");
// Test that redeclarations of functions are only allowed in outermost scope.
TestLocalThrows("{ let f; var f; }");
TestLocalThrows("{ var f; let f; }");
TestLocalThrows("{ function f() {} let f; }");
TestLocalThrows("{ let f; function f() {} }");
TestLocalThrows("{ function f() {} var f; }");
TestLocalThrows("{ var f; function f() {} }");
TestLocalThrows("{ function f() {} class f {} }");
TestLocalThrows("{ class f {}; function f() {} }");
TestLocalThrows("{ function f() {} function f() {} }");
TestLocalThrows("function f() {} let f;");
TestLocalThrows("let f; function f() {}");
TestLocalThrows("function f() {} class f {}");
TestLocalThrows("class f {}; function f() {}");
TestLocalThrows("{ let f; var f; }", SyntaxError);
TestLocalThrows("{ var f; let f; }", SyntaxError);
TestLocalThrows("{ function f() {} let f; }", SyntaxError);
TestLocalThrows("{ let f; function f() {} }", SyntaxError);
TestLocalThrows("{ function f() {} var f; }", SyntaxError);
TestLocalThrows("{ var f; function f() {} }", SyntaxError);
TestLocalThrows("{ function f() {} class f {} }", SyntaxError);
TestLocalThrows("{ class f {}; function f() {} }", SyntaxError);
TestLocalThrows("{ function f() {} function f() {} }", SyntaxError);
TestLocalThrows("function f() {} let f;", SyntaxError);
TestLocalThrows("let f; function f() {}", SyntaxError);
TestLocalThrows("function f() {} class f {}", SyntaxError);
TestLocalThrows("class f {}; function f() {}", SyntaxError);
TestLocalDoesNotThrow("function arg() {}");
TestLocalDoesNotThrow("function f() {} var f;");
TestLocalDoesNotThrow("var f; function f() {}");
......
......@@ -120,19 +120,19 @@ TestLocalDoesNotThrow("switch (true) { case true: class x { }; }");
TestLocalDoesNotThrow("switch (true) { default: class x { }; }");
// Test that redeclarations of functions are only allowed in outermost scope.
TestLocalThrows("{ let f; var f; }");
TestLocalThrows("{ var f; let f; }");
TestLocalThrows("{ function f() {} let f; }");
TestLocalThrows("{ let f; function f() {} }");
TestLocalThrows("{ function f() {} var f; }");
TestLocalThrows("{ var f; function f() {} }");
TestLocalThrows("{ function f() {} class f {} }");
TestLocalThrows("{ class f {}; function f() {} }");
TestLocalThrows("{ function f() {} function f() {} }");
TestLocalThrows("function f() {} let f;");
TestLocalThrows("let f; function f() {}");
TestLocalThrows("function f() {} class f {}");
TestLocalThrows("class f {}; function f() {}");
TestLocalThrows("{ let f; var f; }", SyntaxError);
TestLocalThrows("{ var f; let f; }", SyntaxError);
TestLocalThrows("{ function f() {} let f; }", SyntaxError);
TestLocalThrows("{ let f; function f() {} }", SyntaxError);
TestLocalThrows("{ function f() {} var f; }", SyntaxError);
TestLocalThrows("{ var f; function f() {} }", SyntaxError);
TestLocalThrows("{ function f() {} class f {} }", SyntaxError);
TestLocalThrows("{ class f {}; function f() {} }", SyntaxError);
TestLocalThrows("{ function f() {} function f() {} }", SyntaxError);
TestLocalThrows("function f() {} let f;", SyntaxError);
TestLocalThrows("let f; function f() {}", SyntaxError);
TestLocalThrows("function f() {} class f {}", SyntaxError);
TestLocalThrows("class f {}; function f() {}", SyntaxError);
TestLocalDoesNotThrow("function arg() {}");
TestLocalDoesNotThrow("function f() {} var f;");
TestLocalDoesNotThrow("var f; function f() {}");
......
......@@ -508,6 +508,9 @@ var prettyPrinted;
}
assertThrows = function assertThrows(code, type_opt, cause_opt) {
if (arguments.length > 1 && type_opt === undefined) {
failWithMessage('invalid use of assertThrows, unknown type_opt given');
}
if (type_opt !== undefined && typeof type_opt !== 'function') {
failWithMessage(
'invalid use of assertThrows, maybe you want assertThrowsEquals');
......@@ -535,6 +538,9 @@ var prettyPrinted;
};
assertThrowsAsync = function assertThrowsAsync(promise, type_opt, cause_opt) {
if (arguments.length > 1 && type_opt === undefined) {
failWithMessage('invalid use of assertThrows, unknown type_opt given');
}
if (type_opt !== undefined && typeof type_opt !== 'function') {
failWithMessage(
'invalid use of assertThrows, maybe you want assertThrowsEquals');
......
......@@ -3,15 +3,15 @@
// found in the LICENSE file.
// The actual regression test
assertThrows("(import(foo)) =>", undefined, "Invalid destructuring assignment target");
assertThrows("(import(foo)) =>", SyntaxError, "Invalid destructuring assignment target");
// Other related tests
assertThrows("import(foo) =>", undefined, "Malformed arrow function parameter list");
assertThrows("(a, import(foo)) =>", undefined, "Invalid destructuring assignment target");
assertThrows("(1, import(foo)) =>", undefined, "Invalid destructuring assignment target");
assertThrows("(super(foo)) =>", undefined, "'super' keyword unexpected here");
assertThrows("(bar(foo)) =>", undefined, "Invalid destructuring assignment target");
assertThrows("import(foo) =>", SyntaxError, "Malformed arrow function parameter list");
assertThrows("(a, import(foo)) =>", SyntaxError, "Invalid destructuring assignment target");
assertThrows("(1, import(foo)) =>", SyntaxError, "Invalid destructuring assignment target");
assertThrows("(super(foo)) =>", SyntaxError, "'super' keyword unexpected here");
assertThrows("(bar(foo)) =>", SyntaxError, "Invalid destructuring assignment target");
// No syntax errors
assertThrows("[import(foo).then] = [1];", undefined, "foo is not defined");
assertThrows("[[import(foo).then]] = [[1]];", undefined, "foo is not defined");
assertThrows("[import(foo).then] = [1];", ReferenceError, "foo is not defined");
assertThrows("[[import(foo).then]] = [[1]];", ReferenceError, "foo is not defined");
......@@ -147,11 +147,11 @@ function foo(eval) {\
})();
// Octal literal
CheckStrictMode("var x = 012");
CheckStrictMode("012");
CheckStrictMode("'Hello octal\\032'");
CheckStrictMode("function octal() { return 012; }");
CheckStrictMode("function octal() { return '\\032'; }");
CheckStrictMode("var x = 012", SyntaxError);
CheckStrictMode("012", SyntaxError);
CheckStrictMode("'Hello octal\\032'", SyntaxError);
CheckStrictMode("function octal() { return 012; }", SyntaxError);
CheckStrictMode("function octal() { return '\\032'; }", SyntaxError);
(function ValidEscape() {
"use strict";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment