mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-28 00:00:49 +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();
|
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 );
|
||||||
|
|
|
@ -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 );
|
||||||
|
|
Loading…
Reference in a new issue