8
0

utils.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 utils from '/ckeditor5/core/utils.js';
  7. import coreTestUtils from '/tests/core/_utils/utils.js';
  8. const getIteratorCount = coreTestUtils.getIteratorCount;
  9. describe( 'utils', () => {
  10. describe( 'spy', () => {
  11. it( 'should not have `called` after creation', () => {
  12. let spy = utils.spy();
  13. expect( spy.called ).to.not.be.true();
  14. } );
  15. it( 'should register calls', () => {
  16. let fn1 = utils.spy();
  17. let fn2 = utils.spy();
  18. fn1();
  19. expect( fn1.called ).to.be.true();
  20. expect( fn2.called ).to.not.be.true();
  21. } );
  22. } );
  23. describe( 'uid', () => {
  24. it( 'should return different ids', () => {
  25. let id1 = utils.uid();
  26. let id2 = utils.uid();
  27. let id3 = utils.uid();
  28. expect( id1 ).to.be.a( 'number' );
  29. expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
  30. expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
  31. } );
  32. } );
  33. describe( 'isIterable', () => {
  34. it( 'should be true for string', () => {
  35. let string = 'foo';
  36. expect( utils.isIterable( string ) ).to.be.true;
  37. } );
  38. it( 'should be true for arrays', () => {
  39. let array = [ 1, 2, 3 ];
  40. expect( utils.isIterable( array ) ).to.be.true;
  41. } );
  42. it( 'should be true for iterable classes', () => {
  43. class IterableClass {
  44. constructor() {
  45. this.array = [ 1, 2, 3 ];
  46. }
  47. [ Symbol.iterator ]() {
  48. return this.array[ Symbol.iterator ]();
  49. }
  50. }
  51. let instance = new IterableClass();
  52. expect( utils.isIterable( instance ) ).to.be.true;
  53. } );
  54. it( 'should be false for not iterable objects', () => {
  55. let notIterable = { foo: 'bar' };
  56. expect( utils.isIterable( notIterable ) ).to.be.false;
  57. } );
  58. it( 'should be false for undefined', () => {
  59. expect( utils.isIterable() ).to.be.false;
  60. } );
  61. } );
  62. describe( 'compareArrays', () => {
  63. it( 'should return SAME flag, when arrays are same', () => {
  64. let a = [ 'abc', 0, 3 ];
  65. let b = [ 'abc', 0, 3 ];
  66. let result = utils.compareArrays( a, b );
  67. expect( result ).to.equal( 'SAME' );
  68. } );
  69. it( 'should return PREFIX flag, when all n elements of first array are same as n first elements of the second array', () => {
  70. let a = [ 'abc', 0 ];
  71. let b = [ 'abc', 0, 3 ];
  72. let result = utils.compareArrays( a, b );
  73. expect( result ).to.equal( 'PREFIX' );
  74. } );
  75. it( 'should return EXTENSION flag, when n first elements of first array are same as all elements of the second array', () => {
  76. let a = [ 'abc', 0, 3 ];
  77. let b = [ 'abc', 0 ];
  78. let result = utils.compareArrays( a, b );
  79. expect( result ).to.equal( 'EXTENSION' );
  80. } );
  81. it( 'should return index on which arrays differ, when arrays are not the same', () => {
  82. let a = [ 'abc', 0, 3 ];
  83. let b = [ 'abc', 1, 3 ];
  84. let result = utils.compareArrays( a, b );
  85. expect( result ).to.equal( 1 );
  86. } );
  87. } );
  88. describe( 'toMap', () => {
  89. it( 'should create map from object', () => {
  90. const map = utils.toMap( { foo: 1, bar: 2 } );
  91. expect( getIteratorCount( map ) ).to.equal( 2 );
  92. expect( map.get( 'foo' ) ).to.equal( 1 );
  93. expect( map.get( 'bar' ) ).to.equal( 2 );
  94. } );
  95. it( 'should create map from iterator', () => {
  96. const map = utils.toMap( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
  97. expect( getIteratorCount( map ) ).to.equal( 2 );
  98. expect( map.get( 'foo' ) ).to.equal( 1 );
  99. expect( map.get( 'bar' ) ).to.equal( 2 );
  100. } );
  101. it( 'should create map from another map', () => {
  102. const data = new Map( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
  103. const map = utils.toMap( data );
  104. expect( getIteratorCount( map ) ).to.equal( 2 );
  105. expect( map.get( 'foo' ) ).to.equal( 1 );
  106. expect( map.get( 'bar' ) ).to.equal( 2 );
  107. } );
  108. } );
  109. describe( 'mapsEqual', () => {
  110. it( 'should return true if maps have exactly same entries (order of adding does not matter)', () => {
  111. let mapA = new Map();
  112. let mapB = new Map();
  113. mapA.set( 'foo', 'bar' );
  114. mapA.set( 'abc', 'xyz' );
  115. mapB.set( 'abc', 'xyz' );
  116. mapB.set( 'foo', 'bar' );
  117. expect( utils.mapsEqual( mapA, mapB ) ).to.be.true;
  118. } );
  119. } );
  120. describe( 'nth', () => {
  121. it( 'should return 0th item', () => {
  122. expect( utils.nth( 0, getIterator() ) ).to.equal( 11 );
  123. } );
  124. it( 'should return the last item', () => {
  125. expect( utils.nth( 2, getIterator() ) ).to.equal( 33 );
  126. } );
  127. it( 'should return null if out of range (bottom)', () => {
  128. expect( utils.nth( -1, getIterator() ) ).to.be.null;
  129. } );
  130. it( 'should return null if out of range (top)', () => {
  131. expect( utils.nth( 3, getIterator() ) ).to.be.null;
  132. } );
  133. it( 'should return null if iterator is empty', () => {
  134. expect( utils.nth( 0, [] ) ).to.be.null;
  135. } );
  136. function *getIterator() {
  137. yield 11;
  138. yield 22;
  139. yield 33;
  140. }
  141. } );
  142. describe( 'mix', () => {
  143. const MixinA = {
  144. a() {
  145. return 'a';
  146. }
  147. };
  148. const MixinB = {
  149. b() {
  150. return 'b';
  151. }
  152. };
  153. it( 'mixes 2nd+ argument\'s properties into the first class', () => {
  154. class Foo {}
  155. utils.mix( Foo, MixinA, MixinB );
  156. expect( Foo ).to.not.have.property( 'a' );
  157. expect( Foo ).to.not.have.property( 'b' );
  158. const foo = new Foo();
  159. expect( foo.a() ).to.equal( 'a' );
  160. expect( foo.b() ).to.equal( 'b' );
  161. } );
  162. it( 'does not break the instanceof operator', () => {
  163. class Foo {}
  164. utils.mix( Foo, MixinA );
  165. let foo = new Foo();
  166. expect( foo ).to.be.instanceof( Foo );
  167. } );
  168. it( 'defines properties with the same descriptors as native classes', () => {
  169. class Foo {
  170. foo() {}
  171. }
  172. utils.mix( Foo, MixinA );
  173. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'a' );
  174. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'foo' );
  175. expect( actualDescriptor ).to.have.property( 'writable', expectedDescriptor.writable );
  176. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  177. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  178. } );
  179. it( 'copies setters and getters (with descriptors as of native classes)', () => {
  180. class Foo {
  181. set foo( v ) {
  182. this._foo = v;
  183. }
  184. get foo() {}
  185. }
  186. const Mixin = {
  187. set a( v ) {
  188. this._a = v;
  189. },
  190. get a() {
  191. return this._a;
  192. }
  193. };
  194. utils.mix( Foo, Mixin );
  195. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'a' );
  196. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, 'foo' );
  197. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  198. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  199. const foo = new Foo();
  200. foo.a = 1;
  201. expect( foo._a ).to.equal( 1 );
  202. foo._a = 2;
  203. expect( foo.a ).to.equal( 2 );
  204. } );
  205. it( 'copies symbols (with descriptors as of native classes)', () => {
  206. const symbolA = Symbol( 'a' );
  207. const symbolFoo = Symbol( 'foo' );
  208. // https://github.com/jscs-dev/node-jscs/issues/2078
  209. // jscs:disable disallowSpacesInFunction
  210. class Foo {
  211. [ symbolFoo ]() {
  212. return 'foo';
  213. }
  214. }
  215. const Mixin = {
  216. [ symbolA ]() {
  217. return 'a';
  218. }
  219. };
  220. // jscs:enable disallowSpacesInFunction
  221. utils.mix( Foo, Mixin );
  222. const actualDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, symbolA );
  223. const expectedDescriptor = Object.getOwnPropertyDescriptor( Foo.prototype, symbolFoo );
  224. expect( actualDescriptor ).to.have.property( 'writable', expectedDescriptor.writable );
  225. expect( actualDescriptor ).to.have.property( 'enumerable', expectedDescriptor.enumerable );
  226. expect( actualDescriptor ).to.have.property( 'configurable', expectedDescriptor.configurable );
  227. const foo = new Foo();
  228. expect( foo[ symbolA ]() ).to.equal( 'a' );
  229. } );
  230. it( 'does not copy already existing properties', () => {
  231. class Foo {
  232. a() {
  233. return 'foo';
  234. }
  235. }
  236. utils.mix( Foo, MixinA, MixinB );
  237. const foo = new Foo();
  238. expect( foo.a() ).to.equal( 'foo' );
  239. expect( foo.b() ).to.equal( 'b' );
  240. } );
  241. it( 'does not copy already existing properties - properties deep in the proto chain', () => {
  242. class Foo {
  243. a() {
  244. return 'foo';
  245. }
  246. }
  247. class Bar extends Foo {}
  248. utils.mix( Bar, MixinA, MixinB );
  249. const bar = new Bar();
  250. expect( bar.a() ).to.equal( 'foo' );
  251. expect( bar.b() ).to.equal( 'b' );
  252. } );
  253. } );
  254. } );