Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
refactor: Group tests a bit differently
Browse files Browse the repository at this point in the history
This way it's easier to find tests to the corresponding functions.
  • Loading branch information
vmx committed Jan 18, 2018
1 parent 9802f2d commit a085651
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const expect = chai.expect
chai.use(dirtyChai)
const IpldBitcoin = require('../src/index')

describe('IPLD format util API', () => {
const fixtureBlockHex = loadFixture(__dirname, 'fixtures/block.hex')
const fixtureBlock = Buffer.from(fixtureBlockHex.toString(), 'hex')
const invalidDagNode = {invalid: 'dagNode'}
const fixtureBlockHex = loadFixture(__dirname, 'fixtures/block.hex')
const fixtureBlock = Buffer.from(fixtureBlockHex.toString(), 'hex')
const invalidDagNode = {invalid: 'dagNode'}

it('should deserialize correctly', (done) => {
describe('IPLD format util API deserialize()', () => {
it('should work correctly', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
expect(err).to.not.exist()
verifyBlock(dagNode, {
Expand All @@ -28,28 +28,7 @@ describe('IPLD format util API', () => {
})
})

it('should round-trip (de)serialization correctly', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
expect(err).to.not.exist()
IpldBitcoin.util.serialize(dagNode, (err, binaryBlob) => {
expect(err).to.not.exist()
expect(binaryBlob.equals(fixtureBlock)).to.be.true()
done()
})
})
})

it('should encode the CID correctly', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
expect(err).to.not.exist()
verifyCid(
dagNode,
'56203ec2c691d447b2fd0d6a94742345af1f351037dab1ab9e900200000000000000',
done)
})
})

it('should decode Segwit correctly (a)', (done) => {
it('should deserialize Segwit correctly (a)', (done) => {
const segwitBlockHex = loadFixture(__dirname, 'fixtures/segwit.hex')
const segwitBlock = Buffer.from(segwitBlockHex.toString(), 'hex')
IpldBitcoin.util.deserialize(segwitBlock, (err, dagNode) => {
Expand All @@ -69,7 +48,7 @@ describe('IPLD format util API', () => {
})
})

it('should decode Segwit correctly (b)', (done) => {
it('should deserialize Segwit correctly (b)', (done) => {
const segwitBlockHex = loadFixture(__dirname, 'fixtures/segwit2.hex')
const segwitBlock = Buffer.from(segwitBlockHex.toString(), 'hex')
IpldBitcoin.util.deserialize(segwitBlock, (err, dagNode) => {
Expand All @@ -89,7 +68,7 @@ describe('IPLD format util API', () => {
})
})

it('should decode Segwit correctly (c)', (done) => {
it('should deserialize Segwit correctly (c)', (done) => {
const segwitBlockHex = loadFixture(__dirname, 'fixtures/segwit3.hex')
const segwitBlock = Buffer.from(segwitBlockHex.toString(), 'hex')
IpldBitcoin.util.deserialize(segwitBlock, (err, dagNode) => {
Expand All @@ -109,7 +88,7 @@ describe('IPLD format util API', () => {
})
})

it('should decode a block without transations', (done) => {
it('should deserialize a block without transactions', (done) => {
const hexData = '01000000000102e9b542c5176808107ff1df906f46bb1f2583b16112b95ee5380665ba7fcfc0010000000000ffffffff80e68831516392fcd100d186b3c2c7b95c80b53c77e77c35ba03a66b429a2a1b0000000000ffffffff0280969800000000001976a914de4b231626ef508c9a74a8517e6783c0546d6b2888ac80969800000000001976a9146648a8cd4531e1ec47f35916de8e259237294d1e88ac02483045022100f6a10b8604e6dc910194b79ccfc93e1bc0ec7c03453caaa8987f7d6c3413566002206216229ede9b4d6ec2d325be245c5b508ff0339bf1794078e20bfe0babc7ffe683270063ab68210392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac024730440220032521802a76ad7bf74d0e2c218b72cf0cbc867066e2e53db905ba37f130397e02207709e2188ed7f08f4c952d9d1398'
const block = Buffer.from(hexData.toString(), 'hex')
IpldBitcoin.util.deserialize(block, (err, dagNode) => {
Expand All @@ -122,32 +101,55 @@ describe('IPLD format util API', () => {
})
})

it('should error on deserialzing an invalid block', (done) => {
it('should error on an invalid block', (done) => {
const invalidBlock = Buffer.from('abcdef', 'hex')
IpldBitcoin.util.deserialize(invalidBlock, (err, dagNode) => {
expect(dagNode).to.not.exist()
expect(err).to.be.an('error')
done()
})
})
})

it('should error on serialzing an invalid internal representation',
(done) => {
IpldBitcoin.util.serialize(invalidDagNode, (err, binaryBlob) => {
expect(binaryBlob).to.not.exist()
expect(err).to.be.an('error')
describe('IPLD format util API serialize()', () => {
it('should round-trip (de)serialization correctly', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
expect(err).to.not.exist()
IpldBitcoin.util.serialize(dagNode, (err, binaryBlob) => {
expect(err).to.not.exist()
expect(binaryBlob.equals(fixtureBlock)).to.be.true()
done()
})
})
})

it('should error on getting a CID from invalid internal representation',
(done) => {
IpldBitcoin.util.cid(invalidDagNode, (err, cid) => {
expect(cid).to.not.exist()
expect(err).to.be.an('error')
done()
})
it('should error on an invalid internal representation', (done) => {
IpldBitcoin.util.serialize(invalidDagNode, (err, binaryBlob) => {
expect(binaryBlob).to.not.exist()
expect(err).to.be.an('error')
done()
})
})
})

describe('IPLD format util API cid()', () => {
it('should encode the CID correctly', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
expect(err).to.not.exist()
verifyCid(
dagNode,
'56203ec2c691d447b2fd0d6a94742345af1f351037dab1ab9e900200000000000000',
done)
})
})

it('should error on an invalid internal representation', (done) => {
IpldBitcoin.util.cid(invalidDagNode, (err, cid) => {
expect(cid).to.not.exist()
expect(err).to.be.an('error')
done()
})
})
})

const verifyBlock = (dagNode, expected) => {
Expand Down

0 comments on commit a085651

Please sign in to comment.