js-libp2p-crypto/test/helpers/test-garbage-error-handling.js
Alex Potsides a0f387aeab
fix: replace node buffers with uint8arrays (#180)
* fix: replace node buffers with uint8arrays

All usage of node buffers have been replaced with uint8arrays.

BREAKING CHANGES:

- Where node Buffers were returned, now Uint8Arrays are

* chore: remove commented code
2020-08-07 16:23:02 +02:00

34 lines
1.0 KiB
JavaScript

/* eslint-env mocha */
'use strict'
const uint8ArrayFromString = require('uint8arrays/from-string')
const util = require('util')
const garbage = [uint8ArrayFromString('00010203040506070809', 'base16'), {}, null, false, undefined, true, 1, 0, uint8ArrayFromString(''), 'aGVsbG93b3JsZA==', 'helloworld', '']
function doTests (fncName, fnc, num, skipBuffersAndStrings) {
if (!num) {
num = 1
}
garbage.forEach((garbage) => {
if (skipBuffersAndStrings && (garbage instanceof Uint8Array || (typeof garbage) === 'string')) {
// skip this garbage because it's a Uint8Array or a String and we were told do do that
return
}
const args = []
for (let i = 0; i < num; i++) {
args.push(garbage)
}
it(fncName + '(' + args.map(garbage => util.inspect(garbage)).join(', ') + ')', async () => {
try {
await fnc.apply(null, args)
} catch (err) {
return // expected
}
throw new Error('Expected error to be thrown')
})
})
}
module.exports = { doTests }