/
api.events.coffee
81 lines (74 loc) · 1.86 KB
/
api.events.coffee
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
import { parse } from '../lib/index.js'
import { assert_error } from './api.assert_error.coffee'
describe 'API events', ->
it 'emit readable event', (next) ->
records = []
parser = parse()
parser.on 'readable', ->
while record = this.read()
records.push record
parser.write """
"ABC","45"
"DEF","23"
"""
parser.on 'end', ->
records.should.eql [
[ 'ABC', '45' ]
[ 'DEF', '23' ]
]
next()
parser.end()
it 'emit data event', (next) ->
records = []
parser = parse()
parser.on 'data', (record) ->
records.push record
parser.write """
"ABC","45"
"DEF","23"
"""
parser.on 'end', ->
records.should.eql [
[ 'ABC', '45' ]
[ 'DEF', '23' ]
]
next()
parser.end()
it 'ensure error in _transform is called once', (next) ->
data = '''
x " a b",x " c d"
x " e f", x " g h"
'''
parser = parse (err) ->
assert_error err,
message: 'Invalid Opening Quote: a quote is found on field 0 at line 1, value is " x "'
code: 'INVALID_OPENING_QUOTE'
field: ' x '
next()
parser.write chr for chr in data
parser.end()
it 'emit error', (next) ->
parser = parse()
parser.on 'readable', ->
while @read() then true
parser.on 'end', ->
next Error 'End should not be fired'
parser.on 'error', (err) ->
err.message.should.eql 'Invalid Record Length: expect 3, got 2 on line 2'
next()
parser.write """
a,a,a
b,b
"""
parser.end()
it 'emit error with data as argument', (next) ->
parser = parse """
a,a,a
b,b
c,c,c
"""
parser.on 'end', ->
next Error 'End should not be fired'
parser.on 'error', (err) ->
err.message.should.eql 'Invalid Record Length: expect 3, got 2 on line 2'
next()