Refactored ve.dm.TransactionProcessor tests to use data-providers

Change-Id: I0535f3ac56b6037712d731c71fc68c56ae7ea614
This commit is contained in:
Trevor Parscal 2012-05-14 14:31:32 -07:00
parent 17b58db5c6
commit aa9148c9ee

View file

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