mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-27 15:50:29 +00:00
Improvements to nodeTreeEqual():
* Don't crash if b.children has a different length than expected * Don't crash if b.children is absent but expected * Check if b.children is present when it's not expected * Add assertion when neither a.children not b.children are present; this makes the number of assertions for leaves equal to the number of assertions for branches * Check attributes too * Accept a desc parameter, and add node paths (like list/listItem/paragraph/text) to the descriptions * Use desc parameter for all calls (some tests were already using it, even though it didn't actually exist) Change-Id: Ic56d27d20377e7f4fdfa038fdf4ebe00dcb3e062
This commit is contained in:
parent
3b1d8098e9
commit
0f0c3c9939
|
@ -256,7 +256,7 @@ test( 'rebuildNodes', function() {
|
|||
documentNode = doc.getDocumentNode();
|
||||
// Rebuild without changes
|
||||
doc.rebuildNodes( documentNode, 1, 1, 5, 30 );
|
||||
ve.example.nodeTreeEqual( documentNode, ve.dm.example.tree );
|
||||
ve.example.nodeTreeEqual( documentNode, ve.dm.example.tree, 'rebuild without changes' );
|
||||
|
||||
// XXX: Create a new document node tree from the old one
|
||||
var tree = new ve.dm.DocumentNode( ve.dm.example.tree.getChildren() );
|
||||
|
@ -266,7 +266,7 @@ test( 'rebuildNodes', function() {
|
|||
tree.splice( 1, 1, new ve.dm.ParagraphNode( [new ve.dm.TextNode( 3 )] ) );
|
||||
// Rebuild with changes
|
||||
doc.rebuildNodes( documentNode, 1, 1, 5, 5 );
|
||||
ve.example.nodeTreeEqual( documentNode, tree );
|
||||
ve.example.nodeTreeEqual( documentNode, tree, 'replace table with paragraph' );
|
||||
} );
|
||||
|
||||
test( 'getRelativeContentOffset', 1, function() {
|
||||
|
|
|
@ -2,10 +2,12 @@ module( 've.dm.DocumentFragment' );
|
|||
|
||||
/* Tests */
|
||||
|
||||
test( 'constructor', 115, function() {
|
||||
test( 'constructor', 156, function() {
|
||||
var fragment = new ve.dm.DocumentFragment( ve.dm.example.data );
|
||||
// Test count: ( ( 4 tests x 21 branch nodes ) + ( 3 tests x 10 leaf nodes ) ) = 114
|
||||
ve.example.nodeTreeEqual( fragment.getDocumentNode(), ve.dm.example.tree );
|
||||
// Test count: 5 tests x 31 nodes = 155
|
||||
ve.example.nodeTreeEqual( fragment.getDocumentNode(), ve.dm.example.tree,
|
||||
'node tree matches example data'
|
||||
);
|
||||
|
||||
raises(
|
||||
function() {
|
||||
|
|
|
@ -150,6 +150,7 @@ test( 'commit/rollback', function() {
|
|||
}
|
||||
};
|
||||
// Run tests
|
||||
var originalDoc = new ve.dm.Document( ve.dm.example.data );
|
||||
for ( var msg in cases ) {
|
||||
var doc = new ve.dm.Document( ve.copyArray( ve.dm.example.data ) ),
|
||||
tx = new ve.dm.Transaction();
|
||||
|
@ -160,9 +161,18 @@ test( 'commit/rollback', function() {
|
|||
ve.dm.TransactionProcessor.commit( doc, tx );
|
||||
var expected = ve.copyArray( ve.dm.example.data );
|
||||
cases[msg].expected( expected );
|
||||
deepEqual( doc.getData(), expected, 'commit: ' + msg );
|
||||
deepEqual( doc.getData(), expected, 'commit (data): ' + msg );
|
||||
var expectedDoc = new ve.dm.Document( expected );
|
||||
ve.example.nodeTreeEqual( doc.getDocumentNode(),
|
||||
expectedDoc.getDocumentNode(),
|
||||
'commit (tree): ' + msg
|
||||
);
|
||||
ve.dm.TransactionProcessor.rollback( doc, tx );
|
||||
deepEqual( doc.getData(), ve.dm.example.data, 'rollback: ' + msg );
|
||||
deepEqual( doc.getData(), ve.dm.example.data, 'rollback (data): ' + msg );
|
||||
ve.example.nodeTreeEqual( doc.getDocumentNode(),
|
||||
originalDoc.getDocumentNode(),
|
||||
'rollback (tree): ' + msg
|
||||
);
|
||||
} else if ( 'exception' in cases[msg] ) {
|
||||
/*jshint loopfunc:true */
|
||||
raises(
|
||||
|
|
|
@ -189,19 +189,30 @@ ve.example.getSelectNodesCases = function( doc ) {
|
|||
/**
|
||||
* Asserts that two node trees are equivalent.
|
||||
*
|
||||
* This will perform 4 assertions on each branch node and 3 assertions on each leaf node.
|
||||
* This will perform 5 assertions on each node
|
||||
*
|
||||
* @method
|
||||
*/
|
||||
ve.example.nodeTreeEqual = function( a, b ) {
|
||||
equal( a.getType(), b.getType(), 'type match (' + a.getType() + ')' );
|
||||
equal( a.getLength(), b.getLength(), 'length match' );
|
||||
equal( a.getOuterLength(), b.getOuterLength(), 'outer length match' );
|
||||
if ( a.children ) {
|
||||
equal( a.children.length, b.children.length, 'children count match' );
|
||||
for ( var i = 0; i < a.children.length; i++ ) {
|
||||
ve.example.nodeTreeEqual( a.children[i], b.children[i] );
|
||||
ve.example.nodeTreeEqual = function( a, b, desc, typePath ) {
|
||||
typePath = typePath ? typePath + '/' + a.getType() : a.getType();
|
||||
var descPrefix = desc + ': (' + typePath + ') ';
|
||||
equal( a.getType(), b.getType(), descPrefix + 'type match' );
|
||||
equal( a.getLength(), b.getLength(), descPrefix + 'length match' );
|
||||
equal( a.getOuterLength(), b.getOuterLength(), descPrefix + 'outer length match' );
|
||||
deepEqual( a.attributes, b.attributes, descPrefix + 'attributes match' );
|
||||
if ( a.children && b.children ) {
|
||||
// Prevent crashes if a.children and b.children have different lengths
|
||||
var minLength = a.children.length < b.children.length ? a.children.length : b.children.length;
|
||||
equal( a.children.length, b.children.length, descPrefix + 'children count match' );
|
||||
for ( var i = 0; i < minLength; i++ ) {
|
||||
ve.example.nodeTreeEqual( a.children[i], b.children[i], desc, typePath );
|
||||
}
|
||||
} else if ( a.children ) {
|
||||
ok( false, descPrefix + 'children array expected but not present' );
|
||||
} else if ( b.children ) {
|
||||
ok( false, descPrefix + 'children array present but not expected' );
|
||||
} else {
|
||||
ok( true, descPrefix + 'node is childless' );
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue