-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
11,106 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,81 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { Term } from '@rdfjs/types' | ||
import chai from 'chai' | ||
import { util } from 'chai' | ||
import type { AnyPointer } from 'clownface' | ||
import toNT from '@rdfjs/to-ntriples' | ||
import type deepEqual from 'deep-eql' | ||
|
||
declare global { | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
namespace Chai { | ||
interface Assertion { | ||
term: Assertion | ||
interface Config { | ||
deepEqual: <L, R>(expected: L, actual: R) => void | ||
} | ||
|
||
interface ChaiUtils { | ||
eql: typeof deepEqual | ||
} | ||
} | ||
} | ||
|
||
const plugin: Chai.ChaiPlugin = (_chai, utils) => { | ||
const plugin: Chai.ChaiPlugin = (_chai) => { | ||
_chai.config.deepEqual = (expected, actual) => { | ||
return util.eql(expected, actual, { | ||
comparator: (obj: unknown, other: Term | unknown) => { | ||
if (isTermOrPointer(obj) && isTermOrPointer<Term>(other)) { | ||
return 'terms' in obj ? other.equals(obj.term) : other.equals(obj) | ||
} | ||
|
||
return null | ||
}, | ||
}) | ||
} | ||
|
||
const Assertion = _chai.Assertion | ||
|
||
Assertion.overwriteMethod('eq', function (_super) { | ||
return function (this: any, other: Term) { | ||
if (utils.flag(this, 'rdfjs-term')) { | ||
const obj: AnyPointer | Term = this._obj | ||
;['eq', 'equal', 'equals'].forEach((eq) => { | ||
Assertion.overwriteMethod(eq, function (_super) { | ||
return function (this: any, other: 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, | ||
) | ||
} | ||
|
||
let term: Term | ||
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}', | ||
other.equals(obj.term), | ||
'expected a pointer to #{exp} but got #{act}', | ||
'expected a pointer not to equal #{exp}', | ||
toNT(other), | ||
obj.terms.length, | ||
toNT(obj.term), | ||
) | ||
} | ||
|
||
return this.assert( | ||
other.equals(obj.term), | ||
'expected a pointer to #{exp} but got #{act}', | ||
'expected a pointer not to equal #{exp}', | ||
other.equals(obj), | ||
'expected #{this} to equal #{exp}', | ||
'expected #{this} not to equal #{exp}', | ||
toNT(other), | ||
toNT(obj.term), | ||
toNT(obj), | ||
) | ||
} | ||
|
||
return this.assert( | ||
other.equals(obj), | ||
'expected #{this} to equal #{exp}', | ||
'expected #{this} not to equal #{exp}', | ||
toNT(other), | ||
toNT(obj), | ||
) | ||
_super.call(this, other) | ||
} | ||
|
||
_super.call(this, other) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
utils.addProperty(chai.Assertion.prototype, 'term', function (this: Chai.Assertion) { | ||
utils.flag(this, 'rdfjs-term', true) | ||
}) | ||
function isTermOrPointer<T extends Term | AnyPointer>(value: T | unknown): value is T { | ||
return typeof value === 'object' && value !== null && ('termType' in value || 'terms' in value) | ||
} | ||
|
||
export default plugin |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.