fastdiff.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import fastDiff from '../src/fastdiff';
  7. import diff from '../src/diff';
  8. import diffToChanges from '../src/difftochanges';
  9. describe( 'fastDiff', () => {
  10. describe( 'input types', () => {
  11. it( 'should correctly handle strings', () => {
  12. const changes = fastDiff( '123', 'abc123' );
  13. expect( changes ).to.deep.equal( [ { index: 0, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  14. } );
  15. it( 'should correctly handle arrays', () => {
  16. const changes = fastDiff( [ '1', '2', '3' ], [ 'a', 'b', 'c', '1', '2', '3' ] );
  17. expect( changes ).to.deep.equal( [ { index: 0, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  18. } );
  19. it( 'should correctly handle node lists', () => {
  20. const el1 = document.createElement( 'p' );
  21. const el2 = document.createElement( 'h1' );
  22. el1.appendChild( document.createElement( 'span' ) );
  23. el1.appendChild( document.createElement( 'strong' ) );
  24. el2.appendChild( document.createElement( 'div' ) );
  25. el2.appendChild( document.createElement( 'strong' ) );
  26. const changes = fastDiff( el1.childNodes, el2.childNodes );
  27. expect( changes ).to.deep.equal( [
  28. { index: 0, type: 'insert', values: [ el2.childNodes[ 0 ], el2.childNodes[ 1 ] ] },
  29. { index: 2, type: 'delete', howMany: 2 }
  30. ] );
  31. } );
  32. } );
  33. describe( 'changes object', () => {
  34. it( 'should diff identical texts', () => {
  35. expectDiff( '123', '123', [] );
  36. } );
  37. it( 'should diff identical arrays', () => {
  38. expectDiff( [ '1', '2', '3' ], [ '1', '2', '3' ], [] );
  39. } );
  40. it( 'should diff arrays with custom comparator', () => {
  41. expectDiff( [ 'a', 'b', 'c' ], [ 'A', 'B', 'C' ], [], true, ( a, b ) => a.toLowerCase() === b.toLowerCase() );
  42. } );
  43. describe( 'insertion', () => {
  44. it( 'should diff if old text is empty', () => {
  45. expectDiff( '', '123', [ { index: 0, type: 'insert', values: [ '1', '2', '3' ] } ] );
  46. } );
  47. it( 'should diff if old array is empty', () => {
  48. expectDiff( [], [ '1', '2', '3' ], [ { index: 0, type: 'insert', values: [ '1', '2', '3' ] } ] );
  49. } );
  50. it( 'should diff insertion on the beginning', () => {
  51. expectDiff( '123', 'abc123', [ { index: 0, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  52. } );
  53. it( 'should diff insertion on the beginning (repetitive substring)', () => {
  54. // Do not check compatibility with 'diffToChanges' as it generates:
  55. // [ { index: 0, type: 'insert', values: [ 'a', 'b' ] }, { index: 5, type: 'insert', values: [ 'c', '1', '2', '3' ] } ]
  56. expectDiff( '123', 'ab123c123', [ { index: 0, type: 'insert', values: [ 'a', 'b', '1', '2', '3', 'c' ] } ], false );
  57. } );
  58. it( 'should diff insertion on the end', () => {
  59. expectDiff( '123', '123abc', [ { index: 3, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  60. } );
  61. it( 'should diff insertion on the end (repetitive substring)', () => {
  62. expectDiff( '123', '123ab123c', [ { index: 3, type: 'insert', values: [ 'a', 'b', '1', '2', '3', 'c' ] } ] );
  63. } );
  64. it( 'should diff insertion in the middle', () => {
  65. expectDiff( '123', '12abc3', [ { index: 2, type: 'insert', values: [ 'a', 'b', 'c' ] } ] );
  66. } );
  67. it( 'should diff insertion in the middle (repetitive substring)', () => {
  68. // Do not check compatibility with 'diffToChanges' as it generates:
  69. // [ { index: 2, type: 'insert', values: [ 'a', 'b', '1', '2' ] }, { index: 7, type: 'insert', values: [ 'c', '3' ] } ]
  70. expectDiff( '123', '12ab123c3', [ { index: 2, type: 'insert', values: [ 'a', 'b', '1', '2', '3', 'c' ] } ], false );
  71. } );
  72. it( 'should diff insertion of duplicated content', () => {
  73. expectDiff( '123', '123123', [ { index: 3, type: 'insert', values: [ '1', '2', '3' ] } ] );
  74. } );
  75. it( 'should diff insertion of partially duplicated content', () => {
  76. expectDiff( '123', '12323', [ { index: 3, type: 'insert', values: [ '2', '3' ] } ] );
  77. } );
  78. it( 'should diff insertion on both boundaries', () => {
  79. // Do not check compatibility with 'diffToChanges' as it generates:
  80. // [ { index: 2, type: 'insert', values: [ 'a', 'b' ] }, { index: 5, type: 'insert', values: [ 'c' ] } ]
  81. expectDiff( '123', 'ab123c', [
  82. { index: 0, type: 'insert', values: [ 'a', 'b', '1', '2', '3', 'c' ] },
  83. { index: 6, type: 'delete', howMany: 3 }
  84. ], false );
  85. } );
  86. it( 'should diff insertion in array of objects', () => {
  87. const o1 = { foo: 1 };
  88. const o2 = { bar: 2 };
  89. expectDiff( [ o1, o2 ], [ o1, o2, { baz: 3 } ], [
  90. { index: 2, type: 'insert', values: [ { baz: 3 } ] }
  91. ] );
  92. } );
  93. it( 'should diff insertion in array of objects with comparator', () => {
  94. expectDiff( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'foo' }, { text: 'bar' }, { text: 'baz' } ], [
  95. { index: 2, type: 'insert', values: [ { text: 'baz' } ] }
  96. ], true, ( a, b ) => a.text === b.text );
  97. } );
  98. describe( 'with multi-byte unicode', () => {
  99. describe( 'simple emoji - single unicode code point', () => {
  100. // 🙂 = '\ud83d\ude42' = 2 chars
  101. it( 'should properly handle emoji insertion', () => {
  102. expectDiff( 'abc', 'ab🙂c', [ { index: 2, type: 'insert', values: '🙂'.split( '' ) } ] );
  103. } );
  104. it( 'should properly handle emoji insertion on the end', () => {
  105. expectDiff( 'abc', 'abc🙂', [ { index: 3, type: 'insert', values: '🙂'.split( '' ) } ] );
  106. } );
  107. it( 'should properly handle appending to string containing emoji', () => {
  108. expectDiff( 'abc🙂', 'abc🙂d', [ { index: 5, type: 'insert', values: [ 'd' ] } ] );
  109. } );
  110. it( 'should properly handle insertion to string containing emoji', () => {
  111. expectDiff( 'ab🙂cd', 'ab🙂cde', [ { index: 6, type: 'insert', values: [ 'e' ] } ] );
  112. } );
  113. } );
  114. describe( 'combined emoji - unicode ZWJ sequence', () => {
  115. // 👩‍🦰 = '\ud83d\udc69\u200d\ud83e\uddB0' = 5 chars
  116. it( 'should properly handle emoji with ZWJ insertion', () => {
  117. expectDiff( 'abc', 'ab👩‍🦰c', [ { index: 2, type: 'insert', values: '👩‍🦰'.split( '' ) } ] );
  118. } );
  119. it( 'should properly handle emoji (with ZWJ) insertion on the end', () => {
  120. expectDiff( 'abc', 'abc👩‍🦰', [ { index: 3, type: 'insert', values: '👩‍🦰'.split( '' ) } ] );
  121. } );
  122. it( 'should properly handle appending to string containing emoji (with ZWJ)', () => {
  123. expectDiff( 'ab👩‍🦰', 'ab👩‍🦰c', [ { index: 7, type: 'insert', values: [ 'c' ] } ] );
  124. } );
  125. it( 'should properly handle insertion to string containing emoji (with ZWJ)', () => {
  126. expectDiff( 'a👩‍🦰b', 'a👩‍🦰bc', [ { index: 7, type: 'insert', values: [ 'c' ] } ] );
  127. } );
  128. } );
  129. } );
  130. } );
  131. describe( 'deletion', () => {
  132. it( 'should diff if new text is empty', () => {
  133. expectDiff( '123', '', [ { index: 0, type: 'delete', howMany: 3 } ] );
  134. } );
  135. it( 'should diff if new array is empty', () => {
  136. expectDiff( [ '1', '2', '3' ], [], [ { index: 0, type: 'delete', howMany: 3 } ] );
  137. } );
  138. it( 'should diff deletion on the beginning', () => {
  139. expectDiff( 'abc123', '123', [ { index: 0, type: 'delete', howMany: 3 } ] );
  140. } );
  141. it( 'should diff deletion on the beginning (repetitive substring)', () => {
  142. // Do not check compatibility with 'diffToChanges' as it generates:
  143. // [ { index: 0, type: 'delete', howMany: 2 }, { index: 3, type: 'delete', howMany: 4 } ]
  144. expectDiff( 'ab123c123', '123', [ { index: 0, type: 'delete', howMany: 6 } ], false );
  145. } );
  146. it( 'should diff deletion on the end', () => {
  147. expectDiff( '123abc', '123', [ { index: 3, type: 'delete', howMany: 3 } ] );
  148. } );
  149. it( 'should diff deletion on the end (repetitive substring)', () => {
  150. expectDiff( '123ab123c', '123', [ { index: 3, type: 'delete', howMany: 6 } ] );
  151. } );
  152. it( 'should diff deletion in the middle', () => {
  153. expectDiff( '12abc3', '123', [ { index: 2, type: 'delete', howMany: 3 } ] );
  154. } );
  155. it( 'should diff deletion in the middle (repetitive substring)', () => {
  156. // Do not check compatibility with 'diffToChanges' as it generates:
  157. // [ { index: 2, type: 'delete', howMany: 4 }, { index: 3, type: 'delete', howMany: 2 } ]
  158. expectDiff( '12ab123c3', '123', [ { index: 2, type: 'delete', howMany: 6 } ], false );
  159. } );
  160. it( 'should diff deletion on both boundaries', () => {
  161. // Do not check compatibility with 'diffToChanges' as it generates:
  162. // [ { index: 0, type: 'delete', howMany: 1 }, { index: 3, type: 'delete', howMany: 2 } ]
  163. expectDiff( '12abc3', '2ab', [
  164. { index: 0, type: 'insert', values: [ '2', 'a', 'b' ] },
  165. { index: 3, type: 'delete', howMany: 6 }
  166. ], false );
  167. } );
  168. it( 'should diff deletion of duplicated content', () => {
  169. expectDiff( '123123', '123', [ { index: 3, type: 'delete', howMany: 3 } ] );
  170. } );
  171. it( 'should diff deletion of partially duplicated content', () => {
  172. expectDiff( '12323', '123', [ { index: 3, type: 'delete', howMany: 2 } ] );
  173. } );
  174. it( 'should diff deletion of partially duplicated content 2', () => {
  175. // Do not check compatibility with 'diffToChanges' as it generates:
  176. // [ { index: 1, type: 'delete', howMany: 2 }, { index: 2, type: 'delete', howMany: 1 } ]
  177. expectDiff( '11233', '13', [ { index: 1, type: 'delete', howMany: 3 } ], false );
  178. } );
  179. it( 'should diff deletion in array of objects', () => {
  180. const o1 = { foo: 1 };
  181. const o2 = { bar: 2 };
  182. expectDiff( [ o1, o2 ], [ o2 ], [
  183. { index: 0, type: 'delete', howMany: 1 }
  184. ] );
  185. } );
  186. it( 'should diff insertion in array of objects with comparator', () => {
  187. expectDiff( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'bar' } ], [
  188. { index: 0, type: 'delete', howMany: 1 }
  189. ], true, ( a, b ) => a.text === b.text );
  190. } );
  191. describe( 'with multi-byte unicode', () => {
  192. describe( 'simple emoji - single unicode code point', () => {
  193. // 🙂 = '\ud83d\ude42' = 2 chars
  194. const emojiLength = '🙂'.split( '' ).length;
  195. it( 'should properly handle emoji delete', () => {
  196. expectDiff( 'ab🙂c', 'abc', [ { index: 2, type: 'delete', howMany: emojiLength } ] );
  197. } );
  198. it( 'should properly handle emoji delete at end', () => {
  199. expectDiff( 'ab🙂', 'ab', [ { index: 2, type: 'delete', howMany: emojiLength } ] );
  200. } );
  201. it( 'should properly handle emoji delete at beginning', () => {
  202. expectDiff( '🙂ab', 'ab', [ { index: 0, type: 'delete', howMany: emojiLength } ] );
  203. } );
  204. } );
  205. describe( 'combined emoji - unicode ZWJ sequence', () => {
  206. // 👩‍🦰 = '\ud83d\udc69\u200d\ud83e\uddB0' = 5 chars
  207. const emojiLength = '👩‍🦰'.split( '' ).length;
  208. it( 'should properly handle emoji delete (with ZWJ)', () => {
  209. expectDiff( 'ab👩‍🦰c', 'abc', [ { index: 2, type: 'delete', howMany: emojiLength } ] );
  210. } );
  211. it( 'should properly handle emoji delete at end (with ZWJ)', () => {
  212. expectDiff( 'ab👩‍🦰', 'ab', [ { index: 2, type: 'delete', howMany: emojiLength } ] );
  213. } );
  214. it( 'should properly handle emoji delete at beginning (with ZWJ)', () => {
  215. expectDiff( '👩‍🦰ab', 'ab', [ { index: 0, type: 'delete', howMany: emojiLength } ] );
  216. } );
  217. } );
  218. } );
  219. } );
  220. describe( 'replacement', () => {
  221. it( 'should diff replacement of entire text', () => {
  222. // Do not check compatibility with 'diffToChanges' as it has changes in reveres order ('delete', 'insert') here.
  223. expectDiff( '12345', 'abcd', [
  224. { index: 0, type: 'insert', values: [ 'a', 'b', 'c', 'd' ] },
  225. { index: 4, type: 'delete', howMany: 5 }
  226. ], false );
  227. } );
  228. it( 'should diff replacement on the beginning', () => {
  229. expectDiff( '12345', 'abcd345', [
  230. { index: 0, type: 'insert', values: [ 'a', 'b', 'c', 'd' ] },
  231. { index: 4, type: 'delete', howMany: 2 }
  232. ] );
  233. } );
  234. it( 'should diff replacement on the beginning (repetitive substring)', () => {
  235. // Do not check compatibility with 'diffToChanges' as it has changes in reveres order ('delete', 'insert') here.
  236. expectDiff( '12345', '345345', [
  237. { index: 0, type: 'insert', values: [ '3', '4', '5' ] },
  238. { index: 3, type: 'delete', howMany: 2 }
  239. ], false );
  240. } );
  241. it( 'should diff replacement on the end', () => {
  242. // Do not check compatibility with 'diffToChanges' as it has changes in reveres order ('delete', 'insert') here.
  243. expectDiff( '12345', '12ab', [
  244. { index: 2, type: 'insert', values: [ 'a', 'b' ] },
  245. { index: 4, type: 'delete', howMany: 3 }
  246. ], false );
  247. } );
  248. it( 'should diff replacement on the end (repetitive substring)', () => {
  249. // Do not check compatibility with 'diffToChanges' as it generates:
  250. // [ { index: 3, type: 'insert', values: [ '1', '2', '3' ] }, { index: 7, type: 'delete', howMany: 1 } ]
  251. expectDiff( '12345', '1231234', [
  252. { index: 3, type: 'insert', values: [ '1', '2', '3', '4' ] },
  253. { index: 7, type: 'delete', howMany: 2 }
  254. ], false );
  255. } );
  256. it( 'should diff insertion of duplicated content (case 1)', () => {
  257. expectDiff( '1234', '123123', [
  258. { index: 3, type: 'insert', values: [ '1', '2', '3' ] },
  259. { index: 6, type: 'delete', howMany: 1 }
  260. ], false );
  261. } );
  262. it( 'should diff insertion of duplicated content (case 2)', () => {
  263. expectDiff( '1234', '13424', [
  264. { index: 1, type: 'insert', values: [ '3', '4', '2' ] },
  265. { index: 4, type: 'delete', howMany: 2 }
  266. ], false );
  267. } );
  268. it( 'should diff replacement in the middle', () => {
  269. expectDiff( '12345', '12ab5', [
  270. { index: 2, type: 'insert', values: [ 'a', 'b' ] },
  271. { index: 4, type: 'delete', howMany: 2 }
  272. ] );
  273. } );
  274. it( 'should diff replacement in the middle (repetitive substring)', () => {
  275. // Do not check compatibility with 'diffToChanges' as it generates:
  276. // [ { index: 2, type: 'insert', values: [ '1', '2' ] }, { index: 7, type: 'insert', values: [ '5' ] } ]
  277. expectDiff( '12345', '12123455', [
  278. { index: 2, type: 'insert', values: [ '1', '2', '3', '4', '5' ] },
  279. { index: 7, type: 'delete', howMany: 2 }
  280. ], false );
  281. } );
  282. it( 'should diff replacement of duplicated content', () => {
  283. // Do not check compatibility with 'diffToChanges' as it has changes in reveres order ('delete', 'insert') here.
  284. expectDiff( '123123', '123333', [
  285. { index: 3, type: 'insert', values: '33'.split( '' ) },
  286. { index: 5, type: 'delete', howMany: 2 }
  287. ], false );
  288. } );
  289. it( 'should diff replacement in array of objects', () => {
  290. const o1 = { foo: 1 };
  291. const o2 = { bar: 2 };
  292. expectDiff( [ o1, o2 ], [ o1, { baz: 3 } ], [
  293. { index: 1, type: 'insert', values: [ { baz: 3 } ] },
  294. { index: 2, type: 'delete', howMany: 1 }
  295. ] );
  296. } );
  297. it( 'should diff insertion in array of objects with comparator', () => {
  298. expectDiff( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'foo' }, { text: 'baz' } ], [
  299. { index: 1, type: 'insert', values: [ { text: 'baz' } ] },
  300. { index: 2, type: 'delete', howMany: 1 }
  301. ], true, ( a, b ) => a.text === b.text );
  302. } );
  303. describe( 'with multi-byte unicode', () => {
  304. // 🙂 = '\ud83d\ude42' = 2 chars
  305. const smileEmoji = '🙂'.split( '' );
  306. // 👩 = '\ud83d\udc69' = 2 chars
  307. const womanEmoji = '👩'.split( '' );
  308. // 👩‍🦰 = '\ud83d\udc69\u200d\ud83e\uddB0' = 5 chars
  309. const womanRedHairEmoji = '👩‍🦰'.split( '' );
  310. // Do not check compatibility with 'diffToChanges' as it generates:
  311. // [ { index: 1, type: 'delete', howMany: 2 }, { index: 1, type: 'insert', values: [ 'x' ] } ]
  312. it( 'should properly replace emoji with text', () => {
  313. expectDiff( 'a🙂b', 'axb', [
  314. { index: 1, type: 'insert', values: [ 'x' ] },
  315. { index: 2, type: 'delete', howMany: smileEmoji.length }
  316. ], false );
  317. } );
  318. it( 'should properly replace text with emoji', () => {
  319. expectDiff( 'abc', 'a👩c', [
  320. { index: 1, type: 'insert', values: womanEmoji },
  321. { index: 3, type: 'delete', howMany: 1 }
  322. ] );
  323. } );
  324. it( 'should properly replace emoji with emoji', () => {
  325. // Note that first char of both emoji is the same.
  326. expectDiff( 'a👩b', 'a🙂b', [
  327. { index: 2, type: 'insert', values: smileEmoji.slice( 1 ) },
  328. { index: 3, type: 'delete', howMany: 1 }
  329. ] );
  330. } );
  331. it( 'should properly replace simple emoji with ZWJ sequence of it', () => {
  332. // Note that first 2 chars of both emoji are the same.
  333. expectDiff( 'a👩b', 'a👩‍🦰b', [
  334. { index: 3, type: 'insert', values: womanRedHairEmoji.slice( 2 ) }
  335. ] );
  336. } );
  337. it( 'should properly replace ZWJ sequence with simple emoji (part of sequence)', () => {
  338. // Note that first 2 chars of both emoji are the same.
  339. expectDiff( 'a👩‍🦰b', 'a👩b', [
  340. { index: 3, type: 'delete', howMany: 3 }
  341. ] );
  342. } );
  343. it( 'should properly replace simple emoji with other ZWJ sequence', () => {
  344. // Note that first char of both emoji is the same.
  345. expectDiff( 'a🙂b', 'a👩‍🦰b', [
  346. { index: 2, type: 'insert', values: womanRedHairEmoji.slice( 1 ) },
  347. { index: 6, type: 'delete', howMany: 1 }
  348. ] );
  349. } );
  350. } );
  351. } );
  352. } );
  353. describe( 'changes linear', () => {
  354. it( 'should diff identical texts', () => {
  355. expectDiffLinear( '123', '123', 'eee' );
  356. } );
  357. it( 'should diff identical arrays', () => {
  358. expectDiffLinear( [ '1', '2', '3' ], [ '1', '2', '3' ], 'eee' );
  359. } );
  360. it( 'should diff arrays with custom comparator', () => {
  361. expectDiffLinear( [ 'a', 'b', 'c' ], [ 'A', 'B', 'C' ], 'eee', true, ( a, b ) => a.toLowerCase() === b.toLowerCase() );
  362. } );
  363. describe( 'insertion', () => {
  364. it( 'should diff if old text is empty', () => {
  365. expectDiffLinear( '', '123', 'iii' );
  366. } );
  367. it( 'should diff if old array is empty', () => {
  368. expectDiffLinear( [], [ '1', '2', '3' ], 'iii' );
  369. } );
  370. it( 'should diff insertion on the beginning', () => {
  371. expectDiffLinear( '123', 'abc123', 'iiieee' );
  372. } );
  373. it( 'should diff insertion on the beginning (repetitive substring)', () => {
  374. expectDiffLinear( '123', 'ab123c123', 'iiiiiieee', false );
  375. } );
  376. it( 'should diff insertion on the end', () => {
  377. expectDiffLinear( '123', '123abc', 'eeeiii' );
  378. } );
  379. it( 'should diff insertion on the end (repetitive substring)', () => {
  380. expectDiffLinear( '123', '123ab123c', 'eeeiiiiii' );
  381. } );
  382. it( 'should diff insertion in the middle', () => {
  383. expectDiffLinear( '123', '12abc3', 'eeiiie' );
  384. } );
  385. it( 'should diff insertion in the middle (repetitive substring)', () => {
  386. expectDiffLinear( '123', '12ab123c3', 'eeiiiiiie', false );
  387. } );
  388. it( 'should diff insertion of duplicated content', () => {
  389. expectDiffLinear( '123', '123123', 'eeeiii' );
  390. } );
  391. it( 'should diff insertion of partially duplicated content', () => {
  392. expectDiffLinear( '123', '12323', 'eeeii' );
  393. } );
  394. it( 'should diff insertion on both boundaries', () => {
  395. expectDiffLinear( '123', 'ab123c', 'iiiiiiddd', false );
  396. } );
  397. it( 'should diff insertion in array of objects', () => {
  398. const o1 = { foo: 1 };
  399. const o2 = { bar: 2 };
  400. expectDiffLinear( [ o1, o2 ], [ o1, o2, { baz: 3 } ], 'eei' );
  401. } );
  402. it( 'should diff insertion in array of objects with comparator', () => {
  403. expectDiffLinear( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'foo' }, { text: 'bar' }, { text: 'baz' } ],
  404. 'eei', true, ( a, b ) => a.text === b.text );
  405. } );
  406. } );
  407. describe( 'deletion', () => {
  408. it( 'should diff if new text is empty', () => {
  409. expectDiffLinear( '123', '', 'ddd' );
  410. } );
  411. it( 'should diff if new array is empty', () => {
  412. expectDiffLinear( [ '1', '2', '3' ], [], 'ddd' );
  413. } );
  414. it( 'should diff deletion on the beginning', () => {
  415. expectDiffLinear( 'abc123', '123', 'dddeee' );
  416. } );
  417. it( 'should diff deletion on the beginning (repetitive substring)', () => {
  418. expectDiffLinear( 'ab123c123', '123', 'ddddddeee', false );
  419. } );
  420. it( 'should diff deletion on the end', () => {
  421. expectDiffLinear( '123abc', '123', 'eeeddd' );
  422. } );
  423. it( 'should diff deletion on the end (repetitive substring)', () => {
  424. expectDiffLinear( '123ab123c', '123', 'eeedddddd' );
  425. } );
  426. it( 'should diff deletion in the middle', () => {
  427. expectDiffLinear( '12abc3', '123', 'eeddde' );
  428. } );
  429. it( 'should diff deletion in the middle (repetitive substring)', () => {
  430. expectDiffLinear( '12ab123c3', '123', 'eedddddde', false );
  431. } );
  432. it( 'should diff deletion on both boundaries', () => {
  433. expectDiffLinear( '12abc3', '2ab', 'iiidddddd', false );
  434. } );
  435. it( 'should diff deletion of duplicated content', () => {
  436. expectDiffLinear( '123123', '123', 'eeeddd' );
  437. } );
  438. it( 'should diff deletion of partially duplicated content', () => {
  439. expectDiffLinear( '12323', '123', 'eeedd' );
  440. } );
  441. it( 'should diff deletion of partially duplicated content 2', () => {
  442. expectDiffLinear( '11233', '13', 'eddde', false );
  443. } );
  444. it( 'should diff deletion in array of objects', () => {
  445. const o1 = { foo: 1 };
  446. const o2 = { bar: 2 };
  447. expectDiffLinear( [ o1, o2 ], [ o2 ], 'de' );
  448. } );
  449. it( 'should diff insertion in array of objects with comparator', () => {
  450. expectDiffLinear( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'bar' } ], 'de', true, ( a, b ) => a.text === b.text );
  451. } );
  452. } );
  453. describe( 'replacement', () => {
  454. it( 'should diff replacement of entire text', () => {
  455. expectDiffLinear( '12345', 'abcd', 'iiiiddddd', false );
  456. } );
  457. it( 'should diff replacement on the beginning', () => {
  458. expectDiffLinear( '12345', 'abcd345', 'iiiiddeee' );
  459. } );
  460. it( 'should diff replacement on the beginning (repetitive substring)', () => {
  461. expectDiffLinear( '12345', '345345', 'iiiddeee', false );
  462. } );
  463. it( 'should diff replacement on the end', () => {
  464. expectDiffLinear( '12345', '12ab', 'eeiiddd', false );
  465. } );
  466. it( 'should diff replacement on the end (repetitive substring)', () => {
  467. expectDiffLinear( '12345', '1231234', 'eeeiiiidd', false );
  468. } );
  469. it( 'should diff insertion of duplicated content - case 1', () => {
  470. expectDiffLinear( '1234', '123123', 'eeeiiid' );
  471. } );
  472. it( 'should diff insertion of duplicated content - case 2', () => {
  473. expectDiffLinear( '1234', '13424', 'eiiidde', false );
  474. } );
  475. it( 'should diff replacement in the middle', () => {
  476. expectDiffLinear( '12345', '12ab5', 'eeiidde' );
  477. } );
  478. it( 'should diff replacement in the middle (repetitive substring)', () => {
  479. expectDiffLinear( '12345', '12123455', 'eeiiiiidde', false );
  480. } );
  481. it( 'should diff replacement of duplicated content', () => {
  482. expectDiffLinear( '123123', '123333', 'eeeiidde', false );
  483. } );
  484. it( 'should diff replacement in array of objects', () => {
  485. const o1 = { foo: 1 };
  486. const o2 = { bar: 2 };
  487. expectDiffLinear( [ o1, o2 ], [ o1, { baz: 3 } ], 'eid' );
  488. } );
  489. it( 'should diff insertion in array of objects with comparator', () => {
  490. expectDiffLinear( [ { text: 'foo' }, { text: 'bar' } ], [ { text: 'foo' }, { text: 'baz' } ], 'eid',
  491. true, ( a, b ) => a.text === b.text );
  492. } );
  493. } );
  494. } );
  495. } );
  496. function expectDiff( oldText, newText, expected, checkDiffToChangesCompatibility = true, comparator = null ) {
  497. const result = fastDiff( oldText, newText, comparator );
  498. expect( result ).to.deep.equal( expected, 'fastDiff changes failed' );
  499. if ( checkDiffToChangesCompatibility ) {
  500. expect( result ).to.deep.equal(
  501. diffToChanges( diff( oldText, newText, comparator ), newText ), 'diffToChanges compatibility failed' );
  502. }
  503. }
  504. function expectDiffLinear( oldText, newText, expected, checkDiffCompatibility = true, comparator = null ) {
  505. const actions = { d: 'delete', e: 'equal', i: 'insert' };
  506. const expectedArray = expected.split( '' ).map( item => actions[ item ] );
  507. const result = fastDiff( oldText, newText, comparator, true );
  508. expect( result ).to.deep.equal( expectedArray, 'fastDiff linear result failed' );
  509. if ( checkDiffCompatibility ) {
  510. expect( result ).to.deep.equal( diff( oldText, newText, comparator ), 'diff compatibility failed' );
  511. }
  512. }