|
1 | | -import {blankNode, defaultGraph, literal, namedNode} from "@rdfjs/data-model"; |
| 1 | +import { DataFactory } from "rdf-data-factory"; |
2 | 2 | import {Util} from "../../lib/Util"; |
3 | 3 | import {ERROR_CODES, ErrorCoded, JsonLdContextNormalized} from "jsonld-context-parser"; |
4 | 4 |
|
| 5 | +const DF = new DataFactory(); |
| 6 | + |
5 | 7 | describe('Util', () => { |
6 | 8 |
|
7 | 9 | describe('stringToNativeType', () => { |
@@ -91,168 +93,173 @@ describe('Util', () => { |
91 | 93 |
|
92 | 94 | describe('termToValue', () => { |
93 | 95 | it('should handle named nodes', async () => { |
94 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), new JsonLdContextNormalized({}))) |
| 96 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), new JsonLdContextNormalized({}))) |
95 | 97 | .toEqual({ '@id': 'http://ex.org/' }); |
96 | 98 | }); |
97 | 99 |
|
98 | 100 | it('should handle named nodes with compactIds true', async () => { |
99 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), new JsonLdContextNormalized({}), { compactIds: true })) |
| 101 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), new JsonLdContextNormalized({}), { compactIds: true })) |
100 | 102 | .toEqual('http://ex.org/'); |
101 | 103 | }); |
102 | 104 |
|
103 | 105 | it('should handle named nodes without context vocab in vocab mode', async () => { |
104 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), new JsonLdContextNormalized({}), { vocab: true })) |
| 106 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), new JsonLdContextNormalized({}), { vocab: true })) |
105 | 107 | .toEqual({ '@id': 'http://ex.org/' }); |
106 | 108 | }); |
107 | 109 |
|
108 | 110 | it('should handle named nodes without context vocab in base mode', async () => { |
109 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), new JsonLdContextNormalized({}), { vocab: false })) |
| 111 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), new JsonLdContextNormalized({}), { vocab: false })) |
110 | 112 | .toEqual({ '@id': 'http://ex.org/' }); |
111 | 113 | }); |
112 | 114 |
|
113 | 115 | it('should handle named nodes with context vocab in vocab mode', async () => { |
114 | 116 | const context = new JsonLdContextNormalized({ '@vocab': 'http://ex.org/' }); |
115 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), context, { vocab: true })) |
| 117 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), context, { vocab: true })) |
116 | 118 | .toEqual({ '@id': '' }); |
117 | 119 | }); |
118 | 120 |
|
119 | 121 | it('should handle named nodes with context vocab in base mode', async () => { |
120 | 122 | const context = new JsonLdContextNormalized({ '@vocab': 'http://ex.org/' }); |
121 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), context, { vocab: false })) |
| 123 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), context, { vocab: false })) |
122 | 124 | .toEqual({ '@id': 'http://ex.org/' }); |
123 | 125 | }); |
124 | 126 |
|
125 | 127 | it('should handle named nodes with context base in vocab mode', async () => { |
126 | 128 | const context = new JsonLdContextNormalized({ '@base': 'http://ex.org/' }); |
127 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), context, { vocab: true })) |
| 129 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), context, { vocab: true })) |
128 | 130 | .toEqual({ '@id': 'http://ex.org/' }); |
129 | 131 | }); |
130 | 132 |
|
131 | 133 | it('should handle named nodes with context base in base mode', async () => { |
132 | 134 | const context = new JsonLdContextNormalized({ '@base': 'http://ex.org/' }); |
133 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), context, { vocab: false })) |
| 135 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), context, { vocab: false })) |
134 | 136 | .toEqual({ '@id': '' }); |
135 | 137 | }); |
136 | 138 |
|
137 | 139 | it('should handle named nodes with context term in vocab mode', async () => { |
138 | 140 | const context = new JsonLdContextNormalized({ ex: 'http://ex.org/' }); |
139 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), context, { vocab: true })) |
| 141 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), context, { vocab: true })) |
140 | 142 | .toEqual({ '@id': 'ex' }); |
141 | 143 | }); |
142 | 144 |
|
143 | 145 | it('should handle named nodes with context term in base mode', async () => { |
144 | 146 | const context = new JsonLdContextNormalized({ ex: 'http://ex.org/' }); |
145 | | - return expect(Util.termToValue(namedNode('http://ex.org/'), context, { vocab: false })) |
| 147 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/'), context, { vocab: false })) |
146 | 148 | .toEqual({ '@id': 'http://ex.org/' }); |
147 | 149 | }); |
148 | 150 |
|
149 | 151 | it('should handle named nodes with context prefix in vocab mode', async () => { |
150 | 152 | const context = new JsonLdContextNormalized({ ex: 'http://ex.org/' }); |
151 | | - return expect(Util.termToValue(namedNode('http://ex.org/aa'), context, { vocab: true })) |
| 153 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/aa'), context, { vocab: true })) |
152 | 154 | .toEqual({ '@id': 'ex:aa' }); |
153 | 155 | }); |
154 | 156 |
|
155 | 157 | it('should handle named nodes with context prefix in base mode', async () => { |
156 | 158 | const context = new JsonLdContextNormalized({ ex: 'http://ex.org/' }); |
157 | | - return expect(Util.termToValue(namedNode('http://ex.org/aa'), context, { vocab: false })) |
| 159 | + return expect(Util.termToValue(DF.namedNode('http://ex.org/aa'), context, { vocab: false })) |
158 | 160 | .toEqual({ '@id': 'ex:aa' }); |
159 | 161 | }); |
160 | 162 |
|
161 | 163 | it('should handle default graphs', async () => { |
162 | | - return expect(Util.termToValue(defaultGraph(), new JsonLdContextNormalized({}))) |
| 164 | + return expect(Util.termToValue(DF.defaultGraph(), new JsonLdContextNormalized({}))) |
163 | 165 | .toEqual({ '@id': '' }); |
164 | 166 | }); |
165 | 167 |
|
166 | 168 | it('should handle default graphs with compactIds true', async () => { |
167 | | - return expect(Util.termToValue(defaultGraph(), new JsonLdContextNormalized({}), { compactIds: true })) |
| 169 | + return expect(Util.termToValue(DF.defaultGraph(), new JsonLdContextNormalized({}), { compactIds: true })) |
168 | 170 | .toEqual(''); |
169 | 171 | }); |
170 | 172 |
|
171 | 173 | it('should handle blank nodes', async () => { |
172 | | - return expect(Util.termToValue(blankNode('b0'), new JsonLdContextNormalized({}))) |
| 174 | + return expect(Util.termToValue(DF.blankNode('b0'), new JsonLdContextNormalized({}))) |
173 | 175 | .toEqual({ '@id': '_:b0' }); |
174 | 176 | }); |
175 | 177 |
|
176 | 178 | it('should handle blank nodes with compactIds true', async () => { |
177 | | - return expect(Util.termToValue(blankNode('b0'), new JsonLdContextNormalized({}), { compactIds: true })) |
| 179 | + return expect(Util.termToValue(DF.blankNode('b0'), new JsonLdContextNormalized({}), { compactIds: true })) |
178 | 180 | .toEqual('_:b0'); |
179 | 181 | }); |
180 | 182 |
|
181 | | - it('should handle literals without language and datatype with useNativeTypes false', async () => { |
182 | | - return expect(Util.termToValue(literal('abc'), new JsonLdContextNormalized({}))) |
| 183 | + it('should handle DF.literals without language and datatype with useNativeTypes false', async () => { |
| 184 | + return expect(Util.termToValue(DF.literal('abc'), new JsonLdContextNormalized({}))) |
183 | 185 | .toEqual({ '@value': 'abc' }); |
184 | 186 | }); |
185 | 187 |
|
186 | | - it('should handle literals without language and datatype with useNativeTypes true', async () => { |
187 | | - return expect(Util.termToValue(literal('abc'), new JsonLdContextNormalized({}), { useNativeTypes: true })) |
| 188 | + it('should handle DF.literals without language and datatype with useNativeTypes true', async () => { |
| 189 | + return expect(Util.termToValue(DF.literal('abc'), new JsonLdContextNormalized({}), { useNativeTypes: true })) |
188 | 190 | .toEqual({ '@value': 'abc' }); |
189 | 191 | }); |
190 | 192 |
|
191 | | - it('should handle literals with language with useNativeTypes false', async () => { |
192 | | - return expect(Util.termToValue(literal('abc', 'en'), new JsonLdContextNormalized({}))) |
| 193 | + it('should handle DF.literals with language with useNativeTypes false', async () => { |
| 194 | + return expect(Util.termToValue(DF.literal('abc', 'en'), new JsonLdContextNormalized({}))) |
193 | 195 | .toEqual({ '@value': 'abc', '@language': 'en' }); |
194 | 196 | }); |
195 | 197 |
|
196 | | - it('should handle literals with language with useNativeTypes true', async () => { |
197 | | - return expect(Util.termToValue(literal('abc', 'en'), new JsonLdContextNormalized({}), { useNativeTypes: true })) |
| 198 | + it('should handle DF.literals with language with useNativeTypes true', async () => { |
| 199 | + return expect(Util.termToValue(DF.literal('abc', 'en'), new JsonLdContextNormalized({}), { useNativeTypes: true })) |
198 | 200 | .toEqual({ '@value': 'abc', '@language': 'en' }); |
199 | 201 | }); |
200 | 202 |
|
201 | | - it('should handle string literals with unknown datatype with useNativeTypes false', async () => { |
202 | | - return expect(Util.termToValue(literal('abc', namedNode('http://ex.org/type')), new JsonLdContextNormalized({}))) |
| 203 | + it('should handle string DF.literals with unknown datatype with useNativeTypes false', async () => { |
| 204 | + return expect(Util.termToValue(DF.literal('abc', DF.namedNode('http://ex.org/type')), new JsonLdContextNormalized({}))) |
203 | 205 | .toEqual({ '@value': 'abc', '@type': 'http://ex.org/type' }); |
204 | 206 | }); |
205 | 207 |
|
206 | | - it('should handle string literals with unknown datatype with useNativeTypes true', async () => { |
207 | | - return expect(Util.termToValue(literal('abc', namedNode('http://ex.org/type')), new JsonLdContextNormalized({}), { useNativeTypes: true })) |
| 208 | + it('should handle string DF.literals with unknown datatype with useNativeTypes true', async () => { |
| 209 | + return expect(Util.termToValue(DF.literal('abc', DF.namedNode('http://ex.org/type')), new JsonLdContextNormalized({}), { useNativeTypes: true })) |
208 | 210 | .toEqual({ '@value': 'abc', '@type': 'http://ex.org/type' }); |
209 | 211 | }); |
210 | 212 |
|
211 | | - it('should handle string literals with number datatype with useNativeTypes false', async () => { |
212 | | - return expect(Util.termToValue(literal('10', namedNode('http://www.w3.org/2001/XMLSchema#number')), new JsonLdContextNormalized({}))) |
| 213 | + it('should handle string DF.literals with number datatype with useNativeTypes false', async () => { |
| 214 | + return expect(Util.termToValue(DF.literal('10', DF.namedNode('http://www.w3.org/2001/XMLSchema#number')), new JsonLdContextNormalized({}))) |
213 | 215 | .toEqual({ '@value': '10', '@type': 'http://www.w3.org/2001/XMLSchema#number' }); |
214 | 216 | }); |
215 | 217 |
|
216 | | - it('should handle string literals with number datatype with useNativeTypes true', async () => { |
217 | | - return expect(Util.termToValue(literal('10', namedNode('http://www.w3.org/2001/XMLSchema#number')), new JsonLdContextNormalized({}), |
| 218 | + it('should handle string DF.literals with number datatype with useNativeTypes true', async () => { |
| 219 | + return expect(Util.termToValue(DF.literal('10', DF.namedNode('http://www.w3.org/2001/XMLSchema#number')), new JsonLdContextNormalized({}), |
218 | 220 | { useNativeTypes: true })).toEqual({ '@value': 10 }); |
219 | 221 | }); |
220 | 222 |
|
221 | | - it('should handle string literals with decimal datatype with useNativeTypes true', async () => { |
222 | | - return expect(Util.termToValue(literal('10', namedNode('http://www.w3.org/2001/XMLSchema#decimal')), new JsonLdContextNormalized({}), |
| 223 | + it('should handle string DF.literals with decimal datatype with useNativeTypes true', async () => { |
| 224 | + return expect(Util.termToValue(DF.literal('10', DF.namedNode('http://www.w3.org/2001/XMLSchema#decimal')), new JsonLdContextNormalized({}), |
223 | 225 | { useNativeTypes: true })).toEqual( |
224 | 226 | { '@value': '10', '@type': 'http://www.w3.org/2001/XMLSchema#decimal' }); |
225 | 227 | }); |
226 | 228 |
|
227 | | - it('should handle string literals with JSON datatype', async () => { |
228 | | - return expect(Util.termToValue(literal('true', namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON')), new JsonLdContextNormalized({}))) |
| 229 | + it('should handle string DF.literals with JSON datatype', async () => { |
| 230 | + return expect(Util.termToValue(DF.literal('true', DF.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON')), new JsonLdContextNormalized({}))) |
229 | 231 | .toEqual({ '@value': true, '@type': '@json' }); |
230 | | - return expect(Util.termToValue(literal('{"a":"b"}', namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON')), new JsonLdContextNormalized({}))) |
| 232 | + return expect(Util.termToValue(DF.literal('{"a":"b"}', DF.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON')), new JsonLdContextNormalized({}))) |
231 | 233 | .toEqual({ '@value': { a: 'b' }, '@type': '@json' }); |
232 | 234 | }); |
233 | 235 |
|
234 | | - it('should handle string literals with I18N datatype', async () => { |
235 | | - return expect(Util.termToValue(literal('bla', namedNode('https://www.w3.org/ns/i18n#en-us_rtl')), new JsonLdContextNormalized({}))).toEqual( |
| 236 | + it('should handle string DF.literals with I18N datatype', async () => { |
| 237 | + return expect(Util.termToValue(DF.literal('bla', DF.namedNode('https://www.w3.org/ns/i18n#en-us_rtl')), new JsonLdContextNormalized({}))).toEqual( |
236 | 238 | { '@value': 'bla', '@type': 'https://www.w3.org/ns/i18n#en-us_rtl' }); |
237 | 239 | }); |
238 | 240 |
|
239 | | - it('should handle string literals with I18N datatype with rdfDirection: i18n-datatype', async () => { |
240 | | - expect(Util.termToValue(literal('bla', namedNode('https://www.w3.org/ns/i18n#en-us_rtl')), new JsonLdContextNormalized({}), |
| 241 | + it('should handle string DF.literals with I18N datatype with rdfDirection: i18n-datatype', async () => { |
| 242 | + expect(Util.termToValue(DF.literal('bla', DF.namedNode('https://www.w3.org/ns/i18n#en-us_rtl')), new JsonLdContextNormalized({}), |
241 | 243 | { rdfDirection: 'i18n-datatype' })).toEqual( |
242 | 244 | { '@value': 'bla', '@language': 'en-us', '@direction': 'rtl' }); |
243 | | - expect(Util.termToValue(literal('bla', namedNode('https://www.w3.org/ns/i18n#_rtl')), new JsonLdContextNormalized({}), |
| 245 | + expect(Util.termToValue(DF.literal('bla', DF.namedNode('https://www.w3.org/ns/i18n#_rtl')), new JsonLdContextNormalized({}), |
244 | 246 | { rdfDirection: 'i18n-datatype' })).toEqual( |
245 | 247 | { '@value': 'bla', '@direction': 'rtl' }); |
246 | | - expect(Util.termToValue(literal('bla', namedNode('https://www.w3.org/ns/i18n#en-us_')), new JsonLdContextNormalized({}), |
| 248 | + expect(Util.termToValue(DF.literal('bla', DF.namedNode('https://www.w3.org/ns/i18n#en-us_')), new JsonLdContextNormalized({}), |
247 | 249 | { rdfDirection: 'i18n-datatype' })).toEqual( |
248 | 250 | { '@value': 'bla', '@language': 'en-us' }); |
249 | 251 | }); |
250 | 252 |
|
251 | | - it('should throw on invalid string literals with JSON datatype', async () => { |
252 | | - expect(() => Util.termToValue(literal('{', namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON')), new JsonLdContextNormalized({}))) |
| 253 | + it('should handle string DF.literals with base direction with rdfDirection: undefined', async () => { |
| 254 | + expect(Util.termToValue(DF.literal('bla', { language: 'en-us', direction: 'rtl' }), new JsonLdContextNormalized({}))).toEqual( |
| 255 | + { '@value': 'bla', '@language': 'en-us', '@direction': 'rtl' }); |
| 256 | + }); |
| 257 | + |
| 258 | + it('should throw on invalid string DF.literals with JSON datatype', async () => { |
| 259 | + expect(() => Util.termToValue(DF.literal('{', DF.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON')), new JsonLdContextNormalized({}))) |
253 | 260 | .toThrow('Invalid JSON literal'); |
254 | 261 | try { |
255 | | - Util.termToValue(literal('{', namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON')), new JsonLdContextNormalized({})); |
| 262 | + Util.termToValue(DF.literal('{', DF.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON')), new JsonLdContextNormalized({})); |
256 | 263 | } catch(e) { |
257 | 264 | expect(e).toBeInstanceOf(ErrorCoded); |
258 | 265 | expect(e.code).toEqual(ERROR_CODES.INVALID_JSON_LITERAL); |
|
0 commit comments