| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {Hunk} from 'diff'; |
| 9 | |
| 10 | import {structuredPatch} from 'diff'; |
| 11 | import organizeLinesIntoGroups from '../../SplitDiffView/organizeLinesIntoGroups'; |
| 12 | |
| 13 | test('file with only one line that is changed (no context)', () => { |
| 14 | const hunks = diffIntoHunks(['lowerCamelCase'], ['UpperCamelCase']); |
| 15 | expect(hunks.length).toBe(1); |
| 16 | const groups = organizeLinesIntoGroups(hunks[0].lines); |
| 17 | expect(groups).toEqual([{common: [], removed: ['lowerCamelCase'], added: ['UpperCamelCase']}]); |
| 18 | }); |
| 19 | |
| 20 | test('file with only first line changed', () => { |
| 21 | const hunks = diffIntoHunks(['lowerCamelCase', 'a', 'b', 'c'], ['UpperCamelCase', 'a', 'b', 'c']); |
| 22 | expect(hunks.length).toBe(1); |
| 23 | const groups = organizeLinesIntoGroups(hunks[0].lines); |
| 24 | expect(groups).toEqual([ |
| 25 | {common: [], removed: ['lowerCamelCase'], added: ['UpperCamelCase']}, |
| 26 | {common: ['a', 'b', 'c'], removed: [], added: []}, |
| 27 | ]); |
| 28 | }); |
| 29 | |
| 30 | test('file with only last line changed', () => { |
| 31 | const hunks = diffIntoHunks(['a', 'b', 'c', 'lowerCamelCase'], ['a', 'b', 'c', 'UpperCamelCase']); |
| 32 | expect(hunks.length).toBe(1); |
| 33 | const groups = organizeLinesIntoGroups(hunks[0].lines); |
| 34 | expect(groups).toEqual([ |
| 35 | {common: ['a', 'b', 'c'], removed: ['lowerCamelCase'], added: ['UpperCamelCase']}, |
| 36 | ]); |
| 37 | }); |
| 38 | |
| 39 | test('a mix of changed lines', () => { |
| 40 | const hunks = diffIntoHunks( |
| 41 | ['...', 'The', 'quick', 'fox', 'jumped', 'over', 'dog.', 'THE END'], |
| 42 | ['The', 'quick', 'BROWN', 'fox', 'jumps', 'over', 'the lazy dog.', 'THE END'], |
| 43 | ); |
| 44 | expect(hunks.length).toBe(1); |
| 45 | const groups = organizeLinesIntoGroups(hunks[0].lines); |
| 46 | expect(groups).toEqual([ |
| 47 | { |
| 48 | common: [], |
| 49 | removed: ['...'], |
| 50 | added: [], |
| 51 | }, |
| 52 | { |
| 53 | common: ['The', 'quick'], |
| 54 | removed: [], |
| 55 | added: ['BROWN'], |
| 56 | }, |
| 57 | { |
| 58 | common: ['fox'], |
| 59 | removed: ['jumped'], |
| 60 | added: ['jumps'], |
| 61 | }, |
| 62 | { |
| 63 | common: ['over'], |
| 64 | removed: ['dog.'], |
| 65 | added: ['the lazy dog.'], |
| 66 | }, |
| 67 | { |
| 68 | common: ['THE END'], |
| 69 | removed: [], |
| 70 | added: [], |
| 71 | }, |
| 72 | ]); |
| 73 | }); |
| 74 | |
| 75 | function diffIntoHunks(oldLines: string[], newLines: string[], context = 3): Hunk[] { |
| 76 | const oldText = oldLines.join('\n') + '\n'; |
| 77 | const newText = newLines.join('\n') + '\n'; |
| 78 | const parsedDiff = structuredPatch('old.txt', 'new.txt', oldText, newText, undefined, undefined, { |
| 79 | context, |
| 80 | }); |
| 81 | return parsedDiff.hunks; |
| 82 | } |
| 83 | |