scan_op.test.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // @ts-check
  2. const { expect } = require('chai')
  3. const {
  4. RetainOp,
  5. ScanOp,
  6. InsertOp,
  7. RemoveOp,
  8. } = require('../lib/operation/scan_op')
  9. const { UnprocessableError, ApplyError } = require('../lib/errors')
  10. describe('ScanOp', function () {
  11. describe('fromJSON', function () {
  12. it('constructs a RetainOp from object', function () {
  13. const op = ScanOp.fromJSON({ r: 1 })
  14. expect(op).to.be.instanceOf(RetainOp)
  15. expect(/** @type {RetainOp} */ (op).length).to.equal(1)
  16. })
  17. it('constructs a RetainOp from number', function () {
  18. const op = ScanOp.fromJSON(2)
  19. expect(op).to.be.instanceOf(RetainOp)
  20. expect(/** @type {RetainOp} */ (op).length).to.equal(2)
  21. })
  22. it('constructs an InsertOp from string', function () {
  23. const op = ScanOp.fromJSON('abc')
  24. expect(op).to.be.instanceOf(InsertOp)
  25. expect(/** @type {InsertOp} */ (op).insertion).to.equal('abc')
  26. })
  27. it('constructs an InsertOp from object', function () {
  28. const op = ScanOp.fromJSON({ i: 'abc' })
  29. expect(op).to.be.instanceOf(InsertOp)
  30. expect(/** @type {InsertOp} */ (op).insertion).to.equal('abc')
  31. })
  32. it('constructs a RemoveOp from number', function () {
  33. const op = ScanOp.fromJSON(-2)
  34. expect(op).to.be.instanceOf(RemoveOp)
  35. expect(/** @type {RemoveOp} */ (op).length).to.equal(2)
  36. })
  37. it('throws an error for invalid input', function () {
  38. expect(() => ScanOp.fromJSON({})).to.throw(UnprocessableError)
  39. })
  40. it('throws an error for zero', function () {
  41. expect(() => ScanOp.fromJSON(0)).to.throw(UnprocessableError)
  42. })
  43. })
  44. })
  45. describe('RetainOp', function () {
  46. it('is equal to another RetainOp with the same length', function () {
  47. const op1 = new RetainOp(1)
  48. const op2 = new RetainOp(1)
  49. expect(op1.equals(op2)).to.be.true
  50. })
  51. it('is not equal to another RetainOp with a different length', function () {
  52. const op1 = new RetainOp(1)
  53. const op2 = new RetainOp(2)
  54. expect(op1.equals(op2)).to.be.false
  55. })
  56. it('is not equal to an InsertOp', function () {
  57. const op1 = new RetainOp(1)
  58. const op2 = new InsertOp('a')
  59. expect(op1.equals(op2)).to.be.false
  60. })
  61. it('is not equal to a RemoveOp', function () {
  62. const op1 = new RetainOp(1)
  63. const op2 = new RemoveOp(1)
  64. expect(op1.equals(op2)).to.be.false
  65. })
  66. it('can merge with another RetainOp', function () {
  67. const op1 = new RetainOp(1)
  68. const op2 = new RetainOp(2)
  69. expect(op1.canMergeWith(op2)).to.be.true
  70. op1.mergeWith(op2)
  71. expect(op1.equals(new RetainOp(3))).to.be.true
  72. })
  73. it('cannot merge with an InsertOp', function () {
  74. const op1 = new RetainOp(1)
  75. const op2 = new InsertOp('a')
  76. expect(op1.canMergeWith(op2)).to.be.false
  77. expect(() => op1.mergeWith(op2)).to.throw(Error)
  78. })
  79. it('cannot merge with a RemoveOp', function () {
  80. const op1 = new RetainOp(1)
  81. const op2 = new RemoveOp(1)
  82. expect(op1.canMergeWith(op2)).to.be.false
  83. expect(() => op1.mergeWith(op2)).to.throw(Error)
  84. })
  85. it('can be converted to JSON', function () {
  86. const op = new RetainOp(3)
  87. expect(op.toJSON()).to.equal(3)
  88. })
  89. it('adds to the length and cursor when applied to length', function () {
  90. const op = new RetainOp(3)
  91. const { length, inputCursor } = op.applyToLength({
  92. length: 10,
  93. inputCursor: 10,
  94. inputLength: 30,
  95. })
  96. expect(length).to.equal(13)
  97. expect(inputCursor).to.equal(13)
  98. })
  99. it('adds from the input to the result when applied', function () {
  100. const op = new RetainOp(3)
  101. const { result, inputCursor } = op.apply('abcdefghi', {
  102. result: 'xyz',
  103. inputCursor: 3,
  104. })
  105. expect(result).to.equal('xyzdef')
  106. expect(inputCursor).to.equal(6)
  107. })
  108. })
  109. describe('InsertOp', function () {
  110. it('is equal to another InsertOp with the same insertion', function () {
  111. const op1 = new InsertOp('a')
  112. const op2 = new InsertOp('a')
  113. expect(op1.equals(op2)).to.be.true
  114. })
  115. it('is not equal to another InsertOp with a different insertion', function () {
  116. const op1 = new InsertOp('a')
  117. const op2 = new InsertOp('b')
  118. expect(op1.equals(op2)).to.be.false
  119. })
  120. it('is not equal to a RetainOp', function () {
  121. const op1 = new InsertOp('a')
  122. const op2 = new RetainOp(1)
  123. expect(op1.equals(op2)).to.be.false
  124. })
  125. it('is not equal to a RemoveOp', function () {
  126. const op1 = new InsertOp('a')
  127. const op2 = new RemoveOp(1)
  128. expect(op1.equals(op2)).to.be.false
  129. })
  130. it('can merge with another InsertOp', function () {
  131. const op1 = new InsertOp('a')
  132. const op2 = new InsertOp('b')
  133. expect(op1.canMergeWith(op2)).to.be.true
  134. op1.mergeWith(op2)
  135. expect(op1.equals(new InsertOp('ab'))).to.be.true
  136. })
  137. it('cannot merge with a RetainOp', function () {
  138. const op1 = new InsertOp('a')
  139. const op2 = new RetainOp(1)
  140. expect(op1.canMergeWith(op2)).to.be.false
  141. expect(() => op1.mergeWith(op2)).to.throw(Error)
  142. })
  143. it('cannot merge with a RemoveOp', function () {
  144. const op1 = new InsertOp('a')
  145. const op2 = new RemoveOp(1)
  146. expect(op1.canMergeWith(op2)).to.be.false
  147. expect(() => op1.mergeWith(op2)).to.throw(Error)
  148. })
  149. it('can be converted to JSON', function () {
  150. const op = new InsertOp('a')
  151. expect(op.toJSON()).to.equal('a')
  152. })
  153. it('adds to the length when applied to length', function () {
  154. const op = new InsertOp('abc')
  155. const { length, inputCursor } = op.applyToLength({
  156. length: 10,
  157. inputCursor: 20,
  158. inputLength: 40,
  159. })
  160. expect(length).to.equal(13)
  161. expect(inputCursor).to.equal(20)
  162. })
  163. it('adds from the insertion to the result when applied', function () {
  164. const op = new InsertOp('ghi')
  165. const { result, inputCursor } = op.apply('abcdef', {
  166. result: 'xyz',
  167. inputCursor: 3,
  168. })
  169. expect(result).to.equal('xyzghi')
  170. expect(inputCursor).to.equal(3)
  171. })
  172. it('can apply a retain of the rest of the input', function () {
  173. const op = new RetainOp(10)
  174. const { length, inputCursor } = op.applyToLength({
  175. length: 10,
  176. inputCursor: 5,
  177. inputLength: 15,
  178. })
  179. expect(length).to.equal(20)
  180. expect(inputCursor).to.equal(15)
  181. })
  182. it('cannot apply to length if the input cursor is at the end', function () {
  183. const op = new RetainOp(10)
  184. expect(() =>
  185. op.applyToLength({
  186. length: 10,
  187. inputCursor: 10,
  188. inputLength: 10,
  189. })
  190. ).to.throw(ApplyError)
  191. })
  192. })
  193. describe('RemoveOp', function () {
  194. it('is equal to another RemoveOp with the same length', function () {
  195. const op1 = new RemoveOp(1)
  196. const op2 = new RemoveOp(1)
  197. expect(op1.equals(op2)).to.be.true
  198. })
  199. it('is not equal to another RemoveOp with a different length', function () {
  200. const op1 = new RemoveOp(1)
  201. const op2 = new RemoveOp(2)
  202. expect(op1.equals(op2)).to.be.false
  203. })
  204. it('is not equal to a RetainOp', function () {
  205. const op1 = new RemoveOp(1)
  206. const op2 = new RetainOp(1)
  207. expect(op1.equals(op2)).to.be.false
  208. })
  209. it('is not equal to an InsertOp', function () {
  210. const op1 = new RemoveOp(1)
  211. const op2 = new InsertOp('a')
  212. expect(op1.equals(op2)).to.be.false
  213. })
  214. it('can merge with another RemoveOp', function () {
  215. const op1 = new RemoveOp(1)
  216. const op2 = new RemoveOp(2)
  217. expect(op1.canMergeWith(op2)).to.be.true
  218. op1.mergeWith(op2)
  219. expect(op1.equals(new RemoveOp(3))).to.be.true
  220. })
  221. it('cannot merge with a RetainOp', function () {
  222. const op1 = new RemoveOp(1)
  223. const op2 = new RetainOp(1)
  224. expect(op1.canMergeWith(op2)).to.be.false
  225. expect(() => op1.mergeWith(op2)).to.throw(Error)
  226. })
  227. it('cannot merge with an InsertOp', function () {
  228. const op1 = new RemoveOp(1)
  229. const op2 = new InsertOp('a')
  230. expect(op1.canMergeWith(op2)).to.be.false
  231. expect(() => op1.mergeWith(op2)).to.throw(Error)
  232. })
  233. it('can be converted to JSON', function () {
  234. const op = new RemoveOp(3)
  235. expect(op.toJSON()).to.equal(-3)
  236. })
  237. it('adds to the input cursor when applied to length', function () {
  238. const op = new RemoveOp(3)
  239. const { length, inputCursor } = op.applyToLength({
  240. length: 10,
  241. inputCursor: 10,
  242. inputLength: 30,
  243. })
  244. expect(length).to.equal(10)
  245. expect(inputCursor).to.equal(13)
  246. })
  247. it('does not change the result and adds to the cursor when applied', function () {
  248. const op = new RemoveOp(3)
  249. const { result, inputCursor } = op.apply('abcdefghi', {
  250. result: 'xyz',
  251. inputCursor: 3,
  252. })
  253. expect(result).to.equal('xyz')
  254. expect(inputCursor).to.equal(6)
  255. })
  256. })