-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
64 lines (43 loc) · 1.34 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { createReadStream, readFileSync } from 'fs'
import hash, { sync } from './'
import { Readable } from 'stream'
import { AssertionError } from 'assert'
import test from 'ava'
test(`async: it should resolve to given stream's hash`, async t => {
t.plan(1)
try {
const h = await hash(createReadStream('./.gitignore', { autoClose: true, encoding: 'utf8', flags: 'r' }))
t.is(h, '64c52a5')
} catch (err) {
t.fail(err)
}
})
test(`async: it should resolve to an error if the given stream errors out`, async t => {
t.plan(1)
const s = new Readable
try {
await hash(s)
t.fail()
} catch (err) {
t.pass()
}
s.emit('error', new Error)
})
test(`async: it should throw an AssertionError when passed a non-stream`, t => {
[42, 'foo', new Buffer(1), [], {}, () => { }].forEach(_ => {
t.throws(() => hash(_ as any), AssertionError)
})
})
test(`sync: it should return the given buffer's hash`, t => {
const h = sync(readFileSync('./.gitignore'))
t.is(h, '64c52a5')
})
test(`sync: it should return the given string's hash`, t => {
const h = sync(readFileSync('./.gitignore').toString())
t.is(h, '64c52a5')
})
test(`sync: it should throw an AssertionError when passed a non-buffer and non-string`, t => {
[42, [], {}, () => { }].forEach(_ => {
t.throws(() => sync(_ as any), AssertionError)
})
})