(
ltext: string,
rtext: string,
{ hunks, inlines, isTextCompare }: { hunks?: Diff[]; inlines?: Diff[]; isTextCompare?: boolean },
)
| 4 | import { readFileIfNeed } from "./utils"; |
| 5 | |
| 6 | function 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 | |
| 50 | describe("Comparer", () => { |
| 51 | test("diffVal", () => { |
no test coverage detected