11'use strict'
2+
23var test = require ( 'tape' )
34var HashBase = require ( '../' )
45var Buffer = require ( 'safe-buffer' ) . Buffer
@@ -7,136 +8,138 @@ var utf8text = 'УТФ-8 text'
78var utf8buf = Buffer . from ( utf8text , 'utf8' )
89function noop ( ) { }
910
10- function createHashBase ( t ) { t . base = new HashBase ( 64 ) }
11-
12- function beforeEach ( t ) {
13- var fns = Array . prototype . slice . call ( arguments , 1 )
14- var _test = t . test
15- t . test = function ( name , callback ) {
16- _test ( name , function ( t ) {
17- for ( var i = 0 ; i < fns . length ; ++ i ) t = fns [ i ] ( t ) || t
18- callback ( t )
19- } )
20- }
21- }
22-
2311test ( 'HashBase#_transform' , function ( t ) {
24- beforeEach ( t , createHashBase )
25-
2612 t . test ( 'should use HashBase#update' , function ( t ) {
2713 t . plan ( 3 )
28- t . base . update = function ( ) {
14+ var base = new HashBase ( 64 )
15+ base . update = function ( ) {
2916 t . same ( arguments [ 0 ] , utf8text )
3017 t . same ( arguments [ 1 ] , 'utf8' )
3118 }
32- t . base . _transform ( utf8text , 'utf8' , function ( err ) {
19+ base . _transform ( utf8text , 'utf8' , function ( err ) {
3320 t . same ( err , null )
3421 } )
22+
3523 t . end ( )
3624 } )
3725
3826 t . test ( 'should handle error in HashBase#update' , function ( t ) {
3927 t . plan ( 1 )
4028 var err = new Error ( 'hey' )
41- t . base . update = function ( ) { throw err }
42- t . base . _transform ( Buffer . allocUnsafe ( 0 ) , 'buffer' , function ( _err ) {
29+ var base = new HashBase ( 64 )
30+ base . update = function ( ) { throw err }
31+ base . _transform ( Buffer . allocUnsafe ( 0 ) , 'buffer' , function ( _err ) {
4332 t . true ( _err === err )
4433 } )
34+
4535 t . end ( )
4636 } )
4737
4838 t . end ( )
4939} )
5040
5141test ( 'HashBase#_flush' , function ( t ) {
52- beforeEach ( t , createHashBase )
53-
5442 t . test ( 'should use HashBase#digest' , function ( t ) {
5543 t . plan ( 2 )
5644 var buffer = Buffer . allocUnsafe ( 0 )
57- t . base . push = function ( data ) { t . true ( data === buffer ) }
58- t . base . digest = function ( ) { return buffer }
59- t . base . _flush ( function ( err ) { t . same ( err , null ) } )
45+ var base = new HashBase ( 64 )
46+ base . push = function ( data ) { t . true ( data === buffer ) }
47+ base . digest = function ( ) { return buffer }
48+ base . _flush ( function ( err ) { t . same ( err , null ) } )
49+
6050 t . end ( )
6151 } )
6252
6353 t . test ( 'should handle errors in HashBase#digest' , function ( t ) {
6454 t . plan ( 1 )
55+ var base = new HashBase ( 64 )
6556 var err = new Error ( 'hey' )
66- t . base . digest = function ( ) { throw err }
67- t . base . _flush ( function ( _err ) { t . true ( _err === err ) } )
57+ base . digest = function ( ) { throw err }
58+ base . _flush ( function ( _err ) { t . true ( _err === err ) } )
59+
6860 t . end ( )
6961 } )
7062
7163 t . end ( )
7264} )
7365
7466test ( 'HashBase#update' , function ( t ) {
75- beforeEach ( t , createHashBase )
76-
7767 t . test ( 'only string or buffer is allowed' , function ( t ) {
68+ var base = new HashBase ( 64 )
7869 t . throws ( function ( ) {
79- t . base . update ( null )
70+ base . update ( null )
8071 } , / ^ T y p e E r r o r : D a t a m u s t b e a s t r i n g o r a b u f f e r $ / )
8172 t . end ( )
8273 } )
8374
8475 t . test ( 'should throw error after HashBase#digest' , function ( t ) {
85- t . base . _digest = noop
86- t . base . digest ( )
76+ var base = new HashBase ( 64 )
77+ base . _digest = noop
78+ base . digest ( )
8779 t . throws ( function ( ) {
88- t . base . update ( '' )
80+ base . update ( '' )
8981 } , / ^ E r r o r : D i g e s t a l r e a d y c a l l e d $ / )
9082 t . end ( )
9183 } )
9284
9385 t . test ( 'should use HashBase#_update' , function ( t ) {
9486 t . plan ( 1 )
95- t . base . _update = t . pass
96- t . base . update ( Buffer . allocUnsafe ( 64 ) )
87+
88+ var base = new HashBase ( 64 )
89+ base . _update = t . pass
90+ base . update ( Buffer . allocUnsafe ( 64 ) )
91+
9792 t . end ( )
9893 } )
9994
10095 t . test ( 'default encoding is utf8' , function ( t ) {
10196 t . plan ( 1 )
97+
10298 var buffer = Buffer . allocUnsafe ( 64 )
10399 buffer . fill ( 0 )
104100 utf8buf . copy ( buffer )
105- t . base . _update = function ( ) { t . same ( this . _block , buffer ) }
106- t . base . update ( buffer . toString ( 'utf8' ) )
101+ var base = new HashBase ( 64 )
102+ base . _update = function ( ) { t . same ( this . _block , buffer ) }
103+ base . update ( buffer . toString ( 'utf8' ) )
104+
107105 t . end ( )
108106 } )
109107
110108 t . test ( 'decode string with custom encoding' , function ( t ) {
111109 t . plan ( 1 )
112110 var buffer = Buffer . allocUnsafe ( 64 )
113111 buffer . fill ( 0x42 )
114- t . base . _update = function ( ) { t . same ( this . _block , buffer ) }
115- t . base . update ( buffer . toString ( 'hex' ) , 'hex' )
112+ var base = new HashBase ( 64 )
113+ base . _update = function ( ) { t . same ( this . _block , buffer ) }
114+ base . update ( buffer . toString ( 'hex' ) , 'hex' )
115+
116116 t . end ( )
117117 } )
118118
119119 t . test ( 'data length is more than 2^32 bits' , function ( t ) {
120- t . base . _length = [ Math . pow ( 2 , 32 ) - 1 , 0 , 0 , 0 ]
121- t . base . update ( Buffer . allocUnsafe ( 1 ) )
122- t . same ( t . base . _length , [ 7 , 1 , 0 , 0 ] )
120+ var base = new HashBase ( 64 )
121+ base . _length = [ Math . pow ( 2 , 32 ) - 1 , 0 , 0 , 0 ]
122+ base . update ( Buffer . allocUnsafe ( 1 ) )
123+ t . same ( base . _length , [ 7 , 1 , 0 , 0 ] )
124+
123125 t . end ( )
124126 } )
125127
126128 t . test ( 'should return `this`' , function ( t ) {
127- t . same ( t . base . update ( Buffer . allocUnsafe ( 0 ) ) , t . base )
129+ var base = new HashBase ( 64 )
130+ t . same ( base . update ( Buffer . allocUnsafe ( 0 ) ) , base )
131+
128132 t . end ( )
129133 } )
130134
131135 t . end ( )
132136} )
133137
134138test ( 'HashBase#_update' , function ( t ) {
135- beforeEach ( t , createHashBase )
136-
137139 t . test ( 'is not implemented' , function ( t ) {
140+ var base = new HashBase ( 64 )
138141 t . throws ( function ( ) {
139- t . base . _update ( )
142+ base . _update ( )
140143 } , / ^ E r r o r : _ u p d a t e i s n o t i m p l e m e n t e d $ / )
141144 t . end ( )
142145 } )
@@ -145,45 +148,51 @@ test('HashBase#_update', function (t) {
145148} )
146149
147150test ( 'HashBase#digest' , function ( t ) {
148- beforeEach ( t , createHashBase )
149-
150151 t . test ( 'should throw error on second call' , function ( t ) {
151- t . base . _digest = noop
152- t . base . digest ( )
152+ var base = new HashBase ( 64 )
153+ base . _digest = noop
154+ base . digest ( )
153155 t . throws ( function ( ) {
154- t . base . digest ( )
156+ base . digest ( )
155157 } , / ^ E r r o r : D i g e s t a l r e a d y c a l l e d $ / )
156158 t . end ( )
157159 } )
158160
159161 t . test ( 'should use HashBase#_digest' , function ( t ) {
160162 t . plan ( 1 )
161- t . base . _digest = t . pass
162- t . base . digest ( )
163+
164+ var base = new HashBase ( 64 )
165+ base . _digest = t . pass
166+ base . digest ( )
167+
163168 t . end ( )
164169 } )
165170
166171 t . test ( 'should return buffer by default' , function ( t ) {
167- t . base . _digest = function ( ) { return utf8buf }
168- t . same ( t . base . digest ( ) , utf8buf )
172+ var base = new HashBase ( 64 )
173+
174+ base . _digest = function ( ) { return utf8buf }
175+ t . same ( base . digest ( ) , utf8buf )
176+
169177 t . end ( )
170178 } )
171179
172180 t . test ( 'should encode result with custom encoding' , function ( t ) {
173- t . base . _digest = function ( ) { return utf8buf }
174- t . same ( t . base . digest ( 'utf8' ) , utf8text )
181+ var base = new HashBase ( 64 )
182+ base . _digest = function ( ) { return utf8buf }
183+ t . same ( base . digest ( 'utf8' ) , utf8text )
184+
175185 t . end ( )
176186 } )
177187
178188 t . end ( )
179189} )
180190
181191test ( 'HashBase#_digest' , function ( t ) {
182- beforeEach ( t , createHashBase )
183-
184192 t . test ( 'is not implemented' , function ( t ) {
193+ var base = new HashBase ( 64 )
185194 t . throws ( function ( ) {
186- t . base . _digest ( )
195+ base . _digest ( )
187196 } , / ^ E r r o r : _ d i g e s t i s n o t i m p l e m e n t e d $ / )
188197 t . end ( )
189198 } )
0 commit comments