-
Notifications
You must be signed in to change notification settings - Fork 34
/
test.js
85 lines (76 loc) · 1.84 KB
/
test.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var test = require('tape')
var ndj = require('./')
var os = require('os')
var concat = require('concat-stream')
test('.parse', function(t) {
var parser = ndj.parse()
parser.on('data', function(obj) {
t.equal(obj.hello, 'world')
t.end()
})
parser.write('{"hello": "world"}\n')
})
test('.parse twice', function(t) {
var parser = ndj.parse()
parser.once('data', function(obj) {
t.equal(obj.hello, 'world')
parser.once('data', function(obj) {
t.equal(obj.hola, 'mundo')
t.end()
})
})
parser.write('{"hello": "world"}\n{"hola": "mundo"}\n')
})
test('.parse - strict:true error', function (t) {
var parser = ndj.parse({strict: true})
try {
parser.write('{"no":"json"\n')
} catch(e) {
t.pass('error thrown')
t.end()
}
})
test('.parse - strict:true error event', function (t) {
var parser = ndj.parse({strict: true})
parser.on('error', function (err) {
t.pass('error event called')
t.end()
})
try {
parser.write('{"no":"json"\n')
} catch(e) {
t.fail('should not throw')
}
})
test('.parse - strict:false error', function (t) {
var parser = ndj.parse({strict: false})
parser.once('data', function (data) {
t.ok(data.json, 'parse second one')
t.end()
})
try {
parser.write('{"json":false\n{"json":true}\n')
} catch(e) {
t.fail('should not call an error')
}
})
test('.serialize', function(t) {
var serializer = ndj.serialize()
serializer.pipe(concat(function(data) {
t.equal(data, '{"hello":"world"}' + os.EOL)
t.end()
}))
serializer.write({hello: 'world'})
serializer.end()
})
test('.serialize circular', function(t) {
var serializer = ndj.serialize()
serializer.pipe(concat(function(data) {
t.equal(data, '{"obj":"[Circular ~]"}' + os.EOL)
t.end()
}))
var obj = {}
obj.obj = obj
serializer.write(obj)
serializer.end()
})