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() {
}
var m = Module();
assertThrows(function() { m.w0(counter(5)) }, error);
assertThrows(function() { m.w1(counter(5)) }, error);
assertThrows(function() { m.w2(counter(5)) }, error);
assertThrowsEquals(function() { m.w0(counter(5)) }, error);
assertThrowsEquals(function() { m.w1(counter(5)) }, error);
assertThrowsEquals(function() { m.w2(counter(5)) }, error);
assertEquals(111, m.w3(counter(5)));
......@@ -73,7 +73,7 @@ if (this.os && os.system) {
// Check that they are there.
os.system('ls', [TEST_DIR + '/dir/foo']);
// 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.
os.mkdirp(TEST_DIR + "/dir2/foo");
os.system("ls", [TEST_DIR + "/dir2/foo"]);
......@@ -86,16 +86,16 @@ if (this.os && os.system) {
// 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("ls", [TEST_DIR + "/file1"]);
assertThrows("os.mkdirp(TEST_DIR + '/file1');", "mkdir over file1");
assertThrows("os.mkdirp(TEST_DIR + '/file1/foo');", "mkdir over file2");
assertThrows("os.mkdirp(TEST_DIR + '/file1/');", "mkdir over file3");
assertThrows("os.mkdirp(TEST_DIR + '/file1/foo/');", "mkdir over file4");
assertThrows("os.mkdirp(TEST_DIR + '/file1');");
assertThrows("os.mkdirp(TEST_DIR + '/file1/foo');");
assertThrows("os.mkdirp(TEST_DIR + '/file1/');");
assertThrows("os.mkdirp(TEST_DIR + '/file1/foo/');");
// Create a dir we cannot read.
os.mkdirp(TEST_DIR + "/dir4", 0);
// 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");
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
// the reason why the test cannot be run multi-threaded.
......@@ -103,9 +103,9 @@ if (this.os && os.system) {
// Create a dir we cannot read.
os.mkdirp(TEST_DIR + "/dir5");
// 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");
assertThrows("os.chdir(TEST_DIR + '/dir5');", "chdir dir5 II");
assertThrows("os.chdir(TEST_DIR + '/dir5');");
os.umask(old_umask);
os.mkdirp(TEST_DIR + "/hest/fisk/../fisk/ged");
......@@ -129,10 +129,10 @@ if (this.os && os.system) {
have_echo = false;
}
if (have_sleep) {
assertThrows("os.system('sleep', ['2000'], 20);", "sleep 1");
assertThrows("os.system('sleep', ['2000'], 20);");
// 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.
os.system('sleep', ['1'], -1, -1);
......
......@@ -41,7 +41,7 @@ function testToStringTag(className) {
Object.defineProperty(obj, Symbol.toStringTag, {
get: function() { throw className; }
});
assertThrows(function() {
assertThrowsEquals(function() {
Array.prototype.toString.call(obj);
}, className);
......
......@@ -41,7 +41,7 @@ function testToStringTag(className) {
Object.defineProperty(obj, Symbol.toStringTag, {
get: function() { throw className; }
});
assertThrows(function() {
assertThrowsEquals(function() {
Object.prototype.toString.call(obj);
}, className);
......
......@@ -115,8 +115,8 @@ function TestForInThrow(handler) {
function TestForInThrow2(create, handler) {
var p = create(handler)
var o = Object.create(p)
assertThrows(function(){ for (var x in p) {} }, "myexn")
assertThrows(function(){ for (var x in o) {} }, "myexn")
assertThrowsEquals(function(){ for (var x in p) {} }, "myexn")
assertThrowsEquals(function(){ for (var x in o) {} }, "myexn")
}
TestForInThrow({
......
......@@ -168,14 +168,14 @@ function TestWithGetCallThrow2(create, handler) {
var p = create(handler)
with (p) {
assertThrows(function(){ a() }, "myexn")
assertThrowsEquals(function(){ a() }, "myexn")
assertEquals("local", b())
assertEquals("global", c())
}
var o = Object.create(p, {d: {value: function() { return "own" }}})
with (o) {
assertThrows(function(){ a() }, "myexn")
assertThrowsEquals(function(){ a() }, "myexn")
assertEquals("local", b())
assertEquals("global", c())
assertEquals("own", d())
......@@ -185,10 +185,12 @@ function TestWithGetCallThrow2(create, handler) {
function onproxythrow() { throw "myexn" }
TestWithGetCallThrow({
has: function(r, k) { return k === "a"; },
get: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
})
TestWithGetCallThrow({
has: function(r, k) { return k === "a"; },
get: function(r, k) { return this.get2(r, k) },
get2: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
})
......@@ -305,7 +307,7 @@ function TestWithSetThrow(handler, hasSetter) {
function TestWithSetThrow2(create, handler, hasSetter) {
var p = create(handler)
assertThrows(function(){
assertThrowsEquals(function(){
with (p) {
a = 1
}
......@@ -314,7 +316,7 @@ function TestWithSetThrow2(create, handler, hasSetter) {
if (!hasSetter) return
var o = Object.create(p, {})
assertThrows(function(){
assertThrowsEquals(function(){
with (o) {
a = 1
}
......
This diff is collapsed.
......@@ -87,7 +87,7 @@ function prepare(target) {
var a = { [Symbol.toPrimitive]: function() { return "bla" } };
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
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) {
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
assertTrue(Reflect.set(target, a, 42));
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) {
var a = { [Symbol.toPrimitive]: function() { return "bla" } };
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
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) {
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
assertTrue(Reflect.defineProperty(target, a, {value: 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) {
var a = { [Symbol.toPrimitive]: function() { return "bla" } };
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
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) {
var a = { [Symbol.toPrimitive]: function() { return "bla" } };
var b = { [Symbol.toPrimitive]: function() { throw "gaga" } };
assertEquals(42, Reflect.getOwnPropertyDescriptor(target, a).value);
assertThrows(function() { Reflect.getOwnPropertyDescriptor(target, b); },
"gaga");
assertThrowsEquals(() => Reflect.getOwnPropertyDescriptor(target, b), "gaga");
})();
......
......@@ -93,6 +93,10 @@ var assertNotNull;
// to the type property on the thrown exception.
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.
var assertDoesNotThrow;
......@@ -353,6 +357,8 @@ var assertUnoptimized;
} catch (e) {
if (typeof type_opt === 'function') {
assertInstanceof(e, type_opt);
} else if (type_opt !== void 0) {
fail("invalid use of assertThrows, maybe you want assertThrowsEquals");
}
if (arguments.length >= 3) {
assertEquals(e.type, cause_opt);
......@@ -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) {
if (!(obj instanceof type)) {
var actualTypeName = null;
......
......@@ -120,7 +120,7 @@ function 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.
......@@ -128,7 +128,7 @@ function test9() {
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");
......
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