annotate web/patch.test.ts @ 2408:7e7874fed2e3

buttons to add panels to the layout
author drewp@bigasterisk.com
date Sat, 18 May 2024 21:02:32 -0700
parents 4556eebe5d73
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2257
8ebf79d6b957 add vitest
drewp@bigasterisk.com
parents:
diff changeset
1 import { assert, describe, expect, it } from "vitest";
8ebf79d6b957 add vitest
drewp@bigasterisk.com
parents:
diff changeset
2
2260
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
3 import { Quad, NamedNode } from "n3";
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
4 import { Patch, QuadPattern } from "./patch";
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
5 import * as N3 from "n3";
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
6
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
7 const node1 = new NamedNode("http://example.com/node1");
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
8 const node2 = new NamedNode("http://example.com/node2");
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
9 const node3 = new NamedNode("http://example.com/node3");
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
10
2275
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
11 const decimalDT = new NamedNode("http://www.w3.org/2001/XMLSchema#decimal");
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
12
2260
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
13 function QP(
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
14 subject: N3.Quad_Subject | null, //
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
15 predicate: N3.Quad_Predicate | null,
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
16 object: N3.Quad_Object | null,
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
17 graph: N3.Quad_Graph | null
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
18 ): QuadPattern {
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
19 return { subject, predicate, object, graph };
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
20 }
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
21
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
22 describe("Patch.matches", () => {
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
23 it("matches any quads against an open pattern", () => {
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
24 const quad1 = new Quad(node1, node2, node3);
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
25 const quad2 = new Quad(node1, node2, node3);
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
26 const quad3 = new Quad(node1, node2, node3);
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
27
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
28 const pattern = QP(null, null, null, null);
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
29
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
30 const p = new Patch([quad1, quad2], [quad3]);
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
31
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
32 assert.isTrue(p.matches(pattern));
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
33 });
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
34 it("doesn't match when the patch is empty", () => {
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
35 const p = new Patch([], []);
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
36 assert.isFalse(p.matches(QP(null, null, null, null)));
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
37 });
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
38 it("compares terms correctly", () => {
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
39 assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(node1, null, null, null)));
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
40 assert.isFalse(new Patch([new Quad(node1, node2, node3)], []).matches(QP(node2, null, null, null)));
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
41 });
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
42 it("matches on just one set term", () => {
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
43 assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(node1, null, null, null)));
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
44 assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(null, node2, null, null)));
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
45 assert.isTrue(new Patch([new Quad(node1, node2, node3)], []).matches(QP(null, null, node3, null)));
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
46 });
4b04bf5c4a85 patch.matches
drewp@bigasterisk.com
parents: 2257
diff changeset
47 });
2275
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
48
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
49 describe("Patch.empty", () => {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
50 it("works with no quads", () => {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
51 const p = new Patch([], []);
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
52 assert.isTrue(p.isEmpty());
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
53 });
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
54 it("works with unmatched quads", () => {
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
55 const p = new Patch([], [new Quad(node1, node2, node3)]);
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
56 assert.isFalse(p.isEmpty());
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
57 });
2275
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
58 it("understands floats are equal", () => {
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
59 const p = new Patch(
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
60 [new Quad(node1, node2, N3.DataFactory.literal((0.12345).toPrecision(3), decimalDT))],
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
61 [new Quad(node1, node2, N3.DataFactory.literal((0.1234).toPrecision(3), decimalDT))]
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
62 );
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
63 assert.isTrue(p.isEmpty());
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
64 });
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
65 it("...and when they're not", () => {
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
66 const p = new Patch(
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
67 [new Quad(node1, node2, N3.DataFactory.literal(0.123, decimalDT))], //
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
68 [new Quad(node1, node2, N3.DataFactory.literal(0.124, decimalDT))]
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
69 );
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
70 assert.isFalse(p.isEmpty());
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
71 });
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
72 it("understands literals are equal", () => {
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
73 const p = new Patch(
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
74 [new Quad(node1, node2, node3)], //
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
75 [new Quad(node1, node2, new NamedNode("http://example.com/node" + "3"))]
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
76 );
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
77 assert.isTrue(p.isEmpty());
dd9474bef2a6 decimal not double! this caused patch comparisons to fail and led to redundant work
drewp@bigasterisk.com
parents: 2274
diff changeset
78 });
2274
df9a6c457587 convert Patch to use Immutable
drewp@bigasterisk.com
parents: 2260
diff changeset
79 });