mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
assert: improve assert.fail() API
assert.fail() has two possible function signatures, both of which are
not intuitive. It virtually guarantees that people who try to use
assert.fail() without carefully reading the docs will end up using it
incorrectly.
This change maintains backwards compatibility with the two valid uses
(arguments 1 2 and 4 supplied but argument 3 falsy, and argument 3
supplied but arguments 1 2 and 4 all falsy) but also adds the far more
intuitive first-argument-only and first-two-arguments-only
possibilities.
assert.fail('boom');
// AssertionError: boom
assert.fail('a', 'b');
// AssertionError: 'a' != 'b'
PR-URL: https://github.com/nodejs/node/pull/12293
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
b3f2e3b7e2
commit
758b8b6e5d
3 files changed
+45
-1
No files matched your search
+8
-1
@@ -256,6 +256,7 @@ If the values are not equal, an `AssertionError` is thrown with a `message`
|
||||
property set equal to the value of the `message` parameter. If the `message`
|
||||
parameter is undefined, a default error message is assigned.
|
||||
|
||||
## assert.fail(message)
|
||||
## assert.fail(actual, expected, message, operator)
|
||||
<!-- YAML
|
||||
added: v0.1.21
|
||||
@@ -263,7 +264,7 @@ added: v0.1.21
|
||||
* `actual` {any}
|
||||
* `expected` {any}
|
||||
* `message` {any}
|
||||
* `operator` {string}
|
||||
* `operator` {string} (default: '!=')
|
||||
|
||||
Throws an `AssertionError`. If `message` is falsy, the error message is set as
|
||||
the values of `actual` and `expected` separated by the provided `operator`.
|
||||
@@ -277,6 +278,12 @@ assert.fail(1, 2, undefined, '>');
|
||||
|
||||
assert.fail(1, 2, 'whoops', '>');
|
||||
// AssertionError: whoops
|
||||
|
||||
assert.fail('boom');
|
||||
// AssertionError: boom
|
||||
|
||||
assert.fail('a', 'b');
|
||||
// AssertionError: 'a' != 'b'
|
||||
```
|
||||
|
||||
## assert.ifError(value)
|
||||
|
||||
@@ -79,6 +79,10 @@ function getMessage(self) {
|
||||
// display purposes.
|
||||
|
||||
function fail(actual, expected, message, operator, stackStartFunction) {
|
||||
if (arguments.length === 1)
|
||||
message = actual;
|
||||
if (arguments.length === 2)
|
||||
operator = '!=';
|
||||
throw new assert.AssertionError({
|
||||
message: message,
|
||||
actual: actual,
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
// no args
|
||||
assert.throws(
|
||||
() => { assert.fail(); },
|
||||
/^AssertionError: undefined undefined undefined$/
|
||||
);
|
||||
|
||||
// one arg = message
|
||||
assert.throws(
|
||||
() => { assert.fail('custom message'); },
|
||||
/^AssertionError: custom message$/
|
||||
);
|
||||
|
||||
// two args only, operator defaults to '!='
|
||||
assert.throws(
|
||||
() => { assert.fail('first', 'second'); },
|
||||
/^AssertionError: 'first' != 'second'$/
|
||||
);
|
||||
|
||||
// three args
|
||||
assert.throws(
|
||||
() => { assert.fail('ignored', 'ignored', 'another custom message'); },
|
||||
/^AssertionError: another custom message$/
|
||||
);
|
||||
|
||||
// no third arg (but a fourth arg)
|
||||
assert.throws(
|
||||
() => { assert.fail('first', 'second', undefined, 'operator'); },
|
||||
/^AssertionError: 'first' operator 'second'$/
|
||||
);
|
||||
Reference in new issue
Block a user