Mercurial > code > home > repos > light9
comparison web/patch.test.ts @ 2376:4556eebe5d73
topdir reorgs; let pdm have its src/ dir; separate vite area from light9/
author | drewp@bigasterisk.com |
---|---|
date | Sun, 12 May 2024 19:02:10 -0700 |
parents | light9/web/patch.test.ts@dd9474bef2a6 |
children |
comparison
equal
deleted
inserted
replaced
2375:623836db99af | 2376:4556eebe5d73 |
---|---|
1 import { assert, describe, expect, it } from "vitest"; | |
2 | |
3 import { Quad, NamedNode } from "n3"; | |
4 import { Patch, QuadPattern } from "./patch"; | |
5 import * as N3 from "n3"; | |
6 | |
7 const node1 = new NamedNode("http://example.com/node1"); | |
8 const node2 = new NamedNode("http://example.com/node2"); | |
9 const node3 = new NamedNode("http://example.com/node3"); | |
10 | |
11 const decimalDT = new NamedNode("http://www.w3.org/2001/XMLSchema#decimal"); | |
12 | |
13 function QP( | |
14 subject: N3.Quad_Subject | null, // | |
15 predicate: N3.Quad_Predicate | null, | |
16 object: N3.Quad_Object | null, | |
17 graph: N3.Quad_Graph | null | |
18 ): QuadPattern { | |
19 return { subject, predicate, object, graph }; | |
20 } | |
21 | |
22 describe("Patch.matches", () => { | |
23 it("matches any quads against an open pattern", () => { | |
24 const quad1 = new Quad(node1, node2, node3); | |
25 const quad2 = new Quad(node1, node2, node3); | |
26 const quad3 = new Quad(node1, node2, node3); | |
27 | |
28 const pattern = QP(null, null, null, null); | |
29 | |
30 const p = new Patch([quad1, quad2], [quad3]); | |
31 | |
32 assert.isTrue(p.matches(pattern)); | |
33 }); | |
34 it("doesn't match when the patch is empty", () => { | |
35 const p = new Patch([], []); | |
36 assert.isFalse(p.matches(QP(null, null, null, null))); | |
37 }); | |
38 it("compares terms correctly", () => { | |
39 assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(node1, null, null, null))); | |
40 assert.isFalse(new Patch([new Quad(node1, node2, node3)], []).matches(QP(node2, null, null, null))); | |
41 }); | |
42 it("matches on just one set term", () => { | |
43 assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(node1, null, null, null))); | |
44 assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(null, node2, null, null))); | |
45 assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(null, null, node3, null))); | |
46 }); | |
47 }); | |
48 | |
49 describe("Patch.empty", () => { | |
50 it("works with no quads", () => { | |
51 const p = new Patch([], []); | |
52 assert.isTrue(p.isEmpty()); | |
53 }); | |
54 it("works with unmatched quads", () => { | |
55 const p = new Patch([], [new Quad(node1, node2, node3)]); | |
56 assert.isFalse(p.isEmpty()); | |
57 }); | |
58 it("understands floats are equal", () => { | |
59 const p = new Patch( | |
60 [new Quad(node1, node2, N3.DataFactory.literal((0.12345).toPrecision(3), decimalDT))], | |
61 [new Quad(node1, node2, N3.DataFactory.literal((0.1234).toPrecision(3), decimalDT))] | |
62 ); | |
63 assert.isTrue(p.isEmpty()); | |
64 }); | |
65 it("...and when they're not", () => { | |
66 const p = new Patch( | |
67 [new Quad(node1, node2, N3.DataFactory.literal(0.123, decimalDT))], // | |
68 [new Quad(node1, node2, N3.DataFactory.literal(0.124, decimalDT))] | |
69 ); | |
70 assert.isFalse(p.isEmpty()); | |
71 }); | |
72 it("understands literals are equal", () => { | |
73 const p = new Patch( | |
74 [new Quad(node1, node2, node3)], // | |
75 [new Quad(node1, node2, new NamedNode("http://example.com/node" + "3"))] | |
76 ); | |
77 assert.isTrue(p.isEmpty()); | |
78 }); | |
79 }); |