mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 18:39:52 +00:00
b1d9c83b5d
* For the most common case: - replace ve.extendClass with ve.inheritClass (chose slightly different names to detect usage of the old/new one, and I like 'inherit' better). - move it up to below the constructor, see doc block for why. * Cases where more than 2 arguments were passed to ve.extendClass are handled differently depending on the case. In case of a longer inheritance tree, the other arguments could be omitted (like in "ve.ce.FooBar, ve.FooBar, ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar, because ve.ce.FooBar inherits from ve.Bar). In the case of where it previously had two mixins with ve.extendClass(), either one becomes inheritClass and one a mixin, both to mixinClass(). No visible changes should come from this commit as the instances still all have the same visible properties in the end. No more or less than before. * Misc.: - Be consistent in calling parent constructors in the same order as the inheritance. - Add missing @extends and @param documentation. - Replace invalid {Integer} type hint with {Number}. - Consistent doc comments order: @class, @abstract, @constructor, @extends, @params. - Fix indentation errors A fairly common mistake was a superfluous space before the identifier on the assignment line directly below the documentation comment. $ ack "^ [^*]" --js modules/ve - Typo "Inhertiance" -> "Inheritance". - Replacing the other confusing comment "Inheritance" (inside the constructor) with "Parent constructor". - Add missing @abstract for ve.ui.Tool. - Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js - Add function names to all @constructor functions. Now that we have inheritance it is important and useful to have these functions not be anonymous. Example of debug shot: http://cl.ly/image/1j3c160w3D45 Makes the difference between < documentNode; > ve_dm_DocumentNode ... : ve_dm_BranchNode ... : ve_dm_Node ... : ve_dm_Node ... : Object ... without names (current situation): < documentNode; > Object ... : Object ... : Object ... : Object ... : Object ... though before this commit, it really looks like this (flattened since ve.extendClass really did a mixin): < documentNode; > Object ... ... ... Pattern in Sublime (case-sensitive) to find nameless constructor functions: "^ve\..*\.([A-Z])([^\.]+) = function \(" Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
142 lines
4.5 KiB
JavaScript
142 lines
4.5 KiB
JavaScript
/**
|
|
* VisualEditor data model BranchNode tests.
|
|
*
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
QUnit.module( 've.dm.BranchNode' );
|
|
|
|
/* Stubs */
|
|
|
|
ve.dm.BranchNodeStub = function ve_dm_BranchNodeStub( children, attributes ) {
|
|
// Parent constructor
|
|
ve.dm.BranchNode.call( this, 'branch-stub', children, attributes );
|
|
};
|
|
|
|
ve.inheritClass( ve.dm.BranchNodeStub, ve.dm.BranchNode );
|
|
|
|
ve.dm.BranchNodeStub.rules = {
|
|
'isWrapped': true,
|
|
'isContent': false,
|
|
'canContainContent': false,
|
|
'childNodeTypes': null
|
|
};
|
|
|
|
ve.dm.BranchNodeStub.converters = null;
|
|
|
|
ve.dm.nodeFactory.register( 'branch-stub', ve.dm.BranchNodeStub );
|
|
|
|
/* Tests */
|
|
|
|
QUnit.test( 'canHaveChildren', 1, function ( assert ) {
|
|
var node = new ve.dm.BranchNodeStub();
|
|
assert.equal( node.canHaveChildren(), true );
|
|
} );
|
|
|
|
QUnit.test( 'canHaveGrandchildren', 1, function ( assert ) {
|
|
var node = new ve.dm.BranchNodeStub();
|
|
assert.equal( node.canHaveGrandchildren(), true );
|
|
} );
|
|
|
|
QUnit.test( 'setRoot', 3, function ( assert ) {
|
|
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 );
|
|
assert.strictEqual( node3.getRoot(), node4 );
|
|
assert.strictEqual( node2.getRoot(), node4 );
|
|
assert.strictEqual( node1.getRoot(), node4 );
|
|
} );
|
|
|
|
QUnit.test( 'setDocument', 3, function ( assert ) {
|
|
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 );
|
|
assert.strictEqual( node3.getDocument(), node4 );
|
|
assert.strictEqual( node2.getDocument(), node4 );
|
|
assert.strictEqual( node1.getDocument(), node4 );
|
|
} );
|
|
|
|
QUnit.test( 'push', 3, function ( assert ) {
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
node3 = new ve.dm.BranchNodeStub( [node1] );
|
|
node3.on( 'splice', function () {
|
|
// Will be called 1 time
|
|
assert.ok( true, 'splice was emitted' );
|
|
} );
|
|
assert.strictEqual( node3.push( node2 ), 2 );
|
|
assert.deepEqual( node3.getChildren(), [node1, node2] );
|
|
} );
|
|
|
|
QUnit.test( 'pop', 3, function ( assert ) {
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
node3 = new ve.dm.BranchNodeStub( [node1, node2] );
|
|
node3.on( 'splice', function () {
|
|
// Will be called 1 time
|
|
assert.ok( true, 'splice was emitted' );
|
|
} );
|
|
assert.strictEqual( node3.pop(), node2 );
|
|
assert.deepEqual( node3.getChildren(), [node1] );
|
|
} );
|
|
|
|
QUnit.test( 'unshift', 3, function ( assert ) {
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
node3 = new ve.dm.BranchNodeStub( [node1] );
|
|
node3.on( 'splice', function () {
|
|
// Will be called 1 time
|
|
assert.ok( true, 'splice was emitted' );
|
|
} );
|
|
assert.strictEqual( node3.unshift( node2 ), 2 );
|
|
assert.deepEqual( node3.getChildren(), [node2, node1] );
|
|
} );
|
|
|
|
QUnit.test( 'shift', 3, function ( assert ) {
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
node3 = new ve.dm.BranchNodeStub( [node1, node2] );
|
|
node3.on( 'splice', function () {
|
|
// Will be called 1 time
|
|
assert.ok( true, 'splice was emitted' );
|
|
} );
|
|
assert.strictEqual( node3.shift(), node1 );
|
|
assert.deepEqual( node3.getChildren(), [node2] );
|
|
} );
|
|
|
|
QUnit.test( 'splice', 9, function ( assert ) {
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
node3 = new ve.dm.BranchNodeStub(),
|
|
node4 = new ve.dm.BranchNodeStub( [node1, node2] );
|
|
node4.on( 'splice', function () {
|
|
// Will be called 3 times
|
|
assert.ok( true, 'splice was emitted' );
|
|
} );
|
|
// Insert branch
|
|
assert.deepEqual( node4.splice( 1, 0, node3 ), [] );
|
|
assert.deepEqual( node4.getChildren(), [node1, node3, node2] );
|
|
// Remove branch
|
|
assert.deepEqual( node4.splice( 1, 1 ), [node3] );
|
|
assert.deepEqual( node4.getChildren(), [node1, node2] );
|
|
// Remove branch and insert branch
|
|
assert.deepEqual( node4.splice( 1, 1, node3 ), [node2] );
|
|
assert.deepEqual( node4.getChildren(), [node1, node3] );
|
|
} );
|
|
|
|
QUnit.test( 'getOffsetFromNode', 4, function ( assert ) {
|
|
var node1 = new ve.dm.BranchNodeStub(),
|
|
node2 = new ve.dm.BranchNodeStub(),
|
|
node3 = new ve.dm.BranchNodeStub( [node1, node2] ),
|
|
node4 = new ve.dm.BranchNodeStub( [node3] );
|
|
assert.strictEqual( node4.getOffsetFromNode( node1 ), 1 );
|
|
assert.strictEqual( node4.getOffsetFromNode( node2 ), 3 );
|
|
assert.strictEqual( node4.getOffsetFromNode( node3 ), 0 );
|
|
assert.strictEqual( node4.getOffsetFromNode( node4 ), 0 );
|
|
} );
|