Browse Source

Changed utils.compareArrays and added tests for it.
Code style for utils tests.

Szymon Cofalik 10 years ago
parent
commit
674afccd12
2 changed files with 135 additions and 101 deletions
  1. 15 9
      packages/ckeditor5-engine/src/utils.js
  2. 120 92
      packages/ckeditor5-engine/tests/utils/utils.js

+ 15 - 9
packages/ckeditor5-engine/src/utils.js

@@ -60,15 +60,15 @@ CKEDITOR.define( [ 'utils-lodash', 'lib/lodash/lodash-ckeditor' ], function( lod
 		 * or completely different.
 		 *
 		 *   compareArrays( [ 0, 2 ], [ 0, 2 ] ); // SAME
-		 *   compareArrays( [ 0, 2 ], [ 0 ] ); // PREFIX
-		 *   compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // DIFFERENT
-		 *   compareArrays( [ 0, 2 ], [ 1, 3 ] ); // DIFFERENT
+		 *   compareArrays( [ 0, 2 ], [ 0, 2, 1 ] ); // PREFIX
+		 *   compareArrays( [ 0, 2 ], [ 0 ] ); // SUFFIX
+		 *   compareArrays( [ 0, 2 ], [ 1, 2 ] ); // DIFFERENT
 		 *
 		 * @param {Array} a Array that is compared.
 		 * @param {Array} b Array to compare with.
 		 * @returns {Number} How array `a` is related to array `b`. Represented by one of flags:
-		 * `a` is {@link utils.compareArrays#SAME same}, `a` is a {@link utils.compareArrays#PREFIX prefix}
-		 * or `a` is {@link utils.compareArrays#DIFFERENT different}.
+		 * `a` is {@link utils.compareArrays#SAME same}, `a` is a {@link utils.compareArrays#PREFIX prefix),
+		 * `a` is a {@link utils.compareArrays#SUFFIX suffix}, or `a` is {@link utils.compareArrays#DIFFERENT different}.
 		 */
 		compareArrays( a, b ) {
 			var minLen = Math.min( a.length, b.length );
@@ -87,10 +87,10 @@ CKEDITOR.define( [ 'utils-lodash', 'lib/lodash/lodash-ckeditor' ], function( lod
 			} else if ( a.length < b.length ) {
 				// Compared array is shorter so it is a prefix of the other array.
 				return utils.compareArrays.PREFIX;
+			} else {
+				// Compared array is longer so it is a suffix of the other array.
+				return utils.compareArrays.SUFFIX;
 			}
-
-			// In other case, the arrays are different.
-			return utils.compareArrays.DIFFERENT;
 		}
 	};
 
@@ -106,12 +106,18 @@ CKEDITOR.define( [ 'utils-lodash', 'lib/lodash/lodash-ckeditor' ], function( lod
 	 * @type {number}
 	 */
 	utils.compareArrays.PREFIX = 1;
+	/**
+	 * Flag for "is a suffix of" relation between arrays.
+	 *
+	 * @type {number}
+	 */
+	utils.compareArrays.SUFFIX = 2;
 	/**
 	 * Flag for "is different than" relation between arrays.
 	 *
 	 * @type {number}
 	 */
-	utils.compareArrays.DIFFERENT = 2;
+	utils.compareArrays.DIFFERENT = 3;
 
 	// Extend "utils" with Lo-Dash methods.
 	for ( var i = 0; i < lodashIncludes.length; i++ ) {

+ 120 - 92
packages/ckeditor5-engine/tests/utils/utils.js

@@ -7,130 +7,158 @@
 
 var modules = bender.amd.require( 'utils', 'utils-lodash' );
 
-describe( 'extend()', function() {
-	// Properties of the subsequent objects should override properties of the preceding objects. This is critical for
-	// CKEditor so we keep this test to ensure that Lo-Dash (or whatever) implements it in the way we need it.
-	it( 'should extend by several params in the correct order', function() {
-		var utils = modules.utils;
-
-		var target = {
-			a: 0,
-			b: 0
-		};
-
-		var ext1 = {
-			b: 1,
-			c: 1
-		};
-
-		var ext2 = {
-			c: 2,
-			d: 2
-		};
-
-		utils.extend( target, ext1, ext2 );
-
-		expect( target ).to.have.property( 'a' ).to.equal( 0 );
-		expect( target ).to.have.property( 'b' ).to.equal( 1 );
-		expect( target ).to.have.property( 'c' ).to.equal( 2 );
-		expect( target ).to.have.property( 'd' ).to.equal( 2 );
+describe( 'utils', function() {
+	var utils;
+
+	before( function() {
+		utils = modules.utils;
 	} );
-} );
 
-describe( 'spy', function() {
-	it( 'should not have `called` after creation', function() {
-		var utils = modules.utils;
+	describe( 'extend()', function() {
+		// Properties of the subsequent objects should override properties of the preceding objects. This is critical for
+		// CKEditor so we keep this test to ensure that Lo-Dash (or whatever) implements it in the way we need it.
+		it( 'should extend by several params in the correct order', function() {
+			var target = {
+				a: 0,
+				b: 0
+			};
+
+			var ext1 = {
+				b: 1,
+				c: 1
+			};
+
+			var ext2 = {
+				c: 2,
+				d: 2
+			};
+
+			utils.extend( target, ext1, ext2 );
+
+			expect( target ).to.have.property( 'a' ).to.equal( 0 );
+			expect( target ).to.have.property( 'b' ).to.equal( 1 );
+			expect( target ).to.have.property( 'c' ).to.equal( 2 );
+			expect( target ).to.have.property( 'd' ).to.equal( 2 );
+		} );
+	} );
 
-		var spy = utils.spy();
+	describe( 'spy', function() {
+		it( 'should not have `called` after creation', function() {
+			var spy = utils.spy();
 
-		expect( spy.called ).to.not.be.true();
-	} );
+			expect( spy.called ).to.not.be.true();
+		} );
+
+		it( 'should register calls', function() {
+			var fn1 = utils.spy();
+			var fn2 = utils.spy();
 
-	it( 'should register calls', function() {
-		var utils = modules.utils;
+			fn1();
 
-		var fn1 = utils.spy();
-		var fn2 = utils.spy();
+			expect( fn1.called ).to.be.true();
+			expect( fn2.called ).to.not.be.true();
+		} );
+	} );
 
-		fn1();
+	describe( 'uid', function() {
+		it( 'should return different ids', function() {
+			var id1 = utils.uid();
+			var id2 = utils.uid();
+			var id3 = utils.uid();
 
-		expect( fn1.called ).to.be.true();
-		expect( fn2.called ).to.not.be.true();
+			expect( id1 ).to.be.a( 'number' );
+			expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
+			expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
+		} );
 	} );
-} );
 
-describe( 'uid', function() {
-	it( 'should return different ids', function() {
-		var utils = modules.utils;
+	describe( 'isIterable', function() {
+		it( 'should be true for string', function() {
+			var string = 'foo';
 
-		var id1 = utils.uid();
-		var id2 = utils.uid();
-		var id3 = utils.uid();
+			expect( utils.isIterable( string ) ).to.be.true;
+		} );
 
-		expect( id1 ).to.be.a( 'number' );
-		expect( id2 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id3 );
-		expect( id3 ).to.be.a( 'number' ).to.not.equal( id1 ).to.not.equal( id2 );
-	} );
-} );
+		it( 'should be true for arrays', function() {
+			var array = [ 1, 2, 3 ];
 
-describe( 'isIterable', function() {
-	it( 'should be true for string', function() {
-		var utils = modules.utils;
+			expect( utils.isIterable( array ) ).to.be.true;
+		} );
 
-		var string = 'foo';
+		it( 'should be true for iterable classes', function() {
+			class IterableClass {
+				constructor() {
+					this.array = [ 1, 2, 3 ];
+				}
 
-		expect( utils.isIterable( string ) ).to.be.true;
-	} );
+				[ Symbol.iterator ]() {
+					return this.array[ Symbol.iterator ]();
+				}
+			}
 
-	it( 'should be true for arrays', function() {
-		var utils = modules.utils;
+			var instance = new IterableClass();
 
-		var array = [ 1, 2, 3 ];
+			expect( utils.isIterable( instance ) ).to.be.true;
+		} );
+
+		it( 'should be false for not iterable objects', function() {
+			var notIterable = { foo: 'bar' };
+
+			expect( utils.isIterable( notIterable ) ).to.be.false;
+		} );
 
-		expect( utils.isIterable( array ) ).to.be.true;
+		it( 'should be false for undefined', function() {
+			expect( utils.isIterable() ).to.be.false;
+		} );
 	} );
 
-	it( 'should be true for iterable classes', function() {
-		var utils = modules.utils;
+	describe( 'compareArrays', function() {
+		it( 'should return SAME flag, when arrays are same', function() {
+			var a = [ 'abc', 0, 3 ];
+			var b = [ 'abc', 0, 3 ];
 
-		class IterableClass {
-			constructor() {
-				this.array = [ 1, 2, 3 ];
-			}
+			var result = utils.compareArrays( a, b );
 
-			[ Symbol.iterator ]() {
-				return this.array[ Symbol.iterator ]();
-			}
-		}
+			expect( result ).to.equal( utils.compareArrays.SAME );
+		} );
 
-		var instance = new IterableClass();
+		it( 'should return PREFIX flag, when all n elements of first array are same as n first elements of the second array', function() {
+			var a = [ 'abc', 0 ];
+			var b = [ 'abc', 0, 3 ];
 
-		expect( utils.isIterable( instance ) ).to.be.true;
-	} );
+			var result = utils.compareArrays( a, b );
+
+			expect( result ).to.equal( utils.compareArrays.PREFIX );
+		} );
 
-	it( 'should be false for not iterable objects', function() {
-		var utils = modules.utils;
+		it( 'should return SUFFIX flag, when n first elements of first array are same as all elements of the second array', function() {
+			var a = [ 'abc', 0, 3 ];
+			var b = [ 'abc', 0 ];
 
-		var notIterable = { foo: 'bar' };
+			var result = utils.compareArrays( a, b );
 
-		expect( utils.isIterable( notIterable ) ).to.be.false;
-	} );
+			expect( result ).to.equal( utils.compareArrays.SUFFIX );
+		} );
+
+		it( 'should return DIFFERENT flag, when arrays are not same', function() {
+			var a = [ 'abc', 0, 3 ];
+			var b = [ 'abc', 1, 3 ];
 
-	it( 'should be false for undefined', function() {
-		var utils = modules.utils;
+			var result = utils.compareArrays( a, b );
 
-		expect( utils.isIterable() ).to.be.false;
+			expect( result ).to.equal( utils.compareArrays.DIFFERENT );
+		} );
 	} );
-} );
 
-describe( 'Lo-Dash extensions', function() {
-	// Ensures that the required Lo-Dash extensions are available in `utils`.
-	it( 'should be exposed in utils', function() {
-		var utils = modules.utils;
-		var extensions = modules[ 'utils-lodash' ];
+	describe( 'Lo-Dash extensions', function() {
+		// Ensures that the required Lo-Dash extensions are available in `utils`.
+		it( 'should be exposed in utils', function() {
+			var utils = modules.utils;
+			var extensions = modules[ 'utils-lodash' ];
 
-		extensions.forEach( function( extension ) {
-			expect( utils ).to.have.property( extension ).to.not.be.undefined();
+			extensions.forEach( function( extension ) {
+				expect( utils ).to.have.property( extension ).to.not.be.undefined();
+			} );
 		} );
 	} );
 } );