MCPcopy
hub / github.com/loggerhead/json4u / expectEq

Function expectEq

__tests__/compare.test.ts:6–48  ·  view source on GitHub ↗
(
  ltext: string,
  rtext: string,
  { hunks, inlines, isTextCompare }: { hunks?: Diff[]; inlines?: Diff[]; isTextCompare?: boolean },
)

Source from the content-addressed store, hash-verified

4import { readFileIfNeed } from "./utils";
5
6function expectEq(
7 ltext: string,
8 rtext: string,
9 { hunks, inlines, isTextCompare }: { hunks?: Diff[]; inlines?: Diff[]; isTextCompare?: boolean },
10) {
11 ltext = readFileIfNeed(ltext);
12 rtext = readFileIfNeed(rtext);
13
14 let pairs;
15
16 if (isTextCompare) {
17 pairs = compareText(ltext, rtext);
18 } else {
19 const ltree = parseJSON(ltext);
20 const rtree = parseJSON(rtext);
21 pairs = compareTree(ltree, rtree);
22 }
23
24 const gotInlineDiffs = sort(
25 pairs
26 .map(({ left, right }) => (left?.inlineDiffs ?? []).concat(right?.inlineDiffs ?? []))
27 .reduce((a, b) => a.concat(b), []),
28 );
29 const gotHunkDiffs = sort(
30 pairs
31 .map(({ left, right }) => {
32 left && delete left.inlineDiffs;
33 right && delete right.inlineDiffs;
34 return [left, right];
35 })
36 .reduce((a, b) => a.concat(b), [])
37 .filter((a) => a) as Diff[],
38 );
39
40 expect(hunks !== undefined || inlines !== undefined || isTextCompare !== undefined).toEqual(true);
41
42 if (hunks) {
43 expect(gotHunkDiffs).toMatchObject(hunks);
44 }
45 if (inlines) {
46 expect(gotInlineDiffs).toMatchObject(inlines);
47 }
48}
49
50describe("Comparer", () => {
51 test("diffVal", () => {

Callers 1

compare.test.tsFile · 0.70

Calls 5

readFileIfNeedFunction · 0.90
compareTextFunction · 0.90
parseJSONFunction · 0.90
compareTreeFunction · 0.90
sortFunction · 0.90

Tested by

no test coverage detected