assert: Add equalWithMargin for floating and other number comparison with error margin
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
All checks were successful
continuous-integration/appveyor/branch AppVeyor build succeeded
This commit is contained in:
parent
17d7bb862c
commit
a67479f4bc
4 changed files with 46 additions and 1 deletions
|
@ -111,9 +111,11 @@ $ eltro --watch my_watch_name --npm build
|
||||||
|
|
||||||
Not only does eltro allow you to use any assertion library of your own choosing, it also comes with it's own assertion library based on node's default [assert](https://nodejs.org/api/assert.html) with a few extra methods:
|
Not only does eltro allow you to use any assertion library of your own choosing, it also comes with it's own assertion library based on node's default [assert](https://nodejs.org/api/assert.html) with a few extra methods:
|
||||||
|
|
||||||
|
* `assert.equalWithMargin(value, test, margin, [message])`: Check if number value is equal to test with error margin.
|
||||||
* `assert.notOk(value, [message])`: Assert value is not ok.
|
* `assert.notOk(value, [message])`: Assert value is not ok.
|
||||||
* `assert.match(value, test, [message])`: Check if value matches RegExp test.
|
* `assert.match(value, test, [message])`: Check if value matches RegExp test.
|
||||||
* `assert.notMatch(value, [message])`: Check if value does not match RegExp test.
|
* `assert.notMatch(value, [message])`: Check if value does not match RegExp test.
|
||||||
|
* `assert.throwsAndCatch(fn, [message])`: Checks if function fn throws and returns the thrown error.
|
||||||
* `assert.isFulfilled(promise, [message])`: Assert the promise resolves.
|
* `assert.isFulfilled(promise, [message])`: Assert the promise resolves.
|
||||||
* `assert.isRejected(promise, [message])`: Assert the promise gets rejects.
|
* `assert.isRejected(promise, [message])`: Assert the promise gets rejects.
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,14 @@ assert.throwsAndCatch = (fn, message) => {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert.equalWithMargin = (value, test, margin = 0.005, message) => {
|
||||||
|
assert.strictEqual(typeof value, 'number', 'Value should be number')
|
||||||
|
assert.strictEqual(typeof test, 'number', 'Test should be number')
|
||||||
|
|
||||||
|
let difference = Math.abs(value - test)
|
||||||
|
assert.ok(difference <= margin, message || `${value} ± ${margin} != ${test} (difference of ${difference} > ${margin})`)
|
||||||
|
}
|
||||||
|
|
||||||
assert.isFulfilled = (promise, message) => {
|
assert.isFulfilled = (promise, message) => {
|
||||||
return Promise.resolve(true)
|
return Promise.resolve(true)
|
||||||
.then(() => promise)
|
.then(() => promise)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "eltro",
|
"name": "eltro",
|
||||||
"version": "1.4.6",
|
"version": "1.5.0",
|
||||||
"description": "Eltro is a tiny no-dependancy test framework for node",
|
"description": "Eltro is a tiny no-dependancy test framework for node",
|
||||||
"main": "index.mjs",
|
"main": "index.mjs",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -29,6 +29,41 @@ t.describe('#notOk()', function() {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.describe('#equalWithMargin()', function() {
|
||||||
|
t.test('should support default margin for floating point math', function() {
|
||||||
|
let check = 0.1 + 0.2
|
||||||
|
|
||||||
|
assertExtended.throws(function() {
|
||||||
|
assertExtended.strictEqual(check, 0.3)
|
||||||
|
}, assertExtended.AssertionError)
|
||||||
|
|
||||||
|
assertExtended.equalWithMargin(check, 0.3)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.test('should support custom margin', function() {
|
||||||
|
assertExtended.equalWithMargin(1, 2, 1)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.test('should fail if margin is too small', function() {
|
||||||
|
assertExtended.throws(function() {
|
||||||
|
assertExtended.equalWithMargin(1, 2, 0.9)
|
||||||
|
}, assertExtended.AssertionError)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.test('should support custom message', function () {
|
||||||
|
const assertMessage = 'Hello world'
|
||||||
|
let error = null
|
||||||
|
|
||||||
|
try {
|
||||||
|
assertExtended.equalWithMargin(1, 2, 0, assertMessage)
|
||||||
|
} catch (err) {
|
||||||
|
error = err
|
||||||
|
}
|
||||||
|
assert.ok(error)
|
||||||
|
assert.match(error.message, new RegExp(assertMessage))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
t.describe('#throwAndCatch()', function() {
|
t.describe('#throwAndCatch()', function() {
|
||||||
t.test('should work and return the original error', function() {
|
t.test('should work and return the original error', function() {
|
||||||
const assertError = new Error('Speed')
|
const assertError = new Error('Speed')
|
||||||
|
|
Loading…
Reference in a new issue