2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor data model Node tests.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @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.Node' );
|
2012-04-19 23:03:59 +00:00
|
|
|
|
|
|
|
/* Stubs */
|
|
|
|
|
|
|
|
ve.dm.NodeStub = function( length, attributes ) {
|
|
|
|
// Inheritance
|
|
|
|
ve.dm.Node.call( this, 'stub', length, attributes );
|
|
|
|
};
|
|
|
|
|
2012-05-05 00:50:54 +00:00
|
|
|
ve.dm.NodeStub.rules = {
|
2012-05-22 00:39:03 +00:00
|
|
|
'isWrapped': true,
|
|
|
|
'isContent': true,
|
|
|
|
'canContainContent': false,
|
|
|
|
'childNodeTypes': []
|
2012-05-05 00:50:54 +00:00
|
|
|
};
|
|
|
|
|
2012-05-31 23:50:16 +00:00
|
|
|
ve.dm.NodeStub.converters = null;
|
|
|
|
|
2012-04-19 23:03:59 +00:00
|
|
|
ve.extendClass( ve.dm.NodeStub, ve.dm.Node );
|
|
|
|
|
2012-05-31 22:20:58 +00:00
|
|
|
ve.dm.nodeFactory.register( 'stub', ve.dm.NodeStub );
|
2012-05-05 00:50:54 +00:00
|
|
|
|
2012-04-19 23:03:59 +00:00
|
|
|
/* Tests */
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'canHaveChildren', 1, function( assert ) {
|
2012-05-05 00:50:54 +00:00
|
|
|
var node = new ve.dm.NodeStub();
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.equal( node.canHaveChildren(), false );
|
2012-05-05 00:50:54 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'canHaveGrandchildren', 1, function( assert ) {
|
2012-05-05 00:50:54 +00:00
|
|
|
var node = new ve.dm.NodeStub();
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.equal( node.canHaveGrandchildren(), false );
|
2012-05-05 00:50:54 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'getLength', 2, function( assert ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.NodeStub(),
|
|
|
|
node2 = new ve.dm.NodeStub( 1234 );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( node1.getLength(), 0 );
|
|
|
|
assert.strictEqual( node2.getLength(), 1234 );
|
2012-04-19 23:03:59 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'getOuterLength', 2, function( assert ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.NodeStub(),
|
|
|
|
node2 = new ve.dm.NodeStub( 1234 );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( node1.getOuterLength(), 2 );
|
|
|
|
assert.strictEqual( node2.getOuterLength(), 1236 );
|
2012-04-19 23:03:59 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'setLength', 2, function( assert ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node = new ve.dm.NodeStub();
|
|
|
|
node.setLength( 1234 );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( node.getLength(), 1234 );
|
|
|
|
assert.throws(
|
2012-04-20 01:05:48 +00:00
|
|
|
function() {
|
|
|
|
// Length can not be negative
|
|
|
|
node.setLength( -1 );
|
|
|
|
},
|
|
|
|
/^Length cannot be negative$/,
|
|
|
|
'throws exception if length is negative'
|
|
|
|
);
|
2012-04-19 23:03:59 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'adjustLength', 1, function( assert ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node = new ve.dm.NodeStub( 1234 );
|
|
|
|
node.adjustLength( 5678 );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( node.getLength(), 6912 );
|
2012-04-19 23:03:59 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'getAttribute', 2, function( assert ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node = new ve.dm.NodeStub( 0, { 'a': 1, 'b': 2 } );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( node.getAttribute( 'a' ), 1 );
|
|
|
|
assert.strictEqual( node.getAttribute( 'b' ), 2 );
|
2012-04-19 23:03:59 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'setRoot', 1, function( assert ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.NodeStub(),
|
|
|
|
node2 = new ve.dm.NodeStub();
|
|
|
|
node1.setRoot( node2 );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( node1.getRoot(), node2 );
|
2012-04-19 23:03:59 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'attach', 2, function( assert ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.NodeStub(),
|
|
|
|
node2 = new ve.dm.NodeStub();
|
|
|
|
node1.attach( node2 );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( node1.getParent(), node2 );
|
|
|
|
assert.strictEqual( node1.getRoot(), node2 );
|
2012-04-19 23:03:59 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'detach', 2, function( assert ) {
|
2012-04-19 23:03:59 +00:00
|
|
|
var node1 = new ve.dm.NodeStub(),
|
|
|
|
node2 = new ve.dm.NodeStub();
|
|
|
|
node1.attach( node2 );
|
|
|
|
node1.detach();
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( node1.getParent(), null );
|
|
|
|
assert.strictEqual( node1.getRoot(), node1 );
|
2012-04-19 23:03:59 +00:00
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'canBeMergedWith', 4, function( assert ) {
|
2012-06-01 21:34:59 +00:00
|
|
|
var node1 = new ve.dm.LeafNodeStub(),
|
|
|
|
node2 = new ve.dm.BranchNodeStub( [node1] ),
|
|
|
|
node3 = new ve.dm.BranchNodeStub( [node2] ),
|
|
|
|
node4 = new ve.dm.LeafNodeStub(),
|
|
|
|
node5 = new ve.dm.BranchNodeStub( [node4] );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( node3.canBeMergedWith( node5 ), true, 'same level, same type' );
|
|
|
|
assert.strictEqual( node2.canBeMergedWith( node5 ), false, 'different level, same type' );
|
|
|
|
assert.strictEqual( node2.canBeMergedWith( node1 ), false, 'different level, different type' );
|
|
|
|
assert.strictEqual( node2.canBeMergedWith( node4 ), false, 'same level, different type' );
|
2012-06-01 21:34:59 +00:00
|
|
|
} );
|