diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS deleted file mode 100644 index 31d34a8..0000000 --- a/.github/CODEOWNERS +++ /dev/null @@ -1 +0,0 @@ -* @rdfjs/data-model-spec diff --git a/Factory.js b/Factory.js new file mode 100644 index 0000000..dec562e --- /dev/null +++ b/Factory.js @@ -0,0 +1,74 @@ +import BlankNode from './lib/BlankNode.js' +import DefaultGraph from './lib/DefaultGraph.js' +import fromTerm from './lib/fromTerm.js' +import Literal from './lib/Literal.js' +import NamedNode from './lib/NamedNode.js' +import Quad from './lib/Quad.js' +import Variable from './lib/Variable.js' + +const langStringDatatype = new NamedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') +const stringDatatype = new NamedNode('http://www.w3.org/2001/XMLSchema#string') + +class DataFactory { + constructor () { + this.init() + } + + init () { + this._data = { + blankNodeCounter: 0, + defaultGraph: new DefaultGraph() + } + } + + namedNode (value) { + return new NamedNode(value) + } + + blankNode (value) { + value = value || ('b' + (++this._data.blankNodeCounter)) + + return new BlankNode(value) + } + + literal (value, languageOrDatatype) { + if (typeof languageOrDatatype === 'string') { + return new Literal(value, languageOrDatatype, langStringDatatype) + } else { + return new Literal(value, '', languageOrDatatype || stringDatatype) + } + } + + variable (value) { + return new Variable(value) + } + + defaultGraph () { + return this._data.defaultGraph + } + + quad (subject, predicate, object, graph = this.defaultGraph()) { + return new Quad(subject, predicate, object, graph) + } + + fromTerm (original) { + return fromTerm(this, original) + } + + fromQuad (original) { + return fromTerm(this, original) + } +} + +DataFactory.exports = [ + 'blankNode', + 'defaultGraph', + 'fromQuad', + 'fromTerm', + 'literal', + 'namedNode', + 'quad', + 'variable' +] + +export default DataFactory diff --git a/README.md b/README.md index a58aece..6630aab 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # @rdfjs/data-model -[![Build Status](https://travis-ci.org/rdfjs/data-model.svg?branch=master)](https://travis-ci.org/rdfjs/data-model) +[![Build Status](https://img.shields.io/github/workflow/status/rdfjs-base/data-model/CI)](https://github.com/rdfjs-base/data-model/actions/workflows/ci.yaml) [![npm version](https://img.shields.io/npm/v/@rdfjs/data-model.svg)](https://www.npmjs.com/package/@rdfjs/data-model) -A basic implementation of the [RDFJS Data Model](http://rdf.js.org/). +A basic implementation of the [RDF/JS Data Model](http://rdf.js.org/). See [the specification](http://rdf.js.org/) for further details. diff --git a/bin/test.js b/bin/test.js index 887dc0a..64f6edb 100755 --- a/bin/test.js +++ b/bin/test.js @@ -1,14 +1,21 @@ #!/usr/bin/env node -const path = require('path') -const Mocha = require('mocha') +import { resolve } from 'path' +import Mocha from 'mocha' -const mocha = new Mocha() +async function main () { + const tests = new Mocha() -global.rdf = require(path.resolve(process.argv[2] || '')) + global.rdf = (await import(resolve(process.argv[2] || ''))).default -mocha.addFile(path.join(__dirname, '../test/index.js')).run(failures => { - process.on('exit', () => { - process.exit(failures) + tests.files.push((new URL('../test/index.js', import.meta.url)).pathname) + await tests.loadFilesAsync() + + tests.run(failures => { + process.on('exit', () => { + process.exit(failures) + }) }) -}) +} + +main() diff --git a/dist/rdf-data-model.js b/dist/rdf-data-model.js deleted file mode 100644 index 59f5a53..0000000 --- a/dist/rdf-data-model.js +++ /dev/null @@ -1,233 +0,0 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.rdf = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i(value: Iri): RDF.NamedNode; -export function blankNode(value?: string): RDF.BlankNode; -export function literal(value: string, languageOrDatatype?: string | RDF.NamedNode): RDF.Literal; -export function variable(value: string): RDF.Variable; -export function defaultGraph(): RDF.DefaultGraph; -export function triple( - subject: Q['subject'], predicate: Q['predicate'], object: Q['object']): Q; -export function quad( - subject: Q['subject'], predicate: Q['predicate'], object: Q['object'], graph?: Q['graph']): Q; diff --git a/index.js b/index.js index 1718b66..7b32f33 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ -const DataFactory = require('./lib/DataFactory.js') +import Factory from './Factory.js' -module.exports = DataFactory +const factory = new Factory() + +export default factory diff --git a/lib/BlankNode.js b/lib/BlankNode.js index f279ca5..51394df 100644 --- a/lib/BlankNode.js +++ b/lib/BlankNode.js @@ -1,6 +1,6 @@ class BlankNode { constructor (id) { - this.value = id || ('b' + (++BlankNode.nextId)) + this.value = id } equals (other) { @@ -10,6 +10,4 @@ class BlankNode { BlankNode.prototype.termType = 'BlankNode' -BlankNode.nextId = 0 - -module.exports = BlankNode +export default BlankNode diff --git a/lib/DataFactory.js b/lib/DataFactory.js deleted file mode 100644 index 8d33e84..0000000 --- a/lib/DataFactory.js +++ /dev/null @@ -1,66 +0,0 @@ -const BlankNode = require('./BlankNode.js') -const DefaultGraph = require('./DefaultGraph.js') -const fromTermRaw = require('./fromTerm.js') -const Literal = require('./Literal.js') -const NamedNode = require('./NamedNode.js') -const Quad = require('./Quad.js') -const Variable = require('./Variable.js') - -function namedNode (value) { - return new NamedNode(value) -} - -function blankNode (value) { - return new BlankNode(value) -} - -function literal (value, languageOrDatatype) { - if (typeof languageOrDatatype === 'string') { - if (languageOrDatatype.indexOf(':') === -1) { - return new Literal(value, languageOrDatatype) - } - - return new Literal(value, null, DataFactory.namedNode(languageOrDatatype)) - } - - return new Literal(value, null, languageOrDatatype) -} - -function variable (value) { - return new Variable(value) -} - -function defaultGraph () { - return DataFactory.defaultGraphInstance -} - -function triple (subject, predicate, object) { - return DataFactory.quad(subject, predicate, object) -} - -function quad (subject, predicate, object, graph) { - return new Quad(subject, predicate, object, graph || DataFactory.defaultGraphInstance) -} - -function fromTerm (original) { - return fromTermRaw.call(DataFactory, original) -} - -function fromQuad (original) { - return fromTermRaw.call(DataFactory, original) -} - -const DataFactory = { - namedNode, - blankNode, - literal, - variable, - defaultGraph, - triple, - quad, - fromTerm, - fromQuad, - defaultGraphInstance: new DefaultGraph() -} - -module.exports = DataFactory diff --git a/lib/DefaultGraph.js b/lib/DefaultGraph.js index 243ef5c..53d2fdd 100644 --- a/lib/DefaultGraph.js +++ b/lib/DefaultGraph.js @@ -7,4 +7,4 @@ class DefaultGraph { DefaultGraph.prototype.termType = 'DefaultGraph' DefaultGraph.prototype.value = '' -module.exports = DefaultGraph +export default DefaultGraph diff --git a/lib/Literal.js b/lib/Literal.js index eb4c317..0f40a27 100644 --- a/lib/Literal.js +++ b/lib/Literal.js @@ -1,28 +1,19 @@ -const NamedNode = require('./NamedNode.js') - class Literal { constructor (value, language, datatype) { this.value = value - this.datatype = Literal.stringDatatype - this.language = '' - - if (language) { - this.language = language - this.datatype = Literal.langStringDatatype - } else if (datatype) { - this.datatype = datatype - } + this.language = language + this.datatype = datatype } equals (other) { - return !!other && other.termType === this.termType && other.value === this.value && - other.language === this.language && other.datatype.equals(this.datatype) + return !!other && + other.termType === this.termType && + other.value === this.value && + other.language === this.language && + other.datatype.equals(this.datatype) } } Literal.prototype.termType = 'Literal' -Literal.langStringDatatype = new NamedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') -Literal.stringDatatype = new NamedNode('http://www.w3.org/2001/XMLSchema#string') - -module.exports = Literal +export default Literal diff --git a/lib/NamedNode.js b/lib/NamedNode.js index bcafc20..6033ed9 100644 --- a/lib/NamedNode.js +++ b/lib/NamedNode.js @@ -10,4 +10,4 @@ class NamedNode { NamedNode.prototype.termType = 'NamedNode' -module.exports = NamedNode +export default NamedNode diff --git a/lib/Quad.js b/lib/Quad.js index 5b5bee0..3dcfb2d 100644 --- a/lib/Quad.js +++ b/lib/Quad.js @@ -1,27 +1,23 @@ -const DefaultGraph = require('./DefaultGraph.js') - class Quad { constructor (subject, predicate, object, graph) { this.subject = subject this.predicate = predicate this.object = object - - if (graph) { - this.graph = graph - } else { - this.graph = new DefaultGraph() - } + this.graph = graph } equals (other) { // `|| !other.termType` is for backwards-compatibility with old factories without RDF* support. - return !!other && (other.termType === 'Quad' || !other.termType) && - other.subject.equals(this.subject) && other.predicate.equals(this.predicate) && - other.object.equals(this.object) && other.graph.equals(this.graph) + return !!other && + (other.termType === 'Quad' || !other.termType) && + other.subject.equals(this.subject) && + other.predicate.equals(this.predicate) && + other.object.equals(this.object) && + other.graph.equals(this.graph) } } Quad.prototype.termType = 'Quad' Quad.prototype.value = '' -module.exports = Quad +export default Quad diff --git a/lib/Variable.js b/lib/Variable.js index e71c423..b6c2707 100644 --- a/lib/Variable.js +++ b/lib/Variable.js @@ -10,4 +10,4 @@ class Variable { Variable.prototype.termType = 'Variable' -module.exports = Variable +export default Variable diff --git a/lib/fromTerm.js b/lib/fromTerm.js index b69ca35..a068262 100644 --- a/lib/fromTerm.js +++ b/lib/fromTerm.js @@ -1,38 +1,38 @@ -function fromTerm (original) { +function fromTerm (factory, original) { if (!original) { return null } if (original.termType === 'BlankNode') { - return this.blankNode(original.value) + return factory.blankNode(original.value) } if (original.termType === 'DefaultGraph') { - return this.defaultGraph() + return factory.defaultGraph() } if (original.termType === 'Literal') { - return this.literal(original.value, original.language || this.namedNode(original.datatype.value)) + return factory.literal(original.value, original.language || factory.namedNode(original.datatype.value)) } if (original.termType === 'NamedNode') { - return this.namedNode(original.value) + return factory.namedNode(original.value) } if (original.termType === 'Quad') { - const subject = this.fromTerm(original.subject) - const predicate = this.fromTerm(original.predicate) - const object = this.fromTerm(original.object) - const graph = this.fromTerm(original.graph) + const subject = factory.fromTerm(original.subject) + const predicate = factory.fromTerm(original.predicate) + const object = factory.fromTerm(original.object) + const graph = factory.fromTerm(original.graph) - return this.quad(subject, predicate, object, graph) + return factory.quad(subject, predicate, object, graph) } if (original.termType === 'Variable') { - return this.variable(original.value) + return factory.variable(original.value) } throw new Error(`unknown termType ${original.termType}`) } -module.exports = fromTerm +export default fromTerm diff --git a/package.json b/package.json index 683b89d..89d2531 100644 --- a/package.json +++ b/package.json @@ -1,25 +1,24 @@ { "name": "@rdfjs/data-model", "version": "1.3.4", - "description": "A basic implementation of the RDFJS Data Model", + "description": "A basic implementation of the RDF/JS Data Model", + "type": "module", "main": "index.js", "scripts": { "coverage": "codecov", - "prepublish": "browserify index.js --standalone rdf --outfile dist/rdf-data-model.js", - "test": "stricter-standard *.js bin/*.js lib/*.js test/*.js && c8 --reporter=lcov --reporter=text mocha" + "test": "stricter-standard && c8 --reporter=lcov --reporter=text mocha" }, "bin": { "rdfjs-data-model-test": "./bin/test.js" }, - "types": "./index.d.ts", "repository": { "type": "git", "url": "git://github.com/rdfjs-base/data-model.git" }, "keywords": [ + "data-model", "rdf", - "rdfjs", - "data-model" + "rdfjs" ], "author": "Thomas Bergwinkl (https://www.bergnet.org/people/bergi/card#me)", "license": "MIT", @@ -27,14 +26,10 @@ "url": "https://github.com/rdfjs-base/data-model/issues" }, "homepage": "https://github.com/rdfjs-base/data-model", - "dependencies": { - "@rdfjs/types": ">=1.0.1" - }, "devDependencies": { - "browserify": "^17.0.0", - "c8": "^7.7.3", - "codecov": "^3.8.2", - "mocha": "^9.0.1", + "c8": "^7.10.0", + "codecov": "^3.8.3", + "mocha": "^9.1.3", "stricter-standard": "^0.2.0" } } diff --git a/test/blank-node.test.js b/test/blank-node.test.js index 7d7b082..3ada5f3 100644 --- a/test/blank-node.test.js +++ b/test/blank-node.test.js @@ -1,66 +1,67 @@ -const { strictEqual, notStrictEqual } = require('assert') -const { describe, it } = require('mocha') +import { strictEqual, notStrictEqual } from 'assert' -function runTests (DataFactory) { - describe('.blankNode', function () { - it('should be a static method', function () { - strictEqual(typeof DataFactory.blankNode, 'function') +function runTests ({ factory, mocha }) { + const { describe, it } = mocha + + describe('.blankNode', () => { + it('should be a static method', () => { + strictEqual(typeof factory.blankNode, 'function') }) - it('should create an object with a termType property that contains the value "BlankNode"', function () { - const term = DataFactory.blankNode() + it('should create an object with a termType property that contains the value "BlankNode"', () => { + const term = factory.blankNode() strictEqual(term.termType, 'BlankNode') }) - it('should create an object with a value property that contains a unique identifier', function () { - const term1 = DataFactory.blankNode() - const term2 = DataFactory.blankNode() + it('should create an object with a value property that contains a unique identifier', () => { + const term1 = factory.blankNode() + const term2 = factory.blankNode() notStrictEqual(term1.value, term2.value) }) - it('should create an object with a value property that contains the given identifier', function () { + it('should create an object with a value property that contains the given identifier', () => { const id = 'b1' - const term = DataFactory.blankNode(id) + const term = factory.blankNode(id) strictEqual(term.value, id) }) - describe('.equals', function () { - it('should be a method', function () { - const term = DataFactory.blankNode() + describe('.equals', () => { + it('should be a method', () => { + const term = factory.blankNode() strictEqual(typeof term.equals, 'function') }) - it('should return true if termType and value are equal', function () { + it('should return true if termType and value are equal', () => { const id = 'b1' - const term = DataFactory.blankNode(id) + const term = factory.blankNode(id) const mock = { termType: 'BlankNode', value: id } strictEqual(term.equals(mock), true) }) - it('should return false if termType is not equal', function () { + it('should return false if termType is not equal', () => { const id = 'b1' - const term = DataFactory.blankNode(id) + const term = factory.blankNode(id) const mock = { termType: 'NamedNode', value: id } strictEqual(term.equals(mock), false) }) - it('should return false if value is not equal', function () { + it('should return false if value is not equal', () => { const id = 'b1' - const term = DataFactory.blankNode(id) + const term = factory.blankNode(id) const mock = { termType: 'BlankNode', value: id + '1' } strictEqual(term.equals(mock), false) }) - it('should return false if value is falsy', function () { + it('should return false if value is falsy', () => { const id = 'b1' - const term = DataFactory.blankNode(id) + const term = factory.blankNode(id) strictEqual(term.equals(null), false) }) @@ -68,4 +69,4 @@ function runTests (DataFactory) { }) } -module.exports = runTests +export default runTests diff --git a/test/default-graph.test.js b/test/default-graph.test.js index f707de2..2d9d690 100644 --- a/test/default-graph.test.js +++ b/test/default-graph.test.js @@ -1,47 +1,48 @@ -const { strictEqual } = require('assert') -const { describe, it } = require('mocha') +import { strictEqual } from 'assert' -function runTests (DataFactory) { - describe('.defaultGraph', function () { - it('should be a static method', function () { - strictEqual(typeof DataFactory.defaultGraph, 'function') +function runTests ({ factory, mocha }) { + const { describe, it } = mocha + + describe('.defaultGraph', () => { + it('should be a static method', () => { + strictEqual(typeof factory.defaultGraph, 'function') }) - it('should create an object with a termType property that contains the value "DefaultGraph"', function () { - const term = DataFactory.defaultGraph() + it('should create an object with a termType property that contains the value "DefaultGraph"', () => { + const term = factory.defaultGraph() strictEqual(term.termType, 'DefaultGraph') }) - it('should create an object with a value property that contains an empty string', function () { - const term = DataFactory.defaultGraph() + it('should create an object with a value property that contains an empty string', () => { + const term = factory.defaultGraph() strictEqual(term.value, '') }) - describe('.equals', function () { - it('should be a method', function () { - const term = DataFactory.defaultGraph() + describe('.equals', () => { + it('should be a method', () => { + const term = factory.defaultGraph() strictEqual(typeof term.equals, 'function') }) - it('should return true if termType and value are equal', function () { - const term = DataFactory.defaultGraph() + it('should return true if termType and value are equal', () => { + const term = factory.defaultGraph() const mock = { termType: 'DefaultGraph', value: '' } strictEqual(term.equals(mock), true) }) - it('should return false if termType is not equal', function () { - const term = DataFactory.defaultGraph() + it('should return false if termType is not equal', () => { + const term = factory.defaultGraph() const mock = { termType: 'NamedNode', value: '' } strictEqual(term.equals(mock), false) }) - it('should return false if value is falsy', function () { - const term = DataFactory.defaultGraph() + it('should return false if value is falsy', () => { + const term = factory.defaultGraph() strictEqual(term.equals(null), false) }) @@ -49,4 +50,4 @@ function runTests (DataFactory) { }) } -module.exports = runTests +export default runTests diff --git a/test/fromQuad.test.js b/test/fromQuad.test.js index 8f8c78f..709c11a 100644 --- a/test/fromQuad.test.js +++ b/test/fromQuad.test.js @@ -1,20 +1,21 @@ -const { notStrictEqual, strictEqual, throws } = require('assert') -const { describe, it } = require('mocha') +import { notStrictEqual, strictEqual, throws } from 'assert' -function runTests (DataFactory) { - describe('.fromQuad', function () { - it('should be a static method', function () { - strictEqual(typeof DataFactory.fromQuad, 'function') +function runTests ({ factory, mocha }) { + const { describe, it } = mocha + + describe('.fromQuad', () => { + it('should be a static method', () => { + strictEqual(typeof factory.fromQuad, 'function') }) it('should create a clone of the given Quad', () => { - const subject = DataFactory.blankNode() - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.literal('example') - const graph = DataFactory.namedNode('http://example.org/graph') - const original = DataFactory.quad(subject, predicate, object, graph) + const subject = factory.blankNode() + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.literal('example') + const graph = factory.namedNode('http://example.org/graph') + const original = factory.quad(subject, predicate, object, graph) - const term = DataFactory.fromQuad(original) + const term = factory.fromQuad(original) strictEqual(term.equals(original), true) notStrictEqual(term, original) @@ -25,7 +26,7 @@ function runTests (DataFactory) { }) it('should return null if null is given', () => { - const term = DataFactory.fromQuad(null) + const term = factory.fromQuad(null) strictEqual(term, null) }) @@ -34,10 +35,10 @@ function runTests (DataFactory) { const original = {} throws(() => { - DataFactory.fromQuad(original) + factory.fromQuad(original) }) }) }) } -module.exports = runTests +export default runTests diff --git a/test/fromTerm.test.js b/test/fromTerm.test.js index dee04c5..98678e0 100644 --- a/test/fromTerm.test.js +++ b/test/fromTerm.test.js @@ -1,51 +1,52 @@ -const { notStrictEqual, strictEqual, throws } = require('assert') -const { describe, it } = require('mocha') +import { notStrictEqual, strictEqual, throws } from 'assert' -function runTests (DataFactory) { - describe('.fromTerm', function () { - it('should be a static method', function () { - strictEqual(typeof DataFactory.fromTerm, 'function') +function runTests ({ factory, mocha }) { + const { describe, it } = mocha + + describe('.fromTerm', () => { + it('should be a static method', () => { + strictEqual(typeof factory.fromTerm, 'function') }) it('should create a clone of the given BlankNode', () => { - const original = DataFactory.blankNode() + const original = factory.blankNode() - const term = DataFactory.fromTerm(original) + const term = factory.fromTerm(original) strictEqual(term.equals(original), true) notStrictEqual(term, original) }) it('should create a clone of the given DefaultGraph', () => { - const original = DataFactory.defaultGraph() + const original = factory.defaultGraph() - const term = DataFactory.fromTerm(original) + const term = factory.fromTerm(original) strictEqual(term.equals(original), true) }) it('should create a clone of the given Literal', () => { - const original = DataFactory.literal('example') + const original = factory.literal('example') - const term = DataFactory.fromTerm(original) + const term = factory.fromTerm(original) strictEqual(term.equals(original), true) notStrictEqual(term, original) }) it('should create a clone of the given Literal with language', () => { - const original = DataFactory.literal('example', 'en') + const original = factory.literal('example', 'en') - const term = DataFactory.fromTerm(original) + const term = factory.fromTerm(original) strictEqual(term.equals(original), true) notStrictEqual(term, original) }) it('should create a clone of the given Literal with datatype', () => { - const original = DataFactory.literal('example', DataFactory.namedNode('http://example.org/')) + const original = factory.literal('example', factory.namedNode('http://example.org/')) - const term = DataFactory.fromTerm(original) + const term = factory.fromTerm(original) strictEqual(term.equals(original), true) notStrictEqual(term, original) @@ -53,22 +54,22 @@ function runTests (DataFactory) { }) it('should create a clone of the given NamedNode', () => { - const original = DataFactory.namedNode('http://example.org/') + const original = factory.namedNode('http://example.org/') - const term = DataFactory.fromTerm(original) + const term = factory.fromTerm(original) strictEqual(term.equals(original), true) notStrictEqual(term, original) }) it('should create a clone of the given Quad', () => { - const subject = DataFactory.blankNode() - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.literal('example') - const graph = DataFactory.namedNode('http://example.org/graph') - const original = DataFactory.quad(subject, predicate, object, graph) + const subject = factory.blankNode() + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.literal('example') + const graph = factory.namedNode('http://example.org/graph') + const original = factory.quad(subject, predicate, object, graph) - const term = DataFactory.fromTerm(original) + const term = factory.fromTerm(original) strictEqual(term.equals(original), true) notStrictEqual(term, original) @@ -79,16 +80,16 @@ function runTests (DataFactory) { }) it('should create a clone of the given Variable', () => { - const original = DataFactory.variable('v') + const original = factory.variable('v') - const term = DataFactory.fromTerm(original) + const term = factory.fromTerm(original) strictEqual(term.equals(original), true) notStrictEqual(term, original) }) it('should return null if null is given', () => { - const term = DataFactory.fromTerm(null) + const term = factory.fromTerm(null) strictEqual(term, null) }) @@ -97,10 +98,10 @@ function runTests (DataFactory) { const original = {} throws(() => { - DataFactory.fromTerm(original) + factory.fromTerm(original) }) }) }) } -module.exports = runTests +export default runTests diff --git a/test/index.js b/test/index.js index 04a8d5d..6554048 100644 --- a/test/index.js +++ b/test/index.js @@ -1,16 +1,35 @@ -function runTests (rdf) { - require('./named-node.test.js')(rdf) - require('./blank-node.test.js')(rdf) - require('./fromQuad.test.js')(rdf) - require('./fromTerm.test.js')(rdf) - require('./literal.test.js')(rdf) - require('./default-graph.test.js')(rdf) - require('./variable.test.js')(rdf) - require('./quad.test.js')(rdf) +import blankNodeTest from './blank-node.test.js' +import defaultGraphTest from './default-graph.test.js' +import fromQuadTest from './fromQuad.test.js' +import fromTermTest from './fromTerm.test.js' +import literalTest from './literal.test.js' +import namedNodeTest from './named-node.test.js' +import quadTest from './quad.test.js' +import variableTest from './variable.test.js' + +function runTests ({ factory, mocha }) { + if (!mocha) { + mocha = { describe: global.describe, it: global.it } + } + + const tests = [ + blankNodeTest, + defaultGraphTest, + fromQuadTest, + fromTermTest, + literalTest, + namedNodeTest, + quadTest, + variableTest + ] + + for (const test of tests) { + test({ factory, mocha }) + } } if (global.rdf) { - runTests(global.rdf) + runTests({ factory: global.rdf }) } -module.exports = runTests +export default runTests diff --git a/test/literal.test.js b/test/literal.test.js index ad210c1..68cce95 100644 --- a/test/literal.test.js +++ b/test/literal.test.js @@ -1,147 +1,148 @@ -const { strictEqual } = require('assert') -const { describe, it } = require('mocha') +import { strictEqual } from 'assert' -function runTests (DataFactory) { - describe('.literal', function () { - it('should be a static method', function () { - strictEqual(typeof DataFactory.literal, 'function') +function runTests ({ factory, mocha }) { + const { describe, it } = mocha + + describe('.literal', () => { + it('should be a static method', () => { + strictEqual(typeof factory.literal, 'function') }) - it('should create an object with a termType property that contains the value "Literal"', function () { - const term = DataFactory.literal() + it('should create an object with a termType property that contains the value "Literal"', () => { + const term = factory.literal() strictEqual(term.termType, 'Literal') }) - it('should create an object with a value property that contains the given string', function () { + it('should create an object with a value property that contains the given string', () => { const string = 'example' - const term = DataFactory.literal(string) + const term = factory.literal(string) strictEqual(term.value, string) }) - it('should create an object with a language property that contains an empty string', function () { + it('should create an object with a language property that contains an empty string', () => { const string = 'example' - const term = DataFactory.literal(string) + const term = factory.literal(string) strictEqual(term.language, '') }) - it('should create an object with a language property that contains the given language string', function () { + it('should create an object with a language property that contains the given language string', () => { const string = 'example' const language = 'en' - const term = DataFactory.literal(string, language) + const term = factory.literal(string, language) strictEqual(term.language, language) }) - it('should create an object with a datatype property that contains a NamedNode with the value "http://www.w3.org/2001/XMLSchema#string"', function () { + it('should create an object with a datatype property that contains a NamedNode with the value "http://www.w3.org/2001/XMLSchema#string"', () => { const string = 'example' - const term = DataFactory.literal(string) + const term = factory.literal(string) strictEqual(term.datatype.termType, 'NamedNode') strictEqual(term.datatype.value, 'http://www.w3.org/2001/XMLSchema#string') }) - it('should create an object with a datatype property that contains a NamedNode with the value "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"', function () { + it('should create an object with a datatype property that contains a NamedNode with the value "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"', () => { const string = 'example' const language = 'en' - const term = DataFactory.literal(string, language) + const term = factory.literal(string, language) strictEqual(term.datatype.termType, 'NamedNode') strictEqual(term.datatype.value, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') }) - it('should create an object with a datatype property that contains a NamedNode with the given IRI', function () { + it('should create an object with a datatype property that contains a NamedNode with the given IRI', () => { const string = 'example' - const datatype = DataFactory.namedNode('http://example.org') - const term = DataFactory.literal(string, datatype) + const datatype = factory.namedNode('http://example.org') + const term = factory.literal(string, datatype) strictEqual(term.datatype.termType, 'NamedNode') strictEqual(term.datatype.value, datatype.value) }) - describe('.equals', function () { - it('should be a method', function () { - const term = DataFactory.literal('') + describe('.equals', () => { + it('should be a method', () => { + const term = factory.literal('') strictEqual(typeof term.equals, 'function') }) - it('should return true if termType, value, language and datatype are equal', function () { + it('should return true if termType, value, language and datatype are equal', () => { const string = 'example' const language = 'en' - const term = DataFactory.literal(string, language) + const term = factory.literal(string, language) const mock = { termType: 'Literal', value: string, language: language, - datatype: DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') + datatype: factory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') } strictEqual(term.equals(mock), true) }) - it('should return false if termType is not equal', function () { + it('should return false if termType is not equal', () => { const string = 'example' const language = 'en' - const term = DataFactory.literal(string, language) + const term = factory.literal(string, language) const mock = { termType: 'NamedNode', value: string, language: language, - datatype: DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') + datatype: factory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') } strictEqual(term.equals(mock), false) }) - it('should return false if value is not equal', function () { + it('should return false if value is not equal', () => { const string = 'example' const language = 'en' - const term = DataFactory.literal(string, language) + const term = factory.literal(string, language) const mock = { termType: 'Literal', value: string + '1', language: language, - datatype: DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') + datatype: factory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') } strictEqual(term.equals(mock), false) }) - it('should return false if language is not equal', function () { + it('should return false if language is not equal', () => { const string = 'example' const language = 'en' - const term = DataFactory.literal(string, language) + const term = factory.literal(string, language) const mock = { termType: 'Literal', value: string, language: 'de', - datatype: DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') + datatype: factory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') } strictEqual(term.equals(mock), false) }) - it('should return false if datatype is not equal', function () { + it('should return false if datatype is not equal', () => { const string = 'example' const language = 'en' - const term = DataFactory.literal(string, language) + const term = factory.literal(string, language) const mock = { termType: 'Literal', value: string, language: language, - datatype: DataFactory.namedNode('http://example.org') + datatype: factory.namedNode('http://example.org') } strictEqual(term.equals(mock), false) }) - it('should return false if value is falsy', function () { + it('should return false if value is falsy', () => { const string = 'example' const language = 'en' - const term = DataFactory.literal(string, language) + const term = factory.literal(string, language) strictEqual(term.equals(null), false) }) @@ -149,4 +150,4 @@ function runTests (DataFactory) { }) } -module.exports = runTests +export default runTests diff --git a/test/named-node.test.js b/test/named-node.test.js index 812b4cd..91874f9 100644 --- a/test/named-node.test.js +++ b/test/named-node.test.js @@ -1,61 +1,62 @@ -const { strictEqual } = require('assert') -const { describe, it } = require('mocha') +import { strictEqual } from 'assert' -function runTests (DataFactory) { - describe('.namedNode', function () { - it('should be a static method', function () { - strictEqual(typeof DataFactory.namedNode, 'function') +function runTests ({ factory, mocha }) { + const { describe, it } = mocha + + describe('.namedNode', () => { + it('should be a static method', () => { + strictEqual(typeof factory.namedNode, 'function') }) - it('should create an object with a termType property that contains the value "NamedNode"', function () { + it('should create an object with a termType property that contains the value "NamedNode"', () => { const iri = 'http://example.org' - const term = DataFactory.namedNode(iri) + const term = factory.namedNode(iri) strictEqual(term.termType, 'NamedNode') }) - it('should create an object with a value property that contains the given IRI', function () { + it('should create an object with a value property that contains the given IRI', () => { const iri = 'http://example.org' - const term = DataFactory.namedNode(iri) + const term = factory.namedNode(iri) strictEqual(term.value, iri) }) - describe('.equals', function () { - it('should be a method', function () { + describe('.equals', () => { + it('should be a method', () => { const iri = 'http://example.org' - const term = DataFactory.namedNode(iri) + const term = factory.namedNode(iri) strictEqual(typeof term.equals, 'function') }) - it('should return true if termType and value are equal', function () { + it('should return true if termType and value are equal', () => { const iri = 'http://example.org' - const term = DataFactory.namedNode(iri) + const term = factory.namedNode(iri) const mock = { termType: 'NamedNode', value: iri } strictEqual(term.equals(mock), true) }) - it('should return false if termType is not equal', function () { + it('should return false if termType is not equal', () => { const iri = 'http://example.org' - const term = DataFactory.namedNode(iri) + const term = factory.namedNode(iri) const mock = { termType: 'BlankNode', value: iri } strictEqual(term.equals(mock), false) }) - it('should return false if value is not equal', function () { + it('should return false if value is not equal', () => { const iri = 'http://example.org' - const term = DataFactory.namedNode(iri) + const term = factory.namedNode(iri) const mock = { termType: 'NamedNode', value: iri + '1' } strictEqual(term.equals(mock), false) }) - it('should return false if value is falsy', function () { + it('should return false if value is falsy', () => { const iri = 'http://example.org' - const term = DataFactory.namedNode(iri) + const term = factory.namedNode(iri) strictEqual(term.equals(null), false) }) @@ -63,4 +64,4 @@ function runTests (DataFactory) { }) } -module.exports = runTests +export default runTests diff --git a/test/quad.test.js b/test/quad.test.js index ed6747b..3222eb5 100644 --- a/test/quad.test.js +++ b/test/quad.test.js @@ -1,18 +1,19 @@ -const { strictEqual } = require('assert') -const { describe, it } = require('mocha') +import { strictEqual } from 'assert' -function runTests (DataFactory) { - describe('.quad', function () { - it('should be a static method', function () { - strictEqual(typeof DataFactory.quad, 'function') +function runTests ({ factory, mocha }) { + const { describe, it } = mocha + + describe('.quad', () => { + it('should be a static method', () => { + strictEqual(typeof factory.quad, 'function') }) - it('should create an object with .subject, .predicate, .object and .graph with the given values', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const quad = DataFactory.quad(subject, predicate, object, graph) + it('should create an object with .subject, .predicate, .object and .graph with the given values', () => { + const subject = factory.namedNode('http://example.org/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.namedNode('http://example.org/object') + const graph = factory.namedNode('http://example.org/graph') + const quad = factory.quad(subject, predicate, object, graph) strictEqual(subject.equals(quad.subject), true) strictEqual(predicate.equals(quad.predicate), true) @@ -23,12 +24,12 @@ function runTests (DataFactory) { strictEqual(quad.value, '') }) - it('should create an object .graph set to DefaultGraph if the argument isn\'t given', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.defaultGraph() - const quad = DataFactory.quad(subject, predicate, object) + it('should create an object .graph set to DefaultGraph if the argument isn\'t given', () => { + const subject = factory.namedNode('http://example.org/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.namedNode('http://example.org/object') + const graph = factory.defaultGraph() + const quad = factory.quad(subject, predicate, object) strictEqual(quad.graph.equals(graph), true) @@ -36,129 +37,129 @@ function runTests (DataFactory) { strictEqual(quad.value, '') }) - describe('.equals', function () { - it('should return true if the other quad contains the same subject, predicate, object and graph', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const quad1 = DataFactory.quad(subject, predicate, object, graph) - const quad2 = DataFactory.quad(subject, predicate, object, graph) + describe('.equals', () => { + it('should return true if the other quad contains the same subject, predicate, object and graph', () => { + const subject = factory.namedNode('http://example.org/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.namedNode('http://example.org/object') + const graph = factory.namedNode('http://example.org/graph') + const quad1 = factory.quad(subject, predicate, object, graph) + const quad2 = factory.quad(subject, predicate, object, graph) strictEqual(quad1.equals(quad2), true) }) - it('should return true even if the other equal quad is from a non-RDF* factory', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const quad1 = DataFactory.quad(subject, predicate, object, graph) + it('should return true even if the other equal quad is from a non-RDF* factory', () => { + const subject = factory.namedNode('http://example.org/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.namedNode('http://example.org/object') + const graph = factory.namedNode('http://example.org/graph') + const quad1 = factory.quad(subject, predicate, object, graph) const quad2 = { subject, predicate, object, graph } strictEqual(quad1.equals(quad2), true) }) - it('should return false if the subject of the other quad is not the same', function () { - const subject1 = DataFactory.namedNode('http://example.org/subject') - const subject2 = DataFactory.namedNode('http://example.com/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const quad1 = DataFactory.quad(subject1, predicate, object, graph) - const quad2 = DataFactory.quad(subject2, predicate, object, graph) + it('should return false if the subject of the other quad is not the same', () => { + const subject1 = factory.namedNode('http://example.org/subject') + const subject2 = factory.namedNode('http://example.com/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.namedNode('http://example.org/object') + const graph = factory.namedNode('http://example.org/graph') + const quad1 = factory.quad(subject1, predicate, object, graph) + const quad2 = factory.quad(subject2, predicate, object, graph) strictEqual(quad1.equals(quad2), false) }) - it('should return false even if the other non-equal quad is from a non-RDF* factory', function () { - const subject1 = DataFactory.namedNode('http://example.org/subject') - const subject2 = DataFactory.namedNode('http://example.com/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const quad1 = DataFactory.quad(subject1, predicate, object, graph) + it('should return false even if the other non-equal quad is from a non-RDF* factory', () => { + const subject1 = factory.namedNode('http://example.org/subject') + const subject2 = factory.namedNode('http://example.com/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.namedNode('http://example.org/object') + const graph = factory.namedNode('http://example.org/graph') + const quad1 = factory.quad(subject1, predicate, object, graph) const quad2 = { subject: subject2, predicate, object, graph } strictEqual(quad1.equals(quad2), false) }) - it('should return false if the predicate of the other quad is not the same', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate1 = DataFactory.namedNode('http://example.org/predicate') - const predicate2 = DataFactory.namedNode('http://example.com/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const quad1 = DataFactory.quad(subject, predicate1, object, graph) - const quad2 = DataFactory.quad(subject, predicate2, object, graph) + it('should return false if the predicate of the other quad is not the same', () => { + const subject = factory.namedNode('http://example.org/subject') + const predicate1 = factory.namedNode('http://example.org/predicate') + const predicate2 = factory.namedNode('http://example.com/predicate') + const object = factory.namedNode('http://example.org/object') + const graph = factory.namedNode('http://example.org/graph') + const quad1 = factory.quad(subject, predicate1, object, graph) + const quad2 = factory.quad(subject, predicate2, object, graph) strictEqual(quad1.equals(quad2), false) }) - it('should return false if the object of the other quad is not the same', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object1 = DataFactory.namedNode('http://example.org/object') - const object2 = DataFactory.namedNode('http://example.com/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const quad1 = DataFactory.quad(subject, predicate, object1, graph) - const quad2 = DataFactory.quad(subject, predicate, object2, graph) + it('should return false if the object of the other quad is not the same', () => { + const subject = factory.namedNode('http://example.org/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object1 = factory.namedNode('http://example.org/object') + const object2 = factory.namedNode('http://example.com/object') + const graph = factory.namedNode('http://example.org/graph') + const quad1 = factory.quad(subject, predicate, object1, graph) + const quad2 = factory.quad(subject, predicate, object2, graph) strictEqual(quad1.equals(quad2), false) }) - it('should return false if the graph of the other quad is not the same', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph1 = DataFactory.namedNode('http://example.org/graph') - const graph2 = DataFactory.namedNode('http://example.com/graph') - const quad1 = DataFactory.quad(subject, predicate, object, graph1) - const quad2 = DataFactory.quad(subject, predicate, object, graph2) + it('should return false if the graph of the other quad is not the same', () => { + const subject = factory.namedNode('http://example.org/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.namedNode('http://example.org/object') + const graph1 = factory.namedNode('http://example.org/graph') + const graph2 = factory.namedNode('http://example.com/graph') + const quad1 = factory.quad(subject, predicate, object, graph1) + const quad2 = factory.quad(subject, predicate, object, graph2) strictEqual(quad1.equals(quad2), false) }) - it('should return false if value is falsy', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const quad = DataFactory.quad(subject, predicate, object, graph) + it('should return false if value is falsy', () => { + const subject = factory.namedNode('http://example.org/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.namedNode('http://example.org/object') + const graph = factory.namedNode('http://example.org/graph') + const quad = factory.quad(subject, predicate, object, graph) strictEqual(quad.equals(null), false) }) - it('should return false if value is another term', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const quad = DataFactory.quad(subject, predicate, object, graph) - - strictEqual(quad.equals(DataFactory.namedNode('http://example.org/subject')), false) - strictEqual(quad.equals(DataFactory.literal('abc')), false) - strictEqual(quad.equals(DataFactory.variable('var')), false) - strictEqual(quad.equals(DataFactory.blankNode('bnode')), false) - strictEqual(quad.equals(DataFactory.defaultGraph()), false) + it('should return false if value is another term', () => { + const subject = factory.namedNode('http://example.org/subject') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.namedNode('http://example.org/object') + const graph = factory.namedNode('http://example.org/graph') + const quad = factory.quad(subject, predicate, object, graph) + + strictEqual(quad.equals(factory.namedNode('http://example.org/subject')), false) + strictEqual(quad.equals(factory.literal('abc')), false) + strictEqual(quad.equals(factory.variable('var')), false) + strictEqual(quad.equals(factory.blankNode('bnode')), false) + strictEqual(quad.equals(factory.defaultGraph()), false) }) - it('should return true for an equal nested quad', function () { - const subject = DataFactory.quad( - DataFactory.namedNode('http://example.org/subjectInner1'), - DataFactory.namedNode('http://example.org/predicateInner1'), - DataFactory.namedNode('http://example.org/objectInner1') + it('should return true for an equal nested quad', () => { + const subject = factory.quad( + factory.namedNode('http://example.org/subjectInner1'), + factory.namedNode('http://example.org/predicateInner1'), + factory.namedNode('http://example.org/objectInner1') ) - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.quad( - DataFactory.namedNode('http://example.org/subjectInner2'), - DataFactory.namedNode('http://example.org/predicateInner2'), - DataFactory.namedNode('http://example.org/objectInner2'), - DataFactory.namedNode('http://example.org/graphInner2') + const predicate = factory.namedNode('http://example.org/predicate') + const object = factory.quad( + factory.namedNode('http://example.org/subjectInner2'), + factory.namedNode('http://example.org/predicateInner2'), + factory.namedNode('http://example.org/objectInner2'), + factory.namedNode('http://example.org/graphInner2') ) - const graph = DataFactory.namedNode('http://example.org/graph') - const quad1 = DataFactory.quad(subject, predicate, object, graph) - const quad2 = DataFactory.quad(subject, predicate, object, graph) + const graph = factory.namedNode('http://example.org/graph') + const quad1 = factory.quad(subject, predicate, object, graph) + const quad2 = factory.quad(subject, predicate, object, graph) strictEqual(quad1.equals(quad2), true) }) @@ -166,4 +167,4 @@ function runTests (DataFactory) { }) } -module.exports = runTests +export default runTests diff --git a/test/test.js b/test/test.js index d7ef1cd..7472733 100644 --- a/test/test.js +++ b/test/test.js @@ -1,23 +1,7 @@ -const { strictEqual } = require('assert') -const { describe, it } = require('mocha') -const rdf = require('..') +import mocha from 'mocha' +import rdf from '../index.js' +import runTests from './index.js' -describe('@rdfjs/data-model', () => { - require('.')(rdf) - - // .triple is a legacy method and not required according to the spec - require('./triple.test.js')(rdf) - - it('should allow destructuring assignment of factory methods', () => { - const factory = { ...rdf } - - strictEqual(typeof factory.namedNode, 'function') - strictEqual(typeof factory.blankNode, 'function') - strictEqual(typeof factory.literal, 'function') - strictEqual(typeof factory.variable, 'function') - strictEqual(typeof factory.defaultGraph, 'function') - strictEqual(typeof factory.quad, 'function') - strictEqual(typeof factory.fromTerm, 'function') - strictEqual(typeof factory.fromQuad, 'function') - }) +mocha.describe('@rdfjs/data-model', () => { + runTests({ factory: rdf, mocha }) }) diff --git a/test/triple.test.js b/test/triple.test.js deleted file mode 100644 index ba78635..0000000 --- a/test/triple.test.js +++ /dev/null @@ -1,99 +0,0 @@ -const { strictEqual } = require('assert') -const { describe, it } = require('mocha') - -function runTests (DataFactory) { - describe('.triple', function () { - it('should be a static method', function () { - strictEqual(typeof DataFactory.triple, 'function') - }) - - it('should create an object with .subject, .predicate and .object with the given values and .graph set to DefaultGraph', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const triple = DataFactory.triple(subject, predicate, object) - - strictEqual(subject.equals(triple.subject), true) - strictEqual(predicate.equals(triple.predicate), true) - strictEqual(object.equals(triple.object), true) - strictEqual(DataFactory.defaultGraph().equals(triple.graph), true) - }) - - it('should ignore a 4th parameter and always use DefaultGraph', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const triple = DataFactory.triple(subject, predicate, object, graph) - - strictEqual(DataFactory.defaultGraph().equals(triple.graph), true) - }) - - describe('.equals', function () { - it('should return true if the other triple contains the same subject, predicate and object', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const triple1 = DataFactory.triple(subject, predicate, object) - const triple2 = DataFactory.triple(subject, predicate, object) - - strictEqual(triple1.equals(triple2), true) - }) - - it('should return false if the subject of the other triple is not the same', function () { - const subject1 = DataFactory.namedNode('http://example.org/subject') - const subject2 = DataFactory.namedNode('http://example.com/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const triple1 = DataFactory.triple(subject1, predicate, object) - const triple2 = DataFactory.triple(subject2, predicate, object) - - strictEqual(triple1.equals(triple2), false) - }) - - it('should return false if the predicate of the other triple is not the same', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate1 = DataFactory.namedNode('http://example.org/predicate') - const predicate2 = DataFactory.namedNode('http://example.com/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const triple1 = DataFactory.triple(subject, predicate1, object) - const triple2 = DataFactory.triple(subject, predicate2, object) - - strictEqual(triple1.equals(triple2), false) - }) - - it('should return false if the object of the other triple is not the same', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object1 = DataFactory.namedNode('http://example.org/object') - const object2 = DataFactory.namedNode('http://example.com/object') - const triple1 = DataFactory.triple(subject, predicate, object1) - const triple2 = DataFactory.triple(subject, predicate, object2) - - strictEqual(triple1.equals(triple2), false) - }) - - it('should return false if the graph of the other quad is not the same', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const graph = DataFactory.namedNode('http://example.org/graph') - const triple = DataFactory.triple(subject, predicate, object) - const quad = DataFactory.quad(subject, predicate, object, graph) - - strictEqual(triple.equals(quad), false) - }) - - it('should return false if value is falsy', function () { - const subject = DataFactory.namedNode('http://example.org/subject') - const predicate = DataFactory.namedNode('http://example.org/predicate') - const object = DataFactory.namedNode('http://example.org/object') - const triple = DataFactory.triple(subject, predicate, object) - - strictEqual(triple.equals(null), false) - }) - }) - }) -} - -module.exports = runTests diff --git a/test/variable.test.js b/test/variable.test.js index 4f5e50d..827a583 100644 --- a/test/variable.test.js +++ b/test/variable.test.js @@ -1,66 +1,67 @@ -const { strictEqual } = require('assert') -const { describe, it } = require('mocha') +import { strictEqual } from 'assert' + +function runTests ({ factory, mocha }) { + const { describe, it } = mocha -function runTests (DataFactory) { // support for Variable is optional - if (!DataFactory.variable) { + if (!factory.variable) { return } - describe('.variable', function () { - it('should be a static method', function () { - strictEqual(typeof DataFactory.variable, 'function') + describe('.variable', () => { + it('should be a static method', () => { + strictEqual(typeof factory.variable, 'function') }) - it('should create an object with a termType property that contains the value "Variable"', function () { + it('should create an object with a termType property that contains the value "Variable"', () => { const name = 'v' - const term = DataFactory.variable(name) + const term = factory.variable(name) strictEqual(term.termType, 'Variable') }) - it('should create an object with a value property that contains the given name', function () { + it('should create an object with a value property that contains the given name', () => { const name = 'v' - const term = DataFactory.variable(name) + const term = factory.variable(name) strictEqual(term.value, name) }) - describe('.equals', function () { - it('should be a method', function () { + describe('.equals', () => { + it('should be a method', () => { const name = 'v' - const term = DataFactory.variable(name) + const term = factory.variable(name) strictEqual(typeof term.equals, 'function') }) - it('should return true if termType and value are equal', function () { + it('should return true if termType and value are equal', () => { const name = 'v' - const term = DataFactory.variable(name) + const term = factory.variable(name) const mock = { termType: 'Variable', value: name } strictEqual(term.equals(mock), true) }) - it('should return false if termType is not equal', function () { + it('should return false if termType is not equal', () => { const name = 'v' - const term = DataFactory.variable(name) + const term = factory.variable(name) const mock = { termType: 'NamedNode', value: name } strictEqual(term.equals(mock), false) }) - it('should return false if value is not equal', function () { + it('should return false if value is not equal', () => { const name = 'v' - const term = DataFactory.variable(name) + const term = factory.variable(name) const mock = { termType: 'Variable', value: name + '1' } strictEqual(term.equals(mock), false) }) - it('should return false if value is falsy', function () { + it('should return false if value is falsy', () => { const name = 'v' - const term = DataFactory.variable(name) + const term = factory.variable(name) strictEqual(term.equals(null), false) }) @@ -68,4 +69,4 @@ function runTests (DataFactory) { }) } -module.exports = runTests +export default runTests