r100992 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100991‎ | r100992 | r100993 >
Date:16:03, 27 October 2011
Author:catrope
Status:deferred
Tags:
Comment:
prepareInsertion: Add test cases for on-the-edge and past-the-edge offsets, and fix the code so they pass
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js (modified) (history)
  • /trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js
@@ -433,7 +433,7 @@
434434 );
435435 } );
436436
437 -test( 'es.DocumentModel.prepareInsertion', 6, function() {
 437+test( 'es.DocumentModel.prepareInsertion', 10, function() {
438438 var documentModel = es.DocumentModel.newFromPlainObject( obj );
439439
440440 // Test 1
@@ -532,7 +532,63 @@
533533 'prepareInsertion splits up paragraph when inserting a paragraph closing and opening into a paragraph'
534534 );
535535
536 - // TODO add test cases for expected exceptions for bad offset and malformed input
 536+ // Test 7
 537+ deepEqual(
 538+ documentModel.prepareInsertion(
 539+ 0,
 540+ [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ]
 541+ ),
 542+ [
 543+ {
 544+ 'type': 'insert',
 545+ 'data': [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ]
 546+ },
 547+ { 'type': 'retain', 'length': 28 }
 548+ ],
 549+ 'prepareInsertion inserts at the beginning, then retains up to the end'
 550+ );
 551+
 552+ // Test 8
 553+ deepEqual(
 554+ documentModel.prepareInsertion(
 555+ 28,
 556+ [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ]
 557+ ),
 558+ [
 559+ { 'type': 'retain', 'length': 28 },
 560+ {
 561+ 'type': 'insert',
 562+ 'data': [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ]
 563+ }
 564+ ],
 565+ 'prepareInsertion inserts at the end'
 566+ );
 567+
 568+ // Test 9
 569+ raises(
 570+ function() {
 571+ documentModel.prepareInsertion(
 572+ -1,
 573+ [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ]
 574+ );
 575+ },
 576+ function ( e ) {
 577+ return /^Offset -1 out of bounds/.test( e ); },
 578+ 'prepareInsertion throws exception for negative offset'
 579+ );
 580+
 581+ // Test 10
 582+ raises(
 583+ function() {
 584+ documentModel.prepareInsertion(
 585+ 29,
 586+ [ { 'type': 'paragraph' }, 'f', 'o', 'o', { 'type': '/paragraph' } ]
 587+ );
 588+ },
 589+ function ( e ) {
 590+ return /^Offset 29 out of bounds/.test( e ); },
 591+ 'prepareInsertion throws exception for offset past the end'
 592+ );
537593 } );
538594
539595 test( 'es.DocumentModel.commit, es.DocumentModel.rollback', 10, function() {
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -722,13 +722,16 @@
723723
724724 var tx = new es.Transaction(),
725725 insertedData = data, // may be cloned and modified
726 - isStructuralLoc = es.DocumentModel.isStructuralOffset( this.data, offset ),
 726+ isStructuralLoc,
727727 wrappingElementType;
728728
729729 if ( offset < 0 || offset > this.data.length ) {
730730 throw 'Offset ' + offset + ' out of bounds [0..' + this.data.length + ']';
731731 }
732732
 733+ // Has to be after the bounds check, because isStructuralOffset doesn't like out-of-bounds offsets
 734+ isStructuralLoc = es.DocumentModel.isStructuralOffset( this.data, offset );
 735+
733736 if ( offset > 0 ) {
734737 tx.pushRetain( offset );
735738 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r101003Followup r100992: pass the regex directly rather than wrapping it in a functi...catrope17:19, 27 October 2011

Status & tagging log