compressedhistory.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import CompressedHistory from '/ckeditor5/engine/model/compressedhistory.js';
  7. import Delta from '/ckeditor5/engine/model/delta/delta.js';
  8. import Operation from '/ckeditor5/engine/model/operation/operation.js';
  9. describe( 'CompressedHistory', () => {
  10. let history;
  11. beforeEach( () => {
  12. history = new CompressedHistory();
  13. } );
  14. describe( 'updateDelta', () => {
  15. it( 'should substitute delta from history by given deltas', () => {
  16. history.addDelta( getDelta( 0 ) );
  17. const deltas = getDeltaSet();
  18. history.updateDelta( 0, deltas );
  19. const historyDeltas = Array.from( history.getDeltas() );
  20. expect( historyDeltas ).to.deep.equal( deltas );
  21. } );
  22. it( 'should substitute all updated deltas by new deltas', () => {
  23. history.addDelta( getDelta( 0 ) );
  24. // Change original single delta to three deltas generated by `getDeltaSet`.
  25. // All those deltas should now be seen under base version 0.
  26. history.updateDelta( 0, getDeltaSet() );
  27. const deltas = getDeltaSet();
  28. // Change all three deltas from base version 0 to new set of deltas.
  29. history.updateDelta( 0, deltas );
  30. const historyDeltas = Array.from( history.getDeltas() );
  31. expect( historyDeltas ).to.deep.equal( deltas );
  32. } );
  33. it( 'should do nothing if deltas for given base version has not been found in history', () => {
  34. history.addDelta( getDelta( 0 ) );
  35. history.removeDelta( 0 );
  36. const deltas = getDeltaSet();
  37. history.updateDelta( 0, deltas );
  38. expect( Array.from( history.getDeltas() ).length ).to.equal( 0 );
  39. } );
  40. } );
  41. describe( 'removeDelta', () => {
  42. it( 'should remove deltas that do not have graveyard related operations', () => {
  43. for ( let delta of getDeltaSet() ) {
  44. history.addDelta( delta );
  45. }
  46. history.removeDelta( 3 );
  47. const deltas = Array.from( history.getDeltas() );
  48. expect( deltas.length ).to.equal( 2 );
  49. } );
  50. it( 'should remove multiple updated deltas', () => {
  51. let delta = getDelta( 0 );
  52. history.addDelta( delta );
  53. let updatedDeltas = getDeltaSet( 0 );
  54. history.updateDelta( 0, updatedDeltas );
  55. history.removeDelta( 0 );
  56. const deltas = Array.from( history.getDeltas() );
  57. expect( deltas.length ).to.equal( 0 );
  58. } );
  59. it( 'should do nothing if deltas for given base version has not been found in history', () => {
  60. const deltas = getDeltaSet();
  61. for ( let delta of deltas ) {
  62. history.addDelta( delta );
  63. }
  64. history.removeDelta( 12 );
  65. expect( Array.from( history.getDeltas() ) ).to.deep.equal( deltas );
  66. } );
  67. } );
  68. describe( 'getDelta', () => {
  69. it( 'should return array with one delta with given base version', () => {
  70. let delta = getDelta( 0 );
  71. history.addDelta( delta );
  72. const historyDelta = history.getDelta( 0 );
  73. expect( historyDelta ).to.deep.equal( [ delta ] );
  74. } );
  75. it( 'should return array with all updated deltas of delta with given base version', () => {
  76. let delta = getDelta( 0 );
  77. history.addDelta( delta );
  78. let deltas = getDeltaSet();
  79. history.updateDelta( 0, deltas );
  80. const historyDelta = history.getDelta( 0 );
  81. expect( historyDelta ).to.deep.equal( deltas );
  82. } );
  83. it( 'should return null if delta has not been found in history', () => {
  84. expect( history.getDelta( -1 ) ).to.be.null;
  85. expect( history.getDelta( 2 ) ).to.be.null;
  86. expect( history.getDelta( 20 ) ).to.be.null;
  87. } );
  88. it( 'should return null if delta has been removed by removeDelta', () => {
  89. let delta = getDelta( 0 );
  90. history.addDelta( delta );
  91. history.removeDelta( 0 );
  92. expect( history.getDelta( 0 ) ).to.be.null;
  93. } );
  94. } );
  95. describe( 'getInactiveBaseVersions', () => {
  96. it( 'should return inactive base versions from given base versions range', () => {
  97. for ( let delta of getDeltaSet() ) {
  98. history.addDelta( delta );
  99. }
  100. history.removeDelta( 3 );
  101. history.removeDelta( 6 );
  102. expect( Array.from( history.getInactiveBaseVersions() ) ).to.deep.equal( [ 3, 6 ] );
  103. expect( Array.from( history.getInactiveBaseVersions( 0 ) ) ).to.deep.equal( [ 3, 6 ] );
  104. expect( Array.from( history.getInactiveBaseVersions( 3 ) ) ).to.deep.equal( [ 3, 6 ] );
  105. expect( Array.from( history.getInactiveBaseVersions( 0, 3 ) ) ).to.deep.equal( [] );
  106. expect( Array.from( history.getInactiveBaseVersions( 0, 4 ) ) ).to.deep.equal( [ 3 ] );
  107. expect( Array.from( history.getInactiveBaseVersions( 0, 6 ) ) ).to.deep.equal( [ 3 ] );
  108. expect( Array.from( history.getInactiveBaseVersions( 4 ) ) ).to.deep.equal( [ 6 ] );
  109. expect( Array.from( history.getInactiveBaseVersions( 6 ) ) ).to.deep.equal( [ 6 ] );
  110. expect( Array.from( history.getInactiveBaseVersions( 9 ) ) ).to.deep.equal( [] );
  111. } );
  112. } );
  113. } );
  114. function getDeltaSet() {
  115. const deltas = [];
  116. deltas.push( getDelta( 0 ) );
  117. deltas.push( getDelta( 3 ) );
  118. deltas.push( getDelta( 6 ) );
  119. return deltas;
  120. }
  121. function getDelta( baseVersion ) {
  122. const delta = new Delta();
  123. for ( let i = 0; i < 3; i++ ) {
  124. delta.addOperation( new Operation( i + baseVersion ) );
  125. }
  126. return delta;
  127. }