tools.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. ( () => {
  7. bender.tools.core = {
  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. * bender.tools.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. bender.amd.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. * bender.tools.core.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. } )();