mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-23 22:13:34 +00:00
Add test for MWTemplateSpecModel.getDocumentedParameterOrder
It appears like this was never tested. Now that it is covered it's much easier to play around with the implementation and compact it a bit. Change-Id: Ie9cc14082f69e7240380d352fb362d0a3fa4d341
This commit is contained in:
parent
8cc29eca29
commit
1a09676159
|
@ -248,23 +248,13 @@ ve.dm.MWTemplateSpecModel.prototype.getCanonicalParameterOrder = function () {
|
|||
var undocumentedParameters = this.getUndocumentedParameterNames();
|
||||
|
||||
undocumentedParameters.sort( function ( a, b ) {
|
||||
var aIsNaN = isNaN( a ),
|
||||
bIsNaN = isNaN( b );
|
||||
|
||||
if ( aIsNaN && bIsNaN ) {
|
||||
// Two strings
|
||||
return a.localeCompare( b );
|
||||
if ( isNaN( a ) ) {
|
||||
// If a and b are string, order alphabetically, otherwise numbers before strings
|
||||
return isNaN( b ) ? a.localeCompare( b ) : 1;
|
||||
} else {
|
||||
// If a and b are numeric, order incrementally, otherwise numbers before strings
|
||||
return !isNaN( b ) ? a - b : -1;
|
||||
}
|
||||
if ( aIsNaN ) {
|
||||
// A is a string
|
||||
return 1;
|
||||
}
|
||||
if ( bIsNaN ) {
|
||||
// B is a string
|
||||
return -1;
|
||||
}
|
||||
// Two numbers
|
||||
return a - b;
|
||||
} );
|
||||
|
||||
return this.getDocumentedParameterOrder().concat( undocumentedParameters );
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* @param {string[]} [parameterNames]
|
||||
* @return {ve.dm.MWTemplateModel} but it's a mock
|
||||
*/
|
||||
const createTemplateMock = function ( parameterNames ) {
|
||||
const createTemplateMock = ( parameterNames ) => {
|
||||
const params = {};
|
||||
( parameterNames || [] ).forEach( ( name ) => {
|
||||
params[ name ] = {};
|
||||
|
@ -311,6 +311,13 @@
|
|||
} )
|
||||
);
|
||||
|
||||
QUnit.test( 'getCanonicalParameterOrder sorting undocumented parameters alphabetically', ( assert ) => {
|
||||
const template = createTemplateMock( [ 'ö', '3', 'x', 9, '', 1, 'a' ] ),
|
||||
spec = new ve.dm.MWTemplateSpecModel( template );
|
||||
|
||||
assert.deepEqual( spec.getCanonicalParameterOrder(), [ '1', '3', '9', 'a', 'ö', 'x' ] );
|
||||
} );
|
||||
|
||||
QUnit.test( 'getDocumentedParameterOrder() should not return a reference', ( assert ) => {
|
||||
const template = createTemplateMock(),
|
||||
spec = new ve.dm.MWTemplateSpecModel( template );
|
||||
|
|
Loading…
Reference in a new issue