diff.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. } );