utils.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 amdUtils from '/tests/_utils/amd.js';
  7. const utils = {
  8. /**
  9. * Defines CKEditor plugin which is a mock of an editor creator.
  10. *
  11. * If `proto` is not set or it does not define `create()` and `destroy()` methods,
  12. * then they will be set to Sinon spies. Therefore the shortest usage is:
  13. *
  14. * testUtils.defineEditorCreatorMock( 'test1' );
  15. *
  16. * The mocked creator is available under:
  17. *
  18. * editor.plugins.get( 'creator-thename' );
  19. *
  20. * @param {String} creatorName Name of the creator.
  21. * @param {Object} [proto] Prototype of the creator. Properties from the proto param will
  22. * be copied to the prototype of the creator.
  23. */
  24. defineEditorCreatorMock( creatorName, proto ) {
  25. amdUtils.define( 'creator-' + creatorName, [ 'core/creator' ], ( Creator ) => {
  26. class TestCreator extends Creator {}
  27. if ( proto ) {
  28. for ( let propName in proto ) {
  29. TestCreator.prototype[ propName ] = proto[ propName ];
  30. }
  31. }
  32. if ( !TestCreator.prototype.create ) {
  33. TestCreator.prototype.create = sinon.spy().named( creatorName + '-create' );
  34. }
  35. if ( !TestCreator.prototype.destroy ) {
  36. TestCreator.prototype.destroy = sinon.spy().named( creatorName + '-destroy' );
  37. }
  38. return TestCreator;
  39. } );
  40. },
  41. /**
  42. * Returns the number of elements return by the iterator.
  43. *
  44. * testUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
  45. *
  46. * @param {Iterable.<*>} iterator Any iterator.
  47. * @returns {Number} Number of elements returned by that iterator.
  48. */
  49. getIteratorCount( iterator ) {
  50. let count = 0;
  51. for ( let _ of iterator ) { // jshint ignore:line
  52. count++;
  53. }
  54. return count;
  55. }
  56. };
  57. utils.treemodel = {
  58. /**
  59. * Returns tree structure as a simplified string. Elements are uppercase and characters are lowercase.
  60. * Start and end of an element is marked the same way, by the element's name (in uppercase).
  61. *
  62. * let element = new Element( 'div', [], [ 'abc', new Element( 'p', [], 'foo' ), 'xyz' ] );
  63. * treemodelUtils.getNodesAndText( element ); // abcPfooPxyz
  64. *
  65. * @param {treeModel.Range} range Range to stringify.
  66. * @returns {String} String representing element inner structure.
  67. */
  68. getNodesAndText( range ) {
  69. console.error( 'This should be moved to ckeditor5-core/tests/treemodel/_utils/' ); /* jshint ignore:line */
  70. let txt = '';
  71. for ( let step of range ) {
  72. let node = step.node;
  73. if ( node.character ) {
  74. txt += node.character.toLowerCase();
  75. } else if ( node.name ) {
  76. txt += node.name.toUpperCase();
  77. }
  78. }
  79. return txt;
  80. }
  81. };
  82. export default utils;