Skip to content

Commit

Permalink
Merge pull request #32 from rdf-ext/path-lint-test
Browse files Browse the repository at this point in the history
feat: use path groups when required, added path tests, code formating
  • Loading branch information
bergos authored Sep 29, 2024
2 parents fd9ad36 + 35d5030 commit f9e988e
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 32 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import Path from './lib/Path.js'
import Select from './lib/Select.js'
import SubQuery from './lib/SubQuery.js'
import Union from './lib/Union.js'
import smartAddPatterns from './lib/utils/smartAddPatterns.js'
import ZeroOrMorePath from './lib/ZeroOrMorePath.js'
import ZeroOrOnePath from './lib/ZeroOrOnePath.js'
import smartAddPatterns from './lib/utils/smartAddPatterns.js'

const eq = (a, b) => new CompareFilter('=', a, b)
const ne = (a, b) => new CompareFilter('!=', a, b)
Expand Down
8 changes: 4 additions & 4 deletions lib/AlternativePath.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Node from './Node.js'
import termOrNodeToString from './utils/termOrNodeToString.js'
import termOrNodeToPathString from './utils/termOrNodeToPathString.js'

class Pattern extends Node {
class AlternativePath extends Node {
constructor (elements) {
super({ type: 'AlternativePath' })

this.attr.elements = elements
}

toStringStart () {
return this.attr.elements.map(element => termOrNodeToString(element)).join('|')
return this.attr.elements.map(element => termOrNodeToPathString(element)).join('|')
}
}

export default Pattern
export default AlternativePath
8 changes: 4 additions & 4 deletions lib/InversePath.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Node from './Node.js'
import termOrNodeToString from './utils/termOrNodeToString.js'
import termOrNodeToPathString from './utils/termOrNodeToPathString.js'

class Pattern extends Node {
class InversePath extends Node {
constructor (element) {
super({ type: 'InversePath' })

this.attr.element = element
}

toStringStart () {
return ['^', termOrNodeToString(this.attr.element)].join('')
return ['^', termOrNodeToPathString(this.attr.element)].join('')
}
}

export default Pattern
export default InversePath
16 changes: 11 additions & 5 deletions lib/NegatedPropertySet.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import Node from './Node.js'
import termOrNodeToString from './utils/termOrNodeToString.js'

class Pattern extends Node {
class NegatedPropertySet extends Node {
constructor (element) {
super({ type: 'NegatedPropertySet' })

this.attr.element = element
}

toStringStart () {
return ['!(',
termOrNodeToString(this.attr.element),
')'].join('')
if (Array.isArray(this.attr.element)) {
return [
'!(',
this.attr.element.map(element => termOrNodeToString(element)).join('|'),
')'
].join('')
}

return ['!', termOrNodeToString(this.attr.element)].join('')
}
}

export default Pattern
export default NegatedPropertySet
10 changes: 4 additions & 6 deletions lib/OneOrMorePath.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import Node from './Node.js'
import termOrNodeToString from './utils/termOrNodeToString.js'
import termOrNodeToPathString from './utils/termOrNodeToPathString.js'

class Pattern extends Node {
class OneOrMorePath extends Node {
constructor (element) {
super({ type: 'OneOrMorePath' })

this.attr.element = element
}

toStringStart () {
return ['(',
termOrNodeToString(this.attr.element),
')+'].join('')
return [termOrNodeToPathString(this.attr.element), '+'].join('')
}
}

export default Pattern
export default OneOrMorePath
10 changes: 4 additions & 6 deletions lib/ZeroOrMorePath.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import Node from './Node.js'
import termOrNodeToString from './utils/termOrNodeToString.js'
import termOrNodeToPathString from './utils/termOrNodeToPathString.js'

class Pattern extends Node {
class ZeroOrMorePath extends Node {
constructor (element) {
super({ type: 'ZeroOrMorePath' })

this.attr.element = element
}

toStringStart () {
return ['(',
termOrNodeToString(this.attr.element),
')*'].join('')
return [termOrNodeToPathString(this.attr.element), '*'].join('')
}
}

export default Pattern
export default ZeroOrMorePath
10 changes: 4 additions & 6 deletions lib/ZeroOrOnePath.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import Node from './Node.js'
import termOrNodeToString from './utils/termOrNodeToString.js'
import termOrNodeToPathString from './utils/termOrNodeToPathString.js'

class Pattern extends Node {
class ZeroOrOnePath extends Node {
constructor (element) {
super({ type: 'ZeroOrOnePath' })

this.attr.element = element
}

toStringStart () {
return ['(',
termOrNodeToString(this.attr.element),
')?'].join('')
return [termOrNodeToPathString(this.attr.element), '?'].join('')
}
}

export default Pattern
export default ZeroOrOnePath
7 changes: 7 additions & 0 deletions lib/utils/termOrNodeToPathString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import toNT from '@rdfjs/to-ntriples'

function termOrNodeToPathString (termOrNode) {
return (termOrNode.termType && toNT(termOrNode)) || `(${termOrNode.toString()})`
}

export default termOrNodeToPathString
31 changes: 31 additions & 0 deletions test/AlternativePath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { strictEqual } from 'assert'
import { describe, it } from 'mocha'
import AlternativePath from '../lib/AlternativePath.js'
import Path from '../lib/Path.js'
import ignoreWhitespaceEqual from './support/ignoreWhitespaceEqual.js'
import ns from './support/namespace.js'

describe('AlternativePath', () => {
it('should be a constructor', () => {
strictEqual(typeof AlternativePath, 'function')
})

it('should create an alternative path', () => {
const path = new AlternativePath([ns.ex.measure, ns.ex.temperature])

const expected = '<http://example.org/measure>|<http://example.org/temperature>'

ignoreWhitespaceEqual(path, expected)
})

it('should create an alternative path with a group', () => {
const path = new AlternativePath([
ns.ex.temperature,
new Path([ns.ex.measure, ns.ex.temperature])
])

const expected = '<http://example.org/temperature>|(<http://example.org/measure>/<http://example.org/temperature>)'

ignoreWhitespaceEqual(path, expected)
})
})
28 changes: 28 additions & 0 deletions test/InversePath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { strictEqual } from 'assert'
import { describe, it } from 'mocha'
import InversePath from '../lib/InversePath.js'
import Path from '../lib/Path.js'
import ignoreWhitespaceEqual from './support/ignoreWhitespaceEqual.js'
import ns from './support/namespace.js'

describe('InversePath', () => {
it('should be a constructor', () => {
strictEqual(typeof InversePath, 'function')
})

it('should create an inverse path', () => {
const path = new InversePath(ns.ex.measure)

const expected = '^<http://example.org/measure>'

ignoreWhitespaceEqual(path, expected)
})

it('should create an inverse path with a group', () => {
const path = new InversePath(new Path([ns.ex.measure, ns.ex.temperature]))

const expected = '^(<http://example.org/measure>/<http://example.org/temperature>)'

ignoreWhitespaceEqual(path, expected)
})
})
31 changes: 31 additions & 0 deletions test/NegatedPropertySet.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { strictEqual } from 'assert'
import { describe, it } from 'mocha'
import InversePath from '../lib/InversePath.js'
import NegatedPropertySet from '../lib/NegatedPropertySet.js'
import ignoreWhitespaceEqual from './support/ignoreWhitespaceEqual.js'
import ns from './support/namespace.js'

describe('NegatedPropertySet', () => {
it('should be a constructor', () => {
strictEqual(typeof NegatedPropertySet, 'function')
})

it('should create a negated property set', () => {
const path = new NegatedPropertySet(ns.ex.measure)

const expected = '!<http://example.org/measure>'

ignoreWhitespaceEqual(path, expected)
})

it('should create an negated property set multiple paths', () => {
const path = new NegatedPropertySet([
ns.ex.measure,
new InversePath(ns.ex.temperature)
])

const expected = '!(<http://example.org/measure>|^<http://example.org/temperature>)'

ignoreWhitespaceEqual(path, expected)
})
})
28 changes: 28 additions & 0 deletions test/OneOrMorePath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { strictEqual } from 'assert'
import { describe, it } from 'mocha'
import OneOrMorePath from '../lib/OneOrMorePath.js'
import Path from '../lib/Path.js'
import ignoreWhitespaceEqual from './support/ignoreWhitespaceEqual.js'
import ns from './support/namespace.js'

describe('OneOrMorePath', () => {
it('should be a constructor', () => {
strictEqual(typeof OneOrMorePath, 'function')
})

it('should create a one or more path', () => {
const path = new OneOrMorePath(ns.ex.measure)

const expected = '<http://example.org/measure>+'

ignoreWhitespaceEqual(path, expected)
})

it('should create a one or more path with a group', () => {
const path = new OneOrMorePath(new Path([ns.ex.measure, ns.ex.temperature]))

const expected = '(<http://example.org/measure>/<http://example.org/temperature>)+'

ignoreWhitespaceEqual(path, expected)
})
})
19 changes: 19 additions & 0 deletions test/Path.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { strictEqual } from 'assert'
import { describe, it } from 'mocha'
import Path from '../lib/Path.js'
import ignoreWhitespaceEqual from './support/ignoreWhitespaceEqual.js'
import ns from './support/namespace.js'

describe('Path', () => {
it('should be a constructor', () => {
strictEqual(typeof Path, 'function')
})

it('should create a sequence path', () => {
const path = new Path([ns.ex.measure, ns.ex.temperature])

const expected = '<http://example.org/measure>/<http://example.org/temperature>'

ignoreWhitespaceEqual(path, expected)
})
})
28 changes: 28 additions & 0 deletions test/ZeroOrMorePath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { strictEqual } from 'assert'
import { describe, it } from 'mocha'
import Path from '../lib/Path.js'
import ZeroOrMorePath from '../lib/ZeroOrMorePath.js'
import ignoreWhitespaceEqual from './support/ignoreWhitespaceEqual.js'
import ns from './support/namespace.js'

describe('ZeroOrMorePath', () => {
it('should be a constructor', () => {
strictEqual(typeof ZeroOrMorePath, 'function')
})

it('should create a zero or more path', () => {
const path = new ZeroOrMorePath(ns.ex.measure)

const expected = '<http://example.org/measure>*'

ignoreWhitespaceEqual(path, expected)
})

it('should create a zero or more path with a group', () => {
const path = new ZeroOrMorePath(new Path([ns.ex.measure, ns.ex.temperature]))

const expected = '(<http://example.org/measure>/<http://example.org/temperature>)*'

ignoreWhitespaceEqual(path, expected)
})
})
28 changes: 28 additions & 0 deletions test/ZeroOrOnePath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { strictEqual } from 'assert'
import { describe, it } from 'mocha'
import Path from '../lib/Path.js'
import ZeroOrOnePath from '../lib/ZeroOrOnePath.js'
import ignoreWhitespaceEqual from './support/ignoreWhitespaceEqual.js'
import ns from './support/namespace.js'

describe('ZeroOrOnePath', () => {
it('should be a constructor', () => {
strictEqual(typeof ZeroOrOnePath, 'function')
})

it('should create a zero or one path', () => {
const path = new ZeroOrOnePath(ns.ex.measure)

const expected = '<http://example.org/measure>?'

ignoreWhitespaceEqual(path, expected)
})

it('should create a zero or one path with a group', () => {
const path = new ZeroOrOnePath(new Path([ns.ex.measure, ns.ex.temperature]))

const expected = '(<http://example.org/measure>/<http://example.org/temperature>)?'

ignoreWhitespaceEqual(path, expected)
})
})

0 comments on commit f9e988e

Please sign in to comment.