Skip to content

Commit e6851ca

Browse files
committed
tests: adhere to standard
1 parent 1839fb7 commit e6851ca

File tree

2 files changed

+54
-66
lines changed

2 files changed

+54
-66
lines changed

test/hash.js

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,72 @@
1-
var hexpp = require('../hexpp').defaults({bigendian: false})
1+
var hexpp = require('../hexpp').defaults({ bigendian: false })
22
var tape = require('tape')
33
var Hash = require('../hash')
44

5-
var hex = '0A1B2C3D4E5F6G7H', hexbuf
6-
function equal(t, a, b) {
5+
var hex = '0A1B2C3D4E5F6G7H'
6+
7+
function equal (t, a, b) {
78
t.equal(a.length, b.length)
8-
for(var i = 0; i < a.length; i++)
9+
10+
for (var i = 0; i < a.length; i++) {
911
t.equal(a[i], b[i])
12+
}
1013
}
1114

15+
var hexBuf = new Buffer([48, 65, 49, 66, 50, 67, 51, 68, 52, 69, 53, 70, 54, 71, 55, 72])
1216
var count16 = {
13-
strings: ['0A1B2C3D4E5F6G7H'],
14-
buffers: [
15-
hexbuf = new Buffer([
16-
48, 65, 49, 66, 50, 67, 51, 68,
17-
52, 69, 53, 70, 54, 71, 55, 72
18-
]),
19-
new Buffer([
20-
128, 0, 0, 0, 0, 0, 0, 0,
21-
0, 0, 0, 0, 0, 0, 0, 128
22-
])
23-
]
24-
}
17+
strings: ['0A1B2C3D4E5F6G7H'],
18+
buffers: [
19+
hexBuf,
20+
new Buffer([ 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128])
21+
]
22+
}
23+
2524
var empty = {
26-
strings: [''],
27-
buffers: [
28-
new Buffer([
29-
128, 0, 0, 0, 0, 0, 0, 0,
30-
0, 0, 0, 0, 0, 0, 0, 0
31-
])
32-
]
33-
}
34-
var hh = 'abcdefhijklmnopq'
25+
strings: [''],
26+
buffers: [
27+
new Buffer([ 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])
28+
]
29+
}
3530

3631
var multi = {
3732
strings: ['abcd', 'efhijk', 'lmnopq'],
3833
buffers: [
3934
new Buffer('abcdefhijklmnopq', 'ascii'),
40-
new Buffer([
41-
128, 0, 0, 0, 0, 0, 0, 0,
42-
0, 0, 0, 0, 0, 0, 0, 128
43-
])
35+
new Buffer([128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128])
4436
]
4537
}
4638

4739
var long = {
48-
strings: [hex+hex],
49-
buffers: [
50-
hexbuf,
51-
hexbuf,
52-
new Buffer([
53-
128, 0, 0, 0, 0, 0, 0, 0,
54-
0, 0, 0, 0, 0, 0, 1, 0
55-
])
56-
]
57-
}
40+
strings: [hex + hex],
41+
buffers: [
42+
hexBuf,
43+
hexBuf,
44+
new Buffer([128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0])
45+
]
46+
}
5847

59-
function makeTest(name, data) {
48+
function makeTest (name, data) {
6049
tape(name, function (t) {
6150

6251
var h = new Hash(16, 8)
6352
var hash = new Buffer(20)
6453
var n = 2
6554
var expected = data.buffers.slice()
66-
//t.plan(expected.length + 1)
55+
// t.plan(expected.length + 1)
56+
6757
h._update = function (block) {
6858
var e = expected.shift()
59+
6960
console.log('---block---')
7061
console.log(hexpp(block), block.length)
7162
console.log('---e---')
7263
console.log(hexpp(e), block.length)
7364
console.log(block)
7465
equal(t, block, e)
75-
if(n < 0)
66+
67+
if (n < 0) {
7668
throw new Error('expecting only 2 calls to _update')
69+
}
7770

7871
return hash
7972
}
@@ -84,12 +77,10 @@ function makeTest(name, data) {
8477

8578
equal(t, h.digest(), hash)
8679
t.end()
87-
8880
})
8981
}
9082

9183
makeTest('Hash#update 1 in 1', count16)
9284
makeTest('empty Hash#update', empty)
9385
makeTest('Hash#update 1 in 3', multi)
9486
makeTest('Hash#update 2 in 1', long)
95-

test/test.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ var tape = require('tape')
33
var Sha1 = require('../').sha1
44

55
var inputs = [
6-
['', 'ascii'],
7-
['abc', 'ascii'],
8-
['123', 'ascii'],
9-
['123456789abcdef123456789abcdef123456789abcdef123456789abcdef', 'ascii'],
10-
['123456789abcdef123456789abcdef123456789abcdef123456789abc', 'ascii'],
11-
['123456789abcdef123456789abcdef123456789abcdef123456789ab', 'ascii'],
12-
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde', 'ascii'],
13-
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'ascii'],
14-
['foobarbaz', 'ascii']
6+
['', 'ascii'],
7+
['abc', 'ascii'],
8+
['123', 'ascii'],
9+
['123456789abcdef123456789abcdef123456789abcdef123456789abcdef', 'ascii'],
10+
['123456789abcdef123456789abcdef123456789abcdef123456789abc', 'ascii'],
11+
['123456789abcdef123456789abcdef123456789abcdef123456789ab', 'ascii'],
12+
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde', 'ascii'],
13+
['0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'ascii'],
14+
['foobarbaz', 'ascii']
1515
]
1616

1717
tape("hash is the same as node's crypto", function (t) {
18-
1918
inputs.forEach(function (v) {
2019
var a = new Sha1().update(v[0], v[1]).digest('hex')
2120
var e = crypto.createHash('sha1').update(v[0], v[1]).digest('hex')
@@ -24,19 +23,19 @@ tape("hash is the same as node's crypto", function (t) {
2423
})
2524

2625
t.end()
27-
2826
})
2927

3028
tape('call update multiple times', function (t) {
31-
var n = 1
3229
inputs.forEach(function (v) {
3330
var hash = new Sha1()
3431
var _hash = crypto.createHash('sha1')
35-
for(var i = 0; i < v[0].length; i=(i+1)*2) {
36-
var s = v[0].substring(i, (i+1)*2)
32+
33+
for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
34+
var s = v[0].substring(i, (i + 1) * 2)
3735
hash.update(s, v[1])
3836
_hash.update(s, v[1])
3937
}
38+
4039
var a = hash.digest('hex')
4140
var e = _hash.digest('hex')
4241
console.log(a, '==', e)
@@ -45,11 +44,9 @@ tape('call update multiple times', function (t) {
4544
t.end()
4645
})
4746

48-
4947
tape('call update twice', function (t) {
50-
5148
var _hash = crypto.createHash('sha1')
52-
var hash = new Sha1()
49+
var hash = new Sha1()
5350

5451
_hash.update('foo', 'ascii')
5552
hash.update('foo', 'ascii')
@@ -67,22 +64,22 @@ tape('call update twice', function (t) {
6764
t.end()
6865
})
6966

70-
7167
tape('hex encoding', function (t) {
72-
var n = 1
7368
inputs.forEach(function (v) {
7469
var hash = new Sha1()
7570
var _hash = crypto.createHash('sha1')
76-
for(var i = 0; i < v[0].length; i=(i+1)*2) {
77-
var s = v[0].substring(i, (i+1)*2)
71+
72+
for (var i = 0; i < v[0].length; i = (i + 1) * 2) {
73+
var s = v[0].substring(i, (i + 1) * 2)
7874
hash.update(new Buffer(s, 'ascii').toString('hex'), 'hex')
7975
_hash.update(new Buffer(s, 'ascii').toString('hex'), 'hex')
8076
}
8177
var a = hash.digest('hex')
8278
var e = _hash.digest('hex')
79+
8380
console.log(a, '==', e)
8481
t.equal(a, e)
8582
})
83+
8684
t.end()
8785
})
88-

0 commit comments

Comments
 (0)