Debian Codegraph @

test/N3Writer-test.js

raw
1 import {
2 Writer,
3 NamedNode,
4 BlankNode,
5 Literal,
6 Quad,
7 termFromId,
8 } from '../src/';
9 import namespaces from '../src/IRIs';
10
11 const { xsd } = namespaces;
12
13 describe('Writer', () => {
14 describe('The Writer export', () => {
15 it('should be a function', () => {
16 Writer.should.be.a('function');
17 });
18
19 it('should be a Writer constructor', () => {
20 new Writer().should.be.an.instanceof(Writer);
21 });
22 });
23
24 describe('A Writer instance', () => {
25 it('should serialize a single triple', () => {
26 const writer = new Writer();
27 writer.quadToString(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')).should.equal('<a> <b> <c> .\n');
28 });
29
30 it('should serialize a single quad', () => {
31 const writer = new Writer();
32 writer.quadToString(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')).should.equal('<a> <b> <c> <g> .\n');
33 });
34
35 it('should serialize an array of triples', () => {
36 const writer = new Writer();
37 const triples = [new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')),
38 new Quad(new NamedNode('d'), new NamedNode('e'), new NamedNode('f'))];
39 writer.quadsToString(triples).should.equal('<a> <b> <c> .\n<d> <e> <f> .\n');
40 });
41
42
43 it('should serialize 0 triples',
44 shouldSerialize(''));
45
46 it('should serialize 1 triple',
47 shouldSerialize(['abc', 'def', 'ghi'],
48 '<abc> <def> <ghi>.\n'));
49
50 it('should serialize 2 triples',
51 shouldSerialize(['abc', 'def', 'ghi'],
52 ['jkl', 'mno', 'pqr'],
53 '<abc> <def> <ghi>.\n' +
54 '<jkl> <mno> <pqr>.\n'));
55
56 it('should serialize 3 triples',
57 shouldSerialize(['abc', 'def', 'ghi'],
58 ['jkl', 'mno', 'pqr'],
59 ['stu', 'vwx', 'yz'],
60 '<abc> <def> <ghi>.\n' +
61 '<jkl> <mno> <pqr>.\n' +
62 '<stu> <vwx> <yz>.\n'));
63
64 it('should serialize a literal',
65 shouldSerialize(['a', 'b', '"cde"'],
66 '<a> <b> "cde".\n'));
67
68 it('should serialize a literal with a type',
69 shouldSerialize(['a', 'b', '"cde"^^fgh'],
70 '<a> <b> "cde"^^<fgh>.\n'));
71
72 it('should serialize a literal with a language',
73 shouldSerialize(['a', 'b', '"cde"@en-us'],
74 '<a> <b> "cde"@en-us.\n'));
75
76 // e.g. http://vocab.getty.edu/aat/300264727.ttl
77 it('should serialize a literal with an artificial language',
78 shouldSerialize(['a', 'b', '"cde"@qqq-002'],
79 '<a> <b> "cde"@qqq-002.\n'));
80
81 it('should serialize a literal containing a single quote',
82 shouldSerialize(['a', 'b', '"c\'de"'],
83 '<a> <b> "c\'de".\n'));
84
85 it('should serialize a literal containing a double quote',
86 shouldSerialize(['a', 'b', '"c"de"'],
87 '<a> <b> "c\\"de".\n'));
88
89 it('should serialize a literal containing a backspace',
90 shouldSerialize(['a', 'b', '"c\\de"'],
91 '<a> <b> "c\\\\de".\n'));
92
93 it('should serialize a literal containing a tab character',
94 shouldSerialize(['a', 'b', '"c\tde"'],
95 '<a> <b> "c\\tde".\n'));
96
97 it('should serialize a literal containing a newline character',
98 shouldSerialize(['a', 'b', '"c\nde"'],
99 '<a> <b> "c\\nde".\n'));
100
101 it('should serialize a literal containing a cariage return character',
102 shouldSerialize(['a', 'b', '"c\rde"'],
103 '<a> <b> "c\\rde".\n'));
104
105 it('should serialize a literal containing a backspace character',
106 shouldSerialize(['a', 'b', '"c\bde"'],
107 '<a> <b> "c\\bde".\n'));
108
109 it('should serialize a literal containing a form feed character',
110 shouldSerialize(['a', 'b', '"c\fde"'],
111 '<a> <b> "c\\fde".\n'));
112
113 it('should serialize a literal containing a line separator',
114 shouldSerialize(['a', 'b', '"c\u2028de"'],
115 '<a> <b> "c\u2028de".\n'));
116
117 it('should serialize a literal containing a paragraph separator',
118 shouldSerialize(['a', 'b', '"c\u2029de"'],
119 '<a> <b> "c\u2029de".\n'));
120
121 it('should serialize a literal containing special unicode characters',
122 shouldSerialize(['a', 'b', '"c\u0000\u0001"'],
123 '<a> <b> "c\\u0000\\u0001".\n'));
124
125 it('should serialize a true boolean literal',
126 shouldSerialize(['a', 'b', `"true"^^${xsd.boolean}`],
127 '<a> <b> true.\n'));
128
129 it('should serialize a false boolean literal',
130 shouldSerialize(['a', 'b', `"false"^^${xsd.boolean}`],
131 '<a> <b> false.\n'));
132
133 it('should serialize an invalid boolean literal',
134 shouldSerialize(['a', 'b', `"invalid"^^${xsd.boolean}`],
135 `<a> <b> "invalid"^^<${xsd.boolean}>.\n`));
136
137 it('should serialize an integer literal',
138 shouldSerialize(['a', 'b', `"123"^^${xsd.integer}`],
139 '<a> <b> 123.\n'));
140
141 it('should serialize a positive integer literal',
142 shouldSerialize(['a', 'b', `"+123"^^${xsd.integer}`],
143 '<a> <b> +123.\n'));
144
145 it('should serialize a negative integer literal',
146 shouldSerialize(['a', 'b', `"-123"^^${xsd.integer}`],
147 '<a> <b> -123.\n'));
148
149 it('should serialize an invalid integer literal',
150 shouldSerialize(['a', 'b', `"invalid"^^${xsd.integer}`],
151 `<a> <b> "invalid"^^<${xsd.integer}>.\n`));
152
153 it('should serialize a decimal literal',
154 shouldSerialize(['a', 'b', `"123.456"^^${xsd.decimal}`],
155 '<a> <b> 123.456.\n'));
156
157 it('should serialize a positive decimal literal',
158 shouldSerialize(['a', 'b', `"+123.456"^^${xsd.decimal}`],
159 '<a> <b> +123.456.\n'));
160
161 it('should serialize a negative decimal literal',
162 shouldSerialize(['a', 'b', `"-123.456"^^${xsd.decimal}`],
163 '<a> <b> -123.456.\n'));
164
165 it('should serialize an invalid decimal literal',
166 shouldSerialize(['a', 'b', `"invalid"^^${xsd.decimal}`],
167 `<a> <b> "invalid"^^<${xsd.decimal}>.\n`));
168
169 it('should serialize a double literal',
170 shouldSerialize(['a', 'b', `"123.456E10"^^${xsd.double}`],
171 '<a> <b> 123.456E10.\n'));
172
173 it('should serialize a positive double literal',
174 shouldSerialize(['a', 'b', `"+123.456E10"^^${xsd.double}`],
175 '<a> <b> +123.456E10.\n'));
176
177 it('should serialize a negative double literal',
178 shouldSerialize(['a', 'b', `"-123.456E10"^^${xsd.double}`],
179 '<a> <b> -123.456E10.\n'));
180
181 it('should serialize an invalid double literal',
182 shouldSerialize(['a', 'b', `"invalid"^^${xsd.double}`],
183 `<a> <b> "invalid"^^<${xsd.double}>.\n`));
184
185 it('should serialize blank nodes',
186 shouldSerialize(['_:a', 'b', { termType: 'BlankNode', value: 'c' }],
187 '_:a <b> _:c.\n'));
188
189 it('should not leave leading whitespace if the prefix set is empty',
190 shouldSerialize({},
191 ['a', 'b', 'c'],
192 '<a> <b> <c>.\n'));
193
194 it('should serialize prefixes',
195 shouldSerialize({ prefixes: { a: 'http://a.org/', b: new NamedNode('http://a.org/b#'), c: 'http://a.org/c' } },
196 '@prefix a: <http://a.org/>.\n' +
197 '@prefix b: <http://a.org/b#>.\n' +
198 '@prefix c: <http://a.org/c>.\n\n'));
199
200 it('should use prefixes when possible',
201 shouldSerialize({ prefixes: { a: 'http://a.org/', b: 'http://a.org/b#', c: 'http://a.org/b' } },
202 ['http://a.org/bc', 'http://a.org/b#ef', 'http://a.org/bhi'],
203 ['http://a.org/bc/de', 'http://a.org/b#e#f', 'http://a.org/b#x/t'],
204 ['http://a.org/3a', 'http://a.org/b#3a', 'http://a.org/b#a3'],
205 '@prefix a: <http://a.org/>.\n' +
206 '@prefix b: <http://a.org/b#>.\n' +
207 '@prefix c: <http://a.org/b>.\n\n' +
208 'a:bc b:ef a:bhi.\n' +
209 '<http://a.org/bc/de> <http://a.org/b#e#f> <http://a.org/b#x/t>.\n' +
210 '<http://a.org/3a> <http://a.org/b#3a> b:a3.\n'));
211
212 it('should expand prefixes when possible',
213 shouldSerialize({ prefixes: { a: 'http://a.org/', b: 'http://a.org/b#' } },
214 ['a:bc', 'b:ef', 'c:bhi'],
215 '@prefix a: <http://a.org/>.\n' +
216 '@prefix b: <http://a.org/b#>.\n\n' +
217 'a:bc b:ef <c:bhi>.\n'));
218
219 it('should not repeat the same subjects',
220 shouldSerialize(['abc', 'def', 'ghi'],
221 ['abc', 'mno', 'pqr'],
222 ['stu', 'vwx', 'yz'],
223 '<abc> <def> <ghi>;\n' +
224 ' <mno> <pqr>.\n' +
225 '<stu> <vwx> <yz>.\n'));
226
227 it('should not repeat the same predicates',
228 shouldSerialize(['abc', 'def', 'ghi'],
229 ['abc', 'def', 'pqr'],
230 ['abc', 'bef', 'ghi'],
231 ['abc', 'bef', 'pqr'],
232 ['stu', 'bef', 'yz'],
233 '<abc> <def> <ghi>, <pqr>;\n' +
234 ' <bef> <ghi>, <pqr>.\n' +
235 '<stu> <bef> <yz>.\n'));
236
237 it('should write rdf:type as "a"',
238 shouldSerialize(['abc', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'def'],
239 '<abc> a <def>.\n'));
240
241 it('should serialize a graph with 1 triple',
242 shouldSerialize(['abc', 'def', 'ghi', 'xyz'],
243 '<xyz> {\n' +
244 '<abc> <def> <ghi>\n' +
245 '}\n'));
246
247 it('should serialize a graph with 3 triples',
248 shouldSerialize(['abc', 'def', 'ghi', 'xyz'],
249 ['jkl', 'mno', 'pqr', 'xyz'],
250 ['stu', 'vwx', 'yz', 'xyz'],
251 '<xyz> {\n' +
252 '<abc> <def> <ghi>.\n' +
253 '<jkl> <mno> <pqr>.\n' +
254 '<stu> <vwx> <yz>\n' +
255 '}\n'));
256
257 it('should serialize three graphs',
258 shouldSerialize(['abc', 'def', 'ghi', 'xyz'],
259 ['jkl', 'mno', 'pqr', ''],
260 ['stu', 'vwx', 'yz', 'abc'],
261 '<xyz> {\n<abc> <def> <ghi>\n}\n' +
262 '<jkl> <mno> <pqr>.\n' +
263 '<abc> {\n<stu> <vwx> <yz>\n}\n'));
264
265 it('should output 8-bit unicode characters as escape sequences',
266 shouldSerialize(['\ud835\udc00', '\ud835\udc00', '"\ud835\udc00"^^\ud835\udc00', '\ud835\udc00'],
267 '<\\U0001d400> {\n<\\U0001d400> <\\U0001d400> "\\U0001d400"^^<\\U0001d400>\n}\n'));
268
269 it('should not use escape sequences in blank nodes',
270 shouldSerialize(['_:\ud835\udc00', '_:\ud835\udc00', '_:\ud835\udc00', '_:\ud835\udc00'],
271 '_:\ud835\udc00 {\n_:\ud835\udc00 _:\ud835\udc00 _:\ud835\udc00\n}\n'));
272
273 it('calls the done callback when ending the outputstream errors', done => {
274 const writer = new Writer({
275 write: function () {},
276 end: function () { throw new Error('error'); },
277 });
278 writer.end(done);
279 });
280
281 it('sends output through end when no stream argument is given', done => {
282 const writer = new Writer();
283 let notCalled = true;
284 writer.addQuad(new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')), () => { notCalled = false; });
285 writer.end((error, output) => {
286 output.should.equal('<a> <b> <c>.\n');
287 done(notCalled || error);
288 });
289 });
290
291 it('respects the prefixes argument when no stream argument is given', done => {
292 const writer = new Writer({ prefixes: { a: 'b#' } });
293 writer.addQuad(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c')));
294 writer.end((error, output) => {
295 output.should.equal('@prefix a: <b#>.\n\na:a a:b a:c.\n');
296 done(error);
297 });
298 });
299
300 it('ignores an empty prefix list', done => {
301 const writer = new Writer();
302 writer.addPrefixes({});
303 writer.addQuad(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c')));
304 writer.end((error, output) => {
305 output.should.equal('<b#a> <b#b> <b#c>.\n');
306 done(error);
307 });
308 });
309
310 it('should serialize triples of graph with prefix for local names that begin with underscore', done => {
311 const writer = new Writer();
312 writer.addPrefix('a', 'b#');
313 writer.addQuad(new Quad(new NamedNode('b#_a'), new NamedNode('b#b'), new NamedNode('b#c'), new NamedNode('b#g')));
314 writer.end((error, output) => {
315 output.should.equal('@prefix a: <b#>.\n\na:g {\na:_a a:b a:c\n}\n');
316 done(error);
317 });
318 });
319
320 it('serializes triples of a graph with a prefix declaration in between', done => {
321 const writer = new Writer();
322 writer.addQuad(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c')));
323 writer.addPrefix('a', 'b#');
324 writer.addQuad(new Quad(new NamedNode('b#a'), new NamedNode('b#b'), new NamedNode('b#c'), new NamedNode('b#g')));
325 writer.addPrefix('d', 'e#');
326 writer.addQuad({ subject: new NamedNode('b#a'), predicate: new NamedNode('b#b'), object: new NamedNode('b#d'), graph: new NamedNode('b#g') });
327 writer.end((error, output) => {
328 output.should.equal('<b#a> <b#b> <b#c>.\n' +
329 '@prefix a: <b#>.\n\na:g {\na:a a:b a:c\n}\n' +
330 '@prefix d: <e#>.\n\na:g {\na:a a:b a:d\n}\n');
331 done(error);
332 });
333 });
334
335 it('should not write prefixes in N-Triples mode', done => {
336 const writer = new Writer({ format: 'N-Triples', prefixes: { a: 'b#' } });
337 let called = false;
338 function callback() { called = true; }
339 writer.addPrefix('c', 'd#');
340 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new Literal('"c"'));
341 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new Literal(`"1"^^${xsd.integer}`));
342 writer.addPrefix('e', 'f#', callback);
343 writer.end((error, output) => {
344 called.should.be.true;
345 output.should.equal(`<a> <b> "c" .\n<a> <b> "1"^^<${xsd.integer}> .\n`);
346 done(error);
347 });
348 });
349
350 it('uses a base IRI when given', done => {
351 const writer = new Writer({ baseIRI: 'http://example.org/foo/' });
352 writer.addQuad(new Quad(
353 new NamedNode('http://example.org/foo/'),
354 new NamedNode('http://example.org/foo/#b'),
355 new NamedNode('http://example.org/foo/cdeFgh/ijk')));
356 writer.end((error, output) => {
357 output.should.equal('<> <#b> <cdeFgh/ijk>.\n');
358 done(error);
359 });
360 });
361
362 it('does not use partially match base IRIs', done => {
363 const writer = new Writer({ baseIRI: 'https://pod.example/profile/card' });
364 writer.addQuad(new Quad(
365 new NamedNode('https://pod.example/profile/card#me'),
366 new NamedNode('http://www.w3.org/2002/07/owl#sameAs'),
367 new NamedNode('https://pod.example/profile/card-1234.ttl')));
368 writer.end((error, output) => {
369 output.should.equal('<#me> <http://www.w3.org/2002/07/owl#sameAs> <https://pod.example/profile/card-1234.ttl>.\n');
370 done(error);
371 });
372 });
373
374 it('should accept triples with separated components', done => {
375 const writer = new Writer();
376 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'));
377 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'));
378 writer.end((error, output) => {
379 output.should.equal('<a> <b> <c>, <d>.\n');
380 done(error);
381 });
382 });
383
384 it('should accept quads with separated components', done => {
385 const writer = new Writer();
386 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g'));
387 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'), new NamedNode('g'));
388 writer.end((error, output) => {
389 output.should.equal('<g> {\n<a> <b> <c>, <d>\n}\n');
390 done(error);
391 });
392 });
393
394 it('should serialize triples with an empty blank node as object', done => {
395 const writer = new Writer();
396 writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.blank());
397 writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.blank([]));
398 writer.end((error, output) => {
399 output.should.equal('<a1> <b> [].\n' +
400 '<a2> <b> [].\n');
401 done(error);
402 });
403 });
404
405 it('should serialize triples with a one-triple blank node as object', done => {
406 const writer = new Writer();
407 writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.blank(new NamedNode('d'), new NamedNode('e')));
408 writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.blank({ predicate: new NamedNode('d'), object: new NamedNode('e') }));
409 writer.addQuad(new NamedNode('a3'), new NamedNode('b'), writer.blank([{ predicate: new NamedNode('d'), object: new NamedNode('e') }]));
410 writer.end((error, output) => {
411 output.should.equal('<a1> <b> [ <d> <e> ].\n' +
412 '<a2> <b> [ <d> <e> ].\n' +
413 '<a3> <b> [ <d> <e> ].\n');
414 done(error);
415 });
416 });
417
418 it('should serialize triples with a two-triple blank node as object', done => {
419 const writer = new Writer();
420 writer.addQuad(new NamedNode('a'), new NamedNode('b'), writer.blank([
421 { predicate: new NamedNode('d'), object: new NamedNode('e') },
422 { predicate: new NamedNode('f'), object: new Literal('"g"') },
423 ]));
424 writer.end((error, output) => {
425 output.should.equal('<a> <b> [\n' +
426 ' <d> <e>;\n' +
427 ' <f> "g"\n' +
428 '].\n');
429 done(error);
430 });
431 });
432
433 it('should serialize triples with a three-triple blank node as object', done => {
434 const writer = new Writer();
435 writer.addQuad(new NamedNode('a'), new NamedNode('b'), writer.blank([
436 { predicate: new NamedNode('d'), object: new NamedNode('e') },
437 { predicate: new NamedNode('f'), object: new Literal('"g"') },
438 { predicate: new NamedNode('h'), object: new NamedNode('i') },
439 ]));
440 writer.end((error, output) => {
441 output.should.equal('<a> <b> [\n' +
442 ' <d> <e>;\n' +
443 ' <f> "g";\n' +
444 ' <h> <i>\n' +
445 '].\n');
446 done(error);
447 });
448 });
449
450 it('should serialize triples with predicate-sharing blank node triples as object', done => {
451 const writer = new Writer();
452 writer.addQuad(new NamedNode('a'), new NamedNode('b'), writer.blank([
453 { predicate: new NamedNode('d'), object: new NamedNode('e') },
454 { predicate: new NamedNode('d'), object: new NamedNode('f') },
455 { predicate: new NamedNode('g'), object: new NamedNode('h') },
456 { predicate: new NamedNode('g'), object: new NamedNode('i') },
457 ]));
458 writer.end((error, output) => {
459 output.should.equal('<a> <b> [\n' +
460 ' <d> <e>, <f>;\n' +
461 ' <g> <h>, <i>\n' +
462 '].\n');
463 done(error);
464 });
465 });
466
467 it('should serialize triples with nested blank nodes as object', done => {
468 const writer = new Writer();
469 writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.blank([
470 { predicate: new NamedNode('d'), object: writer.blank() },
471 ]));
472 writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.blank([
473 { predicate: new NamedNode('d'), object: writer.blank(new NamedNode('e'), new NamedNode('f')) },
474 { predicate: new NamedNode('g'), object: writer.blank(new NamedNode('h'), new Literal('"i"')) },
475 ]));
476 writer.addQuad(new NamedNode('a3'), new NamedNode('b'), writer.blank([
477 { predicate: new NamedNode('d'), object: writer.blank([
478 { predicate: new NamedNode('g'), object: writer.blank(new NamedNode('h'), new NamedNode('i')) },
479 { predicate: new NamedNode('j'), object: writer.blank(new NamedNode('k'), new Literal('"l"')) },
480 ]) },
481 ]));
482 writer.end((error, output) => {
483 output.should.equal('<a1> <b> [\n' +
484 ' <d> []\n' +
485 '].\n' +
486 '<a2> <b> [\n' +
487 ' <d> [ <e> <f> ];\n' +
488 ' <g> [ <h> "i" ]\n' +
489 '].\n' +
490 '<a3> <b> [\n' +
491 ' <d> [\n' +
492 ' <g> [ <h> <i> ];\n' +
493 ' <j> [ <k> "l" ]\n' +
494 ']\n' +
495 '].\n');
496 done(error);
497 });
498 });
499
500 it('should serialize triples with an empty blank node as subject', done => {
501 const writer = new Writer();
502 writer.addQuad(writer.blank(), new NamedNode('b'), new NamedNode('c'));
503 writer.addQuad(writer.blank([]), new NamedNode('b'), new NamedNode('c'));
504 writer.end((error, output) => {
505 output.should.equal('[] <b> <c>.\n' +
506 '[] <b> <c>.\n');
507 done(error);
508 });
509 });
510
511 it('should serialize triples with a one-triple blank node as subject', done => {
512 const writer = new Writer();
513 writer.addQuad(writer.blank(new NamedNode('a'), new NamedNode('b')), new NamedNode('c'), new NamedNode('d'));
514 writer.addQuad(writer.blank({ predicate: new NamedNode('a'), object: new NamedNode('b') }), new NamedNode('c'), new NamedNode('d'));
515 writer.addQuad(writer.blank([{ predicate: new NamedNode('a'), object: new NamedNode('b') }]), new NamedNode('c'), new NamedNode('d'));
516 writer.end((error, output) => {
517 output.should.equal('[ <a> <b> ] <c> <d>.\n' +
518 '[ <a> <b> ] <c> <d>.\n' +
519 '[ <a> <b> ] <c> <d>.\n');
520 done(error);
521 });
522 });
523
524 it('should serialize triples with an empty blank node as graph', done => {
525 const writer = new Writer();
526 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), writer.blank());
527 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), writer.blank([]));
528 writer.end((error, output) => {
529 output.should.equal('[] {\n<a> <b> <c>\n}\n' +
530 '[] {\n<a> <b> <c>\n}\n');
531 done(error);
532 });
533 });
534
535 it('should serialize triples with an empty list as object', done => {
536 const writer = new Writer();
537 writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.list());
538 writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.list([]));
539 writer.end((error, output) => {
540 output.should.equal('<a1> <b> ().\n' +
541 '<a2> <b> ().\n');
542 done(error);
543 });
544 });
545
546 it('should serialize triples with a one-element list as object', done => {
547 const writer = new Writer();
548 writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.list([new NamedNode('c')]));
549 writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.list([new Literal('"c"')]));
550 writer.end((error, output) => {
551 output.should.equal('<a1> <b> (<c>).\n' +
552 '<a2> <b> ("c").\n');
553 done(error);
554 });
555 });
556
557 it('should serialize triples with a three-element list as object', done => {
558 const writer = new Writer();
559 writer.addQuad(new NamedNode('a1'), new NamedNode('b'), writer.list([new NamedNode('c'), new NamedNode('d'), new NamedNode('e')]));
560 writer.addQuad(new NamedNode('a2'), new NamedNode('b'), writer.list([new Literal('"c"'), new Literal('"d"'), new Literal('"e"')]));
561 writer.end((error, output) => {
562 output.should.equal('<a1> <b> (<c> <d> <e>).\n' +
563 '<a2> <b> ("c" "d" "e").\n');
564 done(error);
565 });
566 });
567
568 it('should serialize triples with an empty list as subject', done => {
569 const writer = new Writer();
570 writer.addQuad(writer.list(), new NamedNode('b1'), new NamedNode('c'));
571 writer.addQuad(writer.list([]), new NamedNode('b2'), new NamedNode('c'));
572 writer.end((error, output) => {
573 output.should.equal('() <b1> <c>.\n' +
574 '() <b2> <c>.\n');
575 done(error);
576 });
577 });
578
579 it('should serialize triples with a one-element list as subject', done => {
580 const writer = new Writer();
581 writer.addQuad(writer.list([new NamedNode('a')]), new NamedNode('b1'), new NamedNode('c'));
582 writer.addQuad(writer.list([new NamedNode('a')]), new NamedNode('b2'), new NamedNode('c'));
583 writer.end((error, output) => {
584 output.should.equal('(<a>) <b1> <c>.\n' +
585 '(<a>) <b2> <c>.\n');
586 done(error);
587 });
588 });
589
590 it('should serialize triples with a three-element list as subject', done => {
591 const writer = new Writer();
592 writer.addQuad(writer.list([new NamedNode('a1'), new Literal('"b"'), new Literal('"c"')]), new NamedNode('d'), new NamedNode('e'));
593 writer.end((error, output) => {
594 output.should.equal('(<a1> "b" "c") <d> <e>.\n');
595 done(error);
596 });
597 });
598
599 it('should serialize subject and object triples passed by options.listHeads', done => {
600 const lists = {
601 l1: [new NamedNode('c'), new NamedNode('d'), new NamedNode('e')],
602 l2: [new Literal('c'), new Literal('d'), new Literal('e')],
603 };
604
605 const writer = new Writer({ lists });
606 writer.addQuad(new BlankNode('l1'), new NamedNode('b'), new BlankNode('l2'));
607 writer.addQuad(new NamedNode('a3'), new NamedNode('b'), new BlankNode('m3'));
608 writer.end((error, output) => {
609 output.should.equal('(<c> <d> <e>) <b> ("c" "d" "e").\n' +
610 '<a3> <b> _:m3.\n');
611 done(error);
612 });
613 });
614
615 it('should accept triples in bulk', done => {
616 const writer = new Writer();
617 writer.addQuads([new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')),
618 new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'))]);
619 writer.end((error, output) => {
620 output.should.equal('<a> <b> <c>, <d>.\n');
621 done(error);
622 });
623 });
624
625 it('should not allow writing after end', done => {
626 const writer = new Writer();
627 writer.addQuad(new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')));
628 writer.end();
629 writer.addQuad(new Quad(new NamedNode('d'), new NamedNode('e'), new NamedNode('f')), error => {
630 error.should.be.an.instanceof(Error);
631 error.should.have.property('message', 'Cannot write because the writer has been closed.');
632 done();
633 });
634 });
635
636 it('should write simple triples in N-Quads mode', done => {
637 const writer = new Writer({ format: 'N-Quads' });
638 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'));
639 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'));
640 writer.end((error, output) => {
641 output.should.equal('<a> <b> <c> .\n<a> <b> <d> .\n');
642 done(error);
643 });
644 });
645
646 it('should write simple quads in N-Quads mode', done => {
647 const writer = new Writer({ format: 'N-Quads' });
648 let called = false;
649 function callback() { called = true; }
650 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), callback);
651 writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('d'), new NamedNode('g'));
652 writer.end((error, output) => {
653 called.should.be.true;
654 output.should.equal('<a> <b> <c> .\n<a> <b> <d> <g> .\n');
655 done(error);
656 });
657 });
658
659 it('should end when the end option is not set', done => {
660 const outputStream = new QuickStream(), writer = new Writer(outputStream, {});
661 outputStream.should.have.property('ended', false);
662 writer.end(() => {
663 outputStream.should.have.property('ended', true);
664 done();
665 });
666 });
667
668 it('should end when the end option is set to true', done => {
669 const outputStream = new QuickStream(), writer = new Writer(outputStream, { end: true });
670 outputStream.should.have.property('ended', false);
671 writer.end(() => {
672 outputStream.should.have.property('ended', true);
673 done();
674 });
675 });
676
677 it('should not end when the end option is set to false', done => {
678 const outputStream = new QuickStream(), writer = new Writer(outputStream, { end: false });
679 outputStream.should.have.property('ended', false);
680 writer.end(() => {
681 outputStream.should.have.property('ended', false);
682 done();
683 });
684 });
685
686 it('should serialize a triple with a triple with mixed component types as subject', () => {
687 const writer = new Writer();
688 writer.quadToString(new Quad(new BlankNode('b1'), new NamedNode('b'), new Literal('l1')), new NamedNode('b'), new NamedNode('c')).should.equal('<<_:b1 <b> "l">> <b> <c> .\n');
689 });
690
691 it('should serialize a triple with a triple with iris as subject', () => {
692 const writer = new Writer();
693 writer.quadToString(new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')), new NamedNode('b'), new NamedNode('c')).should.equal('<<<a> <b> <c>>> <b> <c> .\n');
694 });
695
696 it('should serialize a triple with a triple with blanknodes as subject', () => {
697 const writer = new Writer();
698 writer.quadToString(new Quad(new BlankNode('b1'), new BlankNode('b2'), new BlankNode('b3')), new NamedNode('b'), new NamedNode('c')).should.equal('<<_:b1 _:b2 _:b3>> <b> <c> .\n');
699 });
700
701 it('should serialize a triple with a triple as object', () => {
702 const writer = new Writer();
703 writer.quadToString(new NamedNode('a'), new NamedNode('b'), new Quad(new BlankNode('b1'), new NamedNode('b'), new Literal('l1'))).should.equal('<a> <b> <<_:b1 <b> "l">> .\n');
704 });
705
706 it('should serialize a triple with a triple with iris as object', () => {
707 const writer = new Writer();
708 writer.quadToString(new NamedNode('a'), new NamedNode('b'), new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'))).should.equal('<a> <b> <<<a> <b> <c>>> .\n');
709 });
710
711 it('should serialize a triple with a triple with blanknodes as object', () => {
712 const writer = new Writer();
713 writer.quadToString(new NamedNode('a'), new NamedNode('b'), new Quad(new BlankNode('b1'), new BlankNode('b2'), new BlankNode('b3'))).should.equal('<a> <b> <<_:b1 _:b2 _:b3>> .\n');
714 });
715
716 it('should serialize a quad with a triple with mixed component types as subject', () => {
717 const writer = new Writer();
718 writer.quadToString(new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')).should.equal('<<<a> <b> <c>>> <b> <c> <g> .\n');
719 });
720
721 it('should serialize a quad with a triple as object', () => {
722 const writer = new Writer();
723 writer.quadToString(new NamedNode('a'), new NamedNode('b'), new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c')), new NamedNode('g')).should.equal('<a> <b> <<<a> <b> <c>>> <g> .\n');
724 });
725
726 it('should serialize a quad with a quad as subject', () => {
727 const writer = new Writer();
728 writer.quadToString(new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')).should.equal('<<<a> <b> <c> <g>>> <b> <c> <g> .\n');
729 });
730
731 it('should serialize a quad with a quad as object', () => {
732 const writer = new Writer();
733 writer.quadToString(new NamedNode('a'), new NamedNode('b'), new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')), new NamedNode('g')).should.equal('<a> <b> <<<a> <b> <c> <g>>> <g> .\n');
734 });
735
736 it('should serialize a triple with a quad as subject', () => {
737 const writer = new Writer();
738 writer.quadToString(new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g')), new NamedNode('b'), new NamedNode('c')).should.equal('<<<a> <b> <c> <g>>> <b> <c> .\n');
739 });
740
741 it('should serialize a triple with a quad as object', () => {
742 const writer = new Writer();
743 writer.quadToString(new NamedNode('a'), new NamedNode('b'), new Quad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'), new NamedNode('g'))).should.equal('<a> <b> <<<a> <b> <c> <g>>> .\n');
744 });
745 });
746 });
747
748 function shouldSerialize(/* prefixes?, tripleArrays..., expectedResult */) {
749 const tripleArrays = Array.prototype.slice.call(arguments),
750 expectedResult = tripleArrays.pop(),
751 prefixes = tripleArrays[0] instanceof Array ? null : tripleArrays.shift();
752
753 return function (done) {
754 const outputStream = new QuickStream(),
755 writer = new Writer(outputStream, prefixes);
756 (function next() {
757 const item = tripleArrays.shift();
758 if (item) {
759 const subject = typeof item[0] === 'string' ? termFromId(item[0]) : item[0];
760 const predicate = typeof item[1] === 'string' ? termFromId(item[1]) : item[1];
761 const object = typeof item[2] === 'string' ? termFromId(item[2]) : item[2];
762 const graph = typeof item[3] === 'string' ? termFromId(item[3]) : item[3];
763 writer.addQuad(new Quad(subject, predicate, object, graph), next);
764 }
765 else
766 writer.end(error => {
767 try {
768 outputStream.result.should.equal(expectedResult);
769 outputStream.should.have.property('ended', true);
770 done(error);
771 }
772 catch (e) {
773 done(e);
774 }
775 });
776 })();
777 };
778 }
779
780 function QuickStream() {
781 const stream = { ended: false };
782 let buffer = '';
783 stream.write = function (chunk, encoding, callback) {
784 buffer += chunk;
785 callback && callback();
786 };
787 stream.end = function (callback) {
788 stream.ended = true;
789 stream.result = buffer;
790 buffer = null;
791 callback();
792 };
793 return stream;
794 }