Debian Codegraph @

test/N3Util-test.js

raw
1 import { Util, DataFactory } from '../src/';
2
3 const { namedNode, blankNode, literal, variable, defaultGraph, quad } = DataFactory;
4
5 describe('Util', () => {
6 describe('isNamedNode', () => {
7 it('matches an IRI', () => {
8 Util.isNamedNode(namedNode('http://example.org/')).should.be.true;
9 });
10
11 it('matches an empty IRI', () => {
12 Util.isNamedNode(namedNode('')).should.be.true;
13 });
14
15 it('does not match a literal', () => {
16 Util.isNamedNode(literal('http://example.org/')).should.be.false;
17 });
18
19 it('does not match a blank node', () => {
20 Util.isNamedNode(blankNode('x')).should.be.false;
21 });
22
23 it('does not match a variable', () => {
24 Util.isNamedNode(variable('x')).should.be.false;
25 });
26
27 it('does not match null', () => {
28 expect(Util.isNamedNode(null)).to.be.false;
29 });
30
31 it('does not match undefined', () => {
32 expect(Util.isNamedNode(undefined)).to.be.false;
33 });
34 });
35
36 describe('isLiteral', () => {
37 it('matches a literal', () => {
38 Util.isLiteral(literal('http://example.org/')).should.be.true;
39 });
40
41 it('matches a literal with a language', () => {
42 Util.isLiteral(literal('English', 'en')).should.be.true;
43 });
44
45 it('matches a literal with a language that contains a number', () => {
46 Util.isLiteral(literal('English', '@es-419')).should.be.true;
47 });
48
49 it('matches a literal with a type', () => {
50 Util.isLiteral(literal('3', 'http://www.w3.org/2001/XMLSchema#integer')).should.be.true;
51 });
52
53 it('matches a literal with a newline', () => {
54 Util.isLiteral(literal('a\nb')).should.be.true;
55 });
56
57 it('matches a literal with a cariage return', () => {
58 Util.isLiteral(literal('a\rb')).should.be.true;
59 });
60
61 it('does not match an IRI', () => {
62 Util.isLiteral(namedNode('http://example.org/')).should.be.false;
63 });
64
65 it('does not match a blank node', () => {
66 Util.isLiteral(blankNode('_:x')).should.be.false;
67 });
68
69 it('does not match a variable', () => {
70 Util.isLiteral(variable('x')).should.be.false;
71 });
72
73 it('does not match null', () => {
74 expect(Util.isLiteral(null)).to.be.false;
75 });
76
77 it('does not match undefined', () => {
78 expect(Util.isLiteral(undefined)).to.be.false;
79 });
80 });
81
82 describe('isBlankNode', () => {
83 it('matches a blank node', () => {
84 Util.isBlankNode(blankNode('x')).should.be.true;
85 });
86
87 it('does not match an IRI', () => {
88 Util.isBlankNode(namedNode('http://example.org/')).should.be.false;
89 });
90
91 it('does not match a literal', () => {
92 Util.isBlankNode(literal('http://example.org/')).should.be.false;
93 });
94
95 it('does not match a variable', () => {
96 Util.isBlankNode(variable('x')).should.be.false;
97 });
98
99 it('does not match null', () => {
100 expect(Util.isBlankNode(null)).to.be.false;
101 });
102
103 it('does not match undefined', () => {
104 expect(Util.isBlankNode(undefined)).to.be.false;
105 });
106 });
107
108 describe('isVariable', () => {
109 it('matches a variable', () => {
110 Util.isVariable(variable('x')).should.be.true;
111 });
112
113 it('does not match an IRI', () => {
114 Util.isVariable(namedNode('http://example.org/')).should.be.false;
115 });
116
117 it('does not match a literal', () => {
118 Util.isVariable(literal('http://example.org/')).should.be.false;
119 });
120
121 it('does not match a blank node', () => {
122 Util.isNamedNode(blankNode('x')).should.be.false;
123 });
124
125 it('does not match null', () => {
126 expect(Util.isVariable(null)).to.be.false;
127 });
128
129 it('does not match undefined', () => {
130 expect(Util.isVariable(undefined)).to.be.false;
131 });
132 });
133
134 describe('isDefaultGraph', () => {
135 it('does not match a blank node', () => {
136 Util.isDefaultGraph(blankNode('x')).should.be.false;
137 });
138
139 it('does not match an IRI', () => {
140 Util.isDefaultGraph(namedNode('http://example.org/')).should.be.false;
141 });
142
143 it('does not match a literal', () => {
144 Util.isDefaultGraph(literal('http://example.org/')).should.be.false;
145 });
146
147 it('does not match null', () => {
148 expect(Util.isVariable(null)).to.be.false;
149 });
150
151 it('does not match undefined', () => {
152 expect(Util.isVariable(undefined)).to.be.false;
153 });
154 });
155
156 describe('inDefaultGraph', () => {
157 it('does not match a blank node', () => {
158 Util.inDefaultGraph(quad(null, null, null, blankNode('x'))).should.be.false;
159 });
160
161 it('does not match an IRI', () => {
162 Util.inDefaultGraph(quad(null, null, null, namedNode('http://example.org/'))).should.be.false;
163 });
164
165 it('does not match a literal', () => {
166 Util.inDefaultGraph(quad(null, null, null, literal('http://example.org/'))).should.be.false;
167 });
168
169 it('matches null', () => {
170 Util.inDefaultGraph(quad(null, null, null, null)).should.be.true;
171 });
172
173 it('matches undefined', () => {
174 Util.inDefaultGraph(quad(null, null, null, undefined)).should.be.true;
175 });
176
177 it('matches the default graph', () => {
178 Util.inDefaultGraph(quad(null, null, null, defaultGraph())).should.be.true;
179 });
180 });
181
182 describe('prefix', () => {
183 const rdfs = Util.prefix('http://www.w3.org/2000/01/rdf-schema#');
184 it('should return a function', () => {
185 expect(rdfs).to.be.an.instanceof(Function);
186 });
187
188 describe('the function', () => {
189 it('should expand the prefix', () => {
190 expect(rdfs('label')).to.deep.equal(namedNode('http://www.w3.org/2000/01/rdf-schema#label'));
191 });
192
193 it('should expand a NamedNode prefix', () => {
194 const rdfs = Util.prefix(namedNode('http://www.w3.org/2000/01/rdf-schema#'));
195 expect(rdfs('label')).to.deep.equal(namedNode('http://www.w3.org/2000/01/rdf-schema#label'));
196 });
197
198 it('should expand a Literal prefix', () => {
199 const rdfs = Util.prefix(literal('http://www.w3.org/2000/01/rdf-schema#'));
200 expect(rdfs('label')).to.deep.equal(namedNode('http://www.w3.org/2000/01/rdf-schema#label'));
201 });
202
203 it('should use a custom factory when specified', () => {
204 const factory = { namedNode: function (s) { return `n-${s}`; } };
205 const rdfs = Util.prefix('http://www.w3.org/2000/01/rdf-schema#', factory);
206 expect(rdfs('label')).to.equal('n-http://www.w3.org/2000/01/rdf-schema#label');
207 });
208 });
209 });
210
211 describe('prefixes', () => {
212 describe('called without arguments', () => {
213 const prefixes = Util.prefixes();
214 it('should return a function', () => {
215 expect(prefixes).to.be.an.instanceof(Function);
216 });
217
218 describe('the function', () => {
219 it('should not expand non-registered prefixes', () => {
220 expect(() => { prefixes('foo'); }).to.throw('Unknown prefix: foo');
221 });
222
223 it('should allow registering prefixes', () => {
224 const p = prefixes('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');
225 const rdfs = prefixes('rdfs');
226 expect(p).to.exist;
227 expect(p).to.equal(rdfs);
228 });
229
230 it('should expand the newly registered prefix', () => {
231 const rdfs = prefixes('rdfs');
232 expect(rdfs('label')).to.deep.equal(namedNode('http://www.w3.org/2000/01/rdf-schema#label'));
233 });
234 });
235 });
236
237 describe('called with a hash of prefixes', () => {
238 const prefixes = Util.prefixes({
239 rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
240 owl: 'http://www.w3.org/2002/07/owl#',
241 });
242 it('should return a function', () => {
243 expect(prefixes).to.be.an.instanceof(Function);
244 });
245
246 describe('the function', () => {
247 it('should expand registered prefixes', () => {
248 expect(prefixes('rdfs')('label')).to.deep.equal(namedNode('http://www.w3.org/2000/01/rdf-schema#label'));
249 expect(prefixes('owl')('sameAs')).to.deep.equal(namedNode('http://www.w3.org/2002/07/owl#sameAs'));
250 });
251
252 it('should not expand non-registered prefixes', () => {
253 expect(() => { prefixes('foo'); }).to.throw('Unknown prefix: foo');
254 });
255
256 it('should allow registering prefixes', () => {
257 const p = prefixes('my', 'http://example.org/#');
258 const my = prefixes('my');
259 expect(p).to.exist;
260 expect(p).to.equal(my);
261 });
262
263 it('should expand the newly registered prefix', () => {
264 const my = prefixes('my');
265 expect(my('me')).to.deep.equal(namedNode('http://example.org/#me'));
266 });
267 });
268 });
269
270 describe('called with a custom factory', () => {
271 const factory = { namedNode: function (s) { return `n-${s}`; } };
272 const prefixes = Util.prefixes({ my: 'http://example.org/#' }, factory);
273
274 it('should use the custom factory', () => {
275 expect(prefixes('my')('foo')).to.equal('n-http://example.org/#foo');
276 });
277 });
278 });
279 });