line-stream.test.js
2.5 KB
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
import {LineStream} from '../src';
import QUnit from 'qunit';
QUnit.module('LineStream', {
beforeEach() {
this.lineStream = new LineStream();
}
});
QUnit.test('empty inputs produce no tokens', function(assert) {
let data = false;
this.lineStream.on('data', function() {
data = true;
});
this.lineStream.push('');
assert.ok(!data, 'no tokens were produced');
});
QUnit.test('splits on newlines', function(assert) {
const lines = [];
this.lineStream.on('data', function(line) {
lines.push(line);
});
this.lineStream.push('#EXTM3U\nmovie.ts\n');
assert.strictEqual(2, lines.length, 'two lines are ready');
assert.strictEqual('#EXTM3U', lines.shift(), 'the first line is the first token');
assert.strictEqual('movie.ts', lines.shift(), 'the second line is the second token');
});
QUnit.test('empty lines become empty strings', function(assert) {
const lines = [];
this.lineStream.on('data', function(line) {
lines.push(line);
});
this.lineStream.push('\n\n');
assert.strictEqual(2, lines.length, 'two lines are ready');
assert.strictEqual('', lines.shift(), 'the first line is empty');
assert.strictEqual('', lines.shift(), 'the second line is empty');
});
QUnit.test('handles lines broken across appends', function(assert) {
const lines = [];
this.lineStream.on('data', function(line) {
lines.push(line);
});
this.lineStream.push('#EXTM');
assert.strictEqual(0, lines.length, 'no lines are ready');
this.lineStream.push('3U\nmovie.ts\n');
assert.strictEqual(2, lines.length, 'two lines are ready');
assert.strictEqual('#EXTM3U', lines.shift(), 'the first line is the first token');
assert.strictEqual('movie.ts', lines.shift(), 'the second line is the second token');
});
QUnit.test('stops sending events after deregistering', function(assert) {
const temporaryLines = [];
const temporary = function(line) {
temporaryLines.push(line);
};
const permanentLines = [];
const permanent = function(line) {
permanentLines.push(line);
};
this.lineStream.on('data', temporary);
this.lineStream.on('data', permanent);
this.lineStream.push('line one\n');
assert.strictEqual(
temporaryLines.length,
permanentLines.length,
'both callbacks receive the event'
);
assert.ok(this.lineStream.off('data', temporary), 'a listener was removed');
this.lineStream.push('line two\n');
assert.strictEqual(1, temporaryLines.length, 'no new events are received');
assert.strictEqual(2, permanentLines.length, 'new events are still received');
});