range.test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // @ts-check
  2. 'use strict'
  3. const { expect } = require('chai')
  4. const Range = require('../lib/range')
  5. describe('Range', function () {
  6. it('should create a range', function () {
  7. const from5to14 = new Range(5, 10)
  8. expect(from5to14.start).to.eql(5)
  9. expect(from5to14.end).to.eql(15)
  10. })
  11. it('should create a range using fromRaw', function () {
  12. const from5to14 = Range.fromRaw({ pos: 5, length: 10 })
  13. expect(from5to14.start).to.eql(5)
  14. expect(from5to14.end).to.eql(15)
  15. })
  16. it('should convert to raw', function () {
  17. const from5to14 = new Range(5, 10)
  18. expect(from5to14.toRaw()).to.eql({ pos: 5, length: 10 })
  19. })
  20. it('should check isEmpty method', function () {
  21. const from5to14 = new Range(5, 10)
  22. expect(from5to14.isEmpty()).to.be.false
  23. const range0length = new Range(5, 0)
  24. expect(range0length.isEmpty()).to.be.true
  25. })
  26. describe('overlaps', function () {
  27. it('same ranges should overlap', function () {
  28. const range1 = new Range(1, 3)
  29. const range2 = new Range(1, 3)
  30. expect(range1.overlaps(range2)).to.eql(true)
  31. })
  32. it('non-touching ranges should not overlap', function () {
  33. const from1to3 = new Range(1, 3)
  34. const from10to12 = new Range(10, 3)
  35. expect(from1to3.overlaps(from10to12)).to.eql(false)
  36. expect(from10to12.overlaps(from1to3)).to.eql(false)
  37. })
  38. it('touching ranges should not overlap', function () {
  39. const from1to3 = new Range(1, 3)
  40. const from4to6 = new Range(4, 3)
  41. expect(from1to3.overlaps(from4to6)).to.eql(false)
  42. expect(from4to6.overlaps(from1to3)).to.eql(false)
  43. })
  44. it('should overlap', function () {
  45. const from1to3 = new Range(1, 3)
  46. const from2to4 = new Range(2, 3)
  47. expect(from1to3.overlaps(from2to4)).to.eql(true)
  48. expect(from2to4.overlaps(from1to3)).to.eql(true)
  49. })
  50. })
  51. describe('touches', function () {
  52. it('should not touch if ranges are the same', function () {
  53. const range1 = new Range(1, 3)
  54. const range2 = new Range(1, 3)
  55. expect(range1.touches(range2)).to.eql(false)
  56. expect(range2.touches(range1)).to.eql(false)
  57. })
  58. it('should return true when ranges touch at one point', function () {
  59. const from1to3 = new Range(1, 3)
  60. const from4to5 = new Range(4, 2)
  61. expect(from1to3.touches(from4to5)).to.eql(true)
  62. expect(from4to5.touches(from1to3)).to.eql(true)
  63. })
  64. it('should return false when ranges do not touch', function () {
  65. const from1to3 = new Range(1, 3)
  66. const from5to6 = new Range(5, 2)
  67. expect(from1to3.touches(from5to6)).to.eql(false)
  68. expect(from5to6.touches(from1to3)).to.eql(false)
  69. })
  70. it('should return false when ranges overlap', function () {
  71. const from1to3 = new Range(1, 3)
  72. const from3to4 = new Range(3, 2)
  73. expect(from1to3.touches(from3to4)).to.eql(false)
  74. expect(from3to4.touches(from1to3)).to.eql(false)
  75. })
  76. })
  77. it('should check if range contains another', function () {
  78. const from0to2 = new Range(0, 3)
  79. const from4to13 = new Range(4, 10)
  80. const from4to14 = new Range(4, 11)
  81. const from4to15 = new Range(4, 12)
  82. const from5to13 = new Range(5, 9)
  83. const from5to14 = new Range(5, 10)
  84. const from5to15 = new Range(5, 11)
  85. const from0to99 = new Range(0, 100)
  86. expect(from0to2.contains(from0to2)).to.eql(true)
  87. expect(from0to2.contains(from4to13)).to.eql(false)
  88. expect(from0to2.contains(from4to14)).to.eql(false)
  89. expect(from0to2.contains(from4to15)).to.eql(false)
  90. expect(from0to2.contains(from5to13)).to.eql(false)
  91. expect(from0to2.contains(from5to14)).to.eql(false)
  92. expect(from0to2.contains(from5to15)).to.eql(false)
  93. expect(from0to2.contains(from0to99)).to.eql(false)
  94. expect(from4to13.contains(from0to2)).to.eql(false)
  95. expect(from4to13.contains(from4to13)).to.eql(true)
  96. expect(from4to13.contains(from4to14)).to.eql(false)
  97. expect(from4to13.contains(from4to15)).to.eql(false)
  98. expect(from4to13.contains(from5to13)).to.eql(true)
  99. expect(from4to13.contains(from5to14)).to.eql(false)
  100. expect(from4to13.contains(from5to15)).to.eql(false)
  101. expect(from4to13.contains(from0to99)).to.eql(false)
  102. expect(from4to14.contains(from0to2)).to.eql(false)
  103. expect(from4to14.contains(from4to13)).to.eql(true)
  104. expect(from4to14.contains(from4to14)).to.eql(true)
  105. expect(from4to14.contains(from4to15)).to.eql(false)
  106. expect(from4to14.contains(from5to13)).to.eql(true)
  107. expect(from4to14.contains(from5to14)).to.eql(true)
  108. expect(from4to14.contains(from5to15)).to.eql(false)
  109. expect(from4to14.contains(from0to99)).to.eql(false)
  110. expect(from4to15.contains(from0to2)).to.eql(false)
  111. expect(from4to15.contains(from4to13)).to.eql(true)
  112. expect(from4to15.contains(from4to14)).to.eql(true)
  113. expect(from4to15.contains(from4to15)).to.eql(true)
  114. expect(from4to15.contains(from5to13)).to.eql(true)
  115. expect(from4to15.contains(from5to14)).to.eql(true)
  116. expect(from4to15.contains(from5to15)).to.eql(true)
  117. expect(from4to15.contains(from0to99)).to.eql(false)
  118. expect(from5to13.contains(from0to2)).to.eql(false)
  119. expect(from5to13.contains(from4to13)).to.eql(false)
  120. expect(from5to13.contains(from4to14)).to.eql(false)
  121. expect(from5to13.contains(from4to15)).to.eql(false)
  122. expect(from5to13.contains(from5to13)).to.eql(true)
  123. expect(from5to13.contains(from5to14)).to.eql(false)
  124. expect(from5to13.contains(from5to15)).to.eql(false)
  125. expect(from5to13.contains(from0to99)).to.eql(false)
  126. expect(from5to14.contains(from0to2)).to.eql(false)
  127. expect(from5to14.contains(from4to13)).to.eql(false)
  128. expect(from5to14.contains(from4to14)).to.eql(false)
  129. expect(from5to14.contains(from4to15)).to.eql(false)
  130. expect(from5to14.contains(from5to13)).to.eql(true)
  131. expect(from5to14.contains(from5to14)).to.eql(true)
  132. expect(from5to14.contains(from5to15)).to.eql(false)
  133. expect(from5to14.contains(from0to99)).to.eql(false)
  134. expect(from5to15.contains(from0to2)).to.eql(false)
  135. expect(from5to15.contains(from4to13)).to.eql(false)
  136. expect(from5to15.contains(from4to14)).to.eql(false)
  137. expect(from5to15.contains(from4to15)).to.eql(false)
  138. expect(from5to15.contains(from5to13)).to.eql(true)
  139. expect(from5to15.contains(from5to14)).to.eql(true)
  140. expect(from5to15.contains(from5to15)).to.eql(true)
  141. expect(from5to15.contains(from0to99)).to.eql(false)
  142. expect(from0to99.contains(from0to2)).to.eql(true)
  143. expect(from0to99.contains(from4to13)).to.eql(true)
  144. expect(from0to99.contains(from4to14)).to.eql(true)
  145. expect(from0to99.contains(from4to15)).to.eql(true)
  146. expect(from0to99.contains(from5to13)).to.eql(true)
  147. expect(from0to99.contains(from5to14)).to.eql(true)
  148. expect(from0to99.contains(from5to15)).to.eql(true)
  149. expect(from0to99.contains(from0to99)).to.eql(true)
  150. })
  151. it('should check if range contains a cursor', function () {
  152. const from5to14 = new Range(5, 10)
  153. expect(from5to14.containsCursor(4)).to.eql(false)
  154. expect(from5to14.containsCursor(5)).to.eql(true)
  155. expect(from5to14.containsCursor(6)).to.eql(true)
  156. expect(from5to14.containsCursor(14)).to.eql(true)
  157. expect(from5to14.containsCursor(15)).to.eql(true)
  158. expect(from5to14.containsCursor(16)).to.eql(false)
  159. })
  160. describe('subtract range from another', function () {
  161. it('should not subtract', function () {
  162. const from1to5 = new Range(1, 6)
  163. const from0to1 = new Range(0, 1)
  164. const subtracted = from1to5.subtract(from0to1)
  165. expect(subtracted.start).to.eql(1)
  166. expect(subtracted.length).to.eql(6)
  167. })
  168. it('should subtract from the left', function () {
  169. const from5to19 = new Range(5, 15)
  170. const from15to24 = new Range(15, 10)
  171. const subtracted = from15to24.subtract(from5to19)
  172. expect(subtracted.start).to.eql(5)
  173. expect(subtracted.end).to.eql(10)
  174. })
  175. it('should subtract from the right', function () {
  176. const from10to24 = new Range(10, 15)
  177. const from5to19 = new Range(5, 15)
  178. const subtracted = from5to19.subtract(from10to24)
  179. expect(subtracted.start).to.eql(5)
  180. expect(subtracted.end).to.eql(10)
  181. })
  182. it('should subtract from the middle', function () {
  183. const from5to19 = new Range(5, 15)
  184. const from10to14 = new Range(10, 5)
  185. const subtracted = from5to19.subtract(from10to14)
  186. expect(subtracted.start).to.eql(5)
  187. expect(subtracted.end).to.eql(15)
  188. })
  189. it('should delete entire range', function () {
  190. const from0to99 = new Range(0, 100)
  191. const from5to19 = new Range(5, 15)
  192. const subtracted = from5to19.subtract(from0to99)
  193. expect(subtracted.start).to.eql(5)
  194. expect(subtracted.end).to.eql(5)
  195. expect(subtracted.length).to.eql(0)
  196. })
  197. it('should not subtract if ranges do not overlap', function () {
  198. const from5to14 = new Range(5, 10)
  199. const from20to29 = new Range(20, 10)
  200. const subtracted1 = from5to14.subtract(from20to29)
  201. const subtracted2 = from20to29.subtract(from5to14)
  202. expect(subtracted1.toRaw()).deep.equal(from5to14.toRaw())
  203. expect(subtracted2.toRaw()).deep.equal(from20to29.toRaw())
  204. })
  205. })
  206. describe('merge ranges', function () {
  207. it('should merge ranges overlaping at the end', function () {
  208. const from5to14 = new Range(5, 10)
  209. const from10to19 = new Range(10, 10)
  210. expect(from5to14.canMerge(from10to19)).to.eql(true)
  211. const result = from5to14.merge(from10to19)
  212. expect(result.start).to.eql(5)
  213. expect(result.end).to.eql(20)
  214. })
  215. it('should merge ranges overlaping at the start', function () {
  216. const from5to14 = new Range(5, 10)
  217. const from0to9 = new Range(0, 10)
  218. expect(from5to14.canMerge(from0to9)).to.eql(true)
  219. const result = from5to14.merge(from0to9)
  220. expect(result.start).to.eql(0)
  221. expect(result.end).to.eql(15)
  222. })
  223. it('should merge ranges if one is covered by another', function () {
  224. const from5to14 = new Range(5, 10)
  225. const from0to19 = new Range(0, 20)
  226. expect(from5to14.canMerge(from0to19)).to.eql(true)
  227. const result = from5to14.merge(from0to19)
  228. expect(result.toRaw()).deep.equal(from0to19.toRaw())
  229. })
  230. it('should produce the same length after merge', function () {
  231. const from5to14 = new Range(5, 10)
  232. const from0to19 = new Range(0, 20)
  233. expect(from0to19.canMerge(from5to14)).to.eql(true)
  234. const result = from0to19.merge(from5to14)
  235. expect(result.start).to.eql(0)
  236. expect(result.end).to.eql(20)
  237. })
  238. it('should not merge ranges if they do not overlap', function () {
  239. const from5to14 = new Range(5, 10)
  240. const from20to29 = new Range(20, 10)
  241. expect(from5to14.canMerge(from20to29)).to.eql(false)
  242. expect(from20to29.canMerge(from5to14)).to.eql(false)
  243. expect(() => from5to14.merge(from20to29)).to.throw()
  244. })
  245. })
  246. it('should check if range starts after a range', function () {
  247. const from0to4 = new Range(0, 5)
  248. const from1to5 = new Range(1, 5)
  249. const from5to9 = new Range(5, 5)
  250. const from6to10 = new Range(6, 5)
  251. const from10to14 = new Range(10, 5)
  252. expect(from0to4.startsAfter(from0to4)).to.eql(false)
  253. expect(from0to4.startsAfter(from1to5)).to.eql(false)
  254. expect(from0to4.startsAfter(from5to9)).to.eql(false)
  255. expect(from0to4.startsAfter(from6to10)).to.eql(false)
  256. expect(from0to4.startsAfter(from10to14)).to.eql(false)
  257. expect(from1to5.startsAfter(from0to4)).to.eql(false)
  258. expect(from1to5.startsAfter(from1to5)).to.eql(false)
  259. expect(from1to5.startsAfter(from5to9)).to.eql(false)
  260. expect(from1to5.startsAfter(from6to10)).to.eql(false)
  261. expect(from1to5.startsAfter(from10to14)).to.eql(false)
  262. expect(from5to9.startsAfter(from0to4)).to.eql(true)
  263. expect(from5to9.startsAfter(from1to5)).to.eql(false)
  264. expect(from5to9.startsAfter(from5to9)).to.eql(false)
  265. expect(from5to9.startsAfter(from6to10)).to.eql(false)
  266. expect(from5to9.startsAfter(from10to14)).to.eql(false)
  267. expect(from6to10.startsAfter(from0to4)).to.eql(true)
  268. expect(from6to10.startsAfter(from1to5)).to.eql(true)
  269. expect(from6to10.startsAfter(from5to9)).to.eql(false)
  270. expect(from6to10.startsAfter(from6to10)).to.eql(false)
  271. expect(from6to10.startsAfter(from10to14)).to.eql(false)
  272. expect(from10to14.startsAfter(from0to4)).to.eql(true)
  273. expect(from10to14.startsAfter(from1to5)).to.eql(true)
  274. expect(from10to14.startsAfter(from5to9)).to.eql(true)
  275. expect(from10to14.startsAfter(from6to10)).to.eql(false)
  276. expect(from10to14.startsAfter(from10to14)).to.eql(false)
  277. })
  278. it('should check if range starts after a position', function () {
  279. const from5to14 = new Range(5, 10)
  280. expect(from5to14.startIsAfter(3)).to.be.true
  281. expect(from5to14.startIsAfter(4)).to.be.true
  282. expect(from5to14.startIsAfter(5)).to.be.false
  283. expect(from5to14.startIsAfter(6)).to.be.false
  284. expect(from5to14.startIsAfter(15)).to.be.false
  285. expect(from5to14.startIsAfter(16)).to.be.false
  286. })
  287. it('should extend the range', function () {
  288. const from5to14 = new Range(5, 10)
  289. const result = from5to14.extendBy(3)
  290. expect(result.length).to.eql(13)
  291. expect(result.start).to.eql(5)
  292. expect(result.end).to.eql(18)
  293. })
  294. it('should shrink the range', function () {
  295. const from5to14 = new Range(5, 10)
  296. const result = from5to14.shrinkBy(3)
  297. expect(result.length).to.eql(7)
  298. expect(result.start).to.eql(5)
  299. expect(result.end).to.eql(12)
  300. })
  301. it('should throw if shrinking too much', function () {
  302. const from5to14 = new Range(5, 10)
  303. expect(() => from5to14.shrinkBy(11)).to.throw()
  304. })
  305. it('should move the range', function () {
  306. const from5to14 = new Range(5, 10)
  307. const result = from5to14.moveBy(3)
  308. expect(result.length).to.eql(10)
  309. expect(result.start).to.eql(8)
  310. expect(result.end).to.eql(18)
  311. })
  312. describe('splitAt', function () {
  313. it('should split at the start', function () {
  314. const range = new Range(5, 10)
  315. const [left, right] = range.splitAt(5)
  316. expect(left.isEmpty()).to.be.true
  317. expect(right.start).to.eql(5)
  318. expect(right.end).to.eql(15)
  319. })
  320. it('should not split before the start', function () {
  321. const range = new Range(5, 10)
  322. expect(() => range.splitAt(4)).to.throw()
  323. })
  324. it('should split at last cursor in range', function () {
  325. const range = new Range(5, 10)
  326. const [left, right] = range.splitAt(14)
  327. expect(left.start).to.equal(5)
  328. expect(left.end).to.equal(14)
  329. expect(right.start).to.equal(14)
  330. expect(right.end).to.equal(15)
  331. })
  332. it('should not split after the end', function () {
  333. const range = new Range(5, 10)
  334. expect(() => range.splitAt(16)).to.throw()
  335. })
  336. it('should split at end', function () {
  337. const range = new Range(5, 10)
  338. const [left, right] = range.splitAt(15)
  339. expect(left.start).to.equal(5)
  340. expect(left.end).to.equal(15)
  341. expect(right.start).to.equal(15)
  342. expect(right.end).to.equal(15)
  343. })
  344. it('should split in the middle', function () {
  345. const range = new Range(5, 10)
  346. const [left, right] = range.splitAt(10)
  347. expect(left.start).to.equal(5)
  348. expect(left.end).to.equal(10)
  349. expect(right.start).to.equal(10)
  350. expect(right.end).to.equal(15)
  351. })
  352. })
  353. describe('insertAt', function () {
  354. it('should insert at the start', function () {
  355. const range = new Range(5, 10)
  356. const [left, inserted, right] = range.insertAt(5, 3)
  357. expect(left.isEmpty()).to.be.true
  358. expect(inserted.start).to.eql(5)
  359. expect(inserted.end).to.eql(8)
  360. expect(right.start).to.eql(8)
  361. expect(right.end).to.eql(18)
  362. })
  363. it('should insert at the end', function () {
  364. const range = new Range(5, 10)
  365. const [left, inserted, right] = range.insertAt(15, 3)
  366. expect(left.start).to.eql(5)
  367. expect(left.end).to.eql(15)
  368. expect(inserted.start).to.eql(15)
  369. expect(inserted.end).to.eql(18)
  370. expect(right.isEmpty()).to.be.true
  371. })
  372. it('should insert in the middle', function () {
  373. const range = new Range(5, 10)
  374. const [left, inserted, right] = range.insertAt(10, 3)
  375. expect(left.start).to.eql(5)
  376. expect(left.end).to.eql(10)
  377. expect(inserted.start).to.eql(10)
  378. expect(inserted.end).to.eql(13)
  379. expect(right.start).to.eql(13)
  380. expect(right.end).to.eql(18)
  381. })
  382. it('should throw if cursor is out of range', function () {
  383. const range = new Range(5, 10)
  384. expect(() => range.insertAt(4, 3)).to.throw()
  385. expect(() => range.insertAt(16, 3)).to.throw()
  386. })
  387. })
  388. })