Bläddra i källkod

Fixed tests for Attribute, Character.
Fixed and added tests for Element.

Szymon Cofalik 10 år sedan
förälder
incheckning
0a4d27d61c

+ 50 - 54
packages/ckeditor5-engine/tests/document/attribute.js

@@ -9,77 +9,73 @@
 
 var modules = bender.amd.require( 'document/attribute' );
 
-describe( 'attribute', function() {
-	beforeEach( function() {
-		var Attribute = modules[ 'document/attribute' ];
+describe( 'Attribute', function() {
+	var Attribute;
 
-		Attribute._register = {};
+	before( function() {
+		Attribute = modules[ 'document/attribute' ];
 	} );
 
-	it( 'should create attribute', function() {
-		var Attribute = modules[ 'document/attribute' ];
-
-		var attr = new Attribute( 'foo', 'bar' );
-
-		expect( attr ).to.have.property( 'key' ).that.equals( 'foo' );
-		expect( attr ).to.have.property( 'value' ).that.equals( 'bar' );
+	beforeEach( function() {
+		Attribute._register = {};
 	} );
 
-	it( 'should create equal instance even if object has different order', function() {
-		var Attribute = modules[ 'document/attribute' ];
+	describe( 'constructor', function() {
+		it( 'should create attribute', function() {
+			var attr = new Attribute( 'foo', 'bar' );
 
-		var attr1 = new Attribute( 'foo', { a: 1, b: 2 } );
-		var attr2 = new Attribute( 'foo', { b: 2, a: 1 } );
+			expect( attr ).to.have.property( 'key' ).that.equals( 'foo' );
+			expect( attr ).to.have.property( 'value' ).that.equals( 'bar' );
+		} );
 
-		expect( attr1.isEqual( attr2 ) ).to.be.true;
-	} );
+		it( 'should create equal instance even if object has different order', function() {
+			var attr1 = new Attribute( 'foo', { a: 1, b: 2 } );
+			var attr2 = new Attribute( 'foo', { b: 2, a: 1 } );
 
-	it( 'should return the same object for registered objects', function() {
-		var Attribute = modules[ 'document/attribute' ];
+			expect( attr1.isEqual( attr2 ) ).to.be.true;
+		} );
 
-		Attribute.register( 'register', true );
+		it( 'should return the same object for registered objects', function() {
+			Attribute.register( 'register', true );
 
-		var attr1 = new Attribute( 'register', true );
-		var attr2 = new Attribute( 'register', true );
+			var attr1 = new Attribute( 'register', true );
+			var attr2 = new Attribute( 'register', true );
 
-		expect( attr1 ).to.equals( attr2 );
-		expect( attr1.isEqual( attr2 ) ).to.be.true;
-	} );
-
-	it( 'should return different objects for different values', function() {
-		var Attribute = modules[ 'document/attribute' ];
-
-		Attribute.register( 'register', true );
+			expect( attr1 ).to.equals( attr2 );
+			expect( attr1.isEqual( attr2 ) ).to.be.true;
+		} );
 
-		var attr1 = new Attribute( 'register', true );
-		var attr2 = new Attribute( 'register', false );
+		it( 'should return different objects for different values', function() {
+			Attribute.register( 'register', true );
 
-		expect( attr1 ).to.not.be.equals( attr2 );
-		expect( attr1.isEqual( attr2 ) ).to.not.be.true;
-	} );
+			var attr1 = new Attribute( 'register', true );
+			var attr2 = new Attribute( 'register', false );
 
-	it( 'should return different objects for not registered objects', function() {
-		var Attribute = modules[ 'document/attribute' ];
+			expect( attr1 ).to.not.be.equals( attr2 );
+			expect( attr1.isEqual( attr2 ) ).to.not.be.true;
+		} );
 
-		Attribute.register( 'register', true );
+		it( 'should return different objects for not registered objects', function() {
+			Attribute.register( 'register', true );
 
-		var attr1 = new Attribute( 'register', false );
-		var attr2 = new Attribute( 'register', false );
+			var attr1 = new Attribute( 'register', false );
+			var attr2 = new Attribute( 'register', false );
 
-		expect( attr1 ).to.not.be.equals( attr2 );
-		expect( attr1.isEqual( attr2 ) ).to.be.true;
+			expect( attr1 ).to.not.be.equals( attr2 );
+			expect( attr1.isEqual( attr2 ) ).to.be.true;
+		} );
 	} );
 
-	it( 'Attribute.register should return registered attribute', function() {
-		var Attribute = modules[ 'document/attribute' ];
-
-		var attr1 = new Attribute( 'register', true );
-		var attr2 = Attribute.register( 'register', true );
-		var attr3 = Attribute.register( 'register', true );
-		var attr4 = new Attribute( 'register', true );
-
-		expect( attr1 ).to.not.be.equals( attr2 );
-		expect( attr2 ).to.equals( attr3 );
-		expect( attr3 ).to.equals( attr4 );
+	describe( 'register', function() {
+		it( 'Attribute.register should return registered attribute', function() {
+			var attr1 = new Attribute( 'register', true );
+			var attr2 = Attribute.register( 'register', true );
+			var attr3 = Attribute.register( 'register', true );
+			var attr4 = new Attribute( 'register', true );
+
+			expect( attr1 ).to.not.be.equals( attr2 );
+			expect( attr2 ).to.equals( attr3 );
+			expect( attr3 ).to.equals( attr4 );
+		} );
 	} );
-} );
+} );

+ 31 - 31
packages/ckeditor5-engine/tests/document/character.js

@@ -15,37 +15,37 @@ var modules = bender.amd.require(
 	'document/element',
 	'document/attribute' );
 
-describe( 'constructor', function() {
-	it( 'should create character without attributes', function() {
-		var Element = modules[ 'document/element' ];
-		var Character = modules[ 'document/character' ];
-		var Node = modules[ 'document/node' ];
-
-		var character = new Character( 'f' );
-		var parent = new Element( 'parent', [], character );
-
-		expect( character ).to.be.an.instanceof( Node );
-		expect( character ).to.have.property( 'character' ).that.equals( 'f' );
-		expect( character ).to.have.property( 'parent' ).that.equals( parent );
-		expect( character._getAttrCount() ).to.equals( 0 );
+describe( 'Character', function() {
+	var Element, Character, Node, Attribute;
+
+	before( function() {
+		Element = modules[ 'document/element' ];
+		Character = modules[ 'document/character' ];
+		Node = modules[ 'document/node' ];
+		Attribute = modules[ 'document/attribute' ];
 	} );
 
-	it( 'should create character with attributes', function() {
-		var Element = modules[ 'document/element' ];
-		var Character = modules[ 'document/character' ];
-		var Node = modules[ 'document/node' ];
-		var Attribute = modules[ 'document/attribute' ];
-
-		var attr = new Attribute( 'foo', 'bar' );
-
-		var character = new Character( 'f', [ attr ] );
-
-		var parent = new Element( 'parent', [], character );
-
-		expect( character ).to.be.an.instanceof( Node );
-		expect( character ).to.have.property( 'character' ).that.equals( 'f' );
-		expect( character ).to.have.property( 'parent' ).that.equals( parent );
-		expect( character._getAttrCount() ).to.equals( 1 );
-		expect( character.getAttr( attr.key ) ).to.equals( attr.value );
+	describe( 'constructor', function() {
+		it( 'should create character without attributes', function() {
+			var character = new Character( 'f' );
+			var parent = new Element( 'parent', [], character );
+
+			expect( character ).to.be.an.instanceof( Node );
+			expect( character ).to.have.property( 'character' ).that.equals( 'f' );
+			expect( character ).to.have.property( 'parent' ).that.equals( parent );
+			expect( character._getAttrCount() ).to.equals( 0 );
+		} );
+
+		it( 'should create character with attributes', function() {
+			var attr = new Attribute( 'foo', 'bar' );
+			var character = new Character( 'f', [ attr ] );
+			var parent = new Element( 'parent', [], character );
+
+			expect( character ).to.be.an.instanceof( Node );
+			expect( character ).to.have.property( 'character' ).that.equals( 'f' );
+			expect( character ).to.have.property( 'parent' ).that.equals( parent );
+			expect( character._getAttrCount() ).to.equals( 1 );
+			expect( character.getAttr( attr.key ) ).to.equals( attr.value );
+		} );
 	} );
-} );
+} );

+ 93 - 93
packages/ckeditor5-engine/tests/document/element.js

@@ -11,113 +11,113 @@
 
 var modules = bender.amd.require(
 	'document/node',
+	'document/nodelist',
 	'document/element',
 	'document/attribute' );
 
-describe( 'constructor', function() {
-	it( 'should create element without attributes', function() {
-		var Element = modules[ 'document/element' ];
-		var Node = modules[ 'document/node' ];
+describe( 'Element', function() {
+	var Element, Node, NodeList, Attribute;
 
-		var element = new Element( 'elem' );
-		var parent = new Element( 'parent', [], [ element ] );
-
-		expect( element ).to.be.an.instanceof( Node );
-		expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
-		expect( element ).to.have.property( 'parent' ).that.equals( parent );
-		expect( element._getAttrCount() ).to.equals( 0 );
+	before( function() {
+		Element = modules[ 'document/element' ];
+		Node = modules[ 'document/node' ];
+		NodeList = modules[ 'document/nodelist' ];
+		Attribute = modules[ 'document/attribute' ];
 	} );
 
-	it( 'should create element with attributes', function() {
-		var Element = modules[ 'document/element' ];
-		var Node = modules[ 'document/node' ];
-		var Attribute = modules[ 'document/attribute' ];
-
-		var attr = new Attribute( 'foo', 'bar' );
-
-		var element = new Element( 'elem', [ attr ] );
-
-		var parent = new Element( 'parent', [], [ element ] );
-
-		expect( element ).to.be.an.instanceof( Node );
-		expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
-		expect( element ).to.have.property( 'parent' ).that.equals( parent );
-		expect( element._getAttrCount() ).to.equals( 1 );
-		expect( element.getAttr( attr.key ) ).to.equals( attr.value );
+	describe( 'constructor', function() {
+		it( 'should create element without attributes', function() {
+			var element = new Element( 'elem' );
+			var parent = new Element( 'parent', [], [ element ] );
+
+			expect( element ).to.be.an.instanceof( Node );
+			expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
+			expect( element ).to.have.property( 'parent' ).that.equals( parent );
+			expect( element._getAttrCount() ).to.equals( 0 );
+		} );
+
+		it( 'should create element with attributes', function() {
+			var attr = new Attribute( 'foo', 'bar' );
+
+			var element = new Element( 'elem', [ attr ] );
+			var parent = new Element( 'parent', [], [ element ] );
+
+			expect( element ).to.be.an.instanceof( Node );
+			expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
+			expect( element ).to.have.property( 'parent' ).that.equals( parent );
+			expect( element._getAttrCount() ).to.equals( 1 );
+			expect( element.getAttr( attr.key ) ).to.equals( attr.value );
+		} );
+
+		it( 'should create element with children', function() {
+			var element = new Element( 'elem', [], 'foo' );
+
+			expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
+			expect( element.getChildCount() ).to.equals( 3 );
+			expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
+			expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
+			expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
+		} );
 	} );
 
-	it( 'should create element with children', function() {
-		var Element = modules[ 'document/element' ];
-
-		var element = new Element( 'elem', [], 'foo' );
-
-		expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
-		expect( element.getChildCount() ).to.equals( 3 );
-		expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
-		expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
-		expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
+	describe( 'insertChildren', function() {
+		it( 'should add children to the element', function() {
+			var element = new Element( 'elem', [], [ 'xy' ] );
+			element.insertChildren( 1, 'foo' );
+
+			expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
+			expect( element.getChildCount() ).to.equals( 5 );
+			expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
+			expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'f' );
+			expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
+			expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
+			expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
+		} );
 	} );
-} );
-
-describe( 'insertChildren', function() {
-	it( 'should add children to the element', function() {
-		var Element = modules[ 'document/element' ];
-
-		var element = new Element( 'elem', [], [ 'xy' ] );
-		element.insertChildren( 1, 'foo' );
 
-		expect( element ).to.have.property( 'name' ).that.equals( 'elem' );
-		expect( element.getChildCount() ).to.equals( 5 );
-		expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'x' );
-		expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'f' );
-		expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'o' );
-		expect( element.getChild( 3 ) ).to.have.property( 'character' ).that.equals( 'o' );
-		expect( element.getChild( 4 ) ).to.have.property( 'character' ).that.equals( 'y' );
+	describe( 'removeChildren', function() {
+		it( 'should remove children from the element and return them as a NodeList', function() {
+			var element = new Element( 'elem', [], [ 'foobar' ] );
+			var o = element.getChild( 2 );
+			var b = element.getChild( 3 );
+			var a = element.getChild( 4 );
+			var removed = element.removeChildren( 2, 3 );
+
+			expect( element.getChildCount() ).to.equals( 3 );
+			expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
+			expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
+			expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
+
+			expect( o ).to.have.property( 'parent' ).that.is.null;
+			expect( b ).to.have.property( 'parent' ).that.is.null;
+			expect( a ).to.have.property( 'parent' ).that.is.null;
+
+			expect( removed ).to.be.instanceof( NodeList );
+			expect( removed.length ).to.equal( 3 );
+			expect( removed.get( 0 ) ).to.equal( o );
+			expect( removed.get( 1 ) ).to.equal( b );
+			expect( removed.get( 2 ) ).to.equal( a );
+		} );
 	} );
-} );
-
-describe( 'removeChildren', function() {
-	it( 'should add children to the element', function() {
-		var Element = modules[ 'document/element' ];
-
-		var element = new Element( 'elem', [], [ 'foobar' ] );
-		var o = element.getChild( 2 );
-		var b = element.getChild( 3 );
-		var a = element.getChild( 4 );
-		element.removeChildren( 2, 3 );
 
-		expect( element.getChildCount() ).to.equals( 3 );
-		expect( element.getChild( 0 ) ).to.have.property( 'character' ).that.equals( 'f' );
-		expect( element.getChild( 1 ) ).to.have.property( 'character' ).that.equals( 'o' );
-		expect( element.getChild( 2 ) ).to.have.property( 'character' ).that.equals( 'r' );
-
-		expect( o ).to.have.property( 'parent' ).that.is.null;
-		expect( b ).to.have.property( 'parent' ).that.is.null;
-		expect( a ).to.have.property( 'parent' ).that.is.null;
+	describe( 'getChildIndex', function() {
+		it( 'should return child index', function() {
+			var element = new Element( 'elem', [], [ 'bar' ] );
+			var b = element.getChild( 0 );
+			var a = element.getChild( 1 );
+			var r = element.getChild( 2 );
+
+			expect( element.getChildIndex( b ) ).to.equals( 0 );
+			expect( element.getChildIndex( a ) ).to.equals( 1 );
+			expect( element.getChildIndex( r ) ).to.equals( 2 );
+		} );
 	} );
-} );
 
-describe( 'getChildIndex', function() {
-	it( 'should return child index', function() {
-		var Element = modules[ 'document/element' ];
+	describe( 'getChildCount', function() {
+		it( 'should return number of children', function() {
+			var element = new Element( 'elem', [], [ 'bar' ] );
 
-		var element = new Element( 'elem', [], [ 'bar' ] );
-		var b = element.getChild( 0 );
-		var a = element.getChild( 1 );
-		var r = element.getChild( 2 );
-
-		expect( element.getChildIndex( b ) ).to.equals( 0 );
-		expect( element.getChildIndex( a ) ).to.equals( 1 );
-		expect( element.getChildIndex( r ) ).to.equals( 2 );
+			expect( element.getChildCount() ).to.equals( 3 );
+		} );
 	} );
 } );
-
-describe( 'getChildCount', function() {
-	it( 'should return number of children', function() {
-		var Element = modules[ 'document/element' ];
-
-		var element = new Element( 'elem', [], [ 'bar' ] );
-
-		expect( element.getChildCount() ).to.equals( 3 );
-	} );
-} );