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:
thiemowmde 2023-04-21 16:22:33 +02:00 committed by Krinkle
parent 8cc29eca29
commit 1a09676159
2 changed files with 14 additions and 17 deletions

View file

@ -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 );

View file

@ -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 );