Skip to content

Commit 22aa05c

Browse files
committed
assert: support inequalities
1 parent 58a8eb4 commit 22aa05c

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

doc/api/assert.md

+48
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,30 @@ added: v0.5.9
551551

552552
An alias of [`assert.ok()`][].
553553

554+
## `assert.atLeast(actual, expected[, message])`
555+
556+
<!-- YAML
557+
added: REPLACEME
558+
-->
559+
560+
* `actual` {number}
561+
* `expected` {number}
562+
* `message` {string|Error}
563+
564+
Expects the `actual` input to be greater than or equal to the `expected` value.
565+
566+
## `assert.atMost(actual, expected[, message])`
567+
568+
<!-- YAML
569+
added: REPLACEME
570+
-->
571+
572+
* `actual` {number}
573+
* `expected` {number}
574+
* `message` {string|Error}
575+
576+
Expects the `actual` input to be less than or equal to the `expected` value.
577+
554578
## `assert.deepEqual(actual, expected[, message])`
555579

556580
<!-- YAML
@@ -1311,6 +1335,18 @@ parameter is undefined, a default error message is assigned. If the `message`
13111335
parameter is an instance of an [`Error`][] then it will be thrown instead of the
13121336
`AssertionError`.
13131337

1338+
## `assert.greaterThan(actual, expected[, message])`
1339+
1340+
<!-- YAML
1341+
added: REPLACEME
1342+
-->
1343+
1344+
* `actual` {number}
1345+
* `expected` {number}
1346+
* `message` {string|Error}
1347+
1348+
Expects the `actual` input to be greater than the `expected` value.
1349+
13141350
## `assert.fail([message])`
13151351

13161352
<!-- YAML
@@ -1450,6 +1486,18 @@ suppressFrame();
14501486
// ...
14511487
```
14521488

1489+
## `assert.lessThan(actual, expected[, message])`
1490+
1491+
<!-- YAML
1492+
added: REPLACEME
1493+
-->
1494+
1495+
* `actual` {number}
1496+
* `expected` {number}
1497+
* `message` {string|Error}
1498+
1499+
Expects the `actual` input to be less than the `expected` value.
1500+
14531501
## `assert.ifError(value)`
14541502

14551503
<!-- YAML

lib/assert.js

+72
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,78 @@ assert.doesNotMatch = function doesNotMatch(string, regexp, message) {
791791
internalMatch(string, regexp, message, doesNotMatch);
792792
};
793793

794+
/**
795+
* Asserts that the first number is greater than the second number.
796+
* @param {number} actual
797+
* @param {number} expected
798+
* @param {string | Error} [message]
799+
*/
800+
assert.greaterThan = function greaterThan(actual, expected, message = `Expected ${actual} to be greater than ${expected}`) {
801+
if (actual <= expected) {
802+
innerFail({
803+
actual,
804+
expected,
805+
message,
806+
operator: '>',
807+
stackStartFn: greaterThan,
808+
});
809+
}
810+
};
811+
812+
/**
813+
* Asserts that the first number is greater than or equal to the second number.
814+
* @param {number} actual
815+
* @param {number} expected
816+
* @param {string | Error} [message]
817+
*/
818+
assert.atLeast = function atLeast(actual, expected, message = `Expected ${actual} to be greater than or equal to ${expected}`) {
819+
if (actual < expected) {
820+
innerFail({
821+
actual,
822+
expected,
823+
message,
824+
operator: '>=',
825+
stackStartFn: atLeast,
826+
});
827+
}
828+
};
829+
830+
/**
831+
* Asserts that the first number is less than the second number.
832+
* @param {number} actual
833+
* @param {number} expected
834+
* @param {string | Error} [message]
835+
*/
836+
assert.lessThan = function lessThan(actual, expected, message = `Expected ${actual} to be less than ${expected}`) {
837+
if (actual >= expected) {
838+
innerFail({
839+
actual,
840+
expected,
841+
message,
842+
operator: '<',
843+
stackStartFn: lessThan,
844+
});
845+
}
846+
};
847+
848+
/**
849+
* Asserts that the first number is less than or equal to the second number.
850+
* @param {number} actual
851+
* @param {number} expected
852+
* @param {string | Error} [message]
853+
*/
854+
assert.atMost = function atMost(actual, expected, message = `Expected ${actual} to be less than or equal to ${expected}`) {
855+
if (actual > expected) {
856+
innerFail({
857+
actual,
858+
expected,
859+
message,
860+
operator: '<=',
861+
stackStartFn: atMost,
862+
});
863+
}
864+
};
865+
794866
assert.CallTracker = deprecate(CallTracker, 'assert.CallTracker is deprecated.', 'DEP0173');
795867

796868
/**
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('node:assert');
5+
const { describe, it } = require('node:test');
6+
7+
const testCases = {
8+
greaterThan: [
9+
{ actual: 5, expected: 3, shouldPass: true },
10+
{ actual: 3, expected: 3, shouldPass: false },
11+
{ actual: 2, expected: 3, shouldPass: false },
12+
],
13+
atLeast: [
14+
{ actual: 5, expected: 3, shouldPass: true },
15+
{ actual: 3, expected: 3, shouldPass: true },
16+
{ actual: 2, expected: 3, shouldPass: false },
17+
],
18+
lessThan: [
19+
{ actual: 2, expected: 3, shouldPass: true },
20+
{ actual: 3, expected: 3, shouldPass: false },
21+
{ actual: 4, expected: 3, shouldPass: false },
22+
],
23+
atMost: [
24+
{ actual: 2, expected: 3, shouldPass: true },
25+
{ actual: 3, expected: 3, shouldPass: true },
26+
{ actual: 4, expected: 3, shouldPass: false },
27+
]
28+
};
29+
30+
const comparison = {
31+
greaterThan: 'greater than',
32+
atLeast: 'greater than or equal to',
33+
lessThan: 'less than',
34+
atMost: 'less than or equal to'
35+
};
36+
37+
const methods = Object.keys(testCases);
38+
39+
for (const method of methods) {
40+
describe(`assert.${method}`, () => {
41+
for (const { actual, expected, shouldPass } of testCases[method]) {
42+
it(`should ${shouldPass ? 'pass' : 'fail'} with (${actual}, ${expected})`, () => {
43+
assert[shouldPass ? 'doesNotThrow' : 'throws'](
44+
() => assert[method](actual, expected),
45+
new RegExp(`Expected ${actual} to be ${comparison[method]} ${expected}`)
46+
);
47+
});
48+
}
49+
});
50+
}

0 commit comments

Comments
 (0)