WMDE-leszek has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/360625 )
Change subject: Add JS implementation of Sense data model class ...................................................................... Add JS implementation of Sense data model class This is to the most extent copying from Form data model implementation, major difference being that, unlike form representations, sense glosses are multilingual. Change-Id: I115562f2058a0253fd6b05becbdd999f4a03d236 --- M extension.json A resources/datamodel/Sense.js M src/WikibaseLexeme.hooks.php A tests/qunit/datamodel/Sense.tests.js 4 files changed, 190 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseLexeme refs/changes/25/360625/1 diff --git a/extension.json b/extension.json index 70bd878..d3374ff 100644 --- a/extension.json +++ b/extension.json @@ -183,6 +183,16 @@ "wikibase.lexeme" ] }, + "wikibase.lexeme.datamodel.Sense": { + "scripts": [ + "datamodel/__namespace.js", + "datamodel/Sense.js" + ], + "dependencies": [ + "util.inherit", + "wikibase.lexeme" + ] + }, "wikibase.lexeme.getDeserializer": { "scripts": "getDeserializer.js", "dependencies": [ diff --git a/resources/datamodel/Sense.js b/resources/datamodel/Sense.js new file mode 100644 index 0000000..abb3c25 --- /dev/null +++ b/resources/datamodel/Sense.js @@ -0,0 +1,88 @@ +( function ( wb, util ) { + 'use strict'; + + /** + * @class wikibase.lexeme.datamodel.Sense + * + * @param {string} id + * @param {string[]} glosses + */ + var Sense = util.inherit( + 'Sense', + function ( id, glosses, statementGroupSet ) { + statementGroupSet = statementGroupSet || new wb.datamodel.StatementGroupSet(); + this._id = id; + this._glosses = glosses; + + if ( + !( statementGroupSet instanceof wb.datamodel.StatementGroupSet ) + ) { + throw new Error( 'Required parameter(s) missing or not defined properly' ); + } + + this._statementGroupSet = statementGroupSet; + }, + { + + /** + * @type {string} + */ + _id: null, + + /** + * @type {object} + */ + _glosses: null, + + /** + * @type {wikibase.datamodel.StatementGroupSet} + */ + _statementGroupSet: null, + + /** + * @return {string} + */ + getId: function () { + return this._id; + }, + + /** + * @return {object} + */ + getGlosses: function () { + return this._glosses; + }, + + /** + * @param language + * @return {string} + */ + getGloss: function ( language ) { + return language in this._glosses ? this._glosses[ language ] : ''; + }, + + /** + * @return {wikibase.datamodel.StatementGroupSet} + */ + getStatements: function () { + return this._statementGroupSet; + }, + + /** + * @param {Sense} other + * @return {boolean} + */ + equals: function ( other ) { + if ( !( other instanceof Sense ) ) { + return false; + } + + // TODO: should this also check statements? + return this.getId() === other.getId() && this.getGlosses() === other.getGlosses(); + } + } + ); + + wb.lexeme.datamodel.Sense = Sense; + +}( wikibase, util ) ); diff --git a/src/WikibaseLexeme.hooks.php b/src/WikibaseLexeme.hooks.php index e362a4b..8a439e9 100644 --- a/src/WikibaseLexeme.hooks.php +++ b/src/WikibaseLexeme.hooks.php @@ -96,6 +96,7 @@ $testModules['qunit']['WikibaseLexeme.tests'] = [ 'scripts' => [ 'tests/qunit/datamodel/LexemeForm.tests.js', + 'tests/qunit/datamodel/Sense.tests.js', 'tests/qunit/jquery.wikibase.lexemeformlistview.tests.js', 'tests/qunit/jquery.wikibase.lexemeformview.tests.js', 'tests/qunit/jquery.wikibase.grammaticalfeatureview.tests.js', @@ -114,6 +115,7 @@ 'jquery.wikibase.grammaticalfeatureview', 'oojs-ui', 'wikibase.lexeme.datamodel.LexemeForm', + 'wikibase.lexeme.datamodel.Sense', 'wikibase.lexeme.serialization.LexemeDeserializer', 'wikibase.lexeme.services.ItemLookup', 'wikibase.lexeme.services.LanguageFromItemExtractor', diff --git a/tests/qunit/datamodel/Sense.tests.js b/tests/qunit/datamodel/Sense.tests.js new file mode 100644 index 0000000..630d081 --- /dev/null +++ b/tests/qunit/datamodel/Sense.tests.js @@ -0,0 +1,90 @@ +/** + * @license GPL-2.0+ + */ +( function ( $, wb, QUnit ) { + 'use strict'; + + QUnit.module( 'wikibase.lexeme.datamodel.Sense' ); + + var Sense = wb.lexeme.datamodel.Sense; + + QUnit.test( 'getId()', function ( assert ) { + var expectedId = 'S123', + sense = new Sense( expectedId, {} ); + + assert.equal( sense.getId(), expectedId ); + } ); + + QUnit.test( 'getGlosses()', function ( assert ) { + var expectedGlosses = { en: 'test gloss' }, + sense = new Sense( 'S123', expectedGlosses ); + + assert.equal( sense.getGlosses(), expectedGlosses ); + } ); + + QUnit.test( 'getGloss() given gloss for language defined', function ( assert ) { + var expectedGloss = 'test gloss', + sense = new Sense( 'S123', { en: expectedGloss } ); + + assert.equal( sense.getGloss( 'en' ), expectedGloss ); + } ); + + QUnit.test( 'getGloss() given no gloss for the language', function ( assert ) { + var sense = new Sense( 'S123', { en: 'test gloss' } ); + + assert.equal( sense.getGloss( 'de' ), '' ); + } ); + + QUnit.test( 'equals()', function ( assert ) { + var id = 'S123', + glosses = { en: 'a gloss' }, + sense = new Sense( id, glosses ), + comparison = new Sense( id, glosses ); + + assert.equal( sense.equals( comparison ), true ); + } ); + + QUnit.test( 'not equals()', function ( assert ) { + var id = 'S123', + glosses = { en: 'a gloss' }, + emptyGlosses = {}, + differentGloss = { en: 'another gloss' }, + anotherLanguageGlosses = { de: 'ein Gloss' }, + sense = new Sense( id, glosses ), + equalsDataProvider = [ + { + comparison: new Sense( id, differentGloss ), + message: 'same id, different gloss in the same language' + }, + { + comparison: new Sense( id, anotherLanguageGlosses ), + message: 'same id, different glosses' + }, + { + comparison: new Sense( id, emptyGlosses ), + message: 'same id, no glosses' + }, + { + comparison: new Sense( 'S234', glosses ), + message: 'different id, same glosses' + }, + { + comparison: new Sense( 'S234', emptyGlosses ), + message: 'different id, different glosses' + }, + { + comparison: null, + message: 'not a Sense object' + } + ]; + + equalsDataProvider.forEach( function ( testData ) { + assert.equal( + sense.equals( testData.comparison ), + false, + testData.message + ); + } ); + } ); + +}( jQuery, wikibase, QUnit ) ); -- To view, visit https://gerrit.wikimedia.org/r/360625 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I115562f2058a0253fd6b05becbdd999f4a03d236 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseLexeme Gerrit-Branch: master Gerrit-Owner: WMDE-leszek <leszek.mani...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits