jenkins-bot has submitted this change and it was merged. Change subject: Add unit tests for ve.dm.Selection sub-classes ......................................................................
Add unit tests for ve.dm.Selection sub-classes Also fixes minor bug with isSingleCell returning undefined instead of false. Change-Id: Ic14b4a9643f4e40754dec7ecc7d54c42af20788d --- M build/modules.json M src/dm/selections/ve.dm.LinearSelection.js M src/dm/selections/ve.dm.TableSelection.js A tests/dm/selections/ve.dm.LinearSelection.test.js A tests/dm/selections/ve.dm.NullSelection.test.js A tests/dm/selections/ve.dm.TableSelection.test.js M tests/index.html 7 files changed, 275 insertions(+), 8 deletions(-) Approvals: Catrope: Looks good to me, approved jenkins-bot: Verified diff --git a/build/modules.json b/build/modules.json index c4e7b2a..3ce9be0 100644 --- a/build/modules.json +++ b/build/modules.json @@ -475,6 +475,9 @@ "tests/dm/ve.dm.SurfaceFragment.test.js", "tests/dm/ve.dm.ModelRegistry.test.js", "tests/dm/ve.dm.MetaList.test.js", + "tests/dm/selections/ve.dm.LinearSelection.test.js", + "tests/dm/selections/ve.dm.NullSelection.test.js", + "tests/dm/selections/ve.dm.TableSelection.test.js", "tests/dm/lineardata/ve.dm.FlatLinearData.test.js", "tests/dm/lineardata/ve.dm.ElementLinearData.test.js", "tests/dm/lineardata/ve.dm.MetaLinearData.test.js", diff --git a/src/dm/selections/ve.dm.LinearSelection.js b/src/dm/selections/ve.dm.LinearSelection.js index 6790c3c..24bada6 100644 --- a/src/dm/selections/ve.dm.LinearSelection.js +++ b/src/dm/selections/ve.dm.LinearSelection.js @@ -1,5 +1,5 @@ /*! - * VisualEditor Text Selection class. + * VisualEditor Linear Selection class. * * @copyright 2011-2015 VisualEditor Team and others; see http://ve.mit-license.org */ diff --git a/src/dm/selections/ve.dm.TableSelection.js b/src/dm/selections/ve.dm.TableSelection.js index 3392f58..be9afcd 100644 --- a/src/dm/selections/ve.dm.TableSelection.js +++ b/src/dm/selections/ve.dm.TableSelection.js @@ -254,14 +254,10 @@ * @return {boolean} The selection spans a single cell */ ve.dm.TableSelection.prototype.isSingleCell = function () { - if ( - // Quick check for single non-merged cell - ( this.fromRow === this.toRow && this.fromCol === this.toCol ) || + // Quick check for single non-merged cell + return ( this.fromRow === this.toRow && this.fromCol === this.toCol ) || // Check for a merged single cell by ignoring placeholders - this.getMatrixCells().length === 1 - ) { - return true; - } + this.getMatrixCells().length === 1; }; /** diff --git a/tests/dm/selections/ve.dm.LinearSelection.test.js b/tests/dm/selections/ve.dm.LinearSelection.test.js new file mode 100644 index 0000000..48cc514 --- /dev/null +++ b/tests/dm/selections/ve.dm.LinearSelection.test.js @@ -0,0 +1,56 @@ +/*! + * VisualEditor DataModel Linear Selection tests. + * + * @copyright 2011-2015 VisualEditor Team and others; see http://ve.mit-license.org + */ + +QUnit.module( 've.dm.LinearSelection' ); + +/* Tests */ + +QUnit.test( 'Construction and getters (getDocument, getRange(s))', 3, function ( assert ) { + var dummyDoc = { a: 1 }, + range = new ve.Range( 200, 100 ), + selection = new ve.dm.LinearSelection( dummyDoc, range ); + + assert.strictEqual( selection.getDocument(), dummyDoc, 'getDocument' ); + assert.deepEqual( selection.getRange(), range, 'getRange' ); + assert.deepEqual( selection.getRanges(), [range], 'getRanges' ); +} ); + +QUnit.test( 'Basic methods (clone, collapse*, isCollased, equals, isNull)', 10, function ( assert ) { + var dummyDoc = { a: 1 }, + dummyDoc2 = { a: 1 }, + range = new ve.Range( 200, 100 ), + selection = new ve.dm.LinearSelection( dummyDoc, range ), + selection2 = new ve.dm.LinearSelection( dummyDoc2, range ), + startSelection = new ve.dm.LinearSelection( dummyDoc, new ve.Range( 100 ) ), + endSelection = new ve.dm.LinearSelection( dummyDoc, new ve.Range( 200 ) ); + + assert.deepEqual( selection.clone(), selection, 'clone' ); + assert.deepEqual( selection.collapseToStart(), startSelection, 'collapseToStart' ); + assert.deepEqual( selection.collapseToEnd(), endSelection, 'collapseToEnd' ); + assert.deepEqual( selection.collapseToFrom(), endSelection, 'collapseToFrom' ); + assert.deepEqual( selection.collapseToTo(), startSelection, 'collapseToTo' ); + assert.strictEqual( selection.isCollapsed(), false, '200-100 is not collapsed' ); + assert.strictEqual( startSelection.isCollapsed(), true, '100-100 is collapsed' ); + assert.strictEqual( selection.equals( selection ), true, 'equals' ); + assert.strictEqual( selection.equals( selection2 ), false, 'not equal when docs are not reference equal' ); + assert.strictEqual( selection.isNull(), false, 'not null' ); +} ); + +QUnit.test( 'Factory methods & serialization (newFromJSON, toJSON, getDescription)', 3, function ( assert ) { + var dummyDoc = { a: 1 }, + range = new ve.Range( 200, 100 ), + selection = new ve.dm.LinearSelection( dummyDoc, range ); + + assert.deepEqual( selection.toJSON(), { type: 'linear', range: range }, 'toJSON' ); + assert.deepEqual( + ve.dm.Selection.static.newFromJSON( dummyDoc, JSON.stringify( { type: 'linear', range: range } ) ), + selection, + 'newFromJSON' + ); + assert.deepEqual( selection.getDescription(), 'Linear: 200 - 100', 'getDescription' ); +} ); + +// TODO: translateByTransaction diff --git a/tests/dm/selections/ve.dm.NullSelection.test.js b/tests/dm/selections/ve.dm.NullSelection.test.js new file mode 100644 index 0000000..d64f2c0 --- /dev/null +++ b/tests/dm/selections/ve.dm.NullSelection.test.js @@ -0,0 +1,49 @@ +/*! + * VisualEditor DataModel Null Selection tests. + * + * @copyright 2011-2015 VisualEditor Team and others; see http://ve.mit-license.org + */ + +QUnit.module( 've.dm.NullSelection' ); + +/* Tests */ + +QUnit.test( 'Construction and getters (getDocument, getRanges)', 2, function ( assert ) { + var dummyDoc = { a: 1 }, + selection = new ve.dm.NullSelection( dummyDoc ); + + assert.strictEqual( selection.getDocument(), dummyDoc, 'getDocument' ); + assert.deepEqual( selection.getRanges(), [], 'getRanges' ); +} ); + +QUnit.test( 'Basic methods (clone, collapse*, isCollased, equals, isNull)', 9, function ( assert ) { + var dummyDoc = { a: 1 }, + dummyDoc2 = { a: 1 }, + selection = new ve.dm.NullSelection( dummyDoc ), + selection2 = new ve.dm.NullSelection( dummyDoc2 ); + + assert.deepEqual( selection.clone(), selection, 'clone' ); + assert.deepEqual( selection.collapseToStart(), selection, 'collapseToStart' ); + assert.deepEqual( selection.collapseToEnd(), selection, 'collapseToEnd' ); + assert.deepEqual( selection.collapseToFrom(), selection, 'collapseToFrom' ); + assert.deepEqual( selection.collapseToTo(), selection, 'collapseToTo' ); + assert.strictEqual( selection.isCollapsed(), true, 'isCollapsed' ); + assert.strictEqual( selection.equals( selection ), true, 'equals' ); + assert.strictEqual( selection.equals( selection2 ), false, 'not equal when docs are not reference equal' ); + assert.strictEqual( selection.isNull(), true, 'null' ); +} ); + +QUnit.test( 'Factory methods & serialization (newFromJSON, toJSON, getDescription)', 3, function ( assert ) { + var dummyDoc = { a: 1 }, + selection = new ve.dm.NullSelection( dummyDoc ); + + assert.deepEqual( selection.toJSON(), { type: 'null' }, 'toJSON' ); + assert.deepEqual( + ve.dm.Selection.static.newFromJSON( dummyDoc, JSON.stringify( { type: 'null' } ) ), + selection, + 'newFromJSON' + ); + assert.deepEqual( selection.getDescription(), 'Null', 'getDescription' ); +} ); + +// TODO: translateByTransaction diff --git a/tests/dm/selections/ve.dm.TableSelection.test.js b/tests/dm/selections/ve.dm.TableSelection.test.js new file mode 100644 index 0000000..f4b3aee --- /dev/null +++ b/tests/dm/selections/ve.dm.TableSelection.test.js @@ -0,0 +1,160 @@ +/*! + * VisualEditor DataModel Table Selection tests. + * + * @copyright 2011-2015 VisualEditor Team and others; see http://ve.mit-license.org + */ + +QUnit.module( 've.dm.TableSelection' ); + +/* Tests */ + +QUnit.test( 'Construction and getters (getDocument, getRanges, getOuterRanges, getTableNode', 28, function ( assert ) { + var i, selection, + doc = ve.dm.example.createExampleDocument( 'mergedCells' ), + tableNode = doc.getBranchNodeFromOffset( 1 ), + tableRange = tableNode.getOuterRange(), + cases = [ + { + msg: 'single cell selection', + selection: new ve.dm.TableSelection( doc, tableRange, 1, 2 ), + fromCol: 1, + fromRow: 2, + toCol: 1, + toRow: 2, + startCol: 1, + startRow: 2, + endCol: 1, + endRow: 2, + ranges: [ + new ve.Range( 65, 69 ) + ], + outerRanges: [ + new ve.Range( 64, 70 ) + ] + }, + { + msg: 'multi cell selection', + selection: new ve.dm.TableSelection( doc, tableRange, 1, 2, 0, 1 ), + fromCol: 1, + fromRow: 2, + toCol: 0, + toRow: 1, + startCol: 0, + startRow: 1, + endCol: 1, + endRow: 2, + ranges: [ + new ve.Range( 36, 39 ), + new ve.Range( 41, 44 ), + new ve.Range( 59, 63 ), + new ve.Range( 65, 69 ) + ], + outerRanges: [ + new ve.Range( 35, 40 ), + new ve.Range( 40, 45 ), + new ve.Range( 58, 64 ), + new ve.Range( 64, 70 ) + ] + }, + { + msg: 'multi cell selection (expanded)', + selection: new ve.dm.TableSelection( doc, tableRange, 1, 2, 0, 1, true ), + fromCol: 2, + fromRow: 2, + toCol: 0, + toRow: 1, + startCol: 0, + startRow: 1, + endCol: 2, + endRow: 2, + ranges: [ + new ve.Range( 36, 39 ), + new ve.Range( 41, 44 ), + new ve.Range( 59, 63 ), + new ve.Range( 65, 69 ), + new ve.Range( 71, 75 ) + ], + outerRanges: [ + new ve.Range( 35, 40 ), + new ve.Range( 40, 45 ), + new ve.Range( 58, 64 ), + new ve.Range( 64, 70 ), + new ve.Range( 70, 76 ) + ] + } + ]; + + QUnit.expect( 12 * cases.length ); + + for ( i in cases ) { + selection = cases[i].selection; + assert.strictEqual( selection.getDocument(), doc, 'getDocument' ); + assert.strictEqual( selection.getTableNode(), tableNode, 'getTableNode' ); + assert.deepEqual( selection.getRanges(), cases[i].ranges, cases[i].msg + ': getRanges' ); + assert.deepEqual( selection.getOuterRanges(), cases[i].outerRanges, cases[i].msg + ': getOuterRanges' ); + assert.strictEqual( selection.fromCol, cases[i].fromCol, cases[i].msg + ': fromCol set' ); + assert.strictEqual( selection.fromRow, cases[i].fromRow, cases[i].msg + ': fromRow set' ); + assert.strictEqual( selection.toCol, cases[i].toCol, cases[i].msg + ': toCol set' ); + assert.strictEqual( selection.toRow, cases[i].toRow, cases[i].msg + ': toRow set' ); + assert.strictEqual( selection.startCol, cases[i].startCol, cases[i].msg + ': startCol set' ); + assert.strictEqual( selection.startRow, cases[i].startRow, cases[i].msg + ': startRow set' ); + assert.strictEqual( selection.endCol, cases[i].endCol, cases[i].msg + ': endCol set' ); + assert.strictEqual( selection.endRow, cases[i].endRow, cases[i].msg + ': endRow set' ); + } + +} ); + +QUnit.test( 'Basic methods (clone, expand, collapse*, getRange(s), isCollased, isSingleCell, equals, isNull)', 13, function ( assert ) { + var doc = ve.dm.example.createExampleDocument( 'mergedCells' ), + doc2 = ve.dm.example.createExampleDocument( 'mergedCells' ), + tableRange = doc.getBranchNodeFromOffset( 1 ).getOuterRange(), + selection = new ve.dm.TableSelection( doc, tableRange, 1, 2, 0, 1, true ), + expandedSelection = new ve.dm.TableSelection( doc, tableRange, 2, 2, 0, 1 ), + selection2 = new ve.dm.TableSelection( doc2, tableRange, 1, 2, 0, 1, true ), + startSelection = new ve.dm.TableSelection( doc, tableRange, 0, 1 ), + endSelection = new ve.dm.TableSelection( doc, tableRange, 2, 2 ), + mergedSingleCell = new ve.dm.TableSelection( doc, tableRange, 1, 3, 3, 5, true ); + + assert.deepEqual( selection.clone(), expandedSelection, 'clone' ); + assert.deepEqual( selection.collapseToStart(), startSelection, 'collapseToStart' ); + assert.deepEqual( selection.collapseToEnd(), endSelection, 'collapseToEnd' ); + assert.deepEqual( selection.collapseToFrom(), endSelection, 'collapseToFrom' ); + assert.deepEqual( selection.collapseToTo(), startSelection, 'collapseToTo' ); + assert.strictEqual( selection.isCollapsed(), false, 'multi cell is not collapsed' ); + assert.strictEqual( startSelection.isCollapsed(), false, 'single cell is not collapsed' ); + assert.strictEqual( selection.isSingleCell(), false, 'multi cell selection is not a single cell' ); + assert.strictEqual( startSelection.isSingleCell(), true, 'single cell selection is a single cell' ); + assert.strictEqual( mergedSingleCell.isSingleCell(), true, 'merged single cell selection is a single cell' ); + assert.strictEqual( selection.equals( selection ), true, 'equals' ); + assert.strictEqual( selection.equals( selection2 ), false, 'not equal when docs are not reference equal' ); + assert.strictEqual( selection.isNull(), false, 'not null' ); +} ); + +QUnit.test( 'Factory methods & serialization (newFromJSON, toJSON, getDescription)', 3, function ( assert ) { + var doc = ve.dm.example.createExampleDocument( 'mergedCells' ), + tableRange = doc.getBranchNodeFromOffset( 1 ).getOuterRange(), + selection = new ve.dm.TableSelection( doc, tableRange, 1, 2, 3, 4 ), + json = { + type: 'table', + tableRange: tableRange, + fromCol: 1, + fromRow: 2, + toCol: 3, + toRow: 4 + }; + + assert.deepEqual( selection.toJSON(), json, 'toJSON' ); + + assert.deepEqual( + ve.dm.Selection.static.newFromJSON( doc, JSON.stringify( json ) ), + selection, + 'newFromJSON' + ); + assert.deepEqual( selection.getDescription(), 'Table: 0 - 171, c1 r2 - c3 r4', 'getDescription' ); +} ); + +// TODO: getMatrixCells +// TODO: translateByTransaction +// TODO: newFromAdjustment +// TODO: isFullRow +// TODO: isFullCol diff --git a/tests/index.html b/tests/index.html index d673e27..d5058b8 100644 --- a/tests/index.html +++ b/tests/index.html @@ -365,6 +365,9 @@ <script src="../tests/dm/ve.dm.SurfaceFragment.test.js"></script> <script src="../tests/dm/ve.dm.ModelRegistry.test.js"></script> <script src="../tests/dm/ve.dm.MetaList.test.js"></script> + <script src="../tests/dm/selections/ve.dm.LinearSelection.test.js"></script> + <script src="../tests/dm/selections/ve.dm.NullSelection.test.js"></script> + <script src="../tests/dm/selections/ve.dm.TableSelection.test.js"></script> <script src="../tests/dm/lineardata/ve.dm.FlatLinearData.test.js"></script> <script src="../tests/dm/lineardata/ve.dm.ElementLinearData.test.js"></script> <script src="../tests/dm/lineardata/ve.dm.MetaLinearData.test.js"></script> -- To view, visit https://gerrit.wikimedia.org/r/196217 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic14b4a9643f4e40754dec7ecc7d54c42af20788d Gerrit-PatchSet: 1 Gerrit-Project: VisualEditor/VisualEditor Gerrit-Branch: master Gerrit-Owner: Esanders <esand...@wikimedia.org> Gerrit-Reviewer: Catrope <roan.katt...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits