Skip to content

Commit

Permalink
assert: support inequalities
Browse files Browse the repository at this point in the history
  • Loading branch information
RedYetiDev committed Nov 9, 2024
1 parent 58a8eb4 commit 22aa05c
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
48 changes: 48 additions & 0 deletions doc/api/assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,30 @@ added: v0.5.9

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

## `assert.atLeast(actual, expected[, message])`

<!-- YAML
added: REPLACEME
-->

* `actual` {number}
* `expected` {number}
* `message` {string|Error}

Expects the `actual` input to be greater than or equal to the `expected` value.

## `assert.atMost(actual, expected[, message])`

<!-- YAML
added: REPLACEME
-->

* `actual` {number}
* `expected` {number}
* `message` {string|Error}

Expects the `actual` input to be less than or equal to the `expected` value.

## `assert.deepEqual(actual, expected[, message])`

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

## `assert.greaterThan(actual, expected[, message])`

<!-- YAML
added: REPLACEME
-->

* `actual` {number}
* `expected` {number}
* `message` {string|Error}

Expects the `actual` input to be greater than the `expected` value.

## `assert.fail([message])`

<!-- YAML
Expand Down Expand Up @@ -1450,6 +1486,18 @@ suppressFrame();
// ...
```

## `assert.lessThan(actual, expected[, message])`

<!-- YAML
added: REPLACEME
-->

* `actual` {number}
* `expected` {number}
* `message` {string|Error}

Expects the `actual` input to be less than the `expected` value.

## `assert.ifError(value)`

<!-- YAML
Expand Down
72 changes: 72 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,78 @@ assert.doesNotMatch = function doesNotMatch(string, regexp, message) {
internalMatch(string, regexp, message, doesNotMatch);
};

/**
* Asserts that the first number is greater than the second number.
* @param {number} actual
* @param {number} expected
* @param {string | Error} [message]
*/
assert.greaterThan = function greaterThan(actual, expected, message = `Expected ${actual} to be greater than ${expected}`) {
if (actual <= expected) {
innerFail({
actual,
expected,
message,
operator: '>',
stackStartFn: greaterThan,
});
}
};

/**
* Asserts that the first number is greater than or equal to the second number.
* @param {number} actual
* @param {number} expected
* @param {string | Error} [message]
*/
assert.atLeast = function atLeast(actual, expected, message = `Expected ${actual} to be greater than or equal to ${expected}`) {
if (actual < expected) {
innerFail({
actual,
expected,
message,
operator: '>=',
stackStartFn: atLeast,
});
}
};

/**
* Asserts that the first number is less than the second number.
* @param {number} actual
* @param {number} expected
* @param {string | Error} [message]
*/
assert.lessThan = function lessThan(actual, expected, message = `Expected ${actual} to be less than ${expected}`) {
if (actual >= expected) {
innerFail({
actual,
expected,
message,
operator: '<',
stackStartFn: lessThan,
});
}
};

/**
* Asserts that the first number is less than or equal to the second number.
* @param {number} actual
* @param {number} expected
* @param {string | Error} [message]
*/
assert.atMost = function atMost(actual, expected, message = `Expected ${actual} to be less than or equal to ${expected}`) {
if (actual > expected) {
innerFail({
actual,
expected,
message,
operator: '<=',
stackStartFn: atMost,
});
}
};

assert.CallTracker = deprecate(CallTracker, 'assert.CallTracker is deprecated.', 'DEP0173');

/**
Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-assert-inequalities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

require('../common');
const assert = require('node:assert');
const { describe, it } = require('node:test');

const testCases = {
greaterThan: [
{ actual: 5, expected: 3, shouldPass: true },
{ actual: 3, expected: 3, shouldPass: false },
{ actual: 2, expected: 3, shouldPass: false },
],
atLeast: [
{ actual: 5, expected: 3, shouldPass: true },
{ actual: 3, expected: 3, shouldPass: true },
{ actual: 2, expected: 3, shouldPass: false },
],
lessThan: [
{ actual: 2, expected: 3, shouldPass: true },
{ actual: 3, expected: 3, shouldPass: false },
{ actual: 4, expected: 3, shouldPass: false },
],
atMost: [
{ actual: 2, expected: 3, shouldPass: true },
{ actual: 3, expected: 3, shouldPass: true },
{ actual: 4, expected: 3, shouldPass: false },
]
};

const comparison = {
greaterThan: 'greater than',
atLeast: 'greater than or equal to',
lessThan: 'less than',
atMost: 'less than or equal to'
};

const methods = Object.keys(testCases);

for (const method of methods) {
describe(`assert.${method}`, () => {
for (const { actual, expected, shouldPass } of testCases[method]) {
it(`should ${shouldPass ? 'pass' : 'fail'} with (${actual}, ${expected})`, () => {
assert[shouldPass ? 'doesNotThrow' : 'throws'](
() => assert[method](actual, expected),
new RegExp(`Expected ${actual} to be ${comparison[method]} ${expected}`)
);
});
}
});
}

0 comments on commit 22aa05c

Please sign in to comment.