浏览代码

Fixed and added tests for RemoveOperation.
Added tests for ReinsertOperation.
Added tests for RootElement.

Szymon Cofalik 10 年之前
父节点
当前提交
a1405fa52d

+ 81 - 0
packages/ckeditor5-utils/tests/document/reinsertoperation.js

@@ -0,0 +1,81 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: document */
+
+'use strict';
+
+var modules = bender.amd.require(
+	'document/document',
+	'document/reinsertoperation',
+	'document/removeoperation',
+	'document/moveoperation',
+	'document/position'
+);
+
+describe( 'ReinsertOperation', function() {
+	var Document, ReinsertOperation, RemoveOperation, MoveOperation, Position;
+
+	before( function() {
+		Document = modules[ 'document/document' ];
+		ReinsertOperation = modules[ 'document/reinsertoperation' ];
+		RemoveOperation = modules[ 'document/removeoperation' ];
+		MoveOperation = modules[ 'document/moveoperation' ];
+		Position = modules[ 'document/position' ];
+	} );
+
+	var doc, root, graveyard, operation, graveyardPosition, rootPosition;
+
+	beforeEach( function() {
+		doc = new Document();
+		root = doc.createRoot( 'root' );
+		graveyard = doc._graveyard;
+
+		graveyardPosition = new Position( [ 0 ], graveyard );
+		rootPosition = new Position( [ 0 ], root );
+
+		operation = new ReinsertOperation(
+			graveyardPosition,
+			rootPosition,
+			2,
+			doc.version
+		);
+	} );
+
+	it( 'should extend MoveOperation class', function() {
+		expect( operation ).to.be.instanceof( MoveOperation );
+	} );
+
+	it( 'should create a remove operation as a reverse', function() {
+		var reverse = operation.reverseOperation();
+
+		expect( reverse ).to.be.an.instanceof( RemoveOperation );
+		expect( reverse.baseVersion ).to.equal( 1 );
+		expect( reverse.howMany ).to.equal( 2 );
+		expect( reverse.sourcePosition.isEqual( rootPosition ) ).to.be.true;
+		expect( reverse.targetPosition.isEqual( graveyardPosition ) ).to.be.true;
+	} );
+
+	it( 'should undo reinsert set of nodes by applying reverse operation', function() {
+		var reverse = operation.reverseOperation();
+
+		graveyard.insertChildren( 0, 'bar' );
+
+		doc.applyOperation( operation );
+
+		expect( doc.version ).to.equal( 1 );
+		expect( root.getChildCount() ).to.equal( 2 );
+
+		doc.applyOperation( reverse );
+
+		expect( doc.version ).to.equal( 2 );
+		expect( root.getChildCount() ).to.equal( 0 );
+		expect( graveyard.getChildCount() ).to.equal( 3 );
+
+		expect( graveyard.getChild( 0 ).character ).to.equal( 'b' );
+		expect( graveyard.getChild( 1 ).character ).to.equal( 'a' );
+		expect( graveyard.getChild( 2 ).character ).to.equal( 'r' );
+	} );
+} );

+ 60 - 107
packages/ckeditor5-utils/tests/document/removeoperation.js

@@ -9,142 +9,95 @@
 
 var modules = bender.amd.require(
 	'document/document',
-	'document/insertoperation',
+	'document/reinsertoperation',
 	'document/removeoperation',
-	'document/position',
-	'document/character',
-	'document/nodelist',
-	'ckeditorerror' );
+	'document/moveoperation',
+	'document/position'
+);
 
 describe( 'RemoveOperation', function() {
-	it( 'should remove node', function() {
-		var Document = modules[ 'document/document' ];
-		var RemoveOperation = modules[ 'document/removeoperation' ];
-		var Position = modules[ 'document/position' ];
-
-		var doc = new Document();
-
-		doc.root.insertChildren( 0, 'x' );
-
-		doc.applyOperation( new RemoveOperation(
-			new Position( [ 0 ], doc.root ),
-			doc.root.getChild( 0 ),
-			doc.version ) );
-
-		expect( doc.version ).to.equal( 1 );
-		expect( doc.root.getChildCount() ).to.equal( 0 );
+	var Document, ReinsertOperation, RemoveOperation, MoveOperation, Position;
+
+	before( function() {
+		Document = modules[ 'document/document' ];
+		ReinsertOperation = modules[ 'document/reinsertoperation' ];
+		RemoveOperation = modules[ 'document/removeoperation' ];
+		MoveOperation = modules[ 'document/removeoperation' ];
+		Position = modules[ 'document/position' ];
 	} );
 
-	it( 'should remove set of nodes', function() {
-		var Document = modules[ 'document/document' ];
-		var RemoveOperation = modules[ 'document/removeoperation' ];
-		var Position = modules[ 'document/position' ];
-
-		var doc = new Document();
+	var doc, root, graveyard;
 
-		doc.root.insertChildren( 0, 'bar' );
-
-		doc.applyOperation( new RemoveOperation(
-			new Position( [ 0 ], doc.root ),
-			[ doc.root.getChild( 0 ), doc.root.getChild( 1 ), doc.root.getChild( 2 ) ],
-			doc.version ) );
-
-		expect( doc.version ).to.equal( 1 );
-		expect( doc.root.getChildCount() ).to.equal( 0 );
+	beforeEach( function() {
+		doc = new Document();
+		root = doc.createRoot( 'root' );
+		graveyard = doc._graveyard;
 	} );
 
-	it( 'should remove from between existing nodes', function() {
-		var Document = modules[ 'document/document' ];
-		var RemoveOperation = modules[ 'document/removeoperation' ];
-		var Position = modules[ 'document/position' ];
-
-		var doc = new Document();
-
-		doc.root.insertChildren( 0, 'bar' );
+	it( 'should extend MoveOperation class', function() {
+		var operation = new RemoveOperation(
+			new Position( [ 2 ], root ),
+			2,
+			doc.version
+		);
 
-		doc.applyOperation( new RemoveOperation(
-			new Position( [ 1 ], doc.root ),
-			[ doc.root.getChild( 1 ) ],
-			doc.version ) );
-
-		expect( doc.version ).to.equal( 1 );
-		expect( doc.root.getChildCount() ).to.equal( 2 );
-		expect( doc.root.getChild( 0 ).character ).to.equal( 'b' );
-		expect( doc.root.getChild( 1 ).character ).to.equal( 'r' );
+		expect( operation ).to.be.instanceof( MoveOperation );
 	} );
 
-	it( 'should create a insert operation as a reverse', function() {
-		var Document = modules[ 'document/document' ];
-		var InsertOperation = modules[ 'document/insertoperation' ];
-		var RemoveOperation = modules[ 'document/removeoperation' ];
-		var Position = modules[ 'document/position' ];
-		var NodeList = modules[ 'document/nodelist' ];
+	it( 'should remove set of nodes and append them to graveyard root', function() {
+		root.insertChildren( 0, 'fozbar' );
 
-		var doc = new Document();
+		var z = root.getChild( 2 );
+		var b = root.getChild( 3 );
+		var a = root.getChild( 4 );
 
-		var nodeList = new NodeList( 'bar' );
-		var position = new Position( [ 0 ], doc.root );
+		doc.applyOperation(
+			new RemoveOperation(
+				new Position( [ 2 ], root ),
+				2,
+				doc.version
+			)
+		);
 
-		doc.root.insertChildren( 0, nodeList );
+		expect( doc.version ).to.equal( 1 );
+		expect( root.getChildCount() ).to.equal( 4 );
+		expect( root.getChild( 2 ) ).to.equal( a );
 
-		var operation = new RemoveOperation( position, nodeList, 0 );
+		expect( graveyard.getChildCount() ).to.equal( 2 );
+		expect( graveyard.getChild( 0 ) ).to.equal( z );
+		expect( graveyard.getChild( 1 ) ).to.equal( b );
+	} );
 
+	it( 'should create a reinsert operation as a reverse', function() {
+		var position = new Position( [ 0 ], root );
+		var operation = new RemoveOperation( position, 2, 0 );
 		var reverse = operation.reverseOperation();
 
-		expect( reverse ).to.be.an.instanceof( InsertOperation );
+		expect( reverse ).to.be.an.instanceof( ReinsertOperation );
 		expect( reverse.baseVersion ).to.equal( 1 );
-		expect( reverse.nodeList ).to.equal( nodeList );
-		expect( reverse.position ).to.equal( position );
+		expect( reverse.howMany ).to.equal( 2 );
+		expect( reverse.sourcePosition ).to.equal( operation.targetPosition );
+		expect( reverse.targetPosition ).to.equal( position );
 	} );
 
 	it( 'should undo remove set of nodes by applying reverse operation', function() {
-		var Document = modules[ 'document/document' ];
-		var RemoveOperation = modules[ 'document/removeoperation' ];
-		var Position = modules[ 'document/position' ];
-		var NodeList = modules[ 'document/nodelist' ];
-
-		var doc = new Document();
-
-		var nodeList = new NodeList( 'bar' );
-		var position = new Position( [ 0 ], doc.root );
-
-		doc.root.insertChildren( 0, nodeList );
-
-		var operation = new RemoveOperation( position, nodeList, 0 );
-
+		var position = new Position( [ 0 ], root );
+		var operation = new RemoveOperation( position, 3, 0 );
 		var reverse = operation.reverseOperation();
 
+		root.insertChildren( 0, 'bar' );
+
 		doc.applyOperation( operation );
 
 		expect( doc.version ).to.equal( 1 );
-		expect( doc.root.getChildCount() ).to.equal( 0 );
+		expect( root.getChildCount() ).to.equal( 0 );
 
 		doc.applyOperation( reverse );
 
 		expect( doc.version ).to.equal( 2 );
-		expect( doc.root.getChildCount() ).to.equal( 3 );
-		expect( doc.root.getChild( 0 ).character ).to.equal( 'b' );
-		expect( doc.root.getChild( 1 ).character ).to.equal( 'a' );
-		expect( doc.root.getChild( 2 ).character ).to.equal( 'r' );
-	}  );
-
-	if ( CKEDITOR.isDebug ) {
-		it( 'should throw error if nodes to remove are different then actual nodes', function() {
-			var Document = modules[ 'document/document' ];
-			var RemoveOperation = modules[ 'document/removeoperation' ];
-			var Position = modules[ 'document/position' ];
-			var CKEditorError = modules.ckeditorerror;
-
-			var doc = new Document();
-
-			doc.root.insertChildren( 0, 'foo' );
-
-			expect( function() {
-				doc.applyOperation( new RemoveOperation(
-					new Position( [ 0 ], doc.root ),
-					'bar',
-					doc.version ) );
-			} ).to.throw( CKEditorError, /operation-remove-node-does-not-exists/ );
-		} );
-	}
+		expect( root.getChildCount() ).to.equal( 3 );
+		expect( root.getChild( 0 ).character ).to.equal( 'b' );
+		expect( root.getChild( 1 ).character ).to.equal( 'a' );
+		expect( root.getChild( 2 ).character ).to.equal( 'r' );
+	} );
 } );

+ 38 - 0
packages/ckeditor5-utils/tests/document/rootelement.js

@@ -0,0 +1,38 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* jshint expr: true */
+
+/* bender-tags: document */
+
+'use strict';
+
+var modules = bender.amd.require(
+	'document/document',
+	'document/element',
+	'document/rootelement'
+);
+
+describe( 'Element', function() {
+	var Document, Element, RootElement;
+
+	before( function() {
+		Document = modules[ 'document/document' ];
+		Element = modules[ 'document/element' ];
+		RootElement = modules[ 'document/rootelement' ];
+	} );
+
+	describe( 'constructor', function() {
+		it( 'should create root element without attributes', function() {
+			var doc = new Document();
+			var root = new RootElement( doc );
+
+			expect( root ).to.be.an.instanceof( Element );
+			expect( root ).to.have.property( 'document' ).that.equals( doc );
+			expect( root._getAttrCount() ).to.equal( 0 );
+			expect( root.getChildCount() ).to.equal( 0 );
+		} );
+	} );
+} );