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