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

Commit 8ddfa1a

Browse files
committed
chore: test with both Buffer and Uint8Array
1 parent 2b8127b commit 8ddfa1a

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

test/index.spec.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,24 @@ function sample (code, size, hex) {
2727
return Buffer.from(`${toHex(code)}${toHex(size)}${hex}`, 'hex')
2828
}
2929

30-
const encodeHex = string => {
31-
const { buffer, byteOffset, byteLength } = Buffer.from(string, 'hex')
32-
return new Uint8Array(buffer, byteOffset, byteLength)
30+
const they = (description, test) => {
31+
it(`${description} (Buffer)`, () => test({
32+
encodeText: Buffer.from,
33+
encodeHex: (text) => Buffer.from(text, 'hex')
34+
}))
35+
36+
it(`${description} (Uint8Array)`, () => test({
37+
encodeText: (text) => textEncoder.encode(text),
38+
encodeHex: (text) => {
39+
const { buffer, byteOffset, byteLength } = Buffer.from(text, 'hex')
40+
return new Uint8Array(buffer, byteOffset, byteLength)
41+
}
42+
}))
3343
}
3444

35-
const encodeText = string => textEncoder.encode(string)
36-
3745
describe('multihash', () => {
3846
describe('toHexString', () => {
39-
it('valid', () => {
47+
they('valid', ({ encodeHex }) => {
4048
validCases.forEach((test) => {
4149
const code = test.encoding.code
4250
const buf = mh.encode(encodeHex(test.hex), code)
@@ -58,7 +66,7 @@ describe('multihash', () => {
5866
})
5967

6068
describe('fromHexString', () => {
61-
it('valid', () => {
69+
they('valid', ({ encodeHex }) => {
6270
validCases.forEach((test) => {
6371
const code = test.encoding.code
6472
const buf = mh.encode(encodeHex(test.hex), code)
@@ -72,10 +80,10 @@ describe('multihash', () => {
7280
})
7381

7482
describe('toB58String', () => {
75-
it('valid', () => {
83+
they('valid', ({ encodeHex }) => {
7684
validCases.forEach((test) => {
7785
const code = test.encoding.code
78-
const buf = mh.encode(encodeHex(test.hex, 'hex'), code)
86+
const buf = mh.encode(encodeHex(test.hex), code)
7987
expect(
8088
mh.toB58String(buf)
8189
).to.be.eql(
@@ -94,7 +102,7 @@ describe('multihash', () => {
94102
})
95103

96104
describe('fromB58String', () => {
97-
it('valid', () => {
105+
they('valid', ({ encodeHex, encodeText }) => {
98106
const src = 'QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE'
99107
const expected = encodeHex('122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461')
100108

@@ -141,7 +149,7 @@ describe('multihash', () => {
141149
})
142150

143151
describe('encode', () => {
144-
it('valid', () => {
152+
they('valid', ({ encodeHex }) => {
145153
validCases.forEach((test) => {
146154
const code = test.encoding.code
147155
const name = test.encoding.name
@@ -161,7 +169,7 @@ describe('multihash', () => {
161169
})
162170
})
163171

164-
it('invalid', () => {
172+
they('invalid', ({ encodeText }) => {
165173
expect(
166174
() => mh.encode()
167175
).to.throw(
@@ -287,7 +295,7 @@ describe('multihash', () => {
287295
})
288296
})
289297

290-
it('invalid', () => {
298+
they('invalid', ({ encodeText }) => {
291299
const invalidNames = [
292300
'sha256',
293301
'sha9',
@@ -316,13 +324,13 @@ describe('multihash', () => {
316324
})
317325
})
318326

319-
it('prefix', () => {
327+
they('prefix', ({ encodeText }) => {
320328
const multihash = mh.encode(encodeText('hey'), 0x11, 3)
321329
const prefix = mh.prefix(multihash)
322330
expect(prefix.toString('hex')).to.eql('1103')
323331
})
324332

325-
it('prefix throws on invalid multihash', () => {
333+
they('prefix throws on invalid multihash', ({ encodeText }) => {
326334
const multihash = encodeText('definitely not valid')
327335

328336
expect(() => mh.prefix(multihash)).to.throw()

0 commit comments

Comments
 (0)