Merge "Add new QUnit test for ve.dm.MWTemplateSpecModel"

This commit is contained in:
jenkins-bot 2021-06-28 09:11:48 +00:00 committed by Gerrit Code Review
commit b661992efe
3 changed files with 184 additions and 0 deletions

View file

@ -2739,6 +2739,7 @@
"modules/ve-mw/tests/dm/ve.dm.MWTemplateModel.test.js",
"modules/ve-mw/tests/dm/ve.dm.MWTransclusionModel.test.js",
"modules/ve-mw/tests/dm/ve.dm.MWInternalLinkAnnotation.test.js",
"modules/ve-mw/tests/dm/models/ve.dm.MWTemplateSpecModel.test.js",
"lib/ve/tests/ce/ve.ce.test.js",
"lib/ve/tests/ce/ve.ce.Document.test.js",
"modules/ve-mw/tests/ce/ve.ce.Document.test.js",

View file

@ -42,6 +42,12 @@ OO.initClass( ve.dm.MWTemplateSpecModel );
/* Static methods */
/**
* @private
* @param {string|Object.<string,string>} stringOrObject
* @param {string} [lang]
* @return {string|undefined}
*/
ve.dm.MWTemplateSpecModel.static.getLocalValue = function ( stringOrObject, lang ) {
return stringOrObject && typeof stringOrObject === 'object' ?
OO.ui.getLocalValue( stringOrObject, lang ) :
@ -112,6 +118,7 @@ ve.dm.MWTemplateSpecModel.prototype.fillFromTemplate = function () {
/**
* Get the default spec for a parameter.
*
* @private
* @param {string} name Parameter name
* @return {Object} Parameter spec
*/

View file

@ -0,0 +1,176 @@
( function () {
QUnit.module( 've.dm.MWTemplateSpecModel', ve.test.utils.mwEnvironment );
/**
* @param {string[]} [parameterNames]
* @return {ve.dm.MWTemplateModel} but it's a mock
*/
function createTemplateMock( parameterNames ) {
const x = {};
( parameterNames || [] ).forEach( ( name ) => {
x[ name ] = {};
} );
return {
params: x,
getTitle: () => null,
getTarget: () => {
return { wt: 'RawTemplateName' };
},
getParameters: function () {
return this.params;
}
};
}
QUnit.test( 'Basic behavior on empty template', ( assert ) => {
const template = createTemplateMock(),
spec = new ve.dm.MWTemplateSpecModel( template );
assert.strictEqual( spec.getLabel(), 'RawTemplateName', 'getLabel' );
assert.strictEqual( spec.getDescription(), null, 'getDescription' );
assert.deepEqual( spec.getCanonicalParameterOrder(), [], 'getCanonicalParameterOrder' );
assert.strictEqual( spec.isParameterKnown( 'unknown' ), false, 'isParameterKnown' );
assert.strictEqual( spec.getParameterDefaultValue( 'unknown' ), '', 'getParameterDefaultValue' );
assert.strictEqual( spec.getParameterAutoValue( 'unknown' ), '', 'getParameterAutoValue' );
assert.deepEqual( spec.getParameterNames(), [], 'getParameterNames' );
assert.deepEqual( spec.getParameterSets(), [], 'getParameterSets' );
assert.deepEqual( spec.getMaps(), {}, 'getMaps' );
} );
QUnit.test( 'Basic behavior on non-empty template', ( assert ) => {
const template = createTemplateMock( [ 'p1', 'p2' ] ),
spec = new ve.dm.MWTemplateSpecModel( template );
assert.strictEqual( spec.getLabel(), 'RawTemplateName', 'getLabel' );
assert.strictEqual( spec.getDescription(), null, 'getDescription' );
assert.deepEqual( spec.getCanonicalParameterOrder(), [], 'getCanonicalParameterOrder' );
assert.strictEqual( spec.isParameterKnown( 'p2' ), true, 'isParameterKnown' );
assert.strictEqual( spec.isParameterAlias( 'p2' ), false, 'isParameterAlias' );
assert.strictEqual( spec.getParameterLabel( 'p2' ), 'p2', 'getParameterLabel' );
assert.strictEqual( spec.getParameterDescription( 'p2' ), null, 'getParameterDescription' );
assert.deepEqual( spec.getParameterSuggestedValues( 'p2' ), [], 'getParameterSuggestedValues' );
assert.strictEqual( spec.getParameterDefaultValue( 'p2' ), '', 'getParameterDefaultValue' );
assert.strictEqual( spec.getParameterExampleValue( 'p2' ), undefined, 'getParameterExampleValue' );
assert.strictEqual( spec.getParameterAutoValue( 'p2' ), undefined, 'getParameterAutoValue' );
assert.strictEqual( spec.getParameterType( 'p2' ), 'string', 'getParameterType' );
assert.deepEqual( spec.getParameterAliases( 'p2' ), [], 'getParameterAliases' );
assert.strictEqual( spec.getParameterName( 'p2' ), 'p2', 'getParameterName' );
assert.strictEqual( spec.isParameterRequired( 'p2' ), false, 'isParameterRequired' );
assert.strictEqual( spec.isParameterSuggested( 'p2' ), false, 'isParameterSuggested' );
assert.strictEqual( spec.isParameterDeprecated( 'p2' ), false, 'isParameterDeprecated' );
assert.strictEqual( spec.getParameterDeprecationDescription( 'p2' ), '', 'getParameterDeprecationDescription' );
assert.deepEqual( spec.getParameterNames(), [ 'p1', 'p2' ], 'getParameterNames' );
assert.deepEqual( spec.getParameterSets(), [], 'getParameterSets' );
assert.deepEqual( spec.getMaps(), {}, 'getMaps' );
} );
QUnit.test( 'Basic behavior with later fillFromTemplate()', ( assert ) => {
const template = createTemplateMock( [ 'p1' ] ),
spec = new ve.dm.MWTemplateSpecModel( template );
template.params.p2 = {};
spec.fillFromTemplate();
assert.strictEqual( spec.getLabel(), 'RawTemplateName', 'getLabel' );
assert.strictEqual( spec.getDescription(), null, 'getDescription' );
assert.deepEqual( spec.getCanonicalParameterOrder(), [], 'getCanonicalParameterOrder' );
assert.strictEqual( spec.isParameterKnown( 'p2' ), true, 'isParameterKnown' );
assert.strictEqual( spec.isParameterAlias( 'p2' ), false, 'isParameterAlias' );
assert.strictEqual( spec.getParameterLabel( 'p2' ), 'p2', 'getParameterLabel' );
assert.strictEqual( spec.getParameterDescription( 'p2' ), null, 'getParameterDescription' );
assert.deepEqual( spec.getParameterSuggestedValues( 'p2' ), [], 'getParameterSuggestedValues' );
assert.strictEqual( spec.getParameterDefaultValue( 'p2' ), '', 'getParameterDefaultValue' );
assert.strictEqual( spec.getParameterExampleValue( 'p2' ), undefined, 'getParameterExampleValue' );
assert.strictEqual( spec.getParameterAutoValue( 'p2' ), undefined, 'getParameterAutoValue' );
assert.strictEqual( spec.getParameterType( 'p2' ), 'string', 'getParameterType' );
assert.deepEqual( spec.getParameterAliases( 'p2' ), [], 'getParameterAliases' );
assert.strictEqual( spec.getParameterName( 'p2' ), 'p2', 'getParameterName' );
assert.strictEqual( spec.isParameterRequired( 'p2' ), false, 'isParameterRequired' );
assert.strictEqual( spec.isParameterSuggested( 'p2' ), false, 'isParameterSuggested' );
assert.strictEqual( spec.isParameterDeprecated( 'p2' ), false, 'isParameterDeprecated' );
assert.strictEqual( spec.getParameterDeprecationDescription( 'p2' ), '', 'getParameterDeprecationDescription' );
assert.deepEqual( spec.getParameterNames(), [ 'p1', 'p2' ], 'getParameterNames' );
assert.deepEqual( spec.getParameterSets(), [], 'getParameterSets' );
assert.deepEqual( spec.getMaps(), {}, 'getMaps' );
} );
QUnit.test( 'Basic behavior with most minimal extend()', ( assert ) => {
const template = createTemplateMock( [ 'p1' ] ),
spec = new ve.dm.MWTemplateSpecModel( template );
spec.extend( { params: { p2: {} } } );
assert.strictEqual( spec.getLabel(), 'RawTemplateName', 'getLabel' );
assert.strictEqual( spec.getDescription(), undefined, 'getDescription' );
assert.deepEqual( spec.getCanonicalParameterOrder(), [ 'p2' ], 'getCanonicalParameterOrder' );
assert.strictEqual( spec.isParameterKnown( 'p2' ), true, 'isParameterKnown' );
assert.strictEqual( spec.isParameterAlias( 'p2' ), false, 'isParameterAlias' );
assert.strictEqual( spec.getParameterLabel( 'p2' ), 'p2', 'getParameterLabel' );
assert.strictEqual( spec.getParameterDescription( 'p2' ), null, 'getParameterDescription' );
assert.deepEqual( spec.getParameterSuggestedValues( 'p2' ), [], 'getParameterSuggestedValues' );
assert.strictEqual( spec.getParameterDefaultValue( 'p2' ), '', 'getParameterDefaultValue' );
assert.strictEqual( spec.getParameterExampleValue( 'p2' ), undefined, 'getParameterExampleValue' );
assert.strictEqual( spec.getParameterAutoValue( 'p2' ), undefined, 'getParameterAutoValue' );
assert.strictEqual( spec.getParameterType( 'p2' ), 'string', 'getParameterType' );
assert.deepEqual( spec.getParameterAliases( 'p2' ), [], 'getParameterAliases' );
assert.strictEqual( spec.getParameterName( 'p2' ), 'p2', 'getParameterName' );
assert.strictEqual( spec.isParameterRequired( 'p2' ), false, 'isParameterRequired' );
assert.strictEqual( spec.isParameterSuggested( 'p2' ), false, 'isParameterSuggested' );
assert.strictEqual( spec.isParameterDeprecated( 'p2' ), false, 'isParameterDeprecated' );
assert.strictEqual( spec.getParameterDeprecationDescription( 'p2' ), '', 'getParameterDeprecationDescription' );
assert.deepEqual( spec.getParameterNames(), [ 'p1', 'p2' ], 'getParameterNames' );
assert.strictEqual( spec.getParameterSets(), undefined, 'getParameterSets' );
assert.deepEqual( spec.getMaps(), {}, 'getMaps' );
} );
QUnit.test( 'Complex extend() with alias', ( assert ) => {
const template = createTemplateMock(),
spec = new ve.dm.MWTemplateSpecModel( template );
spec.extend( {
description: 'TemplateDescription',
params: {
p: {
label: 'ParamLabel',
description: 'ParamDescription',
suggestedvalues: [ 'SuggestedValue' ],
default: 'ParamDefault',
example: 'ParamExample',
autovalue: 'ParamAutoValue',
type: 'DummyType',
aliases: [ 'a' ],
required: true,
suggested: true,
deprecated: 'DeprecationText'
}
},
paramOrder: [ 'DummyOrder' ],
sets: [ 'DummySet' ],
maps: { dummyMap: true }
} );
assert.strictEqual( spec.getLabel(), 'RawTemplateName', 'getLabel' );
assert.strictEqual( spec.getDescription(), 'TemplateDescription', 'getDescription' );
assert.deepEqual( spec.getCanonicalParameterOrder(), [ 'DummyOrder' ], 'getCanonicalParameterOrder' );
assert.strictEqual( spec.isParameterKnown( 'a' ), true, 'isParameterKnown' );
assert.strictEqual( spec.isParameterAlias( 'a' ), true, 'isParameterAlias' );
assert.strictEqual( spec.getParameterLabel( 'a' ), 'ParamLabel', 'getParameterLabel' );
assert.strictEqual( spec.getParameterDescription( 'a' ), 'ParamDescription', 'getParameterDescription' );
assert.deepEqual( spec.getParameterSuggestedValues( 'a' ), [ 'SuggestedValue' ], 'getParameterSuggestedValues' );
assert.strictEqual( spec.getParameterDefaultValue( 'a' ), 'ParamDefault', 'getParameterDefaultValue' );
assert.strictEqual( spec.getParameterExampleValue( 'a' ), 'ParamExample', 'getParameterExampleValue' );
assert.strictEqual( spec.getParameterAutoValue( 'a' ), 'ParamAutoValue', 'getParameterAutoValue' );
assert.strictEqual( spec.getParameterType( 'a' ), 'DummyType', 'getParameterType' );
assert.deepEqual( spec.getParameterAliases( 'a' ), [ 'a' ], 'getParameterAliases' );
assert.strictEqual( spec.getParameterName( 'a' ), 'p', 'getParameterName' );
assert.strictEqual( spec.isParameterRequired( 'a' ), true, 'isParameterRequired' );
assert.strictEqual( spec.isParameterSuggested( 'a' ), true, 'isParameterSuggested' );
assert.strictEqual( spec.isParameterDeprecated( 'a' ), true, 'isParameterDeprecated' );
assert.strictEqual( spec.getParameterDeprecationDescription( 'a' ), 'DeprecationText', 'getParameterDeprecationDescription' );
assert.deepEqual( spec.getParameterNames(), [ 'p' ], 'getParameterNames' );
assert.deepEqual( spec.getParameterSets(), [ 'DummySet' ], 'getParameterSets' );
assert.deepEqual( spec.getMaps(), { dummyMap: true }, 'getMaps' );
} );
}() );