Add property test for textChangeToEdits and applyTextEdits. (#9131)

Add a test for some functionality introduced in #9055.
This commit is contained in:
Kaz Wesley 2024-02-21 13:48:37 -08:00 committed by GitHub
parent fdec1d0671
commit 323d46ec4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,34 @@
import { expect, test } from 'vitest'
import { applyTextEditsToSpans, textChangeToEdits, trimEnd } from '../text'
import { fc, test } from '@fast-check/vitest'
import { expect } from 'vitest'
import { applyTextEdits, applyTextEditsToSpans, textChangeToEdits, trimEnd } from '../text'
test.prop({
before: fc.array(fc.boolean(), { minLength: 32, maxLength: 64 }),
after: fc.array(fc.boolean(), { minLength: 32, maxLength: 64 }),
})('textChangeToEdits / applyTextEdits round-trip', ({ before, after }) => {
// Generate strings composed of a mix of only two characters so that `textChangeToEdits` will find a variety of
// similarities between the inputs.
const stringFromBools = (bools: Array<boolean>) =>
bools.map((bool) => (bool ? 't' : 'f')).join('')
const beforeString = stringFromBools(before)
const afterString = stringFromBools(after)
const edits = textChangeToEdits(beforeString, afterString)
expect(applyTextEdits(beforeString, edits)).toBe(afterString)
})
/** Test that `textChangeToEdits` and `applyTextEdits` work when inputs contain any special characters representable by
* a `string`, including newlines and even incomplete surrogate pairs (invalid Unicode).
*/
test.prop({
before: fc.array(fc.string16bits(), { maxLength: 8 }),
})('textChangeToEdits / applyTextEdits round-trip: Special characters', ({ before }) => {
const beforeString = before.join('\n')
// Produce the after-string by rearranging the lines of the before-string, so that the edit-relationship between them
// is non-trivial.
const afterString = before.sort().join('\n')
const edits = textChangeToEdits(beforeString, afterString)
expect(applyTextEdits(beforeString, edits)).toBe(afterString)
})
/** Tests that:
* - When the code in `a[0]` is edited to become the code in `b[0]`,