Skip to content

Commit

Permalink
Allow multi-pointers in matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomociti committed Oct 31, 2024
1 parent 7792288 commit 03d9be3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-games-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mocha-chai-rdf": patch
---

Allow multi-pointers in matchers
47 changes: 20 additions & 27 deletions lib/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Term } from '@rdfjs/types'
import type { AnyPointer } from 'clownface'
import toNT from '@rdfjs/to-ntriples'
import type deepEqual from 'deep-eql'
import env from '@zazuko/env-node'

declare global {
/* eslint-disable @typescript-eslint/no-namespace */
Expand Down Expand Up @@ -34,36 +35,16 @@ const plugin: Chai.ChaiPlugin = (_chai, util) => {

;['eq', 'equal', 'equals'].forEach((eq) => {
Assertion.overwriteMethod(eq, function (_super) {
return function (this: any, other: Term) {
return function (this: any, other: AnyPointer | Term) {
const obj: AnyPointer | Term | unknown = this._obj

if (isTermOrPointer(obj)) {
if ('terms' in obj) {
if (!obj.term) {
return this.assert(
false,
'expected a pointer with single term #{exp} but got #{act} terms',
'expected a pointer with single term not to equal #{exp}',
toNT(other),
obj.terms.length,
)
}

return this.assert(
other.equals(obj.term),
'expected a pointer to #{exp} but got #{act}',
'expected a pointer not to equal #{exp}',
toNT(other),
toNT(obj.term),
)
}

if (isTermOrPointer(obj) && isTermOrPointer(other)) {
return this.assert(
other.equals(obj),
'expected #{this} to equal #{exp}',
'expected #{this} not to equal #{exp}',
toNT(other),
toNT(obj),
setEqual(getTerms(obj), getTerms(other)),
'expected #{exp} but got #{act}',
'expected #{act} not to equal #{exp}',
getDescription(other),
getDescription(obj),
)
}

Expand All @@ -77,4 +58,16 @@ function isTermOrPointer<T extends Term | AnyPointer>(value: T | unknown): value
return typeof value === 'object' && value !== null && ('termType' in value || 'terms' in value)
}

function getTerms(obj: AnyPointer | Term) {
return env.termSet('terms' in obj ? obj.terms : [obj])
}

function setEqual<T>(left: Set<T>, right: Set<T>) {
return left.size === right.size && [...left].every((term) => right.has(term))
}

function getDescription(obj: AnyPointer | Term): string {
return 'terms' in obj ? `a pointer to ${obj.terms.map(toNT).join(', ')}` : toNT(obj)
}

export default plugin
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 28 additions & 5 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ exports[`mocha-chai-rdf test suites 3`] = `
✔ matchers.js term when actual object is pointer succeeds when equal: 0ms
2) matchers.js term when actual object is pointer fails when not equal
3) matchers.js term when actual object is pointer fails when multi-pointer
✔ matchers.js term when actual and expected objects are pointers succeeds when equal: 0ms
✔ matchers.js term when actual and expected objects are pointers succeeds when equal multi-pointers: 0ms
4) matchers.js term when actual and expected objects are pointers fails when not equal multi-pointers
22 passing (0ms)
3 failing
24 passing (0ms)
4 failing
1) store.js
createStore
Expand All @@ -60,10 +63,10 @@ exports[`mocha-chai-rdf test suites 3`] = `
when actual object is pointer
fails when not equal:
AssertionError: expected a pointer to '\\"http://example.org/\\"' but got '<http://example.org/>'
AssertionError: expected '\\"http://example.org/\\"' but got 'a pointer to <http://example.org/>'
+ expected - actual
-<http://example.org/>
-a pointer to <http://example.org/>
+\\"http://example.org/\\"
at Context.<anonymous>
Expand All @@ -73,7 +76,27 @@ exports[`mocha-chai-rdf test suites 3`] = `
term
when actual object is pointer
fails when multi-pointer:
AssertionError: expected a pointer with single term '<http://example.org/>' but got 2 terms
AssertionError: expected '<http://example.org/>' but got 'a pointer to <http://example.org/1>, …'
+ expected - actual
-a pointer to <http://example.org/1>, <http://example.org/2>
+<http://example.org/>
at Context.<anonymous>
at process.processImmediate
4) matchers.js
term
when actual and expected objects are pointers
fails when not equal multi-pointers:
AssertionError: expected 'a pointer to <http://example.org/1>, …' but got 'a pointer to <http://example.org/1>, …'
+ expected - actual
-a pointer to <http://example.org/1>, <http://example.org/2>
+a pointer to <http://example.org/1>, <http://example.org/3>
at Context.<anonymous>
at process.processImmediate
Expand Down
41 changes: 41 additions & 0 deletions test/tests/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,46 @@ describe('matchers.js', () => {
expect(pointer).to.be.eq(rdf.namedNode(iri))
})
})

context('when actual and expected objects are pointers', () => {
it('succeeds when equal', () => {
// given
const iri = 'http://example.org/'
const actual = rdf.clownface().node(oxigraph.namedNode(iri))
const expected = rdf.clownface().node(rdf.namedNode(iri))

expect(actual).to.be.eq(expected)
})

it('succeeds when equal multi-pointers', () => {
// given
const iri = 'http://example.org/'
const actual = rdf.clownface().node([
oxigraph.namedNode(iri + 1),
oxigraph.namedNode(iri + 2),
])
const expected = rdf.clownface().node([
rdf.namedNode(iri + 1),
rdf.namedNode(iri + 2),
])

expect(actual).to.be.eq(expected)
})

it('fails when not equal multi-pointers', () => {
// given
const iri = 'http://example.org/'
const actual = rdf.clownface().node([
oxigraph.namedNode(iri + 1),
oxigraph.namedNode(iri + 2),
])
const expected = rdf.clownface().node([
rdf.namedNode(iri + 1),
rdf.namedNode(iri + 3),
])

expect(actual).to.be.eq(expected)
})
})
})
})

0 comments on commit 03d9be3

Please sign in to comment.