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(); var undocumentedParameters = this.getUndocumentedParameterNames();
undocumentedParameters.sort( function ( a, b ) { undocumentedParameters.sort( function ( a, b ) {
var aIsNaN = isNaN( a ), if ( isNaN( a ) ) {
bIsNaN = isNaN( b ); // If a and b are string, order alphabetically, otherwise numbers before strings
return isNaN( b ) ? a.localeCompare( b ) : 1;
if ( aIsNaN && bIsNaN ) { } else {
// Two strings // If a and b are numeric, order incrementally, otherwise numbers before strings
return a.localeCompare( b ); 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 ); return this.getDocumentedParameterOrder().concat( undocumentedParameters );

View file

@ -5,7 +5,7 @@
* @param {string[]} [parameterNames] * @param {string[]} [parameterNames]
* @return {ve.dm.MWTemplateModel} but it's a mock * @return {ve.dm.MWTemplateModel} but it's a mock
*/ */
const createTemplateMock = function ( parameterNames ) { const createTemplateMock = ( parameterNames ) => {
const params = {}; const params = {};
( parameterNames || [] ).forEach( ( name ) => { ( parameterNames || [] ).forEach( ( name ) => {
params[ 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 ) => { QUnit.test( 'getDocumentedParameterOrder() should not return a reference', ( assert ) => {
const template = createTemplateMock(), const template = createTemplateMock(),
spec = new ve.dm.MWTemplateSpecModel( template ); spec = new ve.dm.MWTemplateSpecModel( template );