Debian Codegraph @

test/N3StreamParser-test.js

raw
1 import { StreamParser, NamedNode } from '../src/';
2 import { Readable, Writable } from 'readable-stream';
3
4 describe('StreamParser', () => {
5 describe('The StreamParser export', () => {
6 it('should be a function', () => {
7 StreamParser.should.be.a('function');
8 });
9
10 it('should be a StreamParser constructor', () => {
11 new StreamParser().should.be.an.instanceof(StreamParser);
12 });
13 });
14
15 describe('A StreamParser instance', () => {
16 it('parses the empty stream', shouldParse([], 0));
17
18 it('parses the zero-length stream', shouldParse([''], 0));
19
20 it('parses the Bom starting stream', shouldParse(['\ufeff'], 0));
21
22 it('parses the Bom starting stream when first chunk ""', shouldParse(['', '\ufeff'], 0));
23
24 it('parses one triple', shouldParse(['<a> <b> <c>.'], 1));
25
26 it('parses two triples', shouldParse(['<a> <b>', ' <c>. <d> <e> ', '<f>.'], 2));
27
28 it('should parse decimals that are split across chunks in the stream',
29 shouldParse('<sub> <pred> 11.2 .'.match(/.{1,2}/g), 1));
30
31 it('should parse non-breaking spaces that are split across chunks in the stream correctly', done => {
32 const buffer = Buffer.from('<sub> <pred> " " .'),
33 chunks = [buffer, buffer.slice(0, 15), buffer.slice(15, buffer.length)];
34 shouldParse(chunks, 2, triples => {
35 triples[0].should.deep.equal(triples[1]);
36 })(done);
37 });
38
39 it("doesn't parse an invalid stream",
40 shouldNotParse(['z.'], 'Unexpected "z." on line 1.'), { token: undefined, line: 1, previousToken: undefined });
41
42 it('Should Not parse Bom in middle stream',
43 shouldNotParse(['<a> <b>', '\ufeff', '<c>.'], 'Unexpected "" on line 1.'));
44
45 it('emits "prefix" events',
46 shouldEmitPrefixes(['@prefix a: <http://a.org/#>. a:a a:b a:c. @prefix b: <http://b.org/#>.'],
47 { a: new NamedNode('http://a.org/#'), b: new NamedNode('http://b.org/#') }));
48
49 it('passes an error', () => {
50 const input = new Readable(), parser = new StreamParser();
51 let error = null;
52 input._read = function () {};
53 parser.on('error', e => { error = e; });
54 parser.import(input);
55 input.emit('error', new Error());
56 expect(error).to.be.an.instanceof(Error);
57 });
58 });
59 });
60
61
62 function shouldParse(chunks, expectedLength, validateTriples) {
63 return function (done) {
64 const triples = [],
65 inputStream = new ArrayReader(chunks),
66 parser = new StreamParser(),
67 outputStream = new ArrayWriter(triples);
68 parser.import(inputStream).should.equal(parser);
69 parser.pipe(outputStream);
70 parser.on('error', done);
71 parser.on('end', () => {
72 triples.should.have.length(expectedLength);
73 if (validateTriples) validateTriples(triples);
74 done();
75 });
76 };
77 }
78
79 function shouldNotParse(chunks, expectedMessage, expectedContext) {
80 return function (done) {
81 const inputStream = new ArrayReader(chunks),
82 parser = new StreamParser(),
83 outputStream = new ArrayWriter([]);
84 inputStream.pipe(parser);
85 parser.pipe(outputStream);
86 parser.on('error', error => {
87 error.should.be.an.instanceof(Error);
88 error.message.should.equal(expectedMessage);
89 if (expectedContext) error.context.should.deep.equal(expectedContext);
90 done();
91 });
92 };
93 }
94
95 function shouldEmitPrefixes(chunks, expectedPrefixes) {
96 return function (done) {
97 const prefixes = {},
98 parser = new StreamParser(),
99 inputStream = new ArrayReader(chunks);
100 inputStream.pipe(parser);
101 parser.on('data', () => {});
102 parser.on('prefix', (prefix, iri) => { prefixes[prefix] = iri; });
103 parser.on('error', done);
104 parser.on('end', error => {
105 prefixes.should.deep.equal(expectedPrefixes);
106 done(error);
107 });
108 };
109 }
110
111 function ArrayReader(items) {
112 const reader = new Readable();
113 reader._read = function () { this.push(items.shift() || null); };
114 return reader;
115 }
116
117 function ArrayWriter(items) {
118 const writer = new Writable({ objectMode: true });
119 writer._write = function (chunk, encoding, done) { items.push(chunk); done(); };
120 return writer;
121 }