2024-02-01 06:31:11 -05:00
|
|
|
// @ts-check
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const { expect } = require('chai')
|
2024-02-16 08:38:07 -05:00
|
|
|
const Comment = require('../lib/comment')
|
|
|
|
const Range = require('../lib/range')
|
2024-02-01 06:31:11 -05:00
|
|
|
|
|
|
|
describe('Comment', function () {
|
|
|
|
it('should move ranges to the right of insert', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
const resComment = comment.applyInsert(3, 5, false)
|
|
|
|
expect(resComment.ranges).to.eql([new Range(10, 10)])
|
2024-02-01 06:31:11 -05:00
|
|
|
})
|
|
|
|
|
2024-02-20 06:28:51 -05:00
|
|
|
describe('applyInsert', function () {
|
|
|
|
it('should insert 1 char before the range', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
expect(comment.applyInsert(4, 1).ranges).to.eql([new Range(6, 10)])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should insert 1 char at the edge, without expandCommand', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
expect(comment.applyInsert(5, 1).ranges).to.eql([new Range(6, 10)])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should insert 1 char at the edge, with expandCommand', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
expect(comment.applyInsert(5, 1, true).ranges).to.eql([new Range(5, 11)])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should expand the range after insert inside it', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
expect(comment.applyInsert(6, 1, true).ranges).to.eql([new Range(5, 11)])
|
|
|
|
})
|
2024-02-01 06:31:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should split the range if inside another and expandComment is false', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
const commentRes = comment.applyInsert(6, 10, false)
|
|
|
|
expect(commentRes.ranges).to.eql([new Range(5, 1), new Range(16, 9)])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should insert the range if expandComment is false', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
const commentRes = comment.applyInsert(14, 10, false)
|
|
|
|
expect(commentRes.ranges).to.eql([new Range(5, 9), new Range(24, 1)])
|
2024-02-01 06:31:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should move the range if insert is at range start and expandComment is false', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
const commentRes = comment.applyInsert(5, 10, false)
|
|
|
|
expect(commentRes.ranges).to.eql([new Range(15, 10)])
|
2024-02-01 06:31:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should ignore the range if insert is at range end and expandComment is false', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
const commentRes = comment.applyInsert(15, 10, false)
|
|
|
|
expect(commentRes.ranges).to.eql([new Range(5, 10)])
|
2024-02-01 06:31:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should expand the range after inserting on the edge of it if expandComment is true', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
const commentRes = comment.applyInsert(15, 10, true)
|
|
|
|
expect(commentRes.ranges).to.eql([new Range(5, 20)])
|
2024-02-01 06:31:11 -05:00
|
|
|
})
|
|
|
|
|
2024-02-20 06:28:51 -05:00
|
|
|
it('should move comment ranges if delete is before it', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
const commentRes = comment.applyDelete(new Range(3, 5))
|
|
|
|
expect(commentRes.ranges).to.eql([new Range(3, 7)])
|
2024-02-01 06:31:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should merge ranges after delete', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [new Range(5, 10), new Range(20, 10)])
|
2024-02-20 06:28:51 -05:00
|
|
|
const commentRes = comment.applyDelete(new Range(7, 18))
|
|
|
|
expect(commentRes.ranges).to.eql([new Range(5, 7)])
|
2024-02-01 06:31:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should merge overlapping ranges', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [
|
2024-02-01 06:31:11 -05:00
|
|
|
new Range(5, 10),
|
|
|
|
new Range(15, 20),
|
|
|
|
new Range(50, 10),
|
|
|
|
])
|
|
|
|
expect(comment.ranges).to.eql([new Range(5, 30), new Range(50, 10)])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should merge unsorted ranges', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [
|
2024-02-01 06:31:11 -05:00
|
|
|
new Range(15, 20),
|
|
|
|
new Range(50, 10),
|
|
|
|
new Range(5, 10),
|
|
|
|
])
|
|
|
|
expect(comment.ranges).to.eql([new Range(5, 30), new Range(50, 10)])
|
|
|
|
})
|
|
|
|
|
2024-04-16 04:55:22 -04:00
|
|
|
it('should throw error when ranges overlap', function () {
|
|
|
|
expect(
|
2024-04-16 09:03:34 -04:00
|
|
|
() =>
|
|
|
|
new Comment('c1', [
|
|
|
|
new Range(5, 10),
|
|
|
|
new Range(10, 5),
|
|
|
|
new Range(50, 10),
|
|
|
|
])
|
2024-04-16 04:55:22 -04:00
|
|
|
).to.throw()
|
2024-02-01 06:31:11 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should join touching ranges', function () {
|
2024-04-16 09:03:34 -04:00
|
|
|
const comment = new Comment('c1', [
|
2024-02-01 06:31:11 -05:00
|
|
|
new Range(5, 10),
|
|
|
|
new Range(15, 5),
|
|
|
|
new Range(50, 10),
|
|
|
|
])
|
|
|
|
expect(comment.ranges).to.eql([new Range(5, 15), new Range(50, 10)])
|
|
|
|
})
|
|
|
|
})
|