323 lines
6.7 KiB
JavaScript
323 lines
6.7 KiB
JavaScript
require('.')
|
|
|
|
const assert = require('node:assert/strict')
|
|
const test = require('node:test')
|
|
const fs = require('fs/promises')
|
|
|
|
const testObject = {
|
|
property: "value",
|
|
anotherProperty: 1234,
|
|
boolean: true,
|
|
notBoolean: null
|
|
}
|
|
const testObjectStr = JSON.stringify(testObject)
|
|
|
|
test('Object.prototype.map', t => {
|
|
assert.equal(
|
|
testObject.map(JSON.stringify),
|
|
testObjectStr
|
|
)
|
|
})
|
|
|
|
test('Object.prototype.apply', t => {
|
|
assert.equal(
|
|
testObject.apply(JSON.stringify),
|
|
testObjectStr
|
|
)
|
|
})
|
|
|
|
test('String.prototype.lines', t => {
|
|
const str = 'one\ntwo\nthree\n'
|
|
assert.deepEqual(
|
|
['one', 'two', 'three'],
|
|
str.trim().lines()
|
|
)
|
|
assert.deepEqual(
|
|
['one', 'two', 'three'],
|
|
str.lines()
|
|
)
|
|
})
|
|
|
|
test('String.prototype.reverse', t => {
|
|
assert.equal(
|
|
'tac',
|
|
'cat'.reverse()
|
|
)
|
|
})
|
|
|
|
test('String.prototype.includesAny', t => {
|
|
const str = 'longer test string'
|
|
assert(str.includesAny(['test']))
|
|
assert(str.includesAny(['not', 'test']))
|
|
assert(!str.includesAny(['missing']))
|
|
assert.throws(() => str.includesAny())
|
|
assert.throws(() => str.includesAny('a'))
|
|
})
|
|
|
|
test('String.prototype.replaceAt', t => {
|
|
const str = 'test string woof'
|
|
assert.equal(
|
|
'test strong woof',
|
|
str.replaceAt(8, 'o')
|
|
)
|
|
assert.equal(
|
|
'test longer string',
|
|
str.replaceAt(5, 'longer string')
|
|
)
|
|
assert.throws(() => str.replaceAt(str.length+10, 'out of bounds'))
|
|
})
|
|
|
|
test('String.prototype.tryParseJSON', t => {
|
|
assert.deepEqual(
|
|
testObject,
|
|
testObjectStr.tryParseJSON()
|
|
)
|
|
assert.deepEqual(
|
|
testObject,
|
|
`\n\n${testObjectStr}\n\n`.tryParseJSON()
|
|
)
|
|
assert.deepEqual(
|
|
'definitely not json',
|
|
'definitely not json'.tryParseJSON()
|
|
)
|
|
assert.deepEqual(
|
|
'{maybe json?}',
|
|
'{maybe json?}'.tryParseJSON()
|
|
)
|
|
})
|
|
|
|
test('Buffer.prototype.tryParseJSON', t => {
|
|
assert.deepEqual(
|
|
testObject,
|
|
Buffer.from(testObjectStr).tryParseJSON(),
|
|
)
|
|
assert.deepEqual(
|
|
Buffer.from('meow'),
|
|
Buffer.from('meow').tryParseJSON(),
|
|
)
|
|
})
|
|
|
|
test('exec', t => {
|
|
assert.equal(
|
|
'1\n',
|
|
exec('echo', ['1']).stdout.toString()
|
|
)
|
|
})
|
|
|
|
test('exec.orFail', t => {
|
|
assert.throws(_ => exec('false').orFail())
|
|
})
|
|
|
|
test('execnc', t => {
|
|
assert.equal(
|
|
undefined,
|
|
execnc('echo', ['test output']).stdout
|
|
)
|
|
})
|
|
|
|
test('execnc with options', t => {
|
|
assert.equal(
|
|
undefined,
|
|
execnc('echo', ['test output2'], {}).stdout
|
|
)
|
|
})
|
|
|
|
test('apkindex', async t => {
|
|
try {
|
|
await fs.access('/lib/apk/db/installed', fs.constants.R_OK)
|
|
} catch (err) {
|
|
t.skip('not running on alpine')
|
|
return
|
|
}
|
|
|
|
const content = await fs.readFile('/lib/apk/db/installed', 'utf-8')
|
|
const index = apkindex(content)
|
|
|
|
assert.equal('object', typeof index)
|
|
assert(index instanceof Array)
|
|
|
|
const musl = index.find(pkg => pkg.P === 'musl')
|
|
assert(musl)
|
|
assert.equal('musl', musl.o)
|
|
assert.equal('number', typeof musl.S)
|
|
})
|
|
|
|
test('Array.prototype.sum', t => {
|
|
assert.equal(
|
|
10,
|
|
[5, 3, 2].sum()
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.product', t => {
|
|
assert.equal(
|
|
30,
|
|
[5, 3, 2].product()
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.min', t => {
|
|
assert.equal(
|
|
2,
|
|
[5, 3, 2].min()
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.max', t => {
|
|
assert.equal(
|
|
5,
|
|
[5, 3, 2].max()
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.sortNum', t => {
|
|
assert.deepEqual(
|
|
[2, 3, 5],
|
|
[5, 3, 2].sortNum()
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.partition', t => {
|
|
assert.deepEqual(
|
|
[[1, 3], [2, 4]],
|
|
[1, 2, 3, 4].partition(a => a % 2)
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.chunks - size 2, length 2n', t => {
|
|
assert.deepEqual(
|
|
[[1, 2], [3, 4], [5, 6]],
|
|
[1, 2, 3, 4, 5, 6].chunks(2)
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.chunks - size 2, length 2n+1', t => {
|
|
assert.deepEqual(
|
|
[[1, 2], [3, 4], [5, 6], [7]],
|
|
[1, 2, 3, 4, 5, 6, 7].chunks(2)
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.transpose', t => {
|
|
assert.deepEqual(
|
|
[[1, 3], [2, 4]],
|
|
[[1, 2], [3, 4]].transpose()
|
|
)
|
|
})
|
|
|
|
|
|
test('Array.prototype.shuffle', t => {
|
|
const arr = [1, 2, 3, 4, 5, 6, 7, 8]
|
|
assert.notDeepEqual(
|
|
arr,
|
|
arr.shuffle()
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.uniq', t => {
|
|
const arr = [ 5, 5, 3, 8, 4, 6, 7, 2, 1, 2, 8 ]
|
|
assert.deepEqual(
|
|
[ 5, 3, 8, 4, 6, 7, 2, 1 ],
|
|
arr.uniq()
|
|
)
|
|
})
|
|
|
|
test('Array.prototype.lines', t => {
|
|
const arr = [ 'a', 'b', 'test' ]
|
|
assert.deepEqual(
|
|
'a\nb\ntest',
|
|
arr.lines()
|
|
)
|
|
})
|
|
|
|
test('Set.prototype.union', t => {
|
|
const left = new Set([1, 3])
|
|
const right = new Set([2, 4])
|
|
assert.deepEqual(
|
|
new Set([1, 2, 3, 4]),
|
|
left.union(right)
|
|
)
|
|
assert.throws(() => left.union())
|
|
assert.throws(() => left.union(3))
|
|
})
|
|
|
|
test('Set.prototype.intersection', t => {
|
|
const left = new Set([1, 2, 3])
|
|
const right = new Set([2, 4])
|
|
assert.deepEqual(
|
|
new Set([2]),
|
|
left.intersection(right)
|
|
)
|
|
assert.throws(() => left.intersection())
|
|
assert.throws(() => left.intersection(3))
|
|
})
|
|
|
|
test('Set.prototype.difference', t => {
|
|
const left = new Set([1, 2, 3])
|
|
const right = new Set([2, 4])
|
|
assert.deepEqual(
|
|
new Set([1, 3]),
|
|
left.difference(right)
|
|
)
|
|
assert.deepEqual(
|
|
new Set([4]),
|
|
right.difference(left)
|
|
)
|
|
assert.throws(() => left.difference())
|
|
assert.throws(() => left.difference(3))
|
|
})
|
|
|
|
test('Set.prototype.at - single element', t => {
|
|
assert.equal(
|
|
2,
|
|
new Set([2]).at(0)
|
|
)
|
|
})
|
|
|
|
test('Set.prototype.at - multiple elements', t => {
|
|
assert.equal(
|
|
2,
|
|
new Set([1, 2, 3]).at(1)
|
|
)
|
|
})
|
|
|
|
test('matrix - seed with value', t => {
|
|
const reference = [
|
|
[ 0, 0 ],
|
|
[ 0, 0 ]
|
|
]
|
|
assert.deepEqual(reference, matrix(2, 2, 0))
|
|
})
|
|
|
|
test('matrix - seed with function', t => {
|
|
const reference = [
|
|
[ 0, 1, 2 ],
|
|
[ 0, 1, 2 ]
|
|
]
|
|
assert.deepEqual(reference, matrix(3, 2, i => i))
|
|
})
|
|
|
|
test('Number.prototype.toXY', t => {
|
|
assert.deepEqual(
|
|
[ 1, 3 ],
|
|
(16).toXY(5)
|
|
)
|
|
assert.throws(() => (5).toXY())
|
|
assert.throws(() => (5).toXY([]))
|
|
})
|
|
|
|
test('Object.prune', t => {
|
|
assert.deepEqual(
|
|
{
|
|
emptyProperty: null,
|
|
actualProperty: 2
|
|
},
|
|
Object.prune({
|
|
emptyProperty: null,
|
|
actualProperty: 2,
|
|
anotherEmptyProperty: undefined
|
|
})
|
|
)
|
|
assert.throws(() => Object.prune())
|
|
assert.throws(() => Object.prune(5))
|
|
})
|