1
1
import "jest-rdf" ;
2
+ import { RdfParser } from "../lib/RdfParser" ;
3
+
4
+ import { rdfParser } from ".." ;
5
+
2
6
const stringToStream = require ( 'streamify-string' ) ;
3
7
const arrayifyStream = require ( 'arrayify-stream' ) ;
4
8
const quad = require ( 'rdf-quad' ) ;
5
- import { RdfParser } from "../lib/RdfParser" ;
6
-
7
- import parser from ".." ;
8
9
9
10
describe ( 'parser' , ( ) => {
10
11
it ( 'should be an RdfParser instance' , ( ) => {
11
- expect ( parser ) . toBeInstanceOf ( RdfParser ) ;
12
+ expect ( rdfParser ) . toBeInstanceOf ( RdfParser ) ;
12
13
} ) ;
13
14
14
15
it ( 'should get all content types' , async ( ) => {
15
- expect ( ( await parser . getContentTypes ( ) ) . sort ( ) ) . toEqual ( [
16
+ expect ( ( await rdfParser . getContentTypes ( ) ) . sort ( ) ) . toEqual ( [
16
17
'application/ld+json' ,
17
18
'application/json' ,
18
19
'text/html' ,
@@ -32,7 +33,7 @@ describe('parser', () => {
32
33
} ) ;
33
34
34
35
it ( 'should get all prioritized content types' , async ( ) => {
35
- expect ( await parser . getContentTypesPrioritized ( ) ) . toEqual ( {
36
+ expect ( await rdfParser . getContentTypesPrioritized ( ) ) . toEqual ( {
36
37
'application/json' : 0.45 ,
37
38
'application/ld+json' : 0.9 ,
38
39
'application/n-quads' : 1 ,
@@ -55,31 +56,31 @@ describe('parser', () => {
55
56
const stream = stringToStream ( `
56
57
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
57
58
` ) ;
58
- return expect ( ( ) => parser . parse ( stream , < any > { } ) )
59
+ return expect ( ( ) => rdfParser . parse ( stream , < any > { } ) )
59
60
. toThrow ( new Error ( 'Missing \'contentType\' or \'path\' option while parsing.' ) ) ;
60
61
} ) ;
61
62
62
63
it ( 'should fail to parse with path without extension' , ( ) => {
63
64
const stream = stringToStream ( `
64
65
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
65
66
` ) ;
66
- return expect ( ( ) => parser . parse ( stream , { path : 'abc' } ) )
67
+ return expect ( ( ) => rdfParser . parse ( stream , { path : 'abc' } ) )
67
68
. toThrow ( new Error ( 'No valid extension could be detected from the given \'path\' option: \'abc\'' ) ) ;
68
69
} ) ;
69
70
70
71
it ( 'should fail to parse with path with unknown extension' , ( ) => {
71
72
const stream = stringToStream ( `
72
73
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
73
74
` ) ;
74
- return expect ( ( ) => parser . parse ( stream , { path : 'abc.unknown' } ) )
75
+ return expect ( ( ) => rdfParser . parse ( stream , { path : 'abc.unknown' } ) )
75
76
. toThrow ( new Error ( 'No valid extension could be detected from the given \'path\' option: \'abc.unknown\'' ) ) ;
76
77
} ) ;
77
78
78
79
it ( 'should parse text/turtle without baseIRI' , ( ) => {
79
80
const stream = stringToStream ( `
80
81
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
81
82
` ) ;
82
- return expect ( arrayifyStream ( parser . parse ( stream , { contentType : 'text/turtle' } ) ) ) . resolves . toBeRdfIsomorphic ( [
83
+ return expect ( arrayifyStream ( rdfParser . parse ( stream , { contentType : 'text/turtle' } ) ) ) . resolves . toBeRdfIsomorphic ( [
83
84
quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o1' ) ,
84
85
quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o2' ) ,
85
86
] ) ;
@@ -89,7 +90,7 @@ describe('parser', () => {
89
90
const stream = stringToStream ( `
90
91
<http://ex.org/s> <http://ex.org/p> <http://ex.org/o1>, <http://ex.org/o2>.
91
92
` ) ;
92
- return expect ( arrayifyStream ( parser . parse ( stream , { path : 'myfile.ttl' } ) ) ) . resolves . toBeRdfIsomorphic ( [
93
+ return expect ( arrayifyStream ( rdfParser . parse ( stream , { path : 'myfile.ttl' } ) ) ) . resolves . toBeRdfIsomorphic ( [
93
94
quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o1' ) ,
94
95
quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o2' ) ,
95
96
] ) ;
@@ -99,7 +100,7 @@ describe('parser', () => {
99
100
const stream = stringToStream ( `
100
101
<s> <p> <o1>, <o2>.
101
102
` ) ;
102
- return expect ( arrayifyStream ( parser . parse ( stream , { contentType : 'text/turtle' , baseIRI : 'http://ex.org/' } ) ) )
103
+ return expect ( arrayifyStream ( rdfParser . parse ( stream , { contentType : 'text/turtle' , baseIRI : 'http://ex.org/' } ) ) )
103
104
. resolves . toBeRdfIsomorphic ( [
104
105
quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o1' ) ,
105
106
quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o2' ) ,
@@ -110,7 +111,7 @@ describe('parser', () => {
110
111
const stream = stringToStream ( `
111
112
<s> <p> <o1>,
112
113
` ) ;
113
- return expect ( arrayifyStream ( parser . parse ( stream , { contentType : 'text/turtle' } ) ) )
114
+ return expect ( arrayifyStream ( rdfParser . parse ( stream , { contentType : 'text/turtle' } ) ) )
114
115
. rejects . toThrow ( new Error ( 'Expected entity but got eof on line 3.' ) ) ;
115
116
} ) ;
116
117
@@ -125,7 +126,7 @@ describe('parser', () => {
125
126
}
126
127
` ) ;
127
128
const contexts : string [ ] = [ ] ;
128
- const result = await arrayifyStream ( parser . parse ( stream , { contentType : 'application/ld+json' } )
129
+ const result = await arrayifyStream ( rdfParser . parse ( stream , { contentType : 'application/ld+json' } )
129
130
. on ( 'context' , ( context => contexts . push ( context ) ) ) ) ;
130
131
131
132
expect ( result ) . toBeRdfIsomorphic ( [ ] ) ;
@@ -142,7 +143,7 @@ describe('parser', () => {
142
143
"url": ""
143
144
}
144
145
` ) ;
145
- return expect ( arrayifyStream ( parser . parse ( stream , { contentType : 'application/ld+json' } ) ) ) . resolves
146
+ return expect ( arrayifyStream ( rdfParser . parse ( stream , { contentType : 'application/ld+json' } ) ) ) . resolves
146
147
. toBeRdfIsomorphic ( [ ] ) ;
147
148
} ) ;
148
149
@@ -157,8 +158,8 @@ describe('parser', () => {
157
158
"url": ""
158
159
}
159
160
` ) ;
160
- return expect ( arrayifyStream ( parser
161
- . parse ( stream , { contentType : 'application/ld+json' , baseIRI : 'http://ex.org/' } ) ) )
161
+ return expect ( arrayifyStream ( rdfParser
162
+ . parse ( stream , { contentType : 'application/ld+json' , baseIRI : 'http://ex.org/' } ) ) )
162
163
. resolves . toBeRdfIsomorphic ( [
163
164
quad ( 'http://ex.org/' , 'http://schema.org/name' , '"Jane Doe"' ) ,
164
165
quad ( 'http://ex.org/' , 'http://schema.org/url' , 'http://ex.org/' ) ,
@@ -176,8 +177,8 @@ describe('parser', () => {
176
177
"url": ""
177
178
}
178
179
` ) ;
179
- return expect ( arrayifyStream ( parser
180
- . parse ( stream , { path : 'myfile.json' , baseIRI : 'http://ex.org/' } ) ) )
180
+ return expect ( arrayifyStream ( rdfParser
181
+ . parse ( stream , { path : 'myfile.json' , baseIRI : 'http://ex.org/' } ) ) )
181
182
. resolves . toBeRdfIsomorphic ( [
182
183
quad ( 'http://ex.org/' , 'http://schema.org/name' , '"Jane Doe"' ) ,
183
184
quad ( 'http://ex.org/' , 'http://schema.org/url' , 'http://ex.org/' ) ,
@@ -189,13 +190,13 @@ describe('parser', () => {
189
190
const stream = stringToStream ( `
190
191
<s> <p> <o1>,
191
192
` ) ;
192
- return expect ( arrayifyStream ( parser . parse ( stream , { contentType : 'application/ld+json' } ) ) )
193
+ return expect ( arrayifyStream ( rdfParser . parse ( stream , { contentType : 'application/ld+json' } ) ) )
193
194
. rejects . toThrow ( new Error ( 'Unexpected "s" at position 2 in state STOP' ) ) ;
194
195
} ) ;
195
196
196
197
it ( 'should fail to parse an unknown content type' , ( ) => {
197
198
const stream = stringToStream ( `` ) ;
198
- return expect ( arrayifyStream ( parser . parse ( stream , { contentType : 'unknown' } ) ) )
199
+ return expect ( arrayifyStream ( rdfParser . parse ( stream , { contentType : 'unknown' } ) ) )
199
200
. rejects . toBeTruthy ( ) ;
200
201
} ) ;
201
202
@@ -207,8 +208,8 @@ describe('parser', () => {
207
208
</head>
208
209
</html>
209
210
` ) ;
210
- return expect ( arrayifyStream ( parser
211
- . parse ( stream , { contentType : 'text/html' , baseIRI : 'http://ex.org/' } ) ) )
211
+ return expect ( arrayifyStream ( rdfParser
212
+ . parse ( stream , { contentType : 'text/html' , baseIRI : 'http://ex.org/' } ) ) )
212
213
. resolves . toBeRdfIsomorphic ( [
213
214
quad ( 'http://ex.org/' , 'http://schema.org/name' , '"Title"' ) ,
214
215
] ) ;
@@ -233,7 +234,7 @@ describe('parser', () => {
233
234
foaf:name "Spiderman", "Человек-паук"@ru .`
234
235
const stream = stringToStream ( turtle ) ;
235
236
const prefixes : Record < string , string > = { } ;
236
- const result = await arrayifyStream ( parser . parse ( stream , { path : 'myfile.ttl' } )
237
+ const result = await arrayifyStream ( rdfParser . parse ( stream , { path : 'myfile.ttl' } )
237
238
. on ( 'prefix' , ( prefix , iri ) => prefixes [ prefix ] = iri . value ) ) ;
238
239
expect ( result ) . toBeRdfIsomorphic ( [
239
240
quad ( 'http://example.org/#green-goblin' , 'http://www.perceive.net/schemas/relationship/enemyOf' , 'http://example.org/#spiderman' ) ,
@@ -259,7 +260,7 @@ describe('parser', () => {
259
260
const stream = stringToStream ( `
260
261
<s> <p> <o1>, <o2>.
261
262
` ) ;
262
- return expect ( arrayifyStream ( parser . parse ( stream , { contentType : 'text/turtle' , baseIRI : 'http://ex.org/' } ) ) )
263
+ return expect ( arrayifyStream ( rdfParser . parse ( stream , { contentType : 'text/turtle' , baseIRI : 'http://ex.org/' } ) ) )
263
264
. resolves . toBeRdfIsomorphic ( [
264
265
quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o1' ) ,
265
266
quad ( 'http://ex.org/s' , 'http://ex.org/p' , 'http://ex.org/o2' ) ,
@@ -273,7 +274,7 @@ describe('parser', () => {
273
274
274
275
shape cont:ContactsShape {}
275
276
` ) ;
276
- return expect ( arrayifyStream ( parser . parse ( stream , { contentType : 'text/shaclc' } ) ) )
277
+ return expect ( arrayifyStream ( rdfParser . parse ( stream , { contentType : 'text/shaclc' } ) ) )
277
278
. resolves . toBeRdfIsomorphic ( [
278
279
quad ( "http://localhost:3002/ContactsShape#ContactsShape" , "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" , "http://www.w3.org/ns/shacl#NodeShape" ) ,
279
280
quad ( "http://localhost:3002/ContactsShape" , "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" , "http://www.w3.org/2002/07/owl#Ontology" ) ,
0 commit comments