-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for JSON-LD fromRdf tests
- Loading branch information
1 parent
79f4d79
commit 116f653
Showing
10 changed files
with
427 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as RDF from "rdf-js"; | ||
|
||
/** | ||
* A serializer handler. | ||
*/ | ||
export interface ISerializer { | ||
serialize(data: RDF.Quad[], baseIRI: string, options: {[key: string]: any}): Promise<string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {ITestCase} from "../ITestCase"; | ||
import {IParser} from "./IParser"; | ||
import {ISerializer} from "./ISerializer"; | ||
|
||
/** | ||
* An test case data holder for serializations from RDF syntax. | ||
*/ | ||
export interface ITestCaseFromRdfSyntax extends ITestCase<ISerializer> { | ||
type: 'fromrdfsyntax'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import * as RDF from "rdf-js"; | ||
import {Resource} from "rdf-object"; | ||
import {Util} from "../../../Util"; | ||
import {ITestCaseData} from "../../ITestCase"; | ||
import {ITestCaseHandler} from "../../ITestCaseHandler"; | ||
import {ISerializer} from "../ISerializer"; | ||
import {ITestCaseFromRdfSyntax} from "../ITestCaseFromRdfSyntax"; | ||
// tslint:disable:no-var-requires | ||
const arrayifyStream = require('arrayify-stream'); | ||
const stringifyStream = require('stream-to-string'); | ||
|
||
/** | ||
* Test case handler for: | ||
* * https://w3c.github.io/json-ld-api/tests/vocab#FromRDFTest | ||
* * https://w3c.github.io/json-ld-api/tests/vocab#PositiveEvaluationTest | ||
* | ||
* It will check if the serialization from RDF to JSON-LD matches with the expected JSON-LD document. | ||
*/ | ||
export class TestCaseJsonLdFromRdfHandler implements ITestCaseHandler<TestCaseJsonLdFromRdf> { | ||
|
||
public async resourceToTestCase(resource: Resource, testCaseData: ITestCaseData, | ||
cachePath?: string): Promise<TestCaseJsonLdFromRdf> { | ||
if (!resource.property.action) { | ||
throw new Error(`Missing mf:action in ${resource}`); | ||
} | ||
if (!resource.property.result) { | ||
throw new Error(`Missing mf:result in ${resource}`); | ||
} | ||
|
||
// Loop over the options | ||
let useNativeTypes: boolean = false; | ||
let useRdfType: boolean = false; | ||
let processingMode: string = null; | ||
let specVersion: string = '1.0'; | ||
for (const option of resource.properties.jsonLdOptions) { | ||
// Should native types be used? | ||
if (option.property.useNativeTypes) { | ||
useNativeTypes = option.property.useNativeTypes.term.value === 'true'; | ||
} | ||
|
||
// Should RDF type be used? | ||
if (option.property.useRdfType) { | ||
useRdfType = option.property.useRdfType.term.value === 'true'; | ||
} | ||
|
||
// The processing mode | ||
// If undefined, all processors should be able to handle the test, | ||
// otherwise, only processors explicitly supporting that mode should run the test. | ||
if (option.property.processingMode) { | ||
// Remove the 'json-ld-' prefix from the string | ||
processingMode = option.property.processingMode.term.value.substr(8); | ||
} | ||
|
||
// The spec for which this test was defined. | ||
if (option.property.specVersion) { | ||
// Remove the 'json-ld-' prefix from the string | ||
specVersion = option.property.specVersion.term.value.substr(8); | ||
} | ||
} | ||
const options = { useNativeTypes, useRdfType, processingMode, specVersion }; | ||
|
||
return new TestCaseJsonLdFromRdf(testCaseData, | ||
await arrayifyStream(<any> (await Util.fetchRdf(resource.property.action.value, cachePath, true))[1]), | ||
await stringifyStream((await Util.fetchCached(resource.property.result.value, cachePath)).body), | ||
resource.property.action.value, options); | ||
} | ||
|
||
} | ||
|
||
export class TestCaseJsonLdFromRdf implements ITestCaseFromRdfSyntax { | ||
public readonly type = "fromrdfsyntax"; | ||
public readonly approval: string; | ||
public readonly approvedBy: string; | ||
public readonly comment: string; | ||
public readonly types: string[]; | ||
public readonly name: string; | ||
public readonly uri: string; | ||
|
||
public readonly data: RDF.Quad[]; | ||
public readonly expected: string; | ||
public readonly baseIRI: string; | ||
public readonly options: any; | ||
|
||
constructor(testCaseData: ITestCaseData, data: RDF.Quad[], expected: string, baseIRI: string, options: any) { | ||
Object.assign(this, testCaseData); | ||
this.data = data; | ||
this.expected = expected; | ||
this.baseIRI = baseIRI; | ||
this.options = options; | ||
} | ||
|
||
public async test(serializer: ISerializer, injectArguments: any): Promise<void> { | ||
const serialized: string = await serializer.serialize(this.data, this.baseIRI, | ||
{ ...this.options, ...injectArguments }); | ||
if (!objectsIsomorphic(JSON.parse(serialized), JSON.parse(this.expected))) { | ||
throw new Error(`Invalid data serialization | ||
Input: ${this.data} | ||
Expected: ${this.expected} | ||
Got: ${serialized} | ||
`); | ||
} | ||
} | ||
|
||
} | ||
|
||
export function objectsIsomorphic(obj1: any, obj2: any) { | ||
// Loop through properties in object 1 | ||
for (const p in obj1) { | ||
// Check property exists on obj2 | ||
if (!(p in obj2)) { | ||
return false; | ||
} | ||
|
||
switch (typeof (obj1[p])) { | ||
case 'object': | ||
if (!objectsIsomorphic(obj1[p], obj2[p])) { | ||
return false; | ||
} | ||
break; | ||
// Compare values | ||
default: | ||
if (obj1[p] !== obj2[p]) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
// Check object 2 for any extra properties | ||
for (const p in obj2) { | ||
if (!(p in obj1)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.