Browse Source

adding tests for blockchain and collection

pull/563/head
Clement Denis 4 years ago
parent
commit
5fea5c7d74
  1. 54
      js/tests/block-chain_test.js
  2. 59
      js/tests/collections_test.js

54
js/tests/block-chain_test.js

@ -0,0 +1,54 @@
export const tests = []
const t = (f) => tests.push(f)
t(({ eq }) => {
const { chain, ...block } = blockChain({ a: 1 })
return eq(block, {
index: 1,
data: { a: 1 },
prev: { index: 0, hash: '0' },
hash: '1103f27',
})
})
t(({ eq }) => {
const block = blockChain({ a: 1 }).chain({ hello: 'world' })
const chain = block
.chain({ value: 4455 })
.chain({ some: 'data' })
.chain({ cool: 'stuff' })
const fork = block
.chain({ value: 335 })
.chain({ some: 'data' })
.chain({ cool: 'stuff' })
return eq(
{
chainRoot: chain.prev.prev.prev,
forkRoot: chain.prev.prev.prev,
chainHash: chain.hash,
forkHash: fork.hash,
index: chain.index,
data: chain.data,
},
{
chainRoot: block,
forkRoot: block,
chainHash: '1qr3qfs',
forkHash: '1x9gsc1',
index: fork.index,
data: fork.data,
},
)
})
t(({ eq }) => {
const random = Math.random()
const hash = hashCode(`21103f27{"random":${random}}`)
const { prev, chain, ...block } = blockChain({ a: 1 }).chain({ random })
return eq(block, { index: 2, data: { random }, hash })
})
Object.freeze(tests)

59
js/tests/collections_test.js

@ -0,0 +1,59 @@
export const tests = []
const t = f => tests.push(f)
t(({ eq, ctx }) => eq(arrToSet([1, ctx, ctx, 3, 3]), new Set([1, ctx, 3])))
t(({ eq, ctx }) => eq(arrToStr([1, ctx, ctx, 3]), `${1}${ctx}${ctx}${3}`))
t(({ eq, ctx }) => eq(setToArr(new Set([1, ctx, 3])), [1, ctx, 3]))
t(({ eq, ctx }) => eq(setToStr(new Set([1, ctx, 3])), `${1}${ctx}${3}`))
t(({ eq, ctx }) => eq(strToArr(`${1}${ctx}${3}`), ['1', ...ctx, '3']))
t(({ eq, ctx }) =>
eq(strToSet(`12${ctx[0]}23`), new Set(['1', '2', ctx[0], '3'])),
)
t(({ eq, ctx }) =>
eq(
mapToObj(
new Map([
[ctx, ctx],
['a', 2],
]),
),
{ [ctx]: ctx, a: 2 },
),
)
t(({ eq, ctx }) => eq(objToArr({ a: 1, b: ctx }), [1, ctx]))
t(({ eq, ctx }) =>
eq(
objToMap({ [ctx]: ctx, a: 2 }),
new Map([
[ctx, ctx],
['a', 2],
]),
),
)
t(({ eq, ctx }) =>
eq(arrToObj([1, ctx, 'pouet']), { '0': 1, '1': ctx, '2': 'pouet' }),
)
t(({ eq, ctx }) =>
eq(strToObj(`2${ctx[0]}2`), { '0': '2', '1': ctx[0], '2': '2' }),
)
t(({ eq }) => eq(superTypeOf(new Map()), 'Map'))
t(({ eq }) => eq(superTypeOf(new Set()), 'Set'))
t(({ eq }) => eq(superTypeOf({}), 'Object'))
t(({ eq }) => eq(superTypeOf(''), 'String'))
t(({ eq }) => eq(superTypeOf(666), 'Number'))
t(({ eq }) => eq(superTypeOf(NaN), 'Number'))
t(({ eq }) => eq(superTypeOf([]), 'Array'))
t(({ eq }) => eq(superTypeOf(null), 'null'))
t(({ eq }) => eq(superTypeOf(undefined), 'undefined'))
t(({ eq }) => eq(superTypeOf(superTypeOf), 'Function'))
export const setup = () => Math.random().toString(36).slice(2)
Object.freeze(tests)
Loading…
Cancel
Save