mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-09-27 20:26:46 +00:00
Refactored ve.dm.TransactionProcessor tests to use data-providers
Change-Id: I0535f3ac56b6037712d731c71fc68c56ae7ea614
This commit is contained in:
parent
17b58db5c6
commit
aa9148c9ee
|
@ -2,120 +2,108 @@ module( 've.dm.TransactionProcessor' );
|
|||
|
||||
/* Tests */
|
||||
|
||||
test( 'retain', 2, function() {
|
||||
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
|
||||
|
||||
// Transaction for retaining part of the document (lettting the processor retain the rest)
|
||||
var tx = new ve.dm.Transaction();
|
||||
tx.pushRetain( 38 );
|
||||
|
||||
// Commit and test
|
||||
ve.dm.TransactionProcessor.commit( doc, tx );
|
||||
deepEqual( doc.getData(), ve.dm.example.data,
|
||||
'commits retain transaction without changing data' );
|
||||
|
||||
// Roll back and test
|
||||
ve.dm.TransactionProcessor.rollback( doc, tx );
|
||||
deepEqual( doc.getData(), ve.dm.example.data,
|
||||
'rolls back retain transaction without changing data' );
|
||||
} );
|
||||
|
||||
test( 'annotate', function() {
|
||||
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
|
||||
|
||||
// Transaction for setting and clearing various annotations
|
||||
var tx = new ve.dm.Transaction();
|
||||
tx.pushStartAnnotating( 'set', { 'type': 'textStyle/bold' } );
|
||||
tx.pushRetain( 2 );
|
||||
tx.pushStartAnnotating( 'clear', { 'type': 'textStyle/italic' } );
|
||||
tx.pushRetain( 2 );
|
||||
tx.pushStopAnnotating( 'clear', { 'type': 'textStyle/italic' } );
|
||||
tx.pushRetain( 8 );
|
||||
tx.pushStopAnnotating( 'set', { 'type': 'textStyle/bold' } );
|
||||
tx.pushRetain( 4 );
|
||||
tx.pushStartAnnotating( 'set', { 'type': 'textStyle/underline' } );
|
||||
tx.pushRetain( 4 );
|
||||
tx.pushStartAnnotating( 'set', { 'type': 'textStyle/superscript' } );
|
||||
tx.pushRetain( 10 );
|
||||
|
||||
// Expected document data after transaction
|
||||
var data = ve.dm.example.data.slice( 0 );
|
||||
data[1] = [ 'a', { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } } ];
|
||||
data[2] = [ 'b', { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } } ];
|
||||
data[3] = [ 'c', { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } } ];
|
||||
data[9] = [ 'd', { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } } ];
|
||||
data[19] = [ 'f', { '{"type":"textStyle/underline"}': { 'type': 'textStyle/underline' } } ];
|
||||
data[28] = [ 'g', {
|
||||
'{"type":"textStyle/underline"}': { 'type': 'textStyle/underline' },
|
||||
'{"type":"textStyle/superscript"}': { 'type': 'textStyle/superscript' }
|
||||
} ];
|
||||
|
||||
// Commit and test
|
||||
ve.dm.TransactionProcessor.commit( doc, tx );
|
||||
deepEqual( doc.getData(), data, 'commits complex annotation transaction' );
|
||||
|
||||
// Roll back and test
|
||||
ve.dm.TransactionProcessor.rollback( doc, tx );
|
||||
deepEqual( doc.getData(), ve.dm.example.data, 'rolls back complex annotation' );
|
||||
} );
|
||||
|
||||
test( 'attribute', function() {
|
||||
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
|
||||
|
||||
// Expected document data after transaction
|
||||
var data = doc.getData();
|
||||
test( 'commit/rollback', function() {
|
||||
var cases = {
|
||||
'no operations': {
|
||||
'calls': [],
|
||||
'process': function( data ) {}
|
||||
},
|
||||
'retaining': {
|
||||
'calls': [['pushRetain', 38]],
|
||||
'process': function( data ) {}
|
||||
},
|
||||
'annotating content': {
|
||||
'calls': [
|
||||
['pushStartAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
||||
['pushRetain', 2],
|
||||
['pushStartAnnotating', 'clear', { 'type': 'textStyle/italic' }],
|
||||
['pushRetain', 2],
|
||||
['pushStopAnnotating', 'clear', { 'type': 'textStyle/italic' }],
|
||||
['pushRetain', 8],
|
||||
['pushStopAnnotating', 'set', { 'type': 'textStyle/bold' }],
|
||||
['pushRetain', 4],
|
||||
['pushStartAnnotating', 'set', { 'type': 'textStyle/underline' }],
|
||||
['pushRetain', 4],
|
||||
['pushStartAnnotating', 'set', { 'type': 'textStyle/italic' }],
|
||||
['pushRetain', 10]
|
||||
],
|
||||
'process': function( data ) {
|
||||
var b = { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } },
|
||||
u = { '{"type":"textStyle/underline"}': { 'type': 'textStyle/underline' } },
|
||||
i = { '{"type":"textStyle/italic"}': { 'type': 'textStyle/italic' } };
|
||||
data[1] = ['a', b];
|
||||
data[2] = ['b', b];
|
||||
data[3] = ['c', b];
|
||||
data[9] = ['d', b];
|
||||
data[19] = ['f', u];
|
||||
data[28] = ['g', ve.extendObject( {}, u, i ) ];
|
||||
}
|
||||
},
|
||||
'changing, removing and adding attributes': {
|
||||
'calls': [
|
||||
['pushReplaceElementAttribute', 'level', 1, 2],
|
||||
['pushRetain', 11],
|
||||
['pushReplaceElementAttribute', 'style', 'bullet', 'number'],
|
||||
['pushReplaceElementAttribute', 'test', undefined, 'abcd'],
|
||||
['pushRetain', 26],
|
||||
['pushReplaceElementAttribute', 'html/src', 'image.png', undefined]
|
||||
],
|
||||
'process': function( data ) {
|
||||
data[0].attributes.level = 2;
|
||||
data[11].attributes.style = 'number';
|
||||
data[11].attributes.test = 'abcd';
|
||||
delete data[37].attributes['html/src'];
|
||||
|
||||
// Transaction for adding, changing and removing attributes
|
||||
var tx = new ve.dm.Transaction();
|
||||
tx.pushReplaceElementAttribute( 'level', 1, 2 );
|
||||
tx.pushRetain( 11 );
|
||||
tx.pushReplaceElementAttribute( 'style', 'bullet', 'number' );
|
||||
tx.pushReplaceElementAttribute( 'test', undefined, 'abcd' );
|
||||
tx.pushRetain( 26 );
|
||||
tx.pushReplaceElementAttribute( 'html/src', 'image.png', undefined );
|
||||
|
||||
// Commit and test
|
||||
ve.dm.TransactionProcessor.commit( doc, tx );
|
||||
deepEqual( doc.getData(), data, 'commits attribute changes' );
|
||||
|
||||
// Roll back and test
|
||||
ve.dm.TransactionProcessor.rollback( doc, tx );
|
||||
deepEqual( doc.getData(), ve.dm.example.data, 'rolls back attribute changes' );
|
||||
|
||||
// Transaction for setting attributes on non-element data
|
||||
tx = new ve.dm.Transaction();
|
||||
tx.pushRetain( 1 );
|
||||
tx.pushReplaceElementAttribute( 'foo', 23, 42 );
|
||||
|
||||
// Test exception when committing
|
||||
raises(
|
||||
function() { ve.dm.TransactionProcessor.commit( doc, tx ); },
|
||||
/^Invalid element error. Can not set attributes on non-element data.$/,
|
||||
'throws exception when trying to replace attributes on content'
|
||||
);
|
||||
} );
|
||||
|
||||
test( 'replace', function() {
|
||||
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
|
||||
|
||||
// Transaction that replaces "a" with "FOO"
|
||||
var tx = new ve.dm.Transaction();
|
||||
tx.pushRetain( 1 );
|
||||
tx.pushReplace( [ 'a' ], [ 'F', 'O', 'O' ] );
|
||||
|
||||
// Expected document data after transaction
|
||||
var data = ve.dm.example.data.slice( 0 );
|
||||
}
|
||||
},
|
||||
'changing attributes on non-element data throws an exception': {
|
||||
'calls': [
|
||||
['pushRetain', 1],
|
||||
['pushReplaceElementAttribute', 'foo', 23, 42]
|
||||
],
|
||||
'exception': /^Invalid element error. Can not set attributes on non-element data.$/
|
||||
},
|
||||
'replacing content': {
|
||||
'calls': [
|
||||
['pushRetain', 1],
|
||||
['pushReplace', ['a'], ['F', 'O', 'O']]
|
||||
],
|
||||
'process': function( data ) {
|
||||
data.splice( 1, 1, 'F', 'O', 'O' );
|
||||
|
||||
// Commit and test
|
||||
}
|
||||
}
|
||||
};
|
||||
// Run tests
|
||||
for ( var msg in cases ) {
|
||||
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) ),
|
||||
tx = new ve.dm.Transaction();
|
||||
for ( var i = 0; i < cases[msg].calls.length; i++ ) {
|
||||
tx[cases[msg].calls[i][0]].apply( tx, cases[msg].calls[i].slice( 1 ) );
|
||||
}
|
||||
if ( 'process' in cases[msg] ) {
|
||||
ve.dm.TransactionProcessor.commit( doc, tx );
|
||||
deepEqual( doc.getData(), data, 'commits content replace transaction' );
|
||||
|
||||
// Roll back and test
|
||||
var processed = ve.dm.example.data.slice( 0 );
|
||||
cases[msg].process( processed );
|
||||
deepEqual( doc.getData(), processed, 'commit: ' + msg );
|
||||
ve.dm.TransactionProcessor.rollback( doc, tx );
|
||||
deepEqual( doc.getData(), ve.dm.example.data, 'rolls back content replace transaction' );
|
||||
deepEqual( doc.getData(), ve.dm.example.data.slice( 0 ), 'rollback: ' + msg );
|
||||
} else if ( 'exception' in cases[msg] ) {
|
||||
/*jshint loopfunc:true */
|
||||
raises(
|
||||
function() {
|
||||
ve.dm.TransactionProcessor.commit( doc, tx );
|
||||
},
|
||||
cases[msg].exception,
|
||||
'commit: ' + msg
|
||||
);
|
||||
raises(
|
||||
function() {
|
||||
ve.dm.TransactionProcessor.rollback( doc, tx );
|
||||
},
|
||||
cases[msg].exception,
|
||||
'rollback: ' + msg
|
||||
);
|
||||
}
|
||||
}
|
||||
// Calculate expected assertion count
|
||||
expect( ve.getObjectKeys( cases ).length * 2 );
|
||||
} );
|
||||
|
|
Loading…
Reference in a new issue