ptrcports/node-print/test.js

254 lines
5 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.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']))
})
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('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.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('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)
)
})
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)
)
})
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)
)
})
test('Object.prune', t => {
assert.deepEqual(
{
emptyProperty: null,
actualProperty: 2
},
Object.prune({
emptyProperty: null,
actualProperty: 2,
anotherEmptyProperty: undefined
})
)
})
// test('String.prototype.reverse', t => {
// assert.equal(
// )
// })