MCPcopy
hub / github.com/Effect-TS/effect / diffLaws

Function diffLaws

packages/effect/test/Differ.test.ts:5–58  ·  view source on GitHub ↗
(
  differ: Differ.Differ<Value, Patch>,
  gen: () => Value,
  equal: (a: Value, b: Value) => boolean
)

Source from the content-addressed store, hash-verified

3import { Array as Arr, Chunk, Differ, Equal, HashMap, HashSet, pipe } from "effect"
4
5function diffLaws<Value, Patch>(
6 differ: Differ.Differ<Value, Patch>,
7 gen: () => Value,
8 equal: (a: Value, b: Value) => boolean
9): void {
10 const it = (name: string, f: () => void) =>
11 it_(name, () => {
12 for (let i = 0; i < 100; i++) {
13 f()
14 }
15 })
16
17 describe("differ laws", () => {
18 it("combining patches is associative", () => {
19 const value1 = gen()
20 const value2 = gen()
21 const value3 = gen()
22 const value4 = gen()
23 const patch1 = differ.diff(value1, value2)
24 const patch2 = differ.diff(value2, value3)
25 const patch3 = differ.diff(value3, value4)
26 const left = differ.combine(differ.combine(patch1, patch2), patch3)
27 const right = differ.combine(patch1, differ.combine(patch2, patch3))
28 assertTrue(equal(differ.patch(left, value1), differ.patch(right, value1)))
29 })
30
31 it("combining a patch with an empty patch is an identity", () => {
32 const oldValue = gen()
33 const newValue = gen()
34 const patch = differ.diff(oldValue, newValue)
35 const left = differ.combine(patch, differ.empty)
36 const right = differ.combine(differ.empty, patch)
37 assertTrue(equal(differ.patch(left, oldValue), newValue))
38 assertTrue(equal(differ.patch(right, oldValue), newValue))
39 })
40
41 it("diffing a value with itself returns an empty patch", () => {
42 const value = gen()
43 deepStrictEqual(differ.diff(value, value), differ.empty)
44 })
45
46 it("diffing and then patching is an identity", () => {
47 const oldValue = gen()
48 const newValue = gen()
49 const patch = differ.diff(oldValue, newValue)
50 assertTrue(equal(differ.patch(patch, oldValue), newValue))
51 })
52
53 it("patching with an empty patch is an identity", () => {
54 const value = gen()
55 assertTrue(equal(differ.patch(differ.empty, value), value))
56 })
57 })
58}
59
60const min = 1
61const max = 100

Callers 1

Differ.test.tsFile · 0.85

Calls 7

assertTrueFunction · 0.90
deepStrictEqualFunction · 0.90
itFunction · 0.85
diffMethod · 0.65
combineMethod · 0.65
patchMethod · 0.65
genFunction · 0.50

Tested by

no test coverage detected