2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor data model BranchNode tests.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-04-19 21:17:59 +00:00
|
|
|
module( 've.dm.BranchNode' );
|
2012-04-19 23:03:59 +00:00
|
|
|
|
|
|
|
/* Stubs */
|
|
|
|
|
2012-05-04 20:31:14 +00:00
|
|
|
ve.dm.BranchNodeStub = function( children, attributes ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
// Inheritance
|
2012-05-04 20:31:14 +00:00
|
|
|
ve.dm.BranchNode.call( this, 'branch-stub', children, attributes );
|
2012-04-19 23:03:59 +00:00
|
|
|
};
|
|
|
|
|
2012-05-03 18:52:17 +00:00
|
|
|
ve.dm.BranchNodeStub.rules = {
|
2012-05-22 00:39:03 +00:00
|
|
|
'isWrapped': true,
|
|
|
|
'isContent': false,
|
|
|
|
'canContainContent': false,
|
|
|
|
'childNodeTypes': null
|
2012-05-03 18:52:17 +00:00
|
|
|
};
|
|
|
|
|
2012-05-31 23:50:16 +00:00
|
|
|
ve.dm.BranchNodeStub.converters = null;
|
|
|
|
|
2012-04-19 23:03:59 +00:00
|
|
|
ve.extendClass( ve.dm.BranchNodeStub, ve.dm.BranchNode );
|
|
|
|
|
2012-05-31 22:20:58 +00:00
|
|
|
ve.dm.nodeFactory.register( 'branch-stub', ve.dm.BranchNodeStub );
|
2012-05-03 18:52:17 +00:00
|
|
|
|
2012-04-19 23:03:59 +00:00
|
|
|
/* Tests */
|
|
|
|
|
2012-05-05 00:50:54 +00:00
|
|
|
test( 'canHaveChildren', 1, function() {
|
|
|
|
var node = new ve.dm.BranchNodeStub();
|
|
|
|
equal( node.canHaveChildren(), true );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'canHaveGrandchildren', 1, function() {
|
|
|
|
var node = new ve.dm.BranchNodeStub();
|
|
|
|
equal( node.canHaveGrandchildren(), true );
|
|
|
|
} );
|
|
|
|
|
2012-04-27 18:03:08 +00:00
|
|
|
test( 'setRoot', 3, function() {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
|
|
node2 = new ve.dm.BranchNodeStub( [node1] ),
|
|
|
|
node3 = new ve.dm.BranchNodeStub( [node2] ),
|
|
|
|
node4 = new ve.dm.BranchNodeStub();
|
|
|
|
node3.setRoot( node4 );
|
|
|
|
strictEqual( node3.getRoot(), node4 );
|
|
|
|
strictEqual( node2.getRoot(), node4 );
|
|
|
|
strictEqual( node1.getRoot(), node4 );
|
|
|
|
} );
|
|
|
|
|
2012-05-03 20:08:37 +00:00
|
|
|
test( 'setDocument', 3, function() {
|
|
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
|
|
node2 = new ve.dm.BranchNodeStub( [node1] ),
|
|
|
|
node3 = new ve.dm.BranchNodeStub( [node2] ),
|
|
|
|
node4 = new ve.dm.BranchNodeStub();
|
|
|
|
node3.setDocument( node4 );
|
|
|
|
strictEqual( node3.getDocument(), node4 );
|
|
|
|
strictEqual( node2.getDocument(), node4 );
|
|
|
|
strictEqual( node1.getDocument(), node4 );
|
|
|
|
} );
|
|
|
|
|
2012-04-30 23:58:41 +00:00
|
|
|
test( 'push', 3, function() {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
|
|
node3 = new ve.dm.BranchNodeStub( [node1] );
|
2012-04-30 23:58:41 +00:00
|
|
|
node3.on( 'splice', function() {
|
2012-04-20 00:45:58 +00:00
|
|
|
// Will be called 1 time
|
2012-04-30 23:58:41 +00:00
|
|
|
ok( true, 'splice was emitted' );
|
2012-04-20 00:45:58 +00:00
|
|
|
} );
|
2012-04-19 23:03:59 +00:00
|
|
|
strictEqual( node3.push( node2 ), 2 );
|
|
|
|
deepEqual( node3.getChildren(), [node1, node2] );
|
|
|
|
} );
|
|
|
|
|
2012-04-30 23:58:41 +00:00
|
|
|
test( 'pop', 3, function() {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
|
|
node3 = new ve.dm.BranchNodeStub( [node1, node2] );
|
2012-04-30 23:58:41 +00:00
|
|
|
node3.on( 'splice', function() {
|
2012-04-20 00:45:58 +00:00
|
|
|
// Will be called 1 time
|
2012-04-30 23:58:41 +00:00
|
|
|
ok( true, 'splice was emitted' );
|
2012-04-20 00:45:58 +00:00
|
|
|
} );
|
2012-04-19 23:03:59 +00:00
|
|
|
strictEqual( node3.pop(), node2 );
|
|
|
|
deepEqual( node3.getChildren(), [node1] );
|
|
|
|
} );
|
|
|
|
|
2012-04-30 23:58:41 +00:00
|
|
|
test( 'unshift', 3, function() {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
|
|
node3 = new ve.dm.BranchNodeStub( [node1] );
|
2012-04-30 23:58:41 +00:00
|
|
|
node3.on( 'splice', function() {
|
2012-04-20 00:45:58 +00:00
|
|
|
// Will be called 1 time
|
2012-04-30 23:58:41 +00:00
|
|
|
ok( true, 'splice was emitted' );
|
2012-04-20 00:45:58 +00:00
|
|
|
} );
|
2012-04-19 23:03:59 +00:00
|
|
|
strictEqual( node3.unshift( node2 ), 2 );
|
|
|
|
deepEqual( node3.getChildren(), [node2, node1] );
|
|
|
|
} );
|
|
|
|
|
2012-04-30 23:58:41 +00:00
|
|
|
test( 'shift', 3, function() {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
|
|
node3 = new ve.dm.BranchNodeStub( [node1, node2] );
|
2012-04-30 23:58:41 +00:00
|
|
|
node3.on( 'splice', function() {
|
2012-04-20 00:45:58 +00:00
|
|
|
// Will be called 1 time
|
2012-04-30 23:58:41 +00:00
|
|
|
ok( true, 'splice was emitted' );
|
2012-04-20 00:45:58 +00:00
|
|
|
} );
|
2012-04-19 23:03:59 +00:00
|
|
|
strictEqual( node3.shift(), node1 );
|
|
|
|
deepEqual( node3.getChildren(), [node2] );
|
|
|
|
} );
|
|
|
|
|
2012-04-30 23:58:41 +00:00
|
|
|
test( 'splice', 9, function() {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
|
|
node3 = new ve.dm.BranchNodeStub(),
|
|
|
|
node4 = new ve.dm.BranchNodeStub( [node1, node2] );
|
2012-04-30 23:58:41 +00:00
|
|
|
node4.on( 'splice', function() {
|
2012-04-20 00:45:58 +00:00
|
|
|
// Will be called 3 times
|
2012-04-30 23:58:41 +00:00
|
|
|
ok( true, 'splice was emitted' );
|
2012-04-20 00:45:58 +00:00
|
|
|
} );
|
|
|
|
// Insert branch
|
2012-04-19 23:03:59 +00:00
|
|
|
deepEqual( node4.splice( 1, 0, node3 ), [] );
|
|
|
|
deepEqual( node4.getChildren(), [node1, node3, node2] );
|
2012-04-20 00:45:58 +00:00
|
|
|
// Remove branch
|
2012-04-19 23:03:59 +00:00
|
|
|
deepEqual( node4.splice( 1, 1 ), [node3] );
|
|
|
|
deepEqual( node4.getChildren(), [node1, node2] );
|
2012-04-20 00:45:58 +00:00
|
|
|
// Remove branch and insert branch
|
2012-04-19 23:03:59 +00:00
|
|
|
deepEqual( node4.splice( 1, 1, node3 ), [node2] );
|
|
|
|
deepEqual( node4.getChildren(), [node1, node3] );
|
|
|
|
} );
|
2012-05-03 18:52:17 +00:00
|
|
|
|
|
|
|
test( 'getOffsetFromNode', 4, function() {
|
|
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
|
|
node3 = new ve.dm.BranchNodeStub( [node1, node2] ),
|
|
|
|
node4 = new ve.dm.BranchNodeStub( [node3] );
|
|
|
|
strictEqual( node4.getOffsetFromNode( node1 ), 1 );
|
|
|
|
strictEqual( node4.getOffsetFromNode( node2 ), 3 );
|
|
|
|
strictEqual( node4.getOffsetFromNode( node3 ), 0 );
|
|
|
|
strictEqual( node4.getOffsetFromNode( node4 ), 0 );
|
|
|
|
} );
|