Debian Codegraph @

test/Term-test.js

raw
1 import {
2 Term,
3 NamedNode,
4 BlankNode,
5 Literal,
6 Variable,
7 DefaultGraph,
8 Quad,
9 termToId,
10 termFromId,
11 } from '../src/';
12
13 import {
14 escapeQuotes,
15 unescapeQuotes,
16 } from '../src/N3DataFactory';
17
18 describe('Term', () => {
19 describe('The Term module', () => {
20 it('should be a function', () => {
21 Term.should.be.a('function');
22 });
23
24 it('should be a Term constructor', () => {
25 new Term().should.be.an.instanceof(Term);
26 });
27 });
28
29 describe('A Term instance', () => {
30 it('has an integer hashCode', () => {
31 new Term().hashCode().should.equal(0);
32 });
33 });
34
35 describe('termFromId', () => {
36 it('should create a DefaultGraph from a falsy value', () => {
37 termFromId(null).toJSON().should.deep.equal({
38 termType: 'DefaultGraph',
39 value: '',
40 });
41 });
42
43 it('should create a DefaultGraph from the empty string', () => {
44 termFromId('').toJSON().should.deep.equal({
45 termType: 'DefaultGraph',
46 value: '',
47 });
48 });
49
50 it('should create a NamedNode from an IRI', () => {
51 termFromId('http://example.org/foo#bar').toJSON().should.deep.equal({
52 termType: 'NamedNode',
53 value: 'http://example.org/foo#bar',
54 });
55 });
56
57 it('should create a BlankNode from a string that starts with an underscore', () => {
58 termFromId('_:b1').toJSON().should.deep.equal({
59 termType: 'BlankNode',
60 value: 'b1',
61 });
62 });
63
64 it('should create a Variable from a string that starts with a question mark', () => {
65 termFromId('?v1').toJSON().should.deep.equal({
66 termType: 'Variable',
67 value: 'v1',
68 });
69 });
70
71 it('should create a Literal from a string that starts with a quotation mark', () => {
72 termFromId('"abc"@en-us').toJSON().should.deep.equal({
73 termType: 'Literal',
74 value: 'abc',
75 language: 'en-us',
76 datatype: {
77 termType: 'NamedNode',
78 value: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString',
79 },
80 });
81 });
82
83 it('should create a Quad with the default graph if the id doesnt specify the graph', () => {
84 termFromId('<<http://ex.org/a http://ex.org/b "abc"@en-us>>').should.deep.equal(new Quad(
85 new NamedNode('http://ex.org/a'),
86 new NamedNode('http://ex.org/b'),
87 new Literal('"abc"@en-us'),
88 new DefaultGraph()
89 ));
90 });
91
92 it('should create a Quad with the correct graph if the id specifies a graph', () => {
93 const id = '<<http://ex.org/a http://ex.org/b "abc"@en-us http://ex.org/d>>';
94 termFromId(id).should.deep.equal(new Quad(
95 new NamedNode('http://ex.org/a'),
96 new NamedNode('http://ex.org/b'),
97 new Literal('"abc"@en-us'),
98 new NamedNode('http://ex.org/d')
99 ));
100 });
101
102 it('should create a Quad correctly', () => {
103 const id = '<<http://ex.org/a http://ex.org/b http://ex.org/c>>';
104 termFromId(id).should.deep.equal(new Quad(
105 new NamedNode('http://ex.org/a'),
106 new NamedNode('http://ex.org/b'),
107 new NamedNode('http://ex.org/c'),
108 new DefaultGraph()
109 ));
110 });
111
112 it('should create a Quad correctly', () => {
113 const id = '<<_:n3-123 ?var-a ?var-b _:n3-000>>';
114 termFromId(id).should.deep.equal(new Quad(
115 new BlankNode('n3-123'),
116 new Variable('var-a'),
117 new Variable('var-b'),
118 new BlankNode('n3-000')
119 ));
120 });
121
122 it('should create a Quad correctly', () => {
123 const id = '<<?var-a ?var-b "abc"@en-us ?var-d>>';
124 termFromId(id).should.deep.equal(new Quad(
125 new Variable('var-a'),
126 new Variable('var-b'),
127 new Literal('"abc"@en-us'),
128 new Variable('var-d')
129 ));
130 });
131
132 it('should create a Quad correctly', () => {
133 const id = '<<_:n3-000 ?var-b _:n3-123 http://ex.org/d>>';
134 termFromId(id).should.deep.equal(new Quad(
135 new BlankNode('n3-000'),
136 new Variable('var-b'),
137 new BlankNode('n3-123'),
138 new NamedNode('http://ex.org/d')
139 ));
140 });
141
142 it('should create a Quad correctly from literal containing escaped quotes', () => {
143 const id = '<<_:n3-000 ?var-b "Hello ""W""orl""d!"@en-us http://ex.org/d>>';
144 termFromId(id).should.deep.equal(new Quad(
145 new BlankNode('n3-000'),
146 new Variable('var-b'),
147 new Literal('"Hello "W"orl"d!"@en-us'),
148 new NamedNode('http://ex.org/d')
149 ));
150 });
151
152 it('should create a Quad correctly from literal containing escaped quotes', () => {
153 const id = '<<"Hello ""W""orl""d!"@en-us http://ex.org/b http://ex.org/c>>';
154 termFromId(id).should.deep.equal(new Quad(
155 new Literal('"Hello "W"orl"d!"@en-us'),
156 new NamedNode('http://ex.org/b'),
157 new NamedNode('http://ex.org/c'),
158 new DefaultGraph()
159 ));
160 });
161
162 describe('with a custom factory', () => {
163 const factory = {
164 defaultGraph: function () { return ['d']; },
165 namedNode: function (n) { return ['n', n]; },
166 blankNode: function (b) { return ['b', b]; },
167 variable: function (v) { return ['v', v]; },
168 literal: function (l, m) { return ['l', l, m]; },
169 };
170
171 it('should create a DefaultGraph from a falsy value', () => {
172 termFromId(null, factory).should.deep.equal(['d']);
173 });
174
175 it('should create a DefaultGraph from the empty string', () => {
176 termFromId('', factory).should.deep.equal(['d']);
177 });
178
179 it('should create a NamedNode from an IRI', () => {
180 termFromId('http://example.org/foo#bar', factory).should.deep.equal(['n', 'http://example.org/foo#bar']);
181 });
182
183 it('should create a BlankNode from a string that starts with an underscore', () => {
184 termFromId('_:b1', factory).should.deep.equal(['b', 'b1']);
185 });
186
187 it('should create a Variable from a string that starts with a question mark', () => {
188 termFromId('?v1', factory).should.deep.equal(['v', 'v1']);
189 });
190
191 it('should create a Literal without language or datatype', () => {
192 termFromId('"abc"', factory).should.deep.equal(['l', 'abc', undefined]);
193 });
194
195 it('should create a Literal with a language', () => {
196 termFromId('"abc"@en-us', factory).should.deep.equal(['l', 'abc', 'en-us']);
197 });
198
199 it('should create a Literal with a datatype', () => {
200 termFromId('"abc"^^https://ex.org/type', factory).should.deep.equal(['l', 'abc', ['n', 'https://ex.org/type']]);
201 });
202 });
203 });
204
205 describe('termToId', () => {
206 it('should create the empty string a falsy value', () => {
207 termToId(null).should.equal('');
208 termToId(false).should.equal('');
209 termToId('').should.equal('');
210 });
211
212 it('should create the empty string from the DefaultGraph', () => {
213 termToId(new DefaultGraph()).should.equal('');
214 termToId(new DefaultGraph().toJSON()).should.equal('');
215 });
216
217 it('should create an id that starts with a question mark from a Variable', () => {
218 termToId(new Variable('abc')).should.equal('?abc');
219 termToId(new Variable('abc').toJSON()).should.equal('?abc');
220 });
221
222 it('should create an id that starts with a question mark from a Variable string', () => {
223 termToId('?abc').should.equal('?abc');
224 });
225
226 it('should create an id that starts with a quotation mark from a Literal', () => {
227 termToId(new Literal('"abc"')).should.equal('"abc"');
228 termToId(new Literal('"abc"').toJSON()).should.equal('"abc"');
229 });
230
231 it('should create an id that starts with a quotation mark from a Literal string', () => {
232 termToId('"abc"').should.equal('"abc"');
233 });
234
235 it('should create an id that starts with a quotation mark and datatype from a Literal with a datatype', () => {
236 termToId(new Literal('"abc"^^http://example.org')).should.equal('"abc"^^http://example.org');
237 termToId(new Literal('"abc"^^http://example.org').toJSON()).should.equal('"abc"^^http://example.org');
238 });
239
240 it('should create an id that starts with a quotation mark and datatype from a Literal string with a datatype', () => {
241 termToId('"abc"^^http://example.org').should.equal('"abc"^^http://example.org');
242 });
243
244 it('should create an id that starts with a quotation mark and language tag from a Literal with a language', () => {
245 termToId(new Literal('"abc"@en-us')).should.equal('"abc"@en-us');
246 termToId(new Literal('"abc"@en-us').toJSON()).should.equal('"abc"@en-us');
247 });
248
249 it('should create an id that starts with a quotation mark and language tag from a Literal string with a language', () => {
250 termToId('"abc"@en-us').should.equal('"abc"@en-us');
251 });
252
253 it('should create an id that starts with a quotation mark, datatype and language tag from a Literal with a datatype and language', () => {
254 termToId(new Literal('"abc"^^http://example.org@en-us')).should.equal('"abc"^^http://example.org@en-us');
255 termToId(new Literal('"abc"^^http://example.org@en-us').toJSON()).should.equal('"abc"^^http://example.org@en-us');
256 });
257
258 it('should create an id that starts with a quotation mark, datatype and language tag from a Literal string with a datatype and language', () => {
259 termToId('"abc"^^http://example.org@en-us').should.equal('"abc"^^http://example.org@en-us');
260 });
261
262 it('should create an id that starts with an underscore from a BlankNode', () => {
263 termToId(new BlankNode('abc')).should.equal('_:abc');
264 termToId(new BlankNode('abc').toJSON()).should.equal('_:abc');
265 });
266
267 it('should create an id that starts with an underscore from a BlankNode string', () => {
268 termToId('_:abc').should.equal('_:abc');
269 });
270
271 it('should create an IRI from a NamedNode', () => {
272 termToId(new NamedNode('http://example.org/')).should.equal('http://example.org/');
273 termToId(new NamedNode('http://example.org/').toJSON()).should.equal('http://example.org/');
274 });
275
276 it('should create an IRI from a NamedNode string', () => {
277 termToId('http://example.org/').should.equal('http://example.org/');
278 });
279
280 it('should create an id without graph if default graph is used', () => {
281 termToId(new Quad(
282 new NamedNode('http://ex.org/a'),
283 new NamedNode('http://ex.org/b'),
284 new Literal('"abc"@en-us'),
285 new DefaultGraph()
286 )).should.equal('<<http://ex.org/a http://ex.org/b "abc"@en-us>>');
287 });
288
289 it('should create an id from a Quad', () => {
290 termToId(new Quad(
291 new NamedNode('http://ex.org/a'),
292 new NamedNode('http://ex.org/b'),
293 new Literal('"abc"@en-us'),
294 new NamedNode('http://ex.org/d')
295 )).should.equal('<<http://ex.org/a http://ex.org/b "abc"@en-us http://ex.org/d>>');
296 });
297
298 it('should create an id from a manually created Quad', () => {
299 termToId({
300 subject: new NamedNode('http://ex.org/a'),
301 predicate: new NamedNode('http://ex.org/b'),
302 object: new Literal('"abc"@en-us'),
303 graph: new NamedNode('http://ex.org/d'),
304 termType: 'Quad',
305 value: '',
306 }).should.equal('<<http://ex.org/a http://ex.org/b "abc"@en-us http://ex.org/d>>');
307 });
308
309 it('should create an id with escaped literals from a Quad', () => {
310 termToId(new Quad(
311 new BlankNode('n3-000'),
312 new Variable('var-b'),
313 new Literal('"Hello "W"orl"d!"@en-us'),
314 new NamedNode('http://ex.org/d')
315 )).should.equal('<<_:n3-000 ?var-b "Hello ""W""orl""d!"@en-us http://ex.org/d>>');
316 });
317
318 it('should create an id without graph from a Quad with default graph and Quad as subject', () => {
319 termToId(new Quad(
320 new Quad(
321 new BlankNode('n3-000'),
322 new Variable('var-b'),
323 new Literal('"abc"@en-us'),
324 new NamedNode('http://ex.org/d')
325 ),
326 new NamedNode('http://ex.org/b'),
327 new Literal('"abc"@en-us'),
328 new DefaultGraph()
329 )).should.equal('<<<<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> http://ex.org/b "abc"@en-us>>');
330 });
331
332 it('should create an id without graph from a Quad with default graph and Quad as object', () => {
333 termToId(new Quad(
334 new Literal('"abc"@en-us'),
335 new NamedNode('http://ex.org/b'),
336 new Quad(
337 new BlankNode('n3-000'),
338 new Variable('var-b'),
339 new Literal('"abc"@en-us'),
340 new NamedNode('http://ex.org/d')
341 ),
342 new DefaultGraph()
343 )).should.equal('<<"abc"@en-us http://ex.org/b <<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>>>>');
344 });
345
346 it('should create an id without graph from a Quad with default graph and Quad as subject and object', () => {
347 termToId(new Quad(
348 new Quad(
349 new BlankNode('n3-000'),
350 new Variable('var-b'),
351 new Literal('"abc"@en-us'),
352 new NamedNode('http://ex.org/d')
353 ),
354 new NamedNode('http://ex.org/b'),
355 new Quad(
356 new BlankNode('n3-000'),
357 new Variable('var-b'),
358 new Literal('"abc"@en-us'),
359 new NamedNode('http://ex.org/d')
360 ),
361 new DefaultGraph()
362 )).should.equal('<<<<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> http://ex.org/b <<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>>>>');
363 });
364
365 it('should create an id without graph from a Quad with Quad as subject', () => {
366 termToId(new Quad(
367 new Quad(
368 new BlankNode('n3-000'),
369 new Variable('var-b'),
370 new Literal('"abc"@en-us'),
371 new NamedNode('http://ex.org/d')
372 ),
373 new NamedNode('http://ex.org/b'),
374 new Literal('"abc"@en-us'),
375 new NamedNode('http://ex.org/d')
376 )).should.equal('<<<<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> http://ex.org/b "abc"@en-us http://ex.org/d>>');
377 });
378
379 it('should create an id without graph from a Quad with Quad as object', () => {
380 termToId(new Quad(
381 new Literal('"abc"@en-us'),
382 new NamedNode('http://ex.org/b'),
383 new Quad(
384 new BlankNode('n3-000'),
385 new Variable('var-b'),
386 new Literal('"abc"@en-us'),
387 new NamedNode('http://ex.org/d')
388 ),
389 new NamedNode('http://ex.org/d')
390 )).should.equal('<<"abc"@en-us http://ex.org/b <<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> http://ex.org/d>>');
391 });
392
393 it('should create an id from a Quad with Quad as subject and object', () => {
394 termToId(new Quad(
395 new Quad(
396 new BlankNode('n3-000'),
397 new Variable('var-b'),
398 new Literal('"abc"@en-us'),
399 new NamedNode('http://ex.org/d')
400 ),
401 new NamedNode('http://ex.org/b'),
402 new Quad(
403 new BlankNode('n3-000'),
404 new Variable('var-b'),
405 new Literal('"abc"@en-us'),
406 new NamedNode('http://ex.org/d')
407 ),
408 new NamedNode('http://ex.org/d')
409 )).should.equal('<<<<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> http://ex.org/b <<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> http://ex.org/d>>');
410 });
411
412 it('should escape literals in nested Quads', () => {
413 termToId(new Quad(
414 new Quad(
415 new BlankNode('n3-000'),
416 new Variable('var-b'),
417 new Literal('"Hello "W"orl"d!"@en-us'),
418 new NamedNode('http://ex.org/d')
419 ),
420 new NamedNode('http://ex.org/b'),
421 new Quad(
422 new BlankNode('n3-000'),
423 new Variable('var-b'),
424 new Literal('"Hello "W"orl"d!"@en-us'),
425 new NamedNode('http://ex.org/d')
426 ),
427 new DefaultGraph()
428 )).should.equal('<<<<_:n3-000 ?var-b "Hello ""W""orl""d!"@en-us http://ex.org/d>> http://ex.org/b <<_:n3-000 ?var-b "Hello ""W""orl""d!"@en-us http://ex.org/d>>>>');
429 });
430
431 it('should correctly handle deeply nested quads', () => {
432 termToId(new Quad(
433 new Quad(
434 new Quad(
435 new Quad(
436 new BlankNode('n3-000'),
437 new Variable('var-b'),
438 new Literal('"abc"@en-us'),
439 new NamedNode('http://ex.org/d')
440 ),
441 new Variable('var-b'),
442 new Quad(
443 new BlankNode('n3-000'),
444 new Variable('var-b'),
445 new Literal('"abc"@en-us'),
446 new NamedNode('http://ex.org/d')
447 ),
448 new NamedNode('http://ex.org/d')
449 ),
450 new Variable('var-b'),
451 new Quad(
452 new BlankNode('n3-000'),
453 new Variable('var-b'),
454 new Literal('"abc"@en-us'),
455 new NamedNode('http://ex.org/d')
456 ),
457 new NamedNode('http://ex.org/d')
458 ),
459 new NamedNode('http://ex.org/b'),
460 new Quad(
461 new Quad(
462 new BlankNode('n3-000'),
463 new Variable('var-b'),
464 new Literal('"abc"@en-us'),
465 new NamedNode('http://ex.org/d')
466 ),
467 new Variable('var-b'),
468 new Quad(
469 new BlankNode('n3-000'),
470 new Variable('var-b'),
471 new Literal('"abc"@en-us'),
472 new NamedNode('http://ex.org/d')
473 ),
474 new NamedNode('http://ex.org/d')
475 ),
476 new NamedNode('http://ex.org/d')
477 )).should.equal('<<<<<<<<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> ?var-b <<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> http://ex.org/d>> ?var-b <<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> http://ex.org/d>> http://ex.org/b <<<<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> ?var-b <<_:n3-000 ?var-b "abc"@en-us http://ex.org/d>> http://ex.org/d>> http://ex.org/d>>');
478 });
479
480 it('should throw on an unknown type', () => {
481 (function () { termToId({ termType: 'unknown' }); })
482 .should.throw('Unexpected termType: unknown');
483 });
484 });
485
486 describe('escaping', () => {
487 it('should unescape an escaped string correctly', () => {
488 const id = '"Hello ""World"""@en-us';
489 unescapeQuotes(id).should.equal('"Hello "World""@en-us');
490 });
491
492 it('should escape an unescaped string correctly', () => {
493 const id = '"Hello "World""@en-us';
494 escapeQuotes(id).should.equal('"Hello ""World"""@en-us');
495 });
496
497 it('should not change an unescaped string', () => {
498 const id = '"Hello "World""@en-us';
499 unescapeQuotes(id).should.equal(id);
500 });
501
502 it('should not change a string without quotes', () => {
503 const id = '"Hello World"@en-us';
504 escapeQuotes(id).should.equal(id);
505 });
506
507 it('should not change a blank node', () => {
508 const id = '_:b1';
509 escapeQuotes(id).should.equal(id);
510 });
511
512 it('should not change a variable', () => {
513 const id = '?v1';
514 escapeQuotes(id).should.equal(id);
515 });
516
517 it('should not change the empty string', () => {
518 const id = '';
519 escapeQuotes(id).should.equal(id);
520 });
521 });
522 });