r99268 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99267‎ | r99268 | r99269 >
Date:21:43, 7 October 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Improved tests and their messages
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/es.Transaction.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js (modified) (history)
  • /trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js (modified) (history)
  • /trunk/parsers/wikidom/tests/hype/es.ModelNode.test.js (modified) (history)
  • /trunk/parsers/wikidom/tests/hype/index.html (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/hype/es.ModelNode.test.js
@@ -1,6 +1,6 @@
22 module( 'Bases' );
33
4 -test( 'es.ModelNode', 17, function() {
 4+test( 'es.ModelNode',20, function() {
55 // Example data (integers) is used for simplicity of testing
66 var modelNode1 = new es.ModelNode(),
77 modelNode2 = new es.ModelNode(),
@@ -16,53 +16,74 @@
1717 modelNode2.on( 'afterAttach', function() {
1818 attaches++;
1919 } );
 20+ modelNode3.on( 'afterAttach', function() {
 21+ attaches++;
 22+ } );
 23+ modelNode4.on( 'afterAttach', function() {
 24+ attaches++;
 25+ } );
2026 var detaches = 0;
2127 modelNode2.on( 'afterDetach', function() {
2228 detaches++;
2329 } );
 30+ modelNode3.on( 'afterDetach', function() {
 31+ detaches++;
 32+ } );
 33+ modelNode4.on( 'afterDetach', function() {
 34+ detaches++;
 35+ } );
2436
2537 /** @covers es.ModelNode.push */
2638 modelNode1.push( modelNode2 );
27 - equal( updates, 1, 'es.ModelNode emits update events on push' );
28 - equal( modelNode1[0], modelNode2, 'es.ModelNode appends node on push' );
 39+ equal( updates, 1, 'push emits update events' );
 40+ equal( modelNode1[0], modelNode2, 'push appends a node' );
2941
3042 /** @covers es.ModelNode.attach */
31 - equal( attaches, 1, 'es.ModelNode emits afterAttach events when added to another node' );
 43+ equal( attaches, 1, 'push attaches added node' );
3244
3345 /** @covers es.ModelNode.unshift */
3446 modelNode1.unshift( modelNode3 );
35 - equal( updates, 2, 'es.ModelNode emits update events on unshift' );
36 - equal( modelNode1[0], modelNode3, 'es.ModelNode prepends node on unshift' );
 47+ equal( updates, 2, 'unshift emits update events' );
 48+ equal( modelNode1[0], modelNode3, 'unshift prepends a node' );
3749
 50+ /** @covers es.ModelNode.attach */
 51+ equal( attaches, 2, 'unshift attaches added node' );
 52+
3853 /** @covers es.ModelNode.splice */
3954 modelNode1.splice( 1, 0, modelNode4 );
40 - equal( updates, 3, 'es.ModelNode emits update events on splice' );
41 - equal( modelNode1[1], modelNode4, 'es.ModelNode inserts node on splice' );
 55+ equal( updates, 3, 'splice emits update events' );
 56+ equal( modelNode1[1], modelNode4, 'splice inserts nodes' );
4257
 58+ /** @covers es.ModelNode.attach */
 59+ equal( attaches, 3, 'splice attaches added nodes' );
 60+
4361 /** @covers es.ModelNode.reverse */
4462 modelNode1.reverse();
45 - equal( updates, 4, 'es.ModelNode emits update events on reverse' );
 63+ equal( updates, 4, 'reverse emits update events' );
4664
4765 /** @covers es.ModelNode.sort */
4866 modelNode1.sort( function( a, b ) {
4967 return a.length < b.length ? -1 : 1;
5068 } );
51 - equal( updates, 5, 'es.ModelNode emits update events on sort' );
 69+ equal( updates, 5, 'sort emits update events' );
5270 deepEqual(
5371 modelNode1.slice( 0 ),
5472 [modelNode2, modelNode3, modelNode4],
55 - 'es.ModelNode properly orders nodes on sort'
 73+ 'sort reorderes nodes correctly'
5674 );
5775
5876 /** @covers es.ModelNode.pop */
5977 modelNode1.pop();
60 - equal( updates, 6, 'es.ModelNode emits update events on pop' );
 78+ equal( updates, 6, 'pop emits update events' );
6179 deepEqual(
6280 modelNode1.slice( 0 ),
6381 [modelNode2, modelNode3],
64 - 'es.ModelNode removes last node on pop'
 82+ 'pop removes the last child node'
6583 );
6684
 85+ /** @covers es.ModelNode.detach */
 86+ equal( detaches, 1, 'pop detaches a node' );
 87+
6788 /** @covers es.ModelNode.shift */
6889 modelNode1.shift();
6990 equal( updates, 7, 'es.ModelNode emits update events on shift' );
@@ -73,14 +94,14 @@
7495 );
7596
7697 /** @covers es.ModelNode.detach */
77 - equal( detaches, 1, 'es.ModelNode emits afterDetach events when removed from another node' );
 98+ equal( detaches, 2, 'shift detaches a node' );
7899
79100 /** @covers es.ModelNode.getParent */
80 - strictEqual( modelNode3.getParent(), modelNode1, 'Child nodes have correct parent reference' );
 101+ strictEqual( modelNode3.getParent(), modelNode1, 'getParent returns the correct reference' );
81102
82103 try {
83104 var view = modelNode3.createView();
84105 } catch ( err ){
85 - ok( true, 'Calling createView on a plain ModelNode throws an exception' );
 106+ ok( true, 'createView throws an exception when not overridden' );
86107 }
87108 } );
Index: trunk/parsers/wikidom/tests/hype/index.html
@@ -14,6 +14,7 @@
1515 <script src="../../lib/qunit.js"></script>
1616 <script src="../../lib/hype/es.js"></script>
1717 <script src="../../lib/hype/es.Range.js"></script>
 18+ <script src="../../lib/hype/es.Transaction.js"></script>
1819 <script src="../../lib/hype/bases/es.EventEmitter.js"></script>
1920 <script src="../../lib/hype/bases/es.ModelNode.js"></script>
2021 <script src="../../lib/hype/bases/es.ViewNode.js"></script>
Index: trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js
@@ -201,12 +201,12 @@
202202 new es.ParagraphModel( data[25], 1 )
203203 ];
204204
205 -test( 'es.DocumentModel', 11, function() {
 205+test( 'es.DocumentModel', 15, function() {
206206 var documentModel = es.DocumentModel.newFromPlainObject( obj );
207207
208208 deepEqual( documentModel.getData(), data, 'Flattening plain objects results in correct data' );
209209
210 - deepEqual( documentModel.slice( 0 ), tree, 'Nodes contain correct lengths' );
 210+ deepEqual( documentModel.slice( 0 ), tree, 'Nodes in the model tree contain correct lengths' );
211211
212212 deepEqual(
213213 documentModel[0].getContent( new es.Range( 1, 3 ) ),
@@ -214,42 +214,31 @@
215215 ['b', { 'type': 'bold', 'hash': '#bold' }],
216216 ['c', { 'type': 'italic', 'hash': '#italic' }]
217217 ],
218 - 'When getting content for a node, ranges can trim left'
 218+ 'getContent can return an ending portion of the content'
219219 );
220220
221221 deepEqual(
222222 documentModel[0].getContent( new es.Range( 0, 2 ) ),
223 - [
224 - 'a',
225 - ['b', { 'type': 'bold', 'hash': '#bold' }],
226 - ],
227 - 'When getting content for a node, ranges can trim right'
 223+ ['a', ['b', { 'type': 'bold', 'hash': '#bold' }]],
 224+ 'getContent can return a beginning portion of the content'
228225 );
229226
230227 deepEqual(
231228 documentModel[0].getContent( new es.Range( 1, 2 ) ),
232 - [
233 - ['b', { 'type': 'bold', 'hash': '#bold' }],
234 - ],
235 - 'When getting content for a node, ranges can trim left and right'
 229+ [['b', { 'type': 'bold', 'hash': '#bold' }]],
 230+ 'getContent can return a middle portion of the content'
236231 );
237232
238233 try {
239234 documentModel[0].getContent( new es.Range( -1, 3 ) );
240235 } catch ( err ) {
241 - ok(
242 - true,
243 - 'Exceptions are thrown when getting node content within a range starting before 0'
244 - );
 236+ ok( true, 'getContent throws exceptions when given a range with start < 0' );
245237 }
246238
247239 try {
248240 documentModel[0].getContent( new es.Range( 0, 4 ) );
249241 } catch ( err ) {
250 - ok(
251 - true,
252 - 'Exceptions are thrown when getting node content within a range ending after length'
253 - );
 242+ ok( true, 'getContent throws exceptions when given a range with end > length' );
254243 }
255244
256245 deepEqual( documentModel[2].getContent(), ['a'], 'Content can be extracted from nodes' );
@@ -276,4 +265,43 @@
277266 -1,
278267 'getIndexOfAnnotation returns -1 if the annotation was not found'
279268 );
 269+
 270+ deepEqual(
 271+ documentModel.prepareElementAttributeChange( 0, 'set', 'test', 1234 ),
 272+ [
 273+ { 'type': 'attribute', 'method': 'set', 'key': 'test', 'value': 1234 },
 274+ { 'type': 'retain', 'length': 28 }
 275+ ],
 276+ 'prepareElementAttributeChange retains data after attribute change for first element'
 277+ );
 278+
 279+ deepEqual(
 280+ documentModel.prepareElementAttributeChange( 5, 'set', 'test', 1234 ),
 281+ [
 282+ { 'type': 'retain', 'length': 5 },
 283+ { 'type': 'attribute', 'method': 'set', 'key': 'test', 'value': 1234 },
 284+ { 'type': 'retain', 'length': 23 }
 285+ ],
 286+ 'prepareElementAttributeChange retains data before and after attribute change'
 287+ );
 288+
 289+ try {
 290+ documentModel.prepareElementAttributeChange( 1, 'set', 'test', 1234 )
 291+ } catch ( err ) {
 292+ ok(
 293+ true,
 294+ 'prepareElementAttributeChange throws an exception when offset is not an element'
 295+ );
 296+ }
 297+
 298+ try {
 299+ documentModel.prepareElementAttributeChange( 4, 'set', 'test', 1234 )
 300+ } catch ( err ) {
 301+ ok(
 302+ true,
 303+ 'prepareElementAttributeChange throws an exception when offset is a closing element'
 304+ );
 305+ }
 306+
280307 } );
 308+
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -545,6 +545,7 @@
546546 * @returns {es.Transaction}
547547 */
548548 es.DocumentModel.prototype.prepareInsertion = function( offset, data ) {
 549+ var tx = new es.Transaction();
549550 /*
550551 * There are 2 basic types of locations the insertion point can be:
551552 * Structural locations
@@ -570,6 +571,7 @@
571572 * }
572573 * }
573574 */
 575+ return tx;
574576 };
575577
576578 /**
@@ -580,6 +582,7 @@
581583 * @returns {es.Transaction}
582584 */
583585 es.DocumentModel.prototype.prepareRemoval = function( range ) {
 586+ var tx = new es.Transaction();
584587 /*
585588 * if ( The range spans structural elements ) {
586589 * if ( The range partially overlaps structural elements ) {
@@ -591,6 +594,7 @@
592595 * Removing only content is OK, do nothing
593596 * }
594597 */
 598+ return tx;
595599 };
596600
597601 /**
@@ -644,12 +648,17 @@
645649 if ( offset ) {
646650 tx.pushRetain( offset );
647651 }
648 - if ( this.data[offset].type !== undefined ) {
649 - tx.pushChangeElementAttribute( method, key, value );
 652+ if ( this.data[offset].type === undefined ) {
 653+ throw 'Invalid element offset error. Can not set attributes to non-element data.'
650654 }
 655+ if ( this.data[offset].type[0] === '/' ) {
 656+ throw 'Invalid element offset error. Can not set attributes on closing element.'
 657+ }
 658+ tx.pushChangeElementAttribute( method, key, value );
651659 if ( offset < this.data.length ) {
652660 tx.pushRetain( this.data.length - offset );
653661 }
 662+ return tx;
654663 };
655664
656665 /**
Index: trunk/parsers/wikidom/lib/hype/es.Transaction.js
@@ -11,28 +11,28 @@
1212
1313 /* Methods */
1414
15 -es.Transaction.prototype.pushRetain( length ) {
 15+es.Transaction.prototype.pushRetain = function( length ) {
1616 this.push( {
1717 'type': 'retain',
1818 'length': length
1919 } );
2020 };
2121
22 -es.Transaction.prototype.pushInsert( content ) {
 22+es.Transaction.prototype.pushInsert = function( content ) {
2323 this.push( {
2424 'type': 'insert',
2525 'data': data
2626 } );
2727 };
2828
29 -es.Transaction.prototype.pushRemove( data ) {
 29+es.Transaction.prototype.pushRemove = function( data ) {
3030 this.push( {
3131 'type': 'remove',
3232 'data': data
3333 } );
3434 };
3535
36 -es.Transaction.prototype.pushChangeElementAttribute( method, key, value ) {
 36+es.Transaction.prototype.pushChangeElementAttribute = function( method, key, value ) {
3737 this.push( {
3838 'type': 'attribute',
3939 'method': method,
@@ -41,7 +41,7 @@
4242 } );
4343 };
4444
45 -es.Transaction.prototype.pushStartAnnotating( method, annotation ) {
 45+es.Transaction.prototype.pushStartAnnotating = function( method, annotation ) {
4646 this.push( {
4747 'type': 'annotate',
4848 'method': method,
@@ -50,7 +50,7 @@
5151 } );
5252 };
5353
54 -es.Transaction.prototype.pushStopAnnotating( method, annotation ) {
 54+es.Transaction.prototype.pushStopAnnotating = function( method, annotation ) {
5555 this.push( {
5656 'type': 'annotate',
5757 'method': method,

Status & tagging log