-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiff.test.ts
More file actions
115 lines (102 loc) · 3.25 KB
/
Copy pathdiff.test.ts
File metadata and controls
115 lines (102 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { describe, expect, it } from 'vitest'
import {
diffChars,
diffLines,
diffSegments,
diffWords,
summarizeDiff
} from '@/features/miscellaneous/diff-checker/utils/diff'
describe('diffLines', () => {
it('marks identical texts as fully equal', () => {
const lines = diffLines('a\nb', 'a\nb')
expect(lines.every(line => line.type === 'equal')).toBe(true)
})
it('detects an added line', () => {
const lines = diffLines('a\nc', 'a\nb\nc')
expect(summarizeDiff(lines)).toEqual({ added: 1, removed: 0 })
})
it('detects a removed line', () => {
const lines = diffLines('a\nb\nc', 'a\nc')
expect(summarizeDiff(lines)).toEqual({ added: 0, removed: 1 })
})
it('detects a changed line as a remove plus add', () => {
const lines = diffLines('a\nb\nc', 'a\nB\nc')
expect(summarizeDiff(lines)).toEqual({ added: 1, removed: 1 })
})
it('tracks original line numbers through changes', () => {
const lines = diffLines('a\nb\nc', 'a\nc')
const removed = lines.find(line => line.type === 'removed')
expect(removed?.aLine).toBe(2)
expect(removed?.bLine).toBeNull()
})
it('handles large inputs without hanging', () => {
const a = Array.from({ length: 10_000 }, (_, i) => `line ${i}`).join(
'\n'
)
const b = a.replace('line 5000', 'changed')
const lines = diffLines(a, b)
expect(summarizeDiff(lines)).toEqual({ added: 1, removed: 1 })
})
})
describe('diffWords', () => {
it('isolates the changed word', () => {
const segments = diffWords('the quick fox', 'the slow fox')
expect(segments).toEqual([
{ type: 'equal', text: 'the ' },
{ type: 'removed', text: 'quick' },
{ type: 'added', text: 'slow' },
{ type: 'equal', text: ' fox' }
])
})
it('merges consecutive segments of the same type', () => {
const segments = diffWords('a b', 'xyzw')
expect(segments).toEqual([
{ type: 'removed', text: 'a b' },
{ type: 'added', text: 'xyzw' }
])
})
})
describe('diffChars', () => {
it('isolates the changed characters inside a word', () => {
expect(diffChars('color', 'colour')).toEqual([
{ type: 'equal', text: 'colo' },
{ type: 'added', text: 'u' },
{ type: 'equal', text: 'r' }
])
})
it('narrows a change that word mode reports as a whole token', () => {
expect(diffWords('quick', 'quack')).toEqual([
{ type: 'removed', text: 'quick' },
{ type: 'added', text: 'quack' }
])
expect(diffChars('quick', 'quack')).toEqual([
{ type: 'equal', text: 'qu' },
{ type: 'removed', text: 'i' },
{ type: 'added', text: 'a' },
{ type: 'equal', text: 'ck' }
])
})
it('keeps multi-code-unit characters intact', () => {
expect(diffChars('a🙂b', 'a🙂c')).toEqual([
{ type: 'equal', text: 'a🙂' },
{ type: 'removed', text: 'b' },
{ type: 'added', text: 'c' }
])
})
})
describe('diffSegments', () => {
it('reports line granularity as a whole removed plus added pair', () => {
expect(diffSegments('the quick fox', 'the slow fox', 'line')).toEqual([
{ type: 'removed', text: 'the quick fox' },
{ type: 'added', text: 'the slow fox' }
])
})
it('falls back to whole lines when input exceeds the char-mode limit', () => {
const a = 'x'.repeat(1300)
const b = `${'x'.repeat(1299)}y`
expect(diffSegments(a, b, 'char')).toEqual([
{ type: 'removed', text: a },
{ type: 'added', text: b }
])
})
})