Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: property scoped testing
  • Loading branch information
jeswr committed Oct 28, 2023
commit 99249d040fdf55c38307dc544d9a07bca6543708
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"buffer": "^6.0.3",
"canonicalize": "^1.0.1",
"http-link-header": "^1.0.2",
"jsonld-context-parser": "^2.3.3",
"jsonld-context-parser": "../jsonld-context-parser.js/",
"rdf-data-factory": "^1.1.0",
"readable-stream": "^4.0.0"
},
Expand Down
26 changes: 26 additions & 0 deletions spec/parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
const { JsonLdParser } = require("..");
const { ContextParser, FetchDocumentLoader, ContextCache } = require("jsonld-context-parser");
const { ErrorSkipped } = require('rdf-test-suite');


const deepFreeze = obj => {
Object.keys(obj).forEach(prop => {
if (typeof obj[prop] === 'object' && !Object.isFrozen(obj[prop])) deepFreeze(obj[prop]);
});
return Object.freeze(obj);
};

class FrozenContextParser extends ContextParser {
constructor(options) {
super(options);
}

async parse(context, options) {
const parsed = await super.parse(context, options);
deepFreeze(parsed);
return parsed;
}
}


module.exports = {
parse: function (data, baseIRI, options) {
if (options.processingMode && (options.processingMode !== '1.0' && options.processingMode !== '1.1')) {
Expand All @@ -16,6 +38,10 @@ module.exports = {
baseIRI,
validateValueIndexes: true,
normalizeLanguageTags: true, // To simplify testing
contextParser: new FrozenContextParser({
documentLoader: new FetchDocumentLoader(),
contextCache: new ContextCache(),
}),
}, options))));
},
};
81 changes: 66 additions & 15 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {JsonLdParser} from "../index";
import arrayifyStream from 'arrayify-stream';
const streamifyString = require('streamify-string');
import * as RDF from "@rdfjs/types";
import arrayifyStream from 'arrayify-stream';
import { EventEmitter } from 'events';
import {DataFactory} from "rdf-data-factory";
import each from 'jest-each';
import "jest-rdf";
import {ContextParser, ERROR_CODES, ErrorCoded, FetchDocumentLoader, IParseOptions, JsonLdContext, JsonLdContextNormalized} from "jsonld-context-parser";
import {PassThrough} from "stream";
import {Util} from "../lib/Util";
import { ERROR_CODES, ErrorCoded, JsonLdContextNormalized, ContextParser, IParseOptions, JsonLdContext } from "jsonld-context-parser";
import { DataFactory } from "rdf-data-factory";
import { PassThrough } from "stream";
import { JsonLdParser } from "../index";
import { ParsingContext } from '../lib/ParsingContext';
import contexts, { MockedDocumentLoader } from '../mocks/contexts';
import { Util } from "../lib/Util";
import { MockedDocumentLoader } from '../mocks/contexts';
const streamifyString = require('streamify-string');

const DF = new DataFactory<RDF.BaseQuad>();

Expand All @@ -21,13 +21,15 @@ const deepFreeze = obj => {
return Object.freeze(obj);
};

class FrozenContextParser extends ContextParser {
export class FrozenContextParser extends ContextParser {
constructor(options: ConstructorParameters<typeof ContextParser>[0]) {
super(options);
}

public parse(context: JsonLdContext, options?: IParseOptions): Promise<JsonLdContextNormalized> {
return super.parse(context, options)// .then(deepFreeze);
public async parse(context: JsonLdContext, options?: IParseOptions): Promise<JsonLdContextNormalized> {
const parsed = await super.parse(context, options);
deepFreeze(parsed);
return parsed;
}
}

Expand Down Expand Up @@ -310,16 +312,20 @@ describe('JsonLdParser', () => {
});

(<any> each ([
[true],
[false],
])).describe('when instantiated with a data factory and streamingProfile %s', (streamingProfile: boolean) => {
[true, true],
[false, true],
[true, false],
[false, false],
])).describe('when instantiated with a data factory and streamingProfile %s and context caching %s', (streamingProfile: boolean, contextCache: boolean) => {
// Enable the following instead if you want to run tests more conveniently with IDE integration
/*describe('when instantiated with a data factory and streamingProfile %s', () => {
const streamingProfile = true;*/
let parser: any;

beforeEach(() => {
parser = new JsonLdParser({ dataFactory: DF, streamingProfile });
parser = new JsonLdParser({ dataFactory: DF, streamingProfile, contextParser: new ContextParser({
// contextCache: contextCache ? new ContextCache() : undefined,
}) });
});

describe('should parse', () => {
Expand Down Expand Up @@ -11816,6 +11822,51 @@ describe('JsonLdParser', () => {
ERROR_CODES.PROTECTED_TERM_REDEFINITION));
});

it('should error on protected term overrides after a property scoped-context', async () => {
const stream = streamifyString(JSON.stringify({
"@context": {
"@vocab": "http://example.com/",
"@version": 1.1,
"protected": {
"@protected": false
},
"scope1": {
"@protected": false,
"@context": {
"protected": {
"@id": "http://example.com/something-else"
}
}
},
"scope2": {
"@protected": true,
"@context": {
"protected": {
"@protected": true
}
}
}
},
"protected": false,
"scope1": {
"@context": {
"protected": "http://example.com/another-thing"
},
"protected": "property http://example.com/another-thing"
},
"scope2": {
"@context": {
"protected": "http://example.com/another-thing"
},
"protected": "error / property http://example.com/protected"
}
}
));
return expect(arrayifyStream(stream.pipe(parser))).rejects.toThrow(new ErrorCoded(
'Attempted to override the protected keyword protected from \"http://example.com/protected\" to \"http://example.com/another-thing\"',
ERROR_CODES.PROTECTED_TERM_REDEFINITION));
});

it('should not error on protected term, context null in a property scoped-context, and override', async () => {
const stream = streamifyString(`
{
Expand Down
14 changes: 6 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2506,10 +2506,8 @@ jsonify@^0.0.1:
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.1.tgz#2aa3111dae3d34a0f151c63f3a45d995d9420978"
integrity sha512-2/Ki0GcmuqSrgFyelQq9M05y7PS0mEwuIzrf3f1fPqkVDVRvZrPZtVSMHxdgo8Aq0sxAOb/cr2aqqA3LeWHVPg==

jsonld-context-parser@^2.0.2, jsonld-context-parser@^2.1.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/jsonld-context-parser/-/jsonld-context-parser-2.2.3.tgz#51f1042dd1dbd75c9991ae70faf984048020ebe6"
integrity sha512-SMMS/CEKK9VSiKIpmaivVZVpHfC59qax6UM4hbWv5YRcyxx7kmzXWzM/LGxPAwxwx3nwbB644Xd+4443L7OW4g==
jsonld-context-parser@../jsonld-context-parser.js/:
version "2.3.3"
dependencies:
"@types/http-link-header" "^1.0.1"
"@types/node" "^18.0.0"
Expand All @@ -2518,10 +2516,10 @@ jsonld-context-parser@^2.0.2, jsonld-context-parser@^2.1.3:
http-link-header "^1.0.2"
relative-to-absolute-iri "^1.0.5"

jsonld-context-parser@^2.3.3:
version "2.3.3"
resolved "https://registry.yarnpkg.com/jsonld-context-parser/-/jsonld-context-parser-2.3.3.tgz#0bdab9eb5cb4b7e68aa7d6c38e58455363caaf9c"
integrity sha512-H+REInOx7XI2ciF8wJV31D20Bh+ofBmEjN2Tkds51vypqDJIiD341E5g+hYyrEInIKRnbW58TN/Ehz+ACT0l0w==
jsonld-context-parser@^2.0.2, jsonld-context-parser@^2.1.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/jsonld-context-parser/-/jsonld-context-parser-2.2.3.tgz#51f1042dd1dbd75c9991ae70faf984048020ebe6"
integrity sha512-SMMS/CEKK9VSiKIpmaivVZVpHfC59qax6UM4hbWv5YRcyxx7kmzXWzM/LGxPAwxwx3nwbB644Xd+4443L7OW4g==
dependencies:
"@types/http-link-header" "^1.0.1"
"@types/node" "^18.0.0"
Expand Down