eltro/test/callback.test.mjs

217 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

import { runWithCallbackSafe } from '../lib/callback.mjs'
import assert from '../lib/assert.mjs'
import t from '../lib/eltro.mjs'
t.describe('runCb()', function() {
t.test('cb() should work normally', async function() {
let called = false
let test = { func: function(cb) { called = true; cb() } }
await runWithCallbackSafe(test)
assert.strictEqual(called, true)
})
t.test('cb() should work with timeout', async function() {
let called = false
let test = { func: function(cb) {
setImmediate(function() {
called = true;
cb();
})
} }
await runWithCallbackSafe(test)
assert.strictEqual(called, true)
})
t.test('cb() should capture throws outside', async function() {
const assertError = new Error('a')
let test = { func: function(cb) { throw assertError } }
let err = await assert.isRejected(runWithCallbackSafe(test))
assert.strictEqual(err, assertError)
})
t.test('cb() should support callback error', async function() {
const assertError = new Error('a')
let test = { func: function(cb) { cb(assertError) } }
let err = await assert.isRejected(runWithCallbackSafe(test))
assert.strictEqual(err, assertError)
})
t.test('cb() should support callback error in immediate', async function() {
const assertError = new Error('a')
let test = { func: function(cb) {
setImmediate(function() {
cb(assertError)
})
} }
let err = await assert.isRejected(runWithCallbackSafe(test))
assert.strictEqual(err, assertError)
})
t.test('cb.wrap() should return function and work with timeout', async function() {
let calledFirst = false
let calledSecond = false
let test = { func: function(cb) {
let fun = cb.wrap(function() {
calledSecond = true
cb()
})
setImmediate(function() {
calledFirst = true;
fun()
})
} }
await runWithCallbackSafe(test)
assert.strictEqual(calledFirst, true)
assert.strictEqual(calledSecond, true)
})
t.test('cb.wrap() should pass arguments correctly', async function() {
let a = 0
let b = 0
let c = 0
let called = false
let test = { func: function(cb) {
let fun = cb.wrap(function(ina, inb, inc) {
a = ina
b = inb
c = inc
cb()
})
setImmediate(function() {
called = true
fun(1, 2, 3)
})
} }
await runWithCallbackSafe(test)
assert.strictEqual(called, true)
assert.strictEqual(a, 1)
assert.strictEqual(b, 2)
assert.strictEqual(c, 3)
await runWithCallbackSafe(test)
})
t.test('cb.wrap() should throw', async function() {
const assertError = new Error('a')
let test = { func: function(cb) {
setImmediate(cb.wrap(function() {
throw assertError
}))
} }
let err = await assert.isRejected(runWithCallbackSafe(test))
assert.strictEqual(err, assertError)
})
t.test('cb.wrap() should support nested calls', async function() {
const assertError = new Error('a')
let test = { func: function(cb) {
setImmediate(function() {
setImmediate(cb.wrap(function() {
throw assertError
}))
})
} }
let err = await assert.isRejected(runWithCallbackSafe(test))
assert.strictEqual(err, assertError)
})
t.test('cb.finish() should return function and work with timeout and finish', async function() {
let calledFirst = false
let calledSecond = false
let test = { func: function(cb) {
let fun = cb.finish(function() {
calledSecond = true
})
setImmediate(function() {
calledFirst = true;
fun()
})
} }
await runWithCallbackSafe(test)
assert.strictEqual(calledFirst, true)
assert.strictEqual(calledSecond, true)
})
t.test('cb.finish() should pass arguments correctly', async function() {
let a = 0
let b = 0
let c = 0
let called = false
let test = { func: function(cb) {
let fun = cb.finish(function(ina, inb, inc) {
a = ina
b = inb
c = inc
})
setImmediate(function() {
called = true
fun(1, 2, 3)
})
} }
await runWithCallbackSafe(test)
assert.strictEqual(called, true)
assert.strictEqual(a, 1)
assert.strictEqual(b, 2)
assert.strictEqual(c, 3)
await runWithCallbackSafe(test)
})
t.test('cb.finish() should support throw', async function() {
const assertError = new Error('a')
let test = { func: function(cb) {
setImmediate(cb.finish(function() {
throw assertError
}))
} }
let err = await assert.isRejected(runWithCallbackSafe(test))
assert.strictEqual(err, assertError)
})
t.test('cb.finish() should support nested throw calls', async function() {
const assertError = new Error('a')
let test = { func: function(cb) {
setImmediate(function() {
setImmediate(cb.finish(function() {
throw assertError
}))
})
} }
let err = await assert.isRejected(runWithCallbackSafe(test))
assert.strictEqual(err, assertError)
})
})