Skip to content

Commit

Permalink
test: improve testing in regards to constants (#51)
Browse files Browse the repository at this point in the history
The codec `ipfs` and `p2p` both are assigned to `0x01a5`, where `ipfs` is the
deprecated name. Make sure that we resolve to `p2p` if that number is used
as input.
  • Loading branch information
vmx authored and hacdias committed Sep 19, 2019
1 parent 86a6b84 commit 2ed95e2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ const multicodec = require('multicodec')

const prefixedProtobuf = multicodec.addPrefix('protobuf', protobufBuffer)
// prefixedProtobuf 0x50...

// The multicodec codec values can be accessed directly:
console.log(multicodec.DAG_CBOR)
// 113

// To get the string representation of a codec, e.g. for error messages:
console.log(multicodec.print[113])
// dag-cbor
```

### API
Expand Down
27 changes: 27 additions & 0 deletions test/multicodec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,38 @@ describe('multicodec', () => {
it('returns the codec name from code', () => {
expect(multicodec.getName(144)).to.eql('eth-block')
expect(multicodec.getName(112)).to.eql('dag-pb')
expect(multicodec.getName(0xb201)).to.eql('blake2b-8')
})

it('returns the codec number from name', () => {
expect(multicodec.getNumber('eth-block')).to.eql(144)
expect(multicodec.getNumber('dag-pb')).to.eql(112)
// NOTE vmx 2019-09019: Uncomment once
// https://github.com/multiformats/js-multicodec/issues/50 is fixed
// expect(multicodec.getNumber('blake2b-8')).to.eql(0xb201)
})

it('returns the codec number from constant', () => {
expect(multicodec.ETH_BLOCK).to.eql(144)
expect(multicodec.DAG_PB).to.eql(112)
expect(multicodec.BLAKE2B_8).to.eql(0xb201)
})

it('returns the name from codec number', () => {
expect(multicodec.print[144]).to.eql('eth-block')
expect(multicodec.print[112]).to.eql('dag-pb')
expect(multicodec.print[0x0111]).to.eql('udp')
expect(multicodec.print[0xb201]).to.eql('blake2b-8')

expect(multicodec.print[multicodec.ETH_BLOCK]).to.eql('eth-block')
expect(multicodec.print[multicodec.DAG_PB]).to.eql('dag-pb')
expect(multicodec.print[multicodec.UDP]).to.eql('udp')
expect(multicodec.print[multicodec.BLAKE2B_8]).to.eql('blake2b-8')
})

it('returns p2p when 0x01a5 is used', () => {
// `ipfs` and `p2p` are assigned to `0x01a5`, `ipfs` is deprecated
expect(multicodec.print[0x01a5]).to.eql('p2p')
})

it('throws error on unknown codec name when getting the code', () => {
Expand Down

0 comments on commit 2ed95e2

Please sign in to comment.