Commit 837900ef authored by neis's avatar neis Committed by Commit bot

[tests] Fix bogus uses of assertThrows.

Some tests passed a string as second argument to assertThrows, expecting it to
be matched against the exception.  However, assertThrows simply ignored these.
(Some other tests actually seem to use that argument as a comment ...)

This CL
- changes assertThrows to fail if the second argument is not a function,
- adds assertThrowsEquals which compares the exception to a given value using
  assertEquals
- fixes some bogus tests that got exposed by this.

R=jarin@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1544793002

Cr-Commit-Position: refs/heads/master@{#33159}
parent a0d03d72
...@@ -35,7 +35,7 @@ function Module() { ...@@ -35,7 +35,7 @@ function Module() {
} }
var m = Module(); var m = Module();
assertThrows(function() { m.w0(counter(5)) }, error); assertThrowsEquals(function() { m.w0(counter(5)) }, error);
assertThrows(function() { m.w1(counter(5)) }, error); assertThrowsEquals(function() { m.w1(counter(5)) }, error);
assertThrows(function() { m.w2(counter(5)) }, error); assertThrowsEquals(function() { m.w2(counter(5)) }, error);
assertEquals(111, m.w3(counter(5))); assertEquals(111, m.w3(counter(5)));
...@@ -73,7 +73,7 @@ if (this.os && os.system) { ...@@ -73,7 +73,7 @@ if (this.os && os.system) {
// Check that they are there. // Check that they are there.
os.system('ls', [TEST_DIR + '/dir/foo']); os.system('ls', [TEST_DIR + '/dir/foo']);
// Check that we can detect when something is not there. // Check that we can detect when something is not there.
assertThrows("os.system('ls', [TEST_DIR + '/dir/bar']);", "dir not there"); assertThrows("os.system('ls', [TEST_DIR + '/dir/bar']);");
// Check that mkdirp makes intermediate directories. // Check that mkdirp makes intermediate directories.
os.mkdirp(TEST_DIR + "/dir2/foo"); os.mkdirp(TEST_DIR + "/dir2/foo");
os.system("ls", [TEST_DIR + "/dir2/foo"]); os.system("ls", [TEST_DIR + "/dir2/foo"]);
...@@ -86,16 +86,16 @@ if (this.os && os.system) { ...@@ -86,16 +86,16 @@ if (this.os && os.system) {
// Check that we get an error if the name is taken by a file. // Check that we get an error if the name is taken by a file.
os.system("sh", ["-c", "echo foo > " + TEST_DIR + "/file1"]); os.system("sh", ["-c", "echo foo > " + TEST_DIR + "/file1"]);
os.system("ls", [TEST_DIR + "/file1"]); os.system("ls", [TEST_DIR + "/file1"]);
assertThrows("os.mkdirp(TEST_DIR + '/file1');", "mkdir over file1"); assertThrows("os.mkdirp(TEST_DIR + '/file1');");
assertThrows("os.mkdirp(TEST_DIR + '/file1/foo');", "mkdir over file2"); assertThrows("os.mkdirp(TEST_DIR + '/file1/foo');");
assertThrows("os.mkdirp(TEST_DIR + '/file1/');", "mkdir over file3"); assertThrows("os.mkdirp(TEST_DIR + '/file1/');");
assertThrows("os.mkdirp(TEST_DIR + '/file1/foo/');", "mkdir over file4"); assertThrows("os.mkdirp(TEST_DIR + '/file1/foo/');");
// Create a dir we cannot read. // Create a dir we cannot read.
os.mkdirp(TEST_DIR + "/dir4", 0); os.mkdirp(TEST_DIR + "/dir4", 0);
// This test fails if you are root since root can read any dir. // This test fails if you are root since root can read any dir.
assertThrows("os.chdir(TEST_DIR + '/dir4');", "chdir dir4 I"); assertThrows("os.chdir(TEST_DIR + '/dir4');");
os.rmdir(TEST_DIR + "/dir4"); os.rmdir(TEST_DIR + "/dir4");
assertThrows("os.chdir(TEST_DIR + '/dir4');", "chdir dir4 II"); assertThrows("os.chdir(TEST_DIR + '/dir4');");
// Set umask. This changes the umask for the whole process and is // Set umask. This changes the umask for the whole process and is
// the reason why the test cannot be run multi-threaded. // the reason why the test cannot be run multi-threaded.
...@@ -103,9 +103,9 @@ if (this.os && os.system) { ...@@ -103,9 +103,9 @@ if (this.os && os.system) {
// Create a dir we cannot read. // Create a dir we cannot read.
os.mkdirp(TEST_DIR + "/dir5"); os.mkdirp(TEST_DIR + "/dir5");
// This test fails if you are root since root can read any dir. // This test fails if you are root since root can read any dir.
assertThrows("os.chdir(TEST_DIR + '/dir5');", "cd dir5 I"); assertThrows("os.chdir(TEST_DIR + '/dir5');");
os.rmdir(TEST_DIR + "/dir5"); os.rmdir(TEST_DIR + "/dir5");
assertThrows("os.chdir(TEST_DIR + '/dir5');", "chdir dir5 II"); assertThrows("os.chdir(TEST_DIR + '/dir5');");
os.umask(old_umask); os.umask(old_umask);
os.mkdirp(TEST_DIR + "/hest/fisk/../fisk/ged"); os.mkdirp(TEST_DIR + "/hest/fisk/../fisk/ged");
...@@ -129,10 +129,10 @@ if (this.os && os.system) { ...@@ -129,10 +129,10 @@ if (this.os && os.system) {
have_echo = false; have_echo = false;
} }
if (have_sleep) { if (have_sleep) {
assertThrows("os.system('sleep', ['2000'], 20);", "sleep 1"); assertThrows("os.system('sleep', ['2000'], 20);");
// Check we time out with total time. // Check we time out with total time.
assertThrows("os.system('sleep', ['2000'], -1, 20);", "sleep 2"); assertThrows("os.system('sleep', ['2000'], -1, 20);");
// Check that -1 means no timeout. // Check that -1 means no timeout.
os.system('sleep', ['1'], -1, -1); os.system('sleep', ['1'], -1, -1);
......
...@@ -41,7 +41,7 @@ function testToStringTag(className) { ...@@ -41,7 +41,7 @@ function testToStringTag(className) {
Object.defineProperty(obj, Symbol.toStringTag, { Object.defineProperty(obj, Symbol.toStringTag, {
get: function() { throw className; } get: function() { throw className; }
}); });
assertThrows(function() { assertThrowsEquals(function() {
Array.prototype.toString.call(obj); Array.prototype.toString.call(obj);
}, className); }, className);
......
...@@ -41,7 +41,7 @@ function testToStringTag(className) { ...@@ -41,7 +41,7 @@ function testToStringTag(className) {
Object.defineProperty(obj, Symbol.toStringTag, { Object.defineProperty(obj, Symbol.toStringTag, {
get: function() { throw className; } get: function() { throw className; }
}); });
assertThrows(function() { assertThrowsEquals(function() {
Object.prototype.toString.call(obj); Object.prototype.toString.call(obj);
}, className); }, className);
......
...@@ -115,8 +115,8 @@ function TestForInThrow(handler) { ...@@ -115,8 +115,8 @@ function TestForInThrow(handler) {
function TestForInThrow2(create, handler) { function TestForInThrow2(create, handler) {
var p = create(handler) var p = create(handler)
var o = Object.create(p) var o = Object.create(p)
assertThrows(function(){ for (var x in p) {} }, "myexn") assertThrowsEquals(function(){ for (var x in p) {} }, "myexn")
assertThrows(function(){ for (var x in o) {} }, "myexn") assertThrowsEquals(function(){ for (var x in o) {} }, "myexn")
} }
TestForInThrow({ TestForInThrow({
......
...@@ -168,14 +168,14 @@ function TestWithGetCallThrow2(create, handler) { ...@@ -168,14 +168,14 @@ function TestWithGetCallThrow2(create, handler) {
var p = create(handler) var p = create(handler)
with (p) { with (p) {
assertThrows(function(){ a() }, "myexn") assertThrowsEquals(function(){ a() }, "myexn")
assertEquals("local", b()) assertEquals("local", b())
assertEquals("global", c()) assertEquals("global", c())
} }
var o = Object.create(p, {d: {value: function() { return "own" }}}) var o = Object.create(p, {d: {value: function() { return "own" }}})
with (o) { with (o) {
assertThrows(function(){ a() }, "myexn") assertThrowsEquals(function(){ a() }, "myexn")
assertEquals("local", b()) assertEquals("local", b())
assertEquals("global", c()) assertEquals("global", c())
assertEquals("own", d()) assertEquals("own", d())
...@@ -185,10 +185,12 @@ function TestWithGetCallThrow2(create, handler) { ...@@ -185,10 +185,12 @@ function TestWithGetCallThrow2(create, handler) {
function onproxythrow() { throw "myexn" } function onproxythrow() { throw "myexn" }
TestWithGetCallThrow({ TestWithGetCallThrow({
has: function(r, k) { return k === "a"; },
get: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined }, get: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
}) })
TestWithGetCallThrow({ TestWithGetCallThrow({
has: function(r, k) { return k === "a"; },
get: function(r, k) { return this.get2(r, k) }, get: function(r, k) { return this.get2(r, k) },
get2: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined }, get2: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
}) })
...@@ -305,7 +307,7 @@ function TestWithSetThrow(handler, hasSetter) { ...@@ -305,7 +307,7 @@ function TestWithSetThrow(handler, hasSetter) {
function TestWithSetThrow2(create, handler, hasSetter) { function TestWithSetThrow2(create, handler, hasSetter) {
var p = create(handler) var p = create(handler)
assertThrows(function(){ assertThrowsEquals(function(){
with (p) { with (p) {
a = 1 a = 1
} }
...@@ -314,7 +316,7 @@ function TestWithSetThrow2(create, handler, hasSetter) { ...@@ -314,7 +316,7 @@ function TestWithSetThrow2(create, handler, hasSetter) {
if (!hasSetter) return if (!hasSetter) return
var o = Object.create(p, {}) var o = Object.create(p, {})
assertThrows(function(){ assertThrowsEquals(function(){
with (o) { with (o) {
a = 1 a = 1
} }
......
This diff is collapsed.
...@@ -87,7 +87,7 @@ function prepare(target) { ...@@ -87,7 +87,7 @@ function prepare(target) {
var a = { [Symbol.toPrimitive]: function() { return "bla" } }; var a = { [Symbol.toPrimitive]: function() { return "bla" } };
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
assertEquals(42, Reflect.get(target, a)); assertEquals(42, Reflect.get(target, a));
assertThrows(function() { Reflect.get(target, b); }, "gaga"); assertThrowsEquals(function() { Reflect.get(target, b); }, "gaga");
})(); })();
...@@ -156,7 +156,7 @@ function prepare(target) { ...@@ -156,7 +156,7 @@ function prepare(target) {
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
assertTrue(Reflect.set(target, a, 42)); assertTrue(Reflect.set(target, a, 42));
assertEquals(42, target.bla); assertEquals(42, target.bla);
assertThrows(function() { Reflect.set(target, b, 42); }, "gaga"); assertThrowsEquals(function() { Reflect.set(target, b, 42); }, "gaga");
})(); })();
...@@ -297,7 +297,7 @@ function prepare(target) { ...@@ -297,7 +297,7 @@ function prepare(target) {
var a = { [Symbol.toPrimitive]: function() { return "bla" } }; var a = { [Symbol.toPrimitive]: function() { return "bla" } };
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
assertTrue(Reflect.has(target, a)); assertTrue(Reflect.has(target, a));
assertThrows(function() { Reflect.has(target, b); }, "gaga"); assertThrowsEquals(function() { Reflect.has(target, b); }, "gaga");
})(); })();
...@@ -350,7 +350,7 @@ function prepare(target) { ...@@ -350,7 +350,7 @@ function prepare(target) {
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
assertTrue(Reflect.defineProperty(target, a, {value: 42})); assertTrue(Reflect.defineProperty(target, a, {value: 42}));
assertEquals(target.bla, 42); assertEquals(target.bla, 42);
assertThrows(function() { Reflect.defineProperty(target, b); }, "gaga"); assertThrowsEquals(function() { Reflect.defineProperty(target, b); }, "gaga");
})(); })();
...@@ -379,7 +379,7 @@ function prepare(target) { ...@@ -379,7 +379,7 @@ function prepare(target) {
var a = { [Symbol.toPrimitive]: function() { return "bla" } }; var a = { [Symbol.toPrimitive]: function() { return "bla" } };
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
assertTrue(Reflect.deleteProperty(target, a)); assertTrue(Reflect.deleteProperty(target, a));
assertThrows(function() { Reflect.deleteProperty(target, b); }, "gaga"); assertThrowsEquals(function() { Reflect.deleteProperty(target, b); }, "gaga");
})(); })();
...@@ -530,8 +530,7 @@ function prepare(target) { ...@@ -530,8 +530,7 @@ function prepare(target) {
var a = { [Symbol.toPrimitive]: function() { return "bla" } }; var a = { [Symbol.toPrimitive]: function() { return "bla" } };
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
assertEquals(42, Reflect.getOwnPropertyDescriptor(target, a).value); assertEquals(42, Reflect.getOwnPropertyDescriptor(target, a).value);
assertThrows(function() { Reflect.getOwnPropertyDescriptor(target, b); }, assertThrowsEquals(() => Reflect.getOwnPropertyDescriptor(target, b), "gaga");
"gaga");
})(); })();
......
...@@ -93,6 +93,10 @@ var assertNotNull; ...@@ -93,6 +93,10 @@ var assertNotNull;
// to the type property on the thrown exception. // to the type property on the thrown exception.
var assertThrows; var assertThrows;
// Assert that the passed function throws an exception.
// The exception is checked against the second argument using assertEquals.
var assertThrowsEquals;
// Assert that the passed function or eval code does not throw an exception. // Assert that the passed function or eval code does not throw an exception.
var assertDoesNotThrow; var assertDoesNotThrow;
...@@ -353,6 +357,8 @@ var assertUnoptimized; ...@@ -353,6 +357,8 @@ var assertUnoptimized;
} catch (e) { } catch (e) {
if (typeof type_opt === 'function') { if (typeof type_opt === 'function') {
assertInstanceof(e, type_opt); assertInstanceof(e, type_opt);
} else if (type_opt !== void 0) {
fail("invalid use of assertThrows, maybe you want assertThrowsEquals");
} }
if (arguments.length >= 3) { if (arguments.length >= 3) {
assertEquals(e.type, cause_opt); assertEquals(e.type, cause_opt);
...@@ -364,6 +370,17 @@ var assertUnoptimized; ...@@ -364,6 +370,17 @@ var assertUnoptimized;
}; };
assertThrowsEquals = function assertThrowsEquals(fun, val) {
try {
fun();
} catch(e) {
assertEquals(val, e);
return;
}
throw new MjsUnitAssertionError("Did not throw exception");
};
assertInstanceof = function assertInstanceof(obj, type) { assertInstanceof = function assertInstanceof(obj, type) {
if (!(obj instanceof type)) { if (!(obj instanceof type)) {
var actualTypeName = null; var actualTypeName = null;
......
...@@ -120,7 +120,7 @@ function test8() { ...@@ -120,7 +120,7 @@ function test8() {
} }
assertEquals(true, test8(), "test8"); assertEquals(true, test8(), "test8");
assertThrows("x", "test8"); // Global x should be deleted. assertThrows("x"); // Global x should be deleted.
// Delete on a property that is not found anywhere. // Delete on a property that is not found anywhere.
...@@ -128,7 +128,7 @@ function test9() { ...@@ -128,7 +128,7 @@ function test9() {
with ({}) { return delete x; } with ({}) { return delete x; }
} }
assertThrows("x", "test9"); // Make sure it's not there. assertThrows("x"); // Make sure it's not there.
assertEquals(true, test9(), "test9"); assertEquals(true, test9(), "test9");
......
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