Skip to content

Commit

Permalink
test: add second argument to assert.throws
Browse files Browse the repository at this point in the history
This adds RegExp or error constructor arguments to the remaining places
where it is missing in preparation for the commit that will enforce the
presence of at least two arguments.

PR-URL: nodejs/node#12270
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
targos committed Jun 19, 2017
1 parent 593553d commit 7719bc0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
14 changes: 10 additions & 4 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ assert.throws(
{
const re1 = /a/;
re1.lastIndex = 3;
assert.throws(makeBlock(a.deepStrictEqual, re1, /a/));
assert.throws(makeBlock(a.deepStrictEqual, re1, /a/),
/^AssertionError: \/a\/ deepStrictEqual \/a\/$/);
}

// 7.4 - strict
Expand All @@ -262,10 +263,12 @@ assert.doesNotThrow(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4}));
assert.doesNotThrow(makeBlock(a.deepStrictEqual,
{a: 4, b: '2'},
{a: 4, b: '2'}));
assert.throws(makeBlock(a.deepStrictEqual, [4], ['4']));
assert.throws(makeBlock(a.deepStrictEqual, [4], ['4']),
/^AssertionError: \[ 4 ] deepStrictEqual \[ '4' ]$/);
assert.throws(makeBlock(a.deepStrictEqual, {a: 4}, {a: 4, b: true}),
a.AssertionError);
assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'}));
/^AssertionError: { a: 4 } deepStrictEqual { a: 4, b: true }$/);
assert.throws(makeBlock(a.deepStrictEqual, ['a'], {0: 'a'}),
/^AssertionError: \[ 'a' ] deepStrictEqual { '0': 'a' }$/);
//(although not necessarily the same order),
assert.doesNotThrow(makeBlock(a.deepStrictEqual,
{a: 4, b: '1'},
Expand Down Expand Up @@ -342,9 +345,11 @@ function thrower(errorConstructor) {
assert.throws(makeBlock(thrower, a.AssertionError),
a.AssertionError, 'message');
assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
// eslint-disable-next-line assert-throws-arguments
assert.throws(makeBlock(thrower, a.AssertionError));

// if not passing an error, catch all.
// eslint-disable-next-line assert-throws-arguments
assert.throws(makeBlock(thrower, TypeError));

// when passing a type, only catch errors of the appropriate type
Expand Down Expand Up @@ -517,6 +522,7 @@ testAssertionMessage({a: NaN, b: Infinity, c: -Infinity},

// #2893
try {
// eslint-disable-next-line assert-throws-arguments
assert.throws(function() {
assert.ifError(null);
});
Expand Down
18 changes: 10 additions & 8 deletions test/parallel/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ for (const [key, value] of e.entries()) {

assert.throws(function() {
Buffer(8).fill('a', -1);
});
}, /^RangeError: Out of range index$/);

assert.throws(function() {
Buffer(8).fill('a', 0, 9);
});
}, /^RangeError: Out of range index$/);

// Make sure this doesn't hang indefinitely.
Buffer(8).fill('');
Expand Down Expand Up @@ -1433,17 +1433,17 @@ if (common.hasCrypto) {
assert.throws(function() {
const b = Buffer(1);
Buffer.compare(b, 'abc');
});
}, /^TypeError: Arguments must be Buffers$/);

assert.throws(function() {
const b = Buffer(1);
Buffer.compare('abc', b);
});
}, /^TypeError: Arguments must be Buffers$/);

assert.throws(function() {
const b = Buffer(1);
b.compare('abc');
});
}, /^TypeError: Argument must be a Buffer$/);

// Test Equals
{
Expand All @@ -1461,10 +1461,12 @@ assert.throws(function() {
assert.throws(function() {
const b = Buffer(1);
b.equals('abc');
});
}, /^TypeError: Argument must be a Buffer$/);

// Regression test for https://github.com/nodejs/node/issues/649.
assert.throws(function() { Buffer(1422561062959).toString('utf8'); });
assert.throws(function() {
Buffer(1422561062959).toString('utf8');
}, /^RangeError: Invalid typed array length$/);

const ps = Buffer.poolSize;
Buffer.poolSize = 0;
Expand All @@ -1474,7 +1476,7 @@ Buffer.poolSize = ps;
// Test Buffer.copy() segfault
assert.throws(function() {
Buffer(10).copy();
});
}, /^TypeError: argument should be a Buffer$/);

const regErrorMsg = new RegExp('First argument must be a string, Buffer, ' +
'ArrayBuffer, Array, or array-like object.');
Expand Down
16 changes: 12 additions & 4 deletions test/parallel/test-http-mutable-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@ const cookies = [
const s = http.createServer(function(req, res) {
switch (test) {
case 'headers':
assert.throws(function() { res.setHeader(); });
assert.throws(function() { res.setHeader('someHeader'); });
assert.throws(function() { res.getHeader(); });
assert.throws(function() { res.removeHeader(); });
assert.throws(() => {
res.setHeader();
}, /^TypeError: Header name must be a valid HTTP Token \["undefined"\]$/);
assert.throws(() => {
res.setHeader('someHeader');
}, /^Error: "value" required in setHeader\("someHeader", value\)$/);
assert.throws(() => {
res.getHeader();
}, /^Error: "name" argument is required for getHeader\(name\)$/);
assert.throws(() => {
res.removeHeader();
}, /^Error: "name" argument is required for removeHeader\(name\)$/);

res.setHeader('x-test-header', 'testing');
res.setHeader('X-TEST-HEADER2', 'testing');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ function testContext(repl) {
assert.strictEqual(context.global, context);

// ensure that the repl console instance does not have a setter
assert.throws(() => context.console = 'foo');
assert.throws(() => context.console = 'foo', TypeError);
}
2 changes: 1 addition & 1 deletion test/parallel/test-vm-new-script-this-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ console.error('thrown error');
script = new Script('throw new Error(\'test\');');
assert.throws(function() {
script.runInThisContext(script);
});
}, /^Error: test$/);

global.hello = 5;
script = new Script('hello = 2');
Expand Down

0 comments on commit 7719bc0

Please sign in to comment.