8
0

diff.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. import diff from '../src/diff';
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import getLongText from './_utils/longtext';
  8. describe( 'diff', () => {
  9. let fastDiffSpy;
  10. testUtils.createSinonSandbox();
  11. beforeEach( () => {
  12. fastDiffSpy = testUtils.sinon.spy( diff, 'fastDiff' );
  13. } );
  14. it( 'should diff strings', () => {
  15. expect( diff( 'aba', 'acca' ) ).to.deep.equals( [ 'equal', 'insert', 'insert', 'delete', 'equal' ] );
  16. testUtils.sinon.assert.notCalled( fastDiffSpy );
  17. } );
  18. it( 'should diff arrays', () => {
  19. expect( diff( Array.from( 'aba' ), Array.from( 'acca' ) ) ).to.deep.equals( [ 'equal', 'insert', 'insert', 'delete', 'equal' ] );
  20. testUtils.sinon.assert.notCalled( fastDiffSpy );
  21. } );
  22. it( 'should reverse result if the second string is shorter', () => {
  23. expect( diff( 'acca', 'aba' ) ).to.deep.equals( [ 'equal', 'delete', 'delete', 'insert', 'equal' ] );
  24. testUtils.sinon.assert.notCalled( fastDiffSpy );
  25. } );
  26. it( 'should diff if strings are same', () => {
  27. expect( diff( 'abc', 'abc' ) ).to.deep.equals( [ 'equal', 'equal', 'equal' ] );
  28. testUtils.sinon.assert.notCalled( fastDiffSpy );
  29. } );
  30. it( 'should diff if one string is empty', () => {
  31. expect( diff( '', 'abc' ) ).to.deep.equals( [ 'insert', 'insert', 'insert' ] );
  32. testUtils.sinon.assert.notCalled( fastDiffSpy );
  33. } );
  34. it( 'should use custom comparator', () => {
  35. expect( diff( 'aBc', 'abc' ) ).to.deep.equals( [ 'equal', 'insert', 'delete', 'equal' ] );
  36. expect( diff( 'aBc', 'abc', ( a, b ) => a.toLowerCase() == b.toLowerCase() ) ).to.deep.equals( [ 'equal', 'equal', 'equal' ] );
  37. testUtils.sinon.assert.notCalled( fastDiffSpy );
  38. } );
  39. it( 'should use fastDiff() internally for strings with 400+ length and length sum of 1400+', () => {
  40. diff( getLongText( 400 ), getLongText( 1000 ) );
  41. testUtils.sinon.assert.called( fastDiffSpy );
  42. } );
  43. it( 'should use fastDiff() internally for arrays with 400+ length and length sum of 1400+', () => {
  44. diff( getLongText( 500 ).split( '' ), getLongText( 950 ).split( '' ) );
  45. testUtils.sinon.assert.called( fastDiffSpy );
  46. } );
  47. it( 'should use fastDiff() internally for strings with length sum of 2000+', () => {
  48. diff( getLongText( 100 ), getLongText( 2000 ) );
  49. testUtils.sinon.assert.called( fastDiffSpy );
  50. } );
  51. it( 'should use fastDiff() internally for strings with length sum of 2000+', () => {
  52. diff( getLongText( 10 ), getLongText( 1990 ) );
  53. testUtils.sinon.assert.called( fastDiffSpy );
  54. } );
  55. describe( 'with multi-byte unicode', () => {
  56. describe( 'simple emoji - single unicode code point', () => {
  57. // 🙂 = '\ud83d\ude42' = 2 chars
  58. const emojiLength = '🙂'.length;
  59. const emojiDiffInsert = new Array( emojiLength ).fill( 'insert' );
  60. const emojiDiffEqual = new Array( emojiLength ).fill( 'equal' );
  61. const emojiDiffDelete = new Array( emojiLength ).fill( 'delete' );
  62. it( 'should properly handle emoji insertion', () => {
  63. expect( diff( 'abc', 'ab🙂c' ) ).to.deep.equals( [ 'equal', 'equal', ...emojiDiffInsert, 'equal' ] );
  64. } );
  65. it( 'should properly handle emoji insertion on the end', () => {
  66. expect( diff( 'abc', 'abc🙂' ) ).to.deep.equals( [ 'equal', 'equal', 'equal', ...emojiDiffInsert ] );
  67. } );
  68. it( 'should properly handle appending to string containing emoji', () => {
  69. expect( diff( 'abc🙂', 'abc🙂d' ) ).to.deep.equals( [ 'equal', 'equal', 'equal', ...emojiDiffEqual, 'insert' ] );
  70. } );
  71. it( 'should properly handle insertion to string containing emoji', () => {
  72. expect( diff( 'ab🙂cd', 'ab🙂cde' ) ).to.deep.equals( [ 'equal', 'equal', ...emojiDiffEqual, 'equal', 'equal', 'insert' ] );
  73. } );
  74. it( 'should properly remove emoji', () => {
  75. expect( diff( 'a🙂b', 'ab' ) ).to.deep.equals( [ 'equal', ...emojiDiffDelete, 'equal' ] );
  76. } );
  77. it( 'should properly replace emoji', () => {
  78. expect( diff( 'a🙂b', 'axb' ) ).to.deep.equals( [ 'equal', ...emojiDiffDelete, 'insert', 'equal' ] );
  79. } );
  80. } );
  81. describe( 'combined emoji - unicode ZWJ sequence', () => {
  82. // 👩‍🦰 = '\ud83d\udc69\u200d\ud83e\uddB0' = 5 chars
  83. const emojiLength = '👩‍🦰'.length;
  84. const emojiDiffInsert = new Array( emojiLength ).fill( 'insert' );
  85. const emojiDiffEqual = new Array( emojiLength ).fill( 'equal' );
  86. const emojiDiffDelete = new Array( emojiLength ).fill( 'delete' );
  87. it( 'should properly handle emoji insertion (with ZWJ)', () => {
  88. expect( diff( 'abc', 'ab👩‍🦰c' ) ).to.deep.equals( [ 'equal', 'equal', ...emojiDiffInsert, 'equal' ] );
  89. } );
  90. it( 'should properly handle emoji insertion on the end (with ZWJ)', () => {
  91. expect( diff( 'abc', 'abc👩‍🦰' ) ).to.deep.equals( [ 'equal', 'equal', 'equal', ...emojiDiffInsert ] );
  92. } );
  93. it( 'should properly handle appending to string containing emoji (with ZWJ)', () => {
  94. expect( diff( 'ab👩‍🦰', 'ab👩‍🦰c' ) ).to.deep.equals( [ 'equal', 'equal', ...emojiDiffEqual, 'insert' ] );
  95. } );
  96. it( 'should properly handle insertion to string containing emoji (with ZWJ)', () => {
  97. expect( diff( 'a👩‍🦰b', 'a👩‍🦰bc' ) ).to.deep.equals( [ 'equal', ...emojiDiffEqual, 'equal', 'insert' ] );
  98. } );
  99. it( 'should properly remove emoji (with ZWJ)', () => {
  100. expect( diff( 'a👩‍🦰b', 'ab' ) ).to.deep.equals( [ 'equal', ...emojiDiffDelete, 'equal' ] );
  101. } );
  102. it( 'should properly replace emoji (with ZWJ)', () => {
  103. expect( diff( 'a👩‍🦰b', 'axb' ) ).to.deep.equals( [ 'equal', ...emojiDiffDelete, 'insert', 'equal' ] );
  104. } );
  105. it( 'should properly replace ZWJ sequence with simple emoji', () => {
  106. const simpleEmojiDiffInsert = new Array( '🙂'.length ).fill( 'insert' );
  107. // Note that first char of both emoji is the same.
  108. expect( diff( 'a👩‍🦰b', 'a🙂b' ) ).to.deep.equals( [
  109. 'equal', 'equal', ...emojiDiffDelete.slice( 1 ), ...simpleEmojiDiffInsert.slice( 1 ), 'equal'
  110. ] );
  111. } );
  112. it( 'should properly replace simple emoji with ZWJ sequence', () => {
  113. const simpleEmojiDiffDelete = new Array( '🙂'.length ).fill( 'delete' );
  114. // Note that first char of both emoji is the same.
  115. expect( diff( 'a🙂b', 'a👩‍🦰b' ) ).to.deep.equals( [
  116. 'equal', 'equal', ...emojiDiffInsert.slice( 1 ), ...simpleEmojiDiffDelete.slice( 1 ), 'equal'
  117. ] );
  118. } );
  119. } );
  120. } );
  121. } );