1
1
'use strict'
2
+
2
3
var test = require ( 'tape' )
3
4
var HashBase = require ( '../' )
4
5
var Buffer = require ( 'safe-buffer' ) . Buffer
@@ -7,136 +8,138 @@ var utf8text = 'УТФ-8 text'
7
8
var utf8buf = Buffer . from ( utf8text , 'utf8' )
8
9
function noop ( ) { }
9
10
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
-
23
11
test ( 'HashBase#_transform' , function ( t ) {
24
- beforeEach ( t , createHashBase )
25
-
26
12
t . test ( 'should use HashBase#update' , function ( t ) {
27
13
t . plan ( 3 )
28
- t . base . update = function ( ) {
14
+ var base = new HashBase ( 64 )
15
+ base . update = function ( ) {
29
16
t . same ( arguments [ 0 ] , utf8text )
30
17
t . same ( arguments [ 1 ] , 'utf8' )
31
18
}
32
- t . base . _transform ( utf8text , 'utf8' , function ( err ) {
19
+ base . _transform ( utf8text , 'utf8' , function ( err ) {
33
20
t . same ( err , null )
34
21
} )
22
+
35
23
t . end ( )
36
24
} )
37
25
38
26
t . test ( 'should handle error in HashBase#update' , function ( t ) {
39
27
t . plan ( 1 )
40
28
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 ) {
43
32
t . true ( _err === err )
44
33
} )
34
+
45
35
t . end ( )
46
36
} )
47
37
48
38
t . end ( )
49
39
} )
50
40
51
41
test ( 'HashBase#_flush' , function ( t ) {
52
- beforeEach ( t , createHashBase )
53
-
54
42
t . test ( 'should use HashBase#digest' , function ( t ) {
55
43
t . plan ( 2 )
56
44
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
+
60
50
t . end ( )
61
51
} )
62
52
63
53
t . test ( 'should handle errors in HashBase#digest' , function ( t ) {
64
54
t . plan ( 1 )
55
+ var base = new HashBase ( 64 )
65
56
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
+
68
60
t . end ( )
69
61
} )
70
62
71
63
t . end ( )
72
64
} )
73
65
74
66
test ( 'HashBase#update' , function ( t ) {
75
- beforeEach ( t , createHashBase )
76
-
77
67
t . test ( 'only string or buffer is allowed' , function ( t ) {
68
+ var base = new HashBase ( 64 )
78
69
t . throws ( function ( ) {
79
- t . base . update ( null )
70
+ base . update ( null )
80
71
} , / ^ 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 $ / )
81
72
t . end ( )
82
73
} )
83
74
84
75
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 ( )
87
79
t . throws ( function ( ) {
88
- t . base . update ( '' )
80
+ base . update ( '' )
89
81
} , / ^ E r r o r : D i g e s t a l r e a d y c a l l e d $ / )
90
82
t . end ( )
91
83
} )
92
84
93
85
t . test ( 'should use HashBase#_update' , function ( t ) {
94
86
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
+
97
92
t . end ( )
98
93
} )
99
94
100
95
t . test ( 'default encoding is utf8' , function ( t ) {
101
96
t . plan ( 1 )
97
+
102
98
var buffer = Buffer . allocUnsafe ( 64 )
103
99
buffer . fill ( 0 )
104
100
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
+
107
105
t . end ( )
108
106
} )
109
107
110
108
t . test ( 'decode string with custom encoding' , function ( t ) {
111
109
t . plan ( 1 )
112
110
var buffer = Buffer . allocUnsafe ( 64 )
113
111
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
+
116
116
t . end ( )
117
117
} )
118
118
119
119
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
+
123
125
t . end ( )
124
126
} )
125
127
126
128
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
+
128
132
t . end ( )
129
133
} )
130
134
131
135
t . end ( )
132
136
} )
133
137
134
138
test ( 'HashBase#_update' , function ( t ) {
135
- beforeEach ( t , createHashBase )
136
-
137
139
t . test ( 'is not implemented' , function ( t ) {
140
+ var base = new HashBase ( 64 )
138
141
t . throws ( function ( ) {
139
- t . base . _update ( )
142
+ base . _update ( )
140
143
} , / ^ 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 $ / )
141
144
t . end ( )
142
145
} )
@@ -145,45 +148,51 @@ test('HashBase#_update', function (t) {
145
148
} )
146
149
147
150
test ( 'HashBase#digest' , function ( t ) {
148
- beforeEach ( t , createHashBase )
149
-
150
151
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 ( )
153
155
t . throws ( function ( ) {
154
- t . base . digest ( )
156
+ base . digest ( )
155
157
} , / ^ E r r o r : D i g e s t a l r e a d y c a l l e d $ / )
156
158
t . end ( )
157
159
} )
158
160
159
161
t . test ( 'should use HashBase#_digest' , function ( t ) {
160
162
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
+
163
168
t . end ( )
164
169
} )
165
170
166
171
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
+
169
177
t . end ( )
170
178
} )
171
179
172
180
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
+
175
185
t . end ( )
176
186
} )
177
187
178
188
t . end ( )
179
189
} )
180
190
181
191
test ( 'HashBase#_digest' , function ( t ) {
182
- beforeEach ( t , createHashBase )
183
-
184
192
t . test ( 'is not implemented' , function ( t ) {
193
+ var base = new HashBase ( 64 )
185
194
t . throws ( function ( ) {
186
- t . base . _digest ( )
195
+ base . _digest ( )
187
196
} , / ^ 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 $ / )
188
197
t . end ( )
189
198
} )
0 commit comments