2020-11-16 18:43:00 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTemplateModel tests.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
( function () {
|
2021-04-29 14:42:18 +00:00
|
|
|
const transclusionData = {
|
2020-11-16 18:43:00 +00:00
|
|
|
params: {
|
|
|
|
foo: { wt: 'Foo value' },
|
|
|
|
bar: { wt: 'Bar value' },
|
2021-07-07 13:51:50 +00:00
|
|
|
empty: { wt: '' },
|
|
|
|
'': { wt: '' }
|
2020-11-16 18:43:00 +00:00
|
|
|
},
|
|
|
|
target: {
|
|
|
|
href: './Template:Test',
|
|
|
|
wt: 'Test'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-25 08:31:34 +00:00
|
|
|
QUnit.module( 've.dm.MWTemplateModel' );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new MWTemplateModel initialized with a static transclusion data fixture.
|
|
|
|
*
|
|
|
|
* @return {ve.dm.MWTemplateModel}
|
|
|
|
*/
|
|
|
|
function newTemplateModel() {
|
2021-04-29 14:42:18 +00:00
|
|
|
const doc = ve.dm.Document.static.newBlankDocument(),
|
2020-11-16 18:43:00 +00:00
|
|
|
transclusion = new ve.dm.MWTransclusionModel( doc ),
|
|
|
|
clonedTransclusionData = ve.extendObject( {}, transclusionData );
|
|
|
|
|
|
|
|
return ve.dm.MWTemplateModel.newFromData( transclusion, clonedTransclusionData );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tests */
|
|
|
|
|
2021-07-05 09:55:11 +00:00
|
|
|
QUnit.test( 'hasParameter', ( assert ) => {
|
|
|
|
const template = newTemplateModel();
|
|
|
|
|
|
|
|
// All parameters are primary as long as the TemplateData documentation isn't known
|
|
|
|
assert.strictEqual( template.hasParameter( 'bar' ), true );
|
|
|
|
assert.strictEqual( template.hasParameter( 'resolved-bar' ), false );
|
|
|
|
assert.strictEqual( template.hasParameter( 'alternative-bar' ), false );
|
|
|
|
|
|
|
|
template.getSpec().setTemplateData( { params: {
|
|
|
|
'resolved-bar': { aliases: [ 'bar', 'alternative-bar' ] }
|
|
|
|
} } );
|
|
|
|
|
|
|
|
// Now "bar" and "alternative-bar" are aliases, and "resolved-bar" is the primary name
|
|
|
|
assert.strictEqual( template.hasParameter( 'bar' ), true );
|
|
|
|
assert.strictEqual( template.hasParameter( 'resolved-bar' ), true );
|
|
|
|
assert.strictEqual( template.hasParameter( 'alternative-bar' ), true );
|
|
|
|
} );
|
|
|
|
|
2021-04-30 09:33:22 +00:00
|
|
|
QUnit.test( 'serialize input parameters', ( assert ) => {
|
2021-07-07 13:51:50 +00:00
|
|
|
const template = newTemplateModel();
|
2020-11-16 18:43:00 +00:00
|
|
|
|
2021-07-07 13:51:50 +00:00
|
|
|
const serialization = template.serialize();
|
|
|
|
assert.deepEqual( serialization, { template: {
|
|
|
|
params: {
|
|
|
|
foo: { wt: 'Foo value' },
|
|
|
|
bar: { wt: 'Bar value' },
|
|
|
|
empty: { wt: '' }
|
|
|
|
},
|
|
|
|
target: { href: './Template:Test', wt: 'Test' }
|
|
|
|
} } );
|
2020-11-16 18:43:00 +00:00
|
|
|
} );
|
|
|
|
|
2021-04-30 09:33:22 +00:00
|
|
|
QUnit.test( 'serialize changed input parameters', ( assert ) => {
|
2021-07-07 13:51:50 +00:00
|
|
|
const template = newTemplateModel(),
|
|
|
|
newParameter = new ve.dm.MWParameterModel( template, 'baz', 'Baz value' );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
2021-07-07 13:51:50 +00:00
|
|
|
template.addParameter( newParameter );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
2021-07-07 13:51:50 +00:00
|
|
|
const serialization = template.serialize();
|
|
|
|
assert.deepEqual( serialization.template.params.baz, { wt: 'Baz value' } );
|
2020-11-16 18:43:00 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// T75134
|
2021-04-30 09:33:22 +00:00
|
|
|
QUnit.test( 'serialize after parameter was removed', ( assert ) => {
|
2021-07-07 13:51:50 +00:00
|
|
|
const template = newTemplateModel(),
|
|
|
|
existingParameter = template.getParameter( 'bar' );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
2021-07-07 13:51:50 +00:00
|
|
|
template.removeParameter( existingParameter );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
2021-07-07 13:51:50 +00:00
|
|
|
const serialization = template.serialize();
|
|
|
|
assert.deepEqual( serialization.template.params, {
|
|
|
|
foo: { wt: 'Foo value' },
|
|
|
|
empty: { wt: '' }
|
|
|
|
} );
|
2020-11-16 18:43:00 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// T101075
|
2021-04-30 09:33:22 +00:00
|
|
|
QUnit.test( 'serialize without empty parameter not present in original parameter set', ( assert ) => {
|
2021-07-07 13:51:50 +00:00
|
|
|
const template = newTemplateModel(),
|
|
|
|
parameterWithoutValue = new ve.dm.MWParameterModel( template, 'new_empty', '' );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
2021-07-07 13:51:50 +00:00
|
|
|
template.addParameter( parameterWithoutValue );
|
2020-11-16 18:43:00 +00:00
|
|
|
|
2021-07-07 13:51:50 +00:00
|
|
|
const serialization = template.serialize();
|
|
|
|
assert.deepEqual( serialization.template.params, {
|
|
|
|
foo: { wt: 'Foo value' },
|
|
|
|
bar: { wt: 'Bar value' },
|
|
|
|
empty: { wt: '' }
|
|
|
|
} );
|
2020-11-16 18:43:00 +00:00
|
|
|
} );
|
2021-06-18 11:01:39 +00:00
|
|
|
|
2021-06-23 10:17:42 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'serialize with explicit parameter order',
|
|
|
|
spec: {
|
2021-06-29 14:29:11 +00:00
|
|
|
params: {
|
|
|
|
foo: {},
|
|
|
|
empty: {},
|
|
|
|
bar: {}
|
|
|
|
},
|
2021-06-23 10:17:42 +00:00
|
|
|
paramOrder: [ 'bar', 'foo', 'empty' ]
|
|
|
|
},
|
|
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'serialize with no parameter order',
|
|
|
|
spec: {
|
2021-06-29 14:29:11 +00:00
|
|
|
params: {
|
|
|
|
foo: {},
|
|
|
|
empty: {},
|
|
|
|
bar: {}
|
|
|
|
}
|
2021-06-23 10:17:42 +00:00
|
|
|
},
|
|
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'serialize with aliases',
|
|
|
|
spec: {
|
2021-06-29 14:29:11 +00:00
|
|
|
params: {
|
|
|
|
foo: {},
|
|
|
|
empty: {},
|
|
|
|
hasaliases: { aliases: [ 'bar', 'baz' ] }
|
2021-06-23 10:17:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'serialize with unknown params',
|
|
|
|
spec: {
|
2021-06-29 14:29:11 +00:00
|
|
|
params: {
|
|
|
|
bar: {}
|
|
|
|
}
|
2021-06-23 10:17:42 +00:00
|
|
|
},
|
|
|
|
expected: [ 'foo', 'bar', 'empty' ]
|
|
|
|
}
|
|
|
|
].forEach( ( { name, spec, expected } ) => {
|
|
|
|
QUnit.test( name, ( assert ) => {
|
2021-07-07 13:51:50 +00:00
|
|
|
const template = newTemplateModel();
|
2021-06-23 10:17:42 +00:00
|
|
|
|
2021-07-07 13:51:50 +00:00
|
|
|
template.getSpec().setTemplateData( spec );
|
2021-06-23 10:17:42 +00:00
|
|
|
|
2021-07-07 13:51:50 +00:00
|
|
|
const serialization = template.serialize();
|
|
|
|
assert.deepEqual( Object.keys( serialization.template.params ), expected );
|
2021-06-23 10:17:42 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2021-06-18 11:01:39 +00:00
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'no spec retrieved',
|
|
|
|
spec: null,
|
|
|
|
expected: [
|
|
|
|
'bar',
|
|
|
|
'empty',
|
2021-07-07 13:51:50 +00:00
|
|
|
'foo',
|
|
|
|
''
|
2021-06-18 11:01:39 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'empty spec',
|
|
|
|
spec: {},
|
|
|
|
expected: [
|
|
|
|
'bar',
|
|
|
|
'empty',
|
2021-07-07 13:51:50 +00:00
|
|
|
'foo',
|
|
|
|
''
|
2021-06-18 11:01:39 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with explicit paramOrder and all known params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
bar: {},
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
foo: {}
|
|
|
|
},
|
2021-06-18 08:44:32 +00:00
|
|
|
paramOrder: [ 'foo', 'empty', 'bar', 'unused' ]
|
2021-06-18 11:01:39 +00:00
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'foo',
|
|
|
|
'empty',
|
2021-07-07 13:51:50 +00:00
|
|
|
'bar',
|
|
|
|
''
|
2021-06-18 11:01:39 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with explicit paramOrder and some unknown params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
foo: {}
|
|
|
|
},
|
2021-06-18 08:44:32 +00:00
|
|
|
paramOrder: [ 'foo', 'empty', 'unused' ]
|
2021-06-18 11:01:39 +00:00
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'foo',
|
|
|
|
'empty',
|
2021-07-07 13:51:50 +00:00
|
|
|
'bar',
|
|
|
|
''
|
2021-06-18 11:01:39 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with explicit paramOrder but all unknown params',
|
|
|
|
spec: {
|
|
|
|
params: {},
|
|
|
|
paramOrder: []
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'bar',
|
|
|
|
'empty',
|
2021-07-07 13:51:50 +00:00
|
|
|
'foo',
|
|
|
|
''
|
2021-06-18 11:01:39 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with no paramOrder, all known params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
bar: {},
|
|
|
|
foo: {},
|
|
|
|
unused: {},
|
|
|
|
empty: {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'bar',
|
2021-06-23 14:31:07 +00:00
|
|
|
'foo',
|
2021-07-07 13:51:50 +00:00
|
|
|
'empty',
|
|
|
|
''
|
2021-06-18 11:01:39 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with no paramOrder and some unknown params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
foo: {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'empty',
|
2021-06-23 14:31:07 +00:00
|
|
|
'foo',
|
2021-07-07 13:51:50 +00:00
|
|
|
'bar',
|
|
|
|
''
|
2021-06-18 11:01:39 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
].forEach( ( { name, spec, expected } ) => {
|
|
|
|
QUnit.test( 'getOrderedParameterNames: ' + name, ( assert ) => {
|
2021-07-07 13:51:50 +00:00
|
|
|
const template = newTemplateModel();
|
|
|
|
|
|
|
|
if ( spec ) {
|
|
|
|
template.getSpec().setTemplateData( spec );
|
2021-06-18 11:01:39 +00:00
|
|
|
}
|
2021-07-07 13:51:50 +00:00
|
|
|
|
|
|
|
assert.deepEqual( template.getOrderedParameterNames(), expected );
|
2021-06-18 11:01:39 +00:00
|
|
|
} );
|
|
|
|
} );
|
2021-06-18 08:44:32 +00:00
|
|
|
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'no spec retrieved',
|
|
|
|
spec: null,
|
|
|
|
expected: [
|
|
|
|
'bar',
|
|
|
|
'empty',
|
2021-07-07 13:51:50 +00:00
|
|
|
'foo',
|
|
|
|
''
|
2021-06-18 08:44:32 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with explicit paramOrder and all known params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
bar: {},
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
foo: {}
|
|
|
|
},
|
|
|
|
paramOrder: [ 'foo', 'empty', 'unused', 'bar' ]
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'foo',
|
|
|
|
'empty',
|
|
|
|
'unused',
|
2021-07-07 13:51:50 +00:00
|
|
|
'bar',
|
|
|
|
''
|
2021-06-18 08:44:32 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with explicit paramOrder and some unknown params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
foo: {}
|
|
|
|
},
|
|
|
|
paramOrder: [ 'foo', 'empty', 'unused' ]
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'foo',
|
|
|
|
'empty',
|
|
|
|
'unused',
|
2021-07-07 13:51:50 +00:00
|
|
|
'bar',
|
|
|
|
''
|
2021-06-18 08:44:32 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with explicit paramOrder but all unknown params',
|
|
|
|
spec: {
|
|
|
|
params: {},
|
|
|
|
paramOrder: []
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'bar',
|
|
|
|
'empty',
|
2021-07-07 13:51:50 +00:00
|
|
|
'foo',
|
|
|
|
''
|
2021-06-18 08:44:32 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with no paramOrder, all known params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
bar: {},
|
|
|
|
foo: {},
|
|
|
|
unused: {},
|
|
|
|
empty: {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'bar',
|
2021-06-23 14:31:07 +00:00
|
|
|
'foo',
|
|
|
|
'unused',
|
2021-07-07 13:51:50 +00:00
|
|
|
'empty',
|
|
|
|
''
|
2021-06-18 08:44:32 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with no paramOrder and some unknown params',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
foo: {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'empty',
|
2021-06-23 14:31:07 +00:00
|
|
|
'unused',
|
|
|
|
'foo',
|
2021-07-07 13:51:50 +00:00
|
|
|
'bar',
|
|
|
|
''
|
2021-06-18 08:44:32 +00:00
|
|
|
]
|
2021-06-28 10:37:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'spec with explicit paramOrder and aliases',
|
|
|
|
spec: {
|
|
|
|
params: {
|
|
|
|
empty: {},
|
|
|
|
unused: {},
|
|
|
|
hasalias: {
|
|
|
|
aliases: [ 'bar', 'baz' ]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
paramOrder: [ 'hasalias', 'empty', 'unused' ]
|
|
|
|
},
|
|
|
|
expected: [
|
|
|
|
'bar',
|
|
|
|
'empty',
|
|
|
|
'unused',
|
2021-07-07 13:51:50 +00:00
|
|
|
'foo',
|
|
|
|
''
|
2021-06-28 10:37:01 +00:00
|
|
|
]
|
2021-06-18 08:44:32 +00:00
|
|
|
}
|
|
|
|
].forEach( ( { name, spec, expected } ) => {
|
|
|
|
QUnit.test( 'getAllParametersOrdered: ' + name, ( assert ) => {
|
2021-07-07 13:51:50 +00:00
|
|
|
const template = newTemplateModel();
|
|
|
|
|
|
|
|
if ( spec ) {
|
|
|
|
template.getSpec().setTemplateData( spec );
|
2021-06-18 08:44:32 +00:00
|
|
|
}
|
2021-07-07 13:51:50 +00:00
|
|
|
|
|
|
|
assert.deepEqual( template.getAllParametersOrdered(), expected );
|
2021-06-18 08:44:32 +00:00
|
|
|
} );
|
|
|
|
} );
|
2020-11-16 18:43:00 +00:00
|
|
|
}() );
|