Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: border case for deleteList #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 22 additions & 5 deletions lib/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,32 @@ class Context {
deleteItems (start) {
let quads = [start]

while (!quads[quads.length - 1].object.equals(this.namespace.nil)) {
const node = quads[quads.length - 1].object
// Fail if malformed
let lastQuadCount = quads.length
let itGrew = true

while (!quads[quads.length - 1].object.equals(this.namespace.nil) && itGrew) {
const checkPred = (pred) => quads[quads.length - 1].predicate.equals(pred)
if (lastQuadCount > 1 && !(checkPred(this.namespace.first) || checkPred(this.namespace.rest))) {
throw Error('List predicate is either rdf:first or rdf:rest ')
}

const node = quads[quads.length - 1].object
quads = quads.concat([...this.dataset.match(node)])

itGrew = quads.length > lastQuadCount
lastQuadCount = quads.length
}

quads.forEach(quad => {
this.dataset.delete(quad)
})
const wasNil = quads[quads.length - 1].object.equals(this.namespace.nil)

if (wasNil) {
quads.forEach(quad => {
this.dataset.delete(quad)
})
} else if (lastQuadCount > 1) {
throw Error('List ending was not rdf:nil')
}
}

match (subject, predicate, object, graph) {
Expand Down
70 changes: 70 additions & 0 deletions test/Clownface/deleteList.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,74 @@ describe('.deleteList', () => {
assert.strictEqual(dataset.size, 1)
assert([...dataset][0].equals(other))
})

it('should not delete when is not a list', () => {
const dataset = rdf.dataset([
rdf.quad(ns.ex.s, ns.ex.p, ns.ex.notAList)])
const cf = clownface({ term: ns.ex.s, dataset })

cf.deleteList(ns.ex.p)

assert.strictEqual(dataset.size, 1)
})

it('should not delete when node is literal', () => {
const start = rdf.blankNode()
const dataset = rdf.dataset([
rdf.quad(start, ns.list, rdf.literal('not list'))])
const cf = clownface({ dataset })

cf.out(ns.list).deleteList(ns.list)

assert.strictEqual(cf.dataset.size, 1)
})

it('should not delete when list is rdf:nil', () => {
const start = rdf.blankNode()
const dataset = rdf.dataset([
rdf.quad(start, ns.list, ns.nil)])
const cf = clownface({ dataset })

cf.out(ns.list).deleteList(ns.list)

assert.strictEqual(cf.dataset.size, 1)
})

it('should throw error when list is not connected through rest', () => {
const start = rdf.blankNode()
const item = [rdf.blankNode(), rdf.blankNode(), rdf.blankNode()]
const dataset = rdf.dataset([
rdf.quad(start, ns.list, item[0]),
rdf.quad(item[0], ns.first, rdf.literal('1')),
rdf.quad(item[0], ns.dontFollowThis, item[1]),
rdf.quad(item[1], ns.first, rdf.literal('2')),
rdf.quad(item[1], ns.rest, item[2]),
rdf.quad(item[2], ns.first, rdf.literal('3')),
rdf.quad(item[2], ns.rest, ns.nil)])
const cf = clownface({ dataset })

assert.throws(() => {
cf.node(start).deleteList(ns.list)
})
assert.strictEqual(cf.dataset.size, 7)
})

it('should throw error when ending was not nil', () => {
const start = rdf.blankNode()
const item = [rdf.blankNode(), rdf.blankNode(), rdf.blankNode()]
const dataset = rdf.dataset([
rdf.quad(start, ns.list, item[0]),
rdf.quad(item[0], ns.first, rdf.literal('1')),
rdf.quad(item[0], ns.rest, item[1]),
rdf.quad(ns.ex.the, ns.ex.survivor, ns.ex.quad),
rdf.quad(item[1], ns.first, rdf.literal('2')),
rdf.quad(item[1], ns.rest, item[2]),
rdf.quad(item[2], ns.first, rdf.literal('3'))])
const cf = clownface({ dataset })

assert.throws(() => {
cf.node(start).deleteList(ns.list)
})
assert.strictEqual(cf.dataset.size, 7)
})
})