Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

Commit

Permalink
chore: test with both Buffer and Uint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Jul 22, 2020
1 parent 2b8127b commit 8ddfa1a
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@ function sample (code, size, hex) {
return Buffer.from(`${toHex(code)}${toHex(size)}${hex}`, 'hex')
}

const encodeHex = string => {
const { buffer, byteOffset, byteLength } = Buffer.from(string, 'hex')
return new Uint8Array(buffer, byteOffset, byteLength)
const they = (description, test) => {
it(`${description} (Buffer)`, () => test({
encodeText: Buffer.from,
encodeHex: (text) => Buffer.from(text, 'hex')
}))

it(`${description} (Uint8Array)`, () => test({
encodeText: (text) => textEncoder.encode(text),
encodeHex: (text) => {
const { buffer, byteOffset, byteLength } = Buffer.from(text, 'hex')
return new Uint8Array(buffer, byteOffset, byteLength)
}
}))
}

const encodeText = string => textEncoder.encode(string)

describe('multihash', () => {
describe('toHexString', () => {
it('valid', () => {
they('valid', ({ encodeHex }) => {
validCases.forEach((test) => {
const code = test.encoding.code
const buf = mh.encode(encodeHex(test.hex), code)
Expand All @@ -58,7 +66,7 @@ describe('multihash', () => {
})

describe('fromHexString', () => {
it('valid', () => {
they('valid', ({ encodeHex }) => {
validCases.forEach((test) => {
const code = test.encoding.code
const buf = mh.encode(encodeHex(test.hex), code)
Expand All @@ -72,10 +80,10 @@ describe('multihash', () => {
})

describe('toB58String', () => {
it('valid', () => {
they('valid', ({ encodeHex }) => {
validCases.forEach((test) => {
const code = test.encoding.code
const buf = mh.encode(encodeHex(test.hex, 'hex'), code)
const buf = mh.encode(encodeHex(test.hex), code)
expect(
mh.toB58String(buf)
).to.be.eql(
Expand All @@ -94,7 +102,7 @@ describe('multihash', () => {
})

describe('fromB58String', () => {
it('valid', () => {
they('valid', ({ encodeHex, encodeText }) => {
const src = 'QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE'
const expected = encodeHex('122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461')

Expand Down Expand Up @@ -141,7 +149,7 @@ describe('multihash', () => {
})

describe('encode', () => {
it('valid', () => {
they('valid', ({ encodeHex }) => {
validCases.forEach((test) => {
const code = test.encoding.code
const name = test.encoding.name
Expand All @@ -161,7 +169,7 @@ describe('multihash', () => {
})
})

it('invalid', () => {
they('invalid', ({ encodeText }) => {
expect(
() => mh.encode()
).to.throw(
Expand Down Expand Up @@ -287,7 +295,7 @@ describe('multihash', () => {
})
})

it('invalid', () => {
they('invalid', ({ encodeText }) => {
const invalidNames = [
'sha256',
'sha9',
Expand Down Expand Up @@ -316,13 +324,13 @@ describe('multihash', () => {
})
})

it('prefix', () => {
they('prefix', ({ encodeText }) => {
const multihash = mh.encode(encodeText('hey'), 0x11, 3)
const prefix = mh.prefix(multihash)
expect(prefix.toString('hex')).to.eql('1103')
})

it('prefix throws on invalid multihash', () => {
they('prefix throws on invalid multihash', ({ encodeText }) => {
const multihash = encodeText('definitely not valid')

expect(() => mh.prefix(multihash)).to.throw()
Expand Down

0 comments on commit 8ddfa1a

Please sign in to comment.