2023-05-15 15:25:20 +00:00
|
|
|
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'],
|
2023-12-05 22:59:22 +00:00
|
|
|
str.trim().lines()
|
2023-05-15 15:25:20 +00:00
|
|
|
)
|
|
|
|
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']))
|
2023-12-05 22:59:22 +00:00
|
|
|
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'))
|
2023-05-15 15:25:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-12-05 22:59:22 +00:00
|
|
|
test('execnc with options', t => {
|
|
|
|
assert.equal(
|
|
|
|
undefined,
|
|
|
|
execnc('echo', ['test output2'], {}).stdout
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-05-15 15:25:20 +00:00
|
|
|
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()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-12-05 22:59:22 +00:00
|
|
|
test('Array.prototype.min', t => {
|
|
|
|
assert.equal(
|
|
|
|
2,
|
|
|
|
[5, 3, 2].min()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('Array.prototype.max', t => {
|
|
|
|
assert.equal(
|
|
|
|
5,
|
|
|
|
[5, 3, 2].max()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-05-15 15:25:20 +00:00
|
|
|
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()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-12-05 22:59:22 +00:00
|
|
|
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()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-05-15 15:25:20 +00:00
|
|
|
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)
|
|
|
|
)
|
2023-12-05 22:59:22 +00:00
|
|
|
assert.throws(() => left.union())
|
|
|
|
assert.throws(() => left.union(3))
|
2023-05-15 15:25:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
2023-12-05 22:59:22 +00:00
|
|
|
assert.throws(() => left.intersection())
|
|
|
|
assert.throws(() => left.intersection(3))
|
2023-05-15 15:25:20 +00:00
|
|
|
})
|
|
|
|
|
2023-05-19 17:59:00 +00:00
|
|
|
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)
|
|
|
|
)
|
2023-12-05 22:59:22 +00:00
|
|
|
assert.throws(() => left.difference())
|
|
|
|
assert.throws(() => left.difference(3))
|
2023-05-19 17:59:00 +00:00
|
|
|
})
|
|
|
|
|
2023-05-15 15:25:20 +00:00
|
|
|
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)
|
|
|
|
)
|
2023-12-05 22:59:22 +00:00
|
|
|
assert.throws(() => (5).toXY())
|
|
|
|
assert.throws(() => (5).toXY([]))
|
2023-05-15 15:25:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('Object.prune', t => {
|
|
|
|
assert.deepEqual(
|
|
|
|
{
|
|
|
|
emptyProperty: null,
|
|
|
|
actualProperty: 2
|
|
|
|
},
|
|
|
|
Object.prune({
|
|
|
|
emptyProperty: null,
|
|
|
|
actualProperty: 2,
|
|
|
|
anotherEmptyProperty: undefined
|
|
|
|
})
|
|
|
|
)
|
2023-12-05 22:59:22 +00:00
|
|
|
assert.throws(() => Object.prune())
|
|
|
|
assert.throws(() => Object.prune(5))
|
2023-05-15 15:25:20 +00:00
|
|
|
})
|