comment.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // @ts-check
  2. 'use strict'
  3. const { expect } = require('chai')
  4. const Comment = require('../../lib/comment')
  5. const Range = require('../../lib/range')
  6. describe('Comment', function () {
  7. it('should move ranges to the right of insert', function () {
  8. const comment = new Comment('c1', [new Range(5, 10)])
  9. const resComment = comment.applyInsert(3, 5, false)
  10. expect(resComment.ranges).to.eql([new Range(10, 10)])
  11. })
  12. describe('applyInsert', function () {
  13. it('should insert 1 char before the range', function () {
  14. const comment = new Comment('c1', [new Range(5, 10)])
  15. expect(comment.applyInsert(4, 1).ranges).to.eql([new Range(6, 10)])
  16. })
  17. it('should insert 1 char at the edge, without expandCommand', function () {
  18. const comment = new Comment('c1', [new Range(5, 10)])
  19. expect(comment.applyInsert(5, 1).ranges).to.eql([new Range(6, 10)])
  20. })
  21. it('should insert 1 char at the edge, with expandCommand', function () {
  22. const comment = new Comment('c1', [new Range(5, 10)])
  23. expect(comment.applyInsert(5, 1, true).ranges).to.eql([new Range(5, 11)])
  24. })
  25. it('should expand the range after insert inside it', function () {
  26. const comment = new Comment('c1', [new Range(5, 10)])
  27. expect(comment.applyInsert(6, 1, true).ranges).to.eql([new Range(5, 11)])
  28. })
  29. })
  30. it('should split the range if inside another and expandComment is false', function () {
  31. const comment = new Comment('c1', [new Range(5, 10)])
  32. const commentRes = comment.applyInsert(6, 10, false)
  33. expect(commentRes.ranges).to.eql([new Range(5, 1), new Range(16, 9)])
  34. })
  35. it('should insert the range if expandComment is false', function () {
  36. const comment = new Comment('c1', [new Range(5, 10)])
  37. const commentRes = comment.applyInsert(14, 10, false)
  38. expect(commentRes.ranges).to.eql([new Range(5, 9), new Range(24, 1)])
  39. })
  40. it('should move the range if insert is at range start and expandComment is false', function () {
  41. const comment = new Comment('c1', [new Range(5, 10)])
  42. const commentRes = comment.applyInsert(5, 10, false)
  43. expect(commentRes.ranges).to.eql([new Range(15, 10)])
  44. })
  45. it('should ignore the range if insert is at range end and expandComment is false', function () {
  46. const comment = new Comment('c1', [new Range(5, 10)])
  47. const commentRes = comment.applyInsert(15, 10, false)
  48. expect(commentRes.ranges).to.eql([new Range(5, 10)])
  49. })
  50. it('should expand the range after inserting on the edge of it if expandComment is true', function () {
  51. const comment = new Comment('c1', [new Range(5, 10)])
  52. const commentRes = comment.applyInsert(15, 10, true)
  53. expect(commentRes.ranges).to.eql([new Range(5, 20)])
  54. })
  55. it('should move comment ranges if delete is before it', function () {
  56. const comment = new Comment('c1', [new Range(5, 10)])
  57. const commentRes = comment.applyDelete(new Range(3, 5))
  58. expect(commentRes.ranges).to.eql([new Range(3, 7)])
  59. })
  60. it('should merge ranges after delete', function () {
  61. const comment = new Comment('c1', [new Range(5, 10), new Range(20, 10)])
  62. const commentRes = comment.applyDelete(new Range(7, 18))
  63. expect(commentRes.ranges).to.eql([new Range(5, 7)])
  64. })
  65. it('should merge overlapping ranges', function () {
  66. const comment = new Comment('c1', [
  67. new Range(5, 10),
  68. new Range(15, 20),
  69. new Range(50, 10),
  70. ])
  71. expect(comment.ranges).to.eql([new Range(5, 30), new Range(50, 10)])
  72. })
  73. it('should merge unsorted ranges', function () {
  74. const comment = new Comment('c1', [
  75. new Range(15, 20),
  76. new Range(50, 10),
  77. new Range(5, 10),
  78. ])
  79. expect(comment.ranges).to.eql([new Range(5, 30), new Range(50, 10)])
  80. })
  81. it('should throw error when ranges overlap', function () {
  82. expect(
  83. () =>
  84. new Comment('c1', [
  85. new Range(5, 10),
  86. new Range(10, 5),
  87. new Range(50, 10),
  88. ])
  89. ).to.throw()
  90. })
  91. it('should join touching ranges', function () {
  92. const comment = new Comment('c1', [
  93. new Range(5, 10),
  94. new Range(15, 5),
  95. new Range(50, 10),
  96. ])
  97. expect(comment.ranges).to.eql([new Range(5, 15), new Range(50, 10)])
  98. })
  99. })