@@ -27,16 +27,24 @@ function sample (code, size, hex) {
27
27
return Buffer . from ( `${ toHex ( code ) } ${ toHex ( size ) } ${ hex } ` , 'hex' )
28
28
}
29
29
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
+ } ) )
33
43
}
34
44
35
- const encodeText = string => textEncoder . encode ( string )
36
-
37
45
describe ( 'multihash' , ( ) => {
38
46
describe ( 'toHexString' , ( ) => {
39
- it ( 'valid' , ( ) => {
47
+ they ( 'valid' , ( { encodeHex } ) => {
40
48
validCases . forEach ( ( test ) => {
41
49
const code = test . encoding . code
42
50
const buf = mh . encode ( encodeHex ( test . hex ) , code )
@@ -58,7 +66,7 @@ describe('multihash', () => {
58
66
} )
59
67
60
68
describe ( 'fromHexString' , ( ) => {
61
- it ( 'valid' , ( ) => {
69
+ they ( 'valid' , ( { encodeHex } ) => {
62
70
validCases . forEach ( ( test ) => {
63
71
const code = test . encoding . code
64
72
const buf = mh . encode ( encodeHex ( test . hex ) , code )
@@ -72,10 +80,10 @@ describe('multihash', () => {
72
80
} )
73
81
74
82
describe ( 'toB58String' , ( ) => {
75
- it ( 'valid' , ( ) => {
83
+ they ( 'valid' , ( { encodeHex } ) => {
76
84
validCases . forEach ( ( test ) => {
77
85
const code = test . encoding . code
78
- const buf = mh . encode ( encodeHex ( test . hex , 'hex' ) , code )
86
+ const buf = mh . encode ( encodeHex ( test . hex ) , code )
79
87
expect (
80
88
mh . toB58String ( buf )
81
89
) . to . be . eql (
@@ -94,7 +102,7 @@ describe('multihash', () => {
94
102
} )
95
103
96
104
describe ( 'fromB58String' , ( ) => {
97
- it ( 'valid' , ( ) => {
105
+ they ( 'valid' , ( { encodeHex , encodeText } ) => {
98
106
const src = 'QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE'
99
107
const expected = encodeHex ( '122013bf801597d74a660453412635edd8c34271e5998f801fac5d700c6ce8d8e461' )
100
108
@@ -141,7 +149,7 @@ describe('multihash', () => {
141
149
} )
142
150
143
151
describe ( 'encode' , ( ) => {
144
- it ( 'valid' , ( ) => {
152
+ they ( 'valid' , ( { encodeHex } ) => {
145
153
validCases . forEach ( ( test ) => {
146
154
const code = test . encoding . code
147
155
const name = test . encoding . name
@@ -161,7 +169,7 @@ describe('multihash', () => {
161
169
} )
162
170
} )
163
171
164
- it ( 'invalid' , ( ) => {
172
+ they ( 'invalid' , ( { encodeText } ) => {
165
173
expect (
166
174
( ) => mh . encode ( )
167
175
) . to . throw (
@@ -287,7 +295,7 @@ describe('multihash', () => {
287
295
} )
288
296
} )
289
297
290
- it ( 'invalid' , ( ) => {
298
+ they ( 'invalid' , ( { encodeText } ) => {
291
299
const invalidNames = [
292
300
'sha256' ,
293
301
'sha9' ,
@@ -316,13 +324,13 @@ describe('multihash', () => {
316
324
} )
317
325
} )
318
326
319
- it ( 'prefix' , ( ) => {
327
+ they ( 'prefix' , ( { encodeText } ) => {
320
328
const multihash = mh . encode ( encodeText ( 'hey' ) , 0x11 , 3 )
321
329
const prefix = mh . prefix ( multihash )
322
330
expect ( prefix . toString ( 'hex' ) ) . to . eql ( '1103' )
323
331
} )
324
332
325
- it ( 'prefix throws on invalid multihash' , ( ) => {
333
+ they ( 'prefix throws on invalid multihash' , ( { encodeText } ) => {
326
334
const multihash = encodeText ( 'definitely not valid' )
327
335
328
336
expect ( ( ) => mh . prefix ( multihash ) ) . to . throw ( )
0 commit comments