From 3d02c1f3640c8d921d401d44665fb157d48e204f Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Thu, 24 Jun 2021 14:44:10 +0200 Subject: [PATCH] Add new QUnit test for ve.dm.MWTemplateSpecModel This patch also marks 2 methods as @private that are not and should not be used outside of this class. Bug: T285483 Change-Id: I8a8ffc4868a369b5c47068beb0e83f023872543d --- extension.json | 1 + .../dm/models/ve.dm.MWTemplateSpecModel.js | 7 + .../models/ve.dm.MWTemplateSpecModel.test.js | 176 ++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 modules/ve-mw/tests/dm/models/ve.dm.MWTemplateSpecModel.test.js diff --git a/extension.json b/extension.json index efdcb885fb..eb191c5582 100644 --- a/extension.json +++ b/extension.json @@ -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", diff --git a/modules/ve-mw/dm/models/ve.dm.MWTemplateSpecModel.js b/modules/ve-mw/dm/models/ve.dm.MWTemplateSpecModel.js index aca9bf5b19..debd4353cf 100644 --- a/modules/ve-mw/dm/models/ve.dm.MWTemplateSpecModel.js +++ b/modules/ve-mw/dm/models/ve.dm.MWTemplateSpecModel.js @@ -42,6 +42,12 @@ OO.initClass( ve.dm.MWTemplateSpecModel ); /* Static methods */ +/** + * @private + * @param {string|Object.} 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 */ diff --git a/modules/ve-mw/tests/dm/models/ve.dm.MWTemplateSpecModel.test.js b/modules/ve-mw/tests/dm/models/ve.dm.MWTemplateSpecModel.test.js new file mode 100644 index 0000000000..f05f5c6dd8 --- /dev/null +++ b/modules/ve-mw/tests/dm/models/ve.dm.MWTemplateSpecModel.test.js @@ -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' ); + } ); + +}() );