46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { buildDiffView } from '../src/renderer/src/editor'
|
|
import { makeDiff } from '../src/renderer/src/diff'
|
|
|
|
/** The current-side view of one text pair, plus the map the overlay reads. */
|
|
function view(original: string, updated: string): ReturnType<typeof buildDiffView> {
|
|
return buildDiffView(makeDiff('M', original, updated))
|
|
}
|
|
|
|
describe('buildDiffView', () => {
|
|
it('maps an added line to the line it replaced', () => {
|
|
const { lines, peek } = view('a\nold\nb', 'a\nnew\nb')
|
|
expect(lines.map((l) => l.text)).toEqual(['a', 'new', 'b'])
|
|
expect(lines[1].row).toBe('add')
|
|
expect(peek.get(2)).toEqual({ anchor: 2, from: 2, to: 2 })
|
|
expect(lines.every((l) => !l.gap)).toBe(true)
|
|
})
|
|
|
|
it('puts a pure deletion on the line that follows it', () => {
|
|
const { lines, peek } = view('a\ngone\nb', 'a\nb')
|
|
expect(lines[1].text).toBe('b')
|
|
expect(lines[1].gap).toBe('above')
|
|
expect(lines[1].row).toBeNull()
|
|
expect(peek.get(2)).toEqual({ anchor: 2, from: 2, to: 2 })
|
|
})
|
|
|
|
it('puts a deletion at end of file under the last line', () => {
|
|
const { lines, peek } = view('a\nb\ntail', 'a\nb')
|
|
expect(lines[1].gap).toBe('below')
|
|
expect(peek.get(2)).toEqual({ anchor: 3, from: 3, to: 3 })
|
|
})
|
|
|
|
it('anchors an insertion but marks nothing as removed', () => {
|
|
const { lines, peek } = view('a\nb', 'a\nNEW\nb')
|
|
expect(lines[1].row).toBe('add')
|
|
expect(peek.get(2)).toEqual({ anchor: 1, from: null, to: null })
|
|
expect(lines.every((l) => !l.gap)).toBe(true)
|
|
})
|
|
|
|
it('leaves an unchanged file without marks or peek targets', () => {
|
|
const { lines, peek } = view('a\nb\n', 'a\nb\n')
|
|
expect(peek.size).toBe(0)
|
|
expect(lines.every((l) => !l.row && !l.gap)).toBe(true)
|
|
})
|
|
})
|