2020-03-31 17:27:36 +00:00
|
|
|
import util from 'util'
|
|
|
|
import assert from 'assert'
|
|
|
|
import assertExtended from '../lib/assert.mjs'
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
import e from '../lib/eltro.mjs'
|
2020-03-31 17:27:36 +00:00
|
|
|
|
|
|
|
const testLongObject = {
|
|
|
|
a: 1, b:2, c:3, d:4,
|
|
|
|
e: {herp: 51, derp: 23},
|
|
|
|
f: 'asdfgagwegawegawegawegawe',
|
|
|
|
g: '32ghaiwugb23 238023'
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.describe('#notOk()', function() {
|
|
|
|
e.test('should exist', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.ok(assertExtended.notOk)
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should throw for true values', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.throws(function() {
|
|
|
|
assertExtended.notOk(true)
|
|
|
|
}, assertExtended.AssertionError)
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should pass for false values', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.notOk(false)
|
|
|
|
assertExtended.notOk(null)
|
|
|
|
assertExtended.notOk(0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.describe('#isFulfilled()', function() {
|
|
|
|
e.test('should exist', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.ok(assertExtended.isFulfilled)
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should throw for rejected promises', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
return assertExtended.isFulfilled(Promise.reject({}))
|
|
|
|
.catch((err) => {
|
|
|
|
assertExtended.ok(err.message.match(/promise fail/))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should properly parse rejected object response', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
let assertMessage = util.inspect(testLongObject, {depth: 1}).replace(/\n /g, '')
|
|
|
|
assertMessage = assertMessage.slice(0, 64) + '...'
|
|
|
|
|
|
|
|
return assertExtended.isFulfilled(Promise.reject(testLongObject))
|
|
|
|
.catch((err) =>
|
|
|
|
assertExtended.notStrictEqual(err.message.indexOf(assertMessage), -1)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should include error message if error', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
const assertMessage = 'something something dark side'
|
|
|
|
return assertExtended.isFulfilled(Promise.reject(new Error(assertMessage)))
|
|
|
|
.catch((err) => {
|
|
|
|
assertExtended.ok(err.message.match(new RegExp('with ' + assertMessage)))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should pass for resolved promises', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
return assertExtended.isFulfilled(Promise.resolve())
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should support custom message', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
const assertMessage = 'something something dark side'
|
|
|
|
return assertExtended.isFulfilled(Promise.reject({}), assertMessage)
|
|
|
|
.catch((err) => {
|
|
|
|
assertExtended.ok(err.message.match(assertMessage))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should return result for the resolved promise', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
const assertResult = {a: 1}
|
|
|
|
|
|
|
|
return assertExtended.isFulfilled(Promise.resolve(assertResult))
|
|
|
|
.then((data) => assertExtended.strictEqual(data, assertResult))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.describe('#isRejected()', function() {
|
|
|
|
e.test('should exist', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.ok(assertExtended.isRejected)
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should throw for resolved promises', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
let hasFailed = false
|
|
|
|
|
|
|
|
return assertExtended.isRejected(Promise.resolve({}))
|
|
|
|
.catch((err) => {
|
|
|
|
hasFailed = true
|
|
|
|
assertExtended.ok(err.message.match(/fulfilled with/))
|
|
|
|
})
|
|
|
|
.then(function() {
|
|
|
|
assertExtended.strictEqual(hasFailed, true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should properly stringify objects', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
let assertMessage = util.inspect(testLongObject, {depth: 1}).replace(/\n /g, '')
|
|
|
|
assertMessage = assertMessage.slice(0, 64) + '...'
|
|
|
|
|
|
|
|
return assertExtended.isRejected(Promise.resolve(testLongObject))
|
|
|
|
.catch((err) =>
|
|
|
|
assertExtended.notStrictEqual(err.message.indexOf(assertMessage), -1)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should support custom message', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
const assertMessage = 'something something dark side'
|
|
|
|
return assertExtended.isRejected(Promise.resolve({}), assertMessage)
|
|
|
|
.catch((err) => assertExtended.ok(err.message.match(assertMessage)))
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should return result for the unresolved promise', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
const assertResult = {a: 1}
|
|
|
|
|
|
|
|
return assertExtended.isRejected(Promise.reject(assertResult))
|
|
|
|
.then((data) => assertExtended.strictEqual(data, assertResult))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.describe('#match()', function() {
|
|
|
|
e.test('should exist', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.ok(assertExtended.match);
|
|
|
|
});
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should throw if no match', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.throws(function() {
|
|
|
|
assertExtended.match('a', /b/);
|
|
|
|
}, assertExtended.AssertionError);
|
|
|
|
});
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should pass if matches', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.match('a', /a/);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.describe('#notMatch()', function() {
|
|
|
|
e.test('should exist', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.ok(assertExtended.notMatch);
|
|
|
|
});
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should throw if match', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.throws(function() {
|
|
|
|
assertExtended.notMatch('a', /a/);
|
|
|
|
}, assertExtended.AssertionError);
|
|
|
|
});
|
|
|
|
|
2020-04-01 15:35:33 +00:00
|
|
|
e.test('should pass if not matches', function() {
|
2020-03-31 17:27:36 +00:00
|
|
|
assertExtended.notMatch('a', /b/);
|
|
|
|
});
|
|
|
|
})
|