@rdfx/testing 0.0.23 → 0.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type * as RDF from "@rdfjs/types";
2
- import type { IQuadTerms } from "jest-rdf/lib/matchers/toBeRdfDatasetMatching.js";
3
2
  import "vitest";
3
+ import type { IQuadTerms } from "./jestRdfMatchers.js";
4
4
  declare module "vitest" {
5
5
  interface Matchers<T = any> {
6
6
  toBeLeft(expected?: unknown): T;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Code adapted from jest-rdf (https://www.npmjs.com/package/jest-rdf, MIT license) to circumvent build errors in reusing its matchers.
3
+ */
4
+ import type * as RDF from "@rdfjs/types";
5
+ export interface IQuadTerms<TQuad extends RDF.BaseQuad = RDF.Quad> {
6
+ graph?: TQuad["graph"];
7
+ object?: TQuad["object"];
8
+ predicate?: TQuad["predicate"];
9
+ subject?: TQuad["subject"];
10
+ }
11
+ export declare function toBeRdfDatasetContaining<TQuad extends RDF.BaseQuad = RDF.Quad>(dataset: RDF.DatasetCore<TQuad>, ...quads: TQuad[]): {
12
+ message: () => string;
13
+ pass: boolean;
14
+ };
15
+ export declare function toBeRdfDatasetMatching<TQuad extends RDF.BaseQuad = RDF.Quad>(dataset: RDF.DatasetCore<TQuad>, { subject, predicate, object, graph }: IQuadTerms<TQuad>, matches?: number): {
16
+ message: () => string;
17
+ pass: boolean;
18
+ };
19
+ export declare function toBeRdfDatasetOfSize<TQuad extends RDF.BaseQuad = RDF.Quad>(dataset: RDF.DatasetCore<TQuad>, expected: number): {
20
+ message: () => string;
21
+ pass: boolean;
22
+ };
23
+ export declare function toBeRdfIsomorphic<TQuad extends RDF.BaseQuad = RDF.Quad>(received: Iterable<TQuad>, actual: Iterable<TQuad>): {
24
+ message: () => string;
25
+ pass: boolean;
26
+ };
27
+ export declare function toEqualRdfQuad<TQuad extends RDF.BaseQuad = RDF.Quad>(received: TQuad, actual: TQuad): {
28
+ message: () => string;
29
+ pass: boolean;
30
+ };
31
+ export declare function toEqualRdfQuadArray<TQuad extends RDF.BaseQuad = RDF.Quad>(received: TQuad[], actual: TQuad[]): {
32
+ message: () => string;
33
+ pass: boolean;
34
+ };
35
+ export declare function toEqualRdfTerm(received: RDF.Term, actual: RDF.Term): {
36
+ message: () => string;
37
+ pass: false;
38
+ } | {
39
+ message: () => string;
40
+ pass: true;
41
+ };
42
+ export declare function toEqualRdfTermArray(received: RDF.Term[], actual: RDF.Term[]): {
43
+ message: () => string;
44
+ pass: boolean;
45
+ };
46
+ //# sourceMappingURL=jestRdfMatchers.d.ts.map
@@ -0,0 +1,216 @@
1
+ /**
2
+ * Code adapted from jest-rdf (https://www.npmjs.com/package/jest-rdf, MIT license) to circumvent build errors in reusing its matchers.
3
+ */
4
+ import { getGraphBlankNodes, getQuadsWithBlankNodes, hashTerms, isomorphic, uniqGraph, } from "rdf-isomorphic";
5
+ import { quadToStringQuad, termToString } from "rdf-string";
6
+ import { everyTerms, someTerms } from "rdf-terms";
7
+ function fail(received, actual) {
8
+ return {
9
+ message: () => `expected ${termToString(received)} and ${termToString(actual)} to be equal`,
10
+ pass: false,
11
+ };
12
+ }
13
+ function getNonBlankDiff(a1, a2) {
14
+ return a1.filter((quad) => everyTerms(quad, (term) => term.termType !== "BlankNode") &&
15
+ a2.every((q2) => !q2.equals(quad)));
16
+ }
17
+ function quadArrayToString(quadArray) {
18
+ return `[ ${quadArray.map((quad) => JSON.stringify(quadToStringQuad(quad))).join(", ")} ]`;
19
+ }
20
+ function succeed(received, actual) {
21
+ return {
22
+ message: () => `expected ${termToString(received)} and ${termToString(actual)} not to be equal`,
23
+ pass: true,
24
+ };
25
+ }
26
+ function termArrayToString(termArray) {
27
+ return `[ ${termArray.map((term) => JSON.stringify(termToString(term))).join(", ")} ]`;
28
+ }
29
+ function getBnodeDiff(receivedQuads, expectedQuads) {
30
+ // Hash every term based on the signature of the quads if appears in.
31
+ const ungroundedHashesA = unGroundHashes(receivedQuads);
32
+ const ungroundedHashesB = unGroundHashes(expectedQuads);
33
+ const blankA = uniqGraph(getQuadsWithBlankNodes(receivedQuads));
34
+ const blankB = uniqGraph(getQuadsWithBlankNodes(expectedQuads));
35
+ const received = {};
36
+ const expected = {};
37
+ for (const elem of getDiff(ungroundedHashesA, ungroundedHashesB)) {
38
+ received[elem] = blankA.filter((quad) => someTerms(quad, (term) => term.termType === "BlankNode" && term.value === elem.slice(2)));
39
+ }
40
+ for (const elem of getDiff(ungroundedHashesB, ungroundedHashesA)) {
41
+ expected[elem] = blankB.filter((quad) => someTerms(quad, (term) => term.termType === "BlankNode" && term.value === elem.slice(2)));
42
+ }
43
+ return {
44
+ received,
45
+ expected,
46
+ };
47
+ }
48
+ function getDiff(hash1, hash2) {
49
+ const diffed = [];
50
+ const values = new Set(Object.values(hash2));
51
+ for (const key in hash1) {
52
+ if (!values.has(hash1[key])) {
53
+ diffed.push(key);
54
+ }
55
+ }
56
+ return diffed;
57
+ }
58
+ export function toBeRdfDatasetContaining(dataset, ...quads) {
59
+ for (const quad of quads) {
60
+ if (!dataset.has(quad)) {
61
+ return {
62
+ message: () => `expected dataset to have quads ${quadArrayToString(quads)}`,
63
+ pass: false,
64
+ };
65
+ }
66
+ }
67
+ return {
68
+ message: () => `expected dataset to not have quads ${quadArrayToString(quads)}`,
69
+ pass: true,
70
+ };
71
+ }
72
+ export function toBeRdfDatasetMatching(dataset, { subject, predicate, object, graph }, matches = 1) {
73
+ const times = matches === 1 ? "once" : `${matches} times`;
74
+ if (dataset.match(subject, predicate, object, graph).size !== matches) {
75
+ return {
76
+ message: () => `expected dataset to match ${JSON.stringify({ subject, predicate, object, graph })} ${times}`,
77
+ pass: false,
78
+ };
79
+ }
80
+ return {
81
+ message: () => `expected dataset to not match ${JSON.stringify({ subject, predicate, object, graph })} ${times}`,
82
+ pass: true,
83
+ };
84
+ }
85
+ export function toBeRdfDatasetOfSize(dataset, expected) {
86
+ const actual = dataset.size;
87
+ if (expected !== actual) {
88
+ return {
89
+ message: () => `expected dataset to have ${expected} quads, ${actual} received`,
90
+ pass: false,
91
+ };
92
+ }
93
+ return {
94
+ message: () => `expected dataset to not have exactly ${expected} quads`,
95
+ pass: true,
96
+ };
97
+ }
98
+ export function toBeRdfIsomorphic(received, actual) {
99
+ const receivedArray = [...received];
100
+ const actualArray = [...actual];
101
+ if (!isomorphic(receivedArray, actualArray)) {
102
+ const { received: receivedBnodes, expected: actualBnodes } = getBnodeDiff(receivedArray, actualArray);
103
+ return {
104
+ message: () => `expected two graphs to be isomorphic.
105
+
106
+ Expected:
107
+ ${quadArrayToString(actualArray)}
108
+
109
+ Actual:
110
+ ${quadArrayToString(receivedArray)}
111
+
112
+ Missing Quads (that don't contain Blank Nodes):
113
+ ${quadArrayToString(getNonBlankDiff(actualArray, receivedArray))}
114
+
115
+ Additional Quads (that don't contain Blank Nodes):
116
+ ${quadArrayToString(getNonBlankDiff(receivedArray, actualArray))}
117
+
118
+ Missing Blank Node Patterns:
119
+ ${Object.entries(actualBnodes)
120
+ .map(([bnode, quads]) => `${bnode} : ${quadArrayToString(quads)}`)
121
+ .join("\n")}
122
+
123
+ Additional Blank Node Patterns:
124
+ ${Object.entries(receivedBnodes)
125
+ .map(([bnode, quads]) => `${bnode} : ${quadArrayToString(quads)}`)
126
+ .join("\n")}
127
+ `,
128
+ pass: false,
129
+ };
130
+ }
131
+ return {
132
+ message: () => `expected two graphs not to be isomorphic.
133
+
134
+ Expected:
135
+ ${quadArrayToString(actualArray)}
136
+
137
+ Actual:
138
+ ${quadArrayToString(receivedArray)}
139
+ `,
140
+ pass: true,
141
+ };
142
+ }
143
+ export function toEqualRdfQuad(received, actual) {
144
+ const s = toEqualRdfTerm(received.subject, actual.subject);
145
+ if (!s.pass) {
146
+ return s;
147
+ }
148
+ const p = toEqualRdfTerm(received.predicate, actual.predicate);
149
+ if (!p.pass) {
150
+ return p;
151
+ }
152
+ const o = toEqualRdfTerm(received.object, actual.object);
153
+ if (!o.pass) {
154
+ return o;
155
+ }
156
+ const g = toEqualRdfTerm(received.graph, actual.graph);
157
+ if (!g.pass) {
158
+ return g;
159
+ }
160
+ return {
161
+ message: () => `expected
162
+ ${JSON.stringify(quadToStringQuad(received))}
163
+ not to equal
164
+ ${JSON.stringify(quadToStringQuad(actual))}`,
165
+ pass: true,
166
+ };
167
+ }
168
+ export function toEqualRdfQuadArray(received, actual) {
169
+ if (received.length !== actual.length) {
170
+ return {
171
+ message: () => `expected ${quadArrayToString(received)} to equal ${quadArrayToString(actual)}`,
172
+ pass: false,
173
+ };
174
+ }
175
+ for (const [i, element] of received.entries()) {
176
+ const q = toEqualRdfQuad(element, actual[i]);
177
+ if (!q.pass) {
178
+ return q;
179
+ }
180
+ }
181
+ return {
182
+ message: () => `expected ${quadArrayToString(received)} not to equal ${quadArrayToString(actual)}`,
183
+ pass: true,
184
+ };
185
+ }
186
+ export function toEqualRdfTerm(received, actual) {
187
+ if (received.termType !== actual.termType) {
188
+ return fail(received, actual);
189
+ }
190
+ if (received.termType !== "BlankNode" && !received.equals(actual)) {
191
+ return fail(received, actual);
192
+ }
193
+ return succeed(received, actual);
194
+ }
195
+ export function toEqualRdfTermArray(received, actual) {
196
+ if (received.length !== actual.length) {
197
+ return {
198
+ message: () => `expected ${termArrayToString(received)} to equal ${termArrayToString(actual)}`,
199
+ pass: false,
200
+ };
201
+ }
202
+ for (const [i, element] of received.entries()) {
203
+ const q = toEqualRdfTerm(element, actual[i]);
204
+ if (!q.pass) {
205
+ return q;
206
+ }
207
+ }
208
+ return {
209
+ message: () => `expected ${termArrayToString(received)} not to equal ${termArrayToString(actual)}`,
210
+ pass: true,
211
+ };
212
+ }
213
+ function unGroundHashes(graph) {
214
+ return hashTerms(uniqGraph(getQuadsWithBlankNodes(graph)), getGraphBlankNodes(graph), {})[1];
215
+ }
216
+ //# sourceMappingURL=jestRdfMatchers.js.map
@@ -1,5 +1,13 @@
1
+ import { toBeRdfDatasetContaining, toBeRdfDatasetMatching, toBeRdfDatasetOfSize, toBeRdfIsomorphic, toEqualRdfQuad, toEqualRdfQuadArray, toEqualRdfTerm, toEqualRdfTermArray } from "./jestRdfMatchers.js";
1
2
  export declare const matchers: {
2
- toBeLeft: import("vitest").Matcher;
3
- default: {};
3
+ toBeRdfDatasetContaining: typeof toBeRdfDatasetContaining;
4
+ toBeRdfDatasetMatching: typeof toBeRdfDatasetMatching;
5
+ toBeRdfDatasetOfSize: typeof toBeRdfDatasetOfSize;
6
+ toBeRdfIsomorphic: typeof toBeRdfIsomorphic;
7
+ toEqualRdfQuad: typeof toEqualRdfQuad;
8
+ toEqualRdfQuadArray: typeof toEqualRdfQuadArray;
9
+ toEqualRdfTerm: typeof toEqualRdfTerm;
10
+ toEqualRdfTermArray: typeof toEqualRdfTermArray;
11
+ toBeLeft: import("vitest").Matcher<import("vitest").MatcherState, any[]>;
4
12
  };
5
13
  //# sourceMappingURL=matchers.d.ts.map
package/dist/matchers.js CHANGED
@@ -1,7 +1,14 @@
1
- import { default as jestRdfMatchers } from "jest-rdf/lib/matchers/index.js";
2
- import * as purifyMatchers from "./purifyMatchers.js";
1
+ import { toBeRdfDatasetContaining, toBeRdfDatasetMatching, toBeRdfDatasetOfSize, toBeRdfIsomorphic, toEqualRdfQuad, toEqualRdfQuadArray, toEqualRdfTerm, toEqualRdfTermArray, } from "./jestRdfMatchers.js";
2
+ import { toBeLeft } from "./purifyMatchers.js";
3
3
  export const matchers = {
4
- ...jestRdfMatchers,
5
- ...purifyMatchers,
4
+ toBeRdfDatasetContaining,
5
+ toBeRdfDatasetMatching,
6
+ toBeRdfDatasetOfSize,
7
+ toBeRdfIsomorphic,
8
+ toEqualRdfQuad,
9
+ toEqualRdfQuadArray,
10
+ toEqualRdfTerm,
11
+ toEqualRdfTermArray,
12
+ toBeLeft,
6
13
  };
7
14
  //# sourceMappingURL=matchers.js.map
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "dependencies": {
3
3
  "@rdfjs/types": "~2.0.1",
4
- "jest-rdf": "~2.0.0",
5
4
  "purify-ts": "~2.1.4",
5
+ "rdf-isomorphic": "~2.0.1",
6
+ "rdf-string": "~2.0.1",
7
+ "rdf-terms": "~2.0.0",
6
8
  "vitest": "~4.1.5"
7
9
  },
8
10
  "devDependencies": {},
@@ -27,5 +29,5 @@
27
29
  },
28
30
  "type": "module",
29
31
  "types": "./dist/index.d.ts",
30
- "version": "0.0.23"
32
+ "version": "0.0.25"
31
33
  }