@@ -2,7 +2,7 @@ import "jest-rdf";
2
2
const stringToStream = require ( 'streamify-string' ) ;
3
3
const arrayifyStream = require ( 'arrayify-stream' ) ;
4
4
const quad = require ( 'rdf-quad' ) ;
5
- import { RdfParser } from "../lib/RdfParser" ;
5
+ import { RdfParser } from "../lib/RdfParser" ;
6
6
7
7
import parser from ".." ;
8
8
@@ -51,7 +51,7 @@ describe('parser', () => {
51
51
const stream = stringToStream ( `
52
52
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
53
53
` ) ;
54
- return expect ( ( ) => parser . parse ( stream , < any > { } ) )
54
+ return expect ( ( ) => parser . parse ( stream , < any > { } ) )
55
55
. toThrow ( new Error ( 'Missing \'contentType\' or \'path\' option while parsing.' ) ) ;
56
56
} ) ;
57
57
@@ -110,6 +110,24 @@ describe('parser', () => {
110
110
. rejects . toThrow ( new Error ( 'Expected entity but got eof on line 3.' ) ) ;
111
111
} ) ;
112
112
113
+ it ( 'should parse application/ld+json and emit context' , async ( ) : Promise < void > => {
114
+ const stream = stringToStream ( `
115
+ {
116
+ "@context": "http://schema.org/",
117
+ "@type": "Person",
118
+ "@id": "",
119
+ "name": "Jane Doe",
120
+ "url": ""
121
+ }
122
+ ` ) ;
123
+ const contexts : string [ ] = [ ] ;
124
+ const result = await arrayifyStream ( parser . parse ( stream , { contentType : 'application/ld+json' } )
125
+ . on ( 'context' , ( context => contexts . push ( context ) ) ) ) ;
126
+
127
+ expect ( result ) . toBeRdfIsomorphic ( [ ] ) ;
128
+ expect ( contexts ) . toContain ( 'http://schema.org/' ) ;
129
+ } ) ;
130
+
113
131
it ( 'should parse application/ld+json without baseIRI' , ( ) => {
114
132
const stream = stringToStream ( `
115
133
{
@@ -124,6 +142,7 @@ describe('parser', () => {
124
142
. toBeRdfIsomorphic ( [ ] ) ;
125
143
} ) ;
126
144
145
+
127
146
it ( 'should parse application/ld+json with baseIRI' , ( ) => {
128
147
const stream = stringToStream ( `
129
148
{
@@ -190,4 +209,56 @@ describe('parser', () => {
190
209
quad ( 'http://ex.org/' , 'http://schema.org/name' , '"Title"' ) ,
191
210
] ) ;
192
211
} ) ;
212
+
213
+ it ( 'should parse and emit prefixes in text/turtle' , async ( ) : Promise < void > => {
214
+ const turtle = `
215
+ @base <http://example.org/> .
216
+ @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
217
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
218
+ @prefix foaf: <http://xmlns.com/foaf/0.1/> .
219
+ @prefix rel: <http://www.perceive.net/schemas/relationship/> .
220
+
221
+ <#green-goblin>
222
+ rel:enemyOf <#spiderman> ;
223
+ a foaf:Person ; # in the context of the Marvel universe
224
+ foaf:name "Green Goblin" .
225
+
226
+ <#spiderman>
227
+ rel:enemyOf <#green-goblin> ;
228
+ a foaf:Person ;
229
+ foaf:name "Spiderman", "Человек-паук"@ru .`
230
+ const stream = stringToStream ( turtle ) ;
231
+ const prefixes : Record < string , string > = { } ;
232
+ const result = await arrayifyStream ( parser . parse ( stream , { path : 'myfile.ttl' } )
233
+ . on ( 'prefix' , ( prefix , iri ) => prefixes [ prefix ] = iri . value ) ) ;
234
+ expect ( result ) . toBeRdfIsomorphic ( [
235
+ quad ( 'http://example.org/#green-goblin' , 'http://www.perceive.net/schemas/relationship/enemyOf' , 'http://example.org/#spiderman' ) ,
236
+ quad ( 'http://example.org/#green-goblin' , 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' , 'http://xmlns.com/foaf/0.1/Person' ) ,
237
+ quad ( 'http://example.org/#green-goblin' , 'http://xmlns.com/foaf/0.1/name' , '\"Green Goblin\"' ) ,
238
+ quad ( 'http://example.org/#spiderman' , 'http://www.perceive.net/schemas/relationship/enemyOf' , 'http://example.org/#green-goblin' ) ,
239
+ quad ( 'http://example.org/#spiderman' , 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' , 'http://xmlns.com/foaf/0.1/Person' ) ,
240
+ quad ( 'http://example.org/#spiderman' , 'http://xmlns.com/foaf/0.1/name' , '\"Spiderman\"' ) ,
241
+ quad ( 'http://example.org/#spiderman' , 'http://xmlns.com/foaf/0.1/name' , '\"Человек-паук\"@ru' ) ,
242
+ ] ) ;
243
+
244
+ expect ( prefixes ) . toMatchObject ( {
245
+ rdf : 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' ,
246
+ rdfs : 'http://www.w3.org/2000/01/rdf-schema#' ,
247
+ foaf : 'http://xmlns.com/foaf/0.1/' ,
248
+ rel : 'http://www.perceive.net/schemas/relationship/'
249
+ } ) ;
250
+
251
+
252
+ } ) ;
253
+
254
+ it ( 'should parse text/turtle with baseIRI' , ( ) => {
255
+ const stream = stringToStream ( `
256
+ <s> <p> <o1>, <o2>.
257
+ ` ) ;
258
+ return expect ( arrayifyStream ( parser . parse ( stream , { contentType : 'text/turtle' , baseIRI : 'http://ex.org/' } ) ) )
259
+ . resolves . toBeRdfIsomorphic ( [
260
+ quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o1' ) ,
261
+ quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o2' ) ,
262
+ ] ) ;
263
+ } ) ;
193
264
} ) ;
0 commit comments