| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
{ |
| 9 |
const transclusionData = { |
| 10 |
params: { |
| 11 |
foo: { wt: 'Foo value' }, |
| 12 |
bar: { wt: 'Bar value' }, |
| 13 |
Bar: { wt: 'Bar value' }, |
| 14 |
empty: { wt: '' }, |
| 15 |
'': { wt: '' } |
| 16 |
}, |
| 17 |
target: { |
| 18 |
href: './Template:Test', |
| 19 |
wt: 'Test' |
| 20 |
} |
| 21 |
}; |
| 22 |
|
| 23 |
QUnit.module( 've.dm.MWTemplateModel' ); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
const newTemplateModel = function () { |
| 31 |
const doc = ve.dm.Document.static.newBlankDocument(), |
| 32 |
transclusion = new ve.dm.MWTransclusionModel( doc ), |
| 33 |
clonedTransclusionData = ve.extendObject( {}, transclusionData ); |
| 34 |
|
| 35 |
return ve.dm.MWTemplateModel.newFromData( transclusion, clonedTransclusionData ); |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
[ |
| 41 |
[ undefined, null ], |
| 42 |
[ '', null ], |
| 43 |
[ 'no_prefix', 'no prefix' ], |
| 44 |
[ '/unexpected_prefix', '/unexpected prefix' ], |
| 45 |
[ './Template:%C3%9Cnicode%5Fexample/subpage', 'Template:Ünicode example/subpage' ], |
| 46 |
[ './Template:Possibly_invalid%5B%5D', 'Template:Possibly invalid[]' ] |
| 47 |
].forEach( ( [ href, expected ] ) => QUnit.test( 'getTitle: ' + href, ( assert ) => { |
| 48 |
const transclusion = { nextUniquePartId: () => '' }, |
| 49 |
template = new ve.dm.MWTemplateModel( transclusion, { href } ); |
| 50 |
assert.strictEqual( template.getTitle(), expected ); |
| 51 |
} ) |
| 52 |
); |
| 53 |
|
| 54 |
QUnit.test( 'hasParameter', ( assert ) => { |
| 55 |
const template = newTemplateModel(); |
| 56 |
|
| 57 |
|
| 58 |
assert.strictEqual( template.hasParameter( 'bar' ), true ); |
| 59 |
assert.strictEqual( template.hasParameter( 'resolved-bar' ), false ); |
| 60 |
assert.strictEqual( template.hasParameter( 'alternative-bar' ), false ); |
| 61 |
|
| 62 |
template.getSpec().setTemplateData( { params: { |
| 63 |
'resolved-bar': { aliases: [ 'bar', 'alternative-bar' ] } |
| 64 |
} } ); |
| 65 |
|
| 66 |
|
| 67 |
assert.strictEqual( template.hasParameter( 'bar' ), true ); |
| 68 |
assert.strictEqual( template.hasParameter( 'resolved-bar' ), true ); |
| 69 |
assert.strictEqual( template.hasParameter( 'alternative-bar' ), true ); |
| 70 |
} ); |
| 71 |
|
| 72 |
QUnit.test( 'getOriginalParameterName', ( assert ) => { |
| 73 |
const template = newTemplateModel(); |
| 74 |
template.addParameter( new ve.dm.MWParameterModel( template, 'p1' ) ); |
| 75 |
template.addParameter( new ve.dm.MWParameterModel( template, 'p2-alias' ) ); |
| 76 |
|
| 77 |
|
| 78 |
assert.strictEqual( template.getOriginalParameterName( 'p1' ), 'p1' ); |
| 79 |
assert.strictEqual( template.getOriginalParameterName( 'p1-alias' ), 'p1-alias' ); |
| 80 |
assert.strictEqual( template.getOriginalParameterName( 'p2' ), 'p2' ); |
| 81 |
assert.strictEqual( template.getOriginalParameterName( 'p2-alias' ), 'p2-alias' ); |
| 82 |
assert.strictEqual( template.getOriginalParameterName( 'p3' ), 'p3' ); |
| 83 |
assert.strictEqual( template.getOriginalParameterName( 'p3-alias' ), 'p3-alias' ); |
| 84 |
|
| 85 |
template.getSpec().setTemplateData( { params: { |
| 86 |
p1: { aliases: [ 'p1-alias' ] }, |
| 87 |
p2: { aliases: [ 'p2-alias' ] }, |
| 88 |
p3: { aliases: [ 'p3-alias' ] } |
| 89 |
} } ); |
| 90 |
|
| 91 |
assert.strictEqual( template.getOriginalParameterName( 'p1' ), 'p1' ); |
| 92 |
assert.strictEqual( template.getOriginalParameterName( 'p1-alias' ), 'p1' ); |
| 93 |
assert.strictEqual( template.getOriginalParameterName( 'p2' ), 'p2-alias' ); |
| 94 |
assert.strictEqual( template.getOriginalParameterName( 'p2-alias' ), 'p2-alias' ); |
| 95 |
assert.strictEqual( template.getOriginalParameterName( 'p3' ), 'p3' ); |
| 96 |
assert.strictEqual( template.getOriginalParameterName( 'p3-alias' ), 'p3' ); |
| 97 |
} ); |
| 98 |
|
| 99 |
QUnit.test( 'serialize input parameters', ( assert ) => { |
| 100 |
const template = newTemplateModel(); |
| 101 |
|
| 102 |
const serialization = template.serialize(); |
| 103 |
assert.deepEqual( serialization, { template: { |
| 104 |
params: { |
| 105 |
foo: { wt: 'Foo value' }, |
| 106 |
bar: { wt: 'Bar value' }, |
| 107 |
Bar: { wt: 'Bar value' }, |
| 108 |
empty: { wt: '' } |
| 109 |
}, |
| 110 |
target: { href: './Template:Test', wt: 'Test' } |
| 111 |
} } ); |
| 112 |
} ); |
| 113 |
|
| 114 |
QUnit.test( 'serialize changed input parameters', ( assert ) => { |
| 115 |
const template = newTemplateModel(), |
| 116 |
newParameter = new ve.dm.MWParameterModel( template, 'baz', 'Baz value' ); |
| 117 |
|
| 118 |
template.addParameter( newParameter ); |
| 119 |
|
| 120 |
const serialization = template.serialize(); |
| 121 |
assert.deepEqual( serialization.template.params.baz, { wt: 'Baz value' } ); |
| 122 |
} ); |
| 123 |
|
| 124 |
|
| 125 |
QUnit.test( 'serialize after parameter was removed', ( assert ) => { |
| 126 |
const template = newTemplateModel(), |
| 127 |
existingParameter = template.getParameter( 'bar' ); |
| 128 |
|
| 129 |
template.removeParameter( existingParameter ); |
| 130 |
|
| 131 |
const serialization = template.serialize(); |
| 132 |
assert.deepEqual( serialization.template.params, { |
| 133 |
foo: { wt: 'Foo value' }, |
| 134 |
Bar: { wt: 'Bar value' }, |
| 135 |
empty: { wt: '' } |
| 136 |
} ); |
| 137 |
} ); |
| 138 |
|
| 139 |
|
| 140 |
QUnit.test( 'serialize without empty parameter not present in original parameter set', ( assert ) => { |
| 141 |
const template = newTemplateModel(), |
| 142 |
parameterWithoutValue = new ve.dm.MWParameterModel( template, 'new_empty', '' ); |
| 143 |
|
| 144 |
template.addParameter( parameterWithoutValue ); |
| 145 |
|
| 146 |
const serialization = template.serialize(); |
| 147 |
assert.deepEqual( serialization.template.params, { |
| 148 |
foo: { wt: 'Foo value' }, |
| 149 |
bar: { wt: 'Bar value' }, |
| 150 |
Bar: { wt: 'Bar value' }, |
| 151 |
empty: { wt: '' } |
| 152 |
} ); |
| 153 |
} ); |
| 154 |
|
| 155 |
[ |
| 156 |
{ |
| 157 |
name: 'serialize with explicit parameter order', |
| 158 |
spec: { |
| 159 |
params: { |
| 160 |
foo: {}, |
| 161 |
empty: {}, |
| 162 |
bar: {}, |
| 163 |
Bar: {} |
| 164 |
}, |
| 165 |
paramOrder: [ 'bar', 'foo', 'empty', 'Bar' ] |
| 166 |
}, |
| 167 |
expected: [ 'foo', 'bar', 'Bar', 'empty' ] |
| 168 |
}, |
| 169 |
{ |
| 170 |
name: 'serialize with no parameter order', |
| 171 |
spec: { |
| 172 |
params: { |
| 173 |
foo: {}, |
| 174 |
empty: {}, |
| 175 |
bar: {}, |
| 176 |
Bar: {} |
| 177 |
} |
| 178 |
}, |
| 179 |
expected: [ 'foo', 'bar', 'Bar', 'empty' ] |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: 'serialize with aliases', |
| 183 |
spec: { |
| 184 |
params: { |
| 185 |
foo: {}, |
| 186 |
Bar: {}, |
| 187 |
empty: {}, |
| 188 |
hasaliases: { aliases: [ 'bar', 'baz' ] } |
| 189 |
} |
| 190 |
}, |
| 191 |
expected: [ 'foo', 'bar', 'Bar', 'empty' ] |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: 'serialize with unknown params', |
| 195 |
spec: { |
| 196 |
params: { |
| 197 |
bar: {} |
| 198 |
} |
| 199 |
}, |
| 200 |
expected: [ 'foo', 'bar', 'Bar', 'empty' ] |
| 201 |
} |
| 202 |
].forEach( ( { name, spec, expected } ) => QUnit.test( name, ( assert ) => { |
| 203 |
const template = newTemplateModel(); |
| 204 |
|
| 205 |
template.getSpec().setTemplateData( spec ); |
| 206 |
|
| 207 |
const serialization = template.serialize(); |
| 208 |
assert.deepEqual( Object.keys( serialization.template.params ), expected ); |
| 209 |
} ) |
| 210 |
); |
| 211 |
|
| 212 |
[ |
| 213 |
{ |
| 214 |
name: 'no spec retrieved', |
| 215 |
spec: null, |
| 216 |
expected: [ |
| 217 |
'bar', |
| 218 |
'Bar', |
| 219 |
'empty', |
| 220 |
'foo', |
| 221 |
'' |
| 222 |
] |
| 223 |
}, |
| 224 |
{ |
| 225 |
name: 'empty spec', |
| 226 |
spec: {}, |
| 227 |
expected: [ |
| 228 |
'bar', |
| 229 |
'Bar', |
| 230 |
'empty', |
| 231 |
'foo', |
| 232 |
'' |
| 233 |
] |
| 234 |
}, |
| 235 |
{ |
| 236 |
name: 'spec with explicit paramOrder and all known params', |
| 237 |
spec: { |
| 238 |
params: { |
| 239 |
bar: {}, |
| 240 |
Bar: {}, |
| 241 |
empty: {}, |
| 242 |
unused: {}, |
| 243 |
foo: {} |
| 244 |
}, |
| 245 |
paramOrder: [ 'foo', 'Bar', 'empty', 'bar', 'unused' ] |
| 246 |
}, |
| 247 |
expected: [ |
| 248 |
'foo', |
| 249 |
'Bar', |
| 250 |
'empty', |
| 251 |
'bar', |
| 252 |
'' |
| 253 |
] |
| 254 |
}, |
| 255 |
{ |
| 256 |
name: 'spec with explicit paramOrder and some unknown params', |
| 257 |
spec: { |
| 258 |
params: { |
| 259 |
empty: {}, |
| 260 |
unused: {}, |
| 261 |
foo: {} |
| 262 |
}, |
| 263 |
paramOrder: [ 'foo', 'empty', 'unused' ] |
| 264 |
}, |
| 265 |
expected: [ |
| 266 |
'foo', |
| 267 |
'empty', |
| 268 |
'bar', |
| 269 |
'Bar', |
| 270 |
'' |
| 271 |
] |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: 'spec with explicit paramOrder but all unknown params', |
| 275 |
spec: { |
| 276 |
params: {}, |
| 277 |
paramOrder: [] |
| 278 |
}, |
| 279 |
expected: [ |
| 280 |
'bar', |
| 281 |
'Bar', |
| 282 |
'empty', |
| 283 |
'foo', |
| 284 |
'' |
| 285 |
] |
| 286 |
}, |
| 287 |
{ |
| 288 |
name: 'spec with no paramOrder, all known params', |
| 289 |
spec: { |
| 290 |
params: { |
| 291 |
bar: {}, |
| 292 |
Bar: {}, |
| 293 |
foo: {}, |
| 294 |
unused: {}, |
| 295 |
empty: {} |
| 296 |
} |
| 297 |
}, |
| 298 |
expected: [ |
| 299 |
'bar', |
| 300 |
'Bar', |
| 301 |
'foo', |
| 302 |
'empty', |
| 303 |
'' |
| 304 |
] |
| 305 |
}, |
| 306 |
{ |
| 307 |
name: 'spec with no paramOrder and some unknown params', |
| 308 |
spec: { |
| 309 |
params: { |
| 310 |
empty: {}, |
| 311 |
unused: {}, |
| 312 |
foo: {} |
| 313 |
} |
| 314 |
}, |
| 315 |
expected: [ |
| 316 |
'empty', |
| 317 |
'foo', |
| 318 |
'bar', |
| 319 |
'Bar', |
| 320 |
'' |
| 321 |
] |
| 322 |
} |
| 323 |
].forEach( ( { name, spec, expected } ) => QUnit.test( 'getOrderedParameterNames: ' + name, ( assert ) => { |
| 324 |
const template = newTemplateModel(); |
| 325 |
|
| 326 |
if ( spec ) { |
| 327 |
template.getSpec().setTemplateData( spec ); |
| 328 |
} |
| 329 |
|
| 330 |
assert.deepEqual( template.getOrderedParameterNames(), expected ); |
| 331 |
} ) |
| 332 |
); |
| 333 |
|
| 334 |
[ |
| 335 |
{ |
| 336 |
name: 'no spec retrieved', |
| 337 |
spec: null, |
| 338 |
expected: [ |
| 339 |
'bar', |
| 340 |
'Bar', |
| 341 |
'empty', |
| 342 |
'foo', |
| 343 |
'' |
| 344 |
] |
| 345 |
}, |
| 346 |
{ |
| 347 |
name: 'spec with explicit paramOrder and all known params', |
| 348 |
spec: { |
| 349 |
params: { |
| 350 |
bar: {}, |
| 351 |
Bar: {}, |
| 352 |
empty: {}, |
| 353 |
unused: {}, |
| 354 |
foo: {} |
| 355 |
}, |
| 356 |
paramOrder: [ 'foo', 'Bar', 'empty', 'unused', 'bar' ] |
| 357 |
}, |
| 358 |
expected: [ |
| 359 |
'foo', |
| 360 |
'Bar', |
| 361 |
'empty', |
| 362 |
'unused', |
| 363 |
'bar', |
| 364 |
'' |
| 365 |
] |
| 366 |
}, |
| 367 |
{ |
| 368 |
name: 'spec with explicit paramOrder and some unknown params', |
| 369 |
spec: { |
| 370 |
params: { |
| 371 |
empty: {}, |
| 372 |
unused: {}, |
| 373 |
foo: {} |
| 374 |
}, |
| 375 |
paramOrder: [ 'foo', 'empty', 'unused' ] |
| 376 |
}, |
| 377 |
expected: [ |
| 378 |
'foo', |
| 379 |
'empty', |
| 380 |
'unused', |
| 381 |
'bar', |
| 382 |
'Bar', |
| 383 |
'' |
| 384 |
] |
| 385 |
}, |
| 386 |
{ |
| 387 |
name: 'spec with explicit paramOrder but all unknown params', |
| 388 |
spec: { |
| 389 |
params: {}, |
| 390 |
paramOrder: [] |
| 391 |
}, |
| 392 |
expected: [ |
| 393 |
'bar', |
| 394 |
'Bar', |
| 395 |
'empty', |
| 396 |
'foo', |
| 397 |
'' |
| 398 |
] |
| 399 |
}, |
| 400 |
{ |
| 401 |
name: 'spec with no paramOrder, all known params', |
| 402 |
spec: { |
| 403 |
params: { |
| 404 |
bar: {}, |
| 405 |
Bar: {}, |
| 406 |
foo: {}, |
| 407 |
unused: {}, |
| 408 |
empty: {} |
| 409 |
} |
| 410 |
}, |
| 411 |
expected: [ |
| 412 |
'bar', |
| 413 |
'Bar', |
| 414 |
'foo', |
| 415 |
'unused', |
| 416 |
'empty', |
| 417 |
'' |
| 418 |
] |
| 419 |
}, |
| 420 |
{ |
| 421 |
name: 'spec with no paramOrder and some unknown params', |
| 422 |
spec: { |
| 423 |
params: { |
| 424 |
empty: {}, |
| 425 |
unused: {}, |
| 426 |
foo: {} |
| 427 |
} |
| 428 |
}, |
| 429 |
expected: [ |
| 430 |
'empty', |
| 431 |
'unused', |
| 432 |
'foo', |
| 433 |
'bar', |
| 434 |
'Bar', |
| 435 |
'' |
| 436 |
] |
| 437 |
}, |
| 438 |
{ |
| 439 |
name: 'spec with explicit paramOrder and aliases', |
| 440 |
spec: { |
| 441 |
params: { |
| 442 |
empty: {}, |
| 443 |
unused: {}, |
| 444 |
hasalias: { |
| 445 |
aliases: [ 'bar', 'baz' ] |
| 446 |
} |
| 447 |
}, |
| 448 |
paramOrder: [ 'hasalias', 'empty', 'unused' ] |
| 449 |
}, |
| 450 |
expected: [ |
| 451 |
'bar', |
| 452 |
'empty', |
| 453 |
'unused', |
| 454 |
'Bar', |
| 455 |
'foo', |
| 456 |
'' |
| 457 |
] |
| 458 |
} |
| 459 |
].forEach( ( { name, spec, expected } ) => QUnit.test( 'getAllParametersOrdered: ' + name, ( assert ) => { |
| 460 |
const template = newTemplateModel(); |
| 461 |
|
| 462 |
if ( spec ) { |
| 463 |
template.getSpec().setTemplateData( spec ); |
| 464 |
} |
| 465 |
|
| 466 |
assert.deepEqual( template.getAllParametersOrdered(), expected ); |
| 467 |
} ) |
| 468 |
); |
| 469 |
|
| 470 |
QUnit.test( 'same documented parameter used with different aliases', ( assert ) => { |
| 471 |
const transclusion = new ve.dm.MWTransclusionModel(); |
| 472 |
const template = ve.dm.MWTemplateModel.newFromData( transclusion, { |
| 473 |
target: {}, |
| 474 |
params: { d: {}, c: {}, b: {}, a: {} } |
| 475 |
} ); |
| 476 |
template.getSpec().setTemplateData( { |
| 477 |
params: { a: { aliases: [ 'b', 'c' ] }, e: {} } |
| 478 |
} ); |
| 479 |
|
| 480 |
assert.deepEqual( template.getOrderedParameterNames(), [ 'a', 'c', 'b', 'd' ] ); |
| 481 |
assert.deepEqual( template.getAllParametersOrdered(), [ 'a', 'c', 'b', 'e', 'd' ] ); |
| 482 |
} ); |
| 483 |
|
| 484 |
[ |
| 485 |
[ 'a', 'b', 'Template:A', 'prefers .wt when it is a valid title' ], |
| 486 |
[ '{{a}}', 'subst:b', 'subst:b', 'falls back to unmodified getTitle' ], |
| 487 |
[ 'subst:a', 'b', 'Template:A', 'strips subst:' ], |
| 488 |
[ 'safesubst:a', 'b', 'Template:A', 'strips safesubst:' ], |
| 489 |
[ ' SUBST: a\n', 'b', 'Template:A', 'ignores capitalization and whitespace' ], |
| 490 |
[ 'subst :a', 'b', 'Template:Subst :a', 'leaves bad whitespace untouched' ], |
| 491 |
[ 'int:a', 'b', 'Template:Int:a', 'leaves other prefixes untouched' ] |
| 492 |
].forEach( ( [ wt, href, expected, message ] ) => QUnit.test( 'getTemplateDataQueryTitle: ' + message, ( assert ) => { |
| 493 |
const transclusion = { nextUniquePartId: () => '' }, |
| 494 |
data = { target: { wt, href } }, |
| 495 |
model = ve.dm.MWTemplateModel.newFromData( transclusion, data ); |
| 496 |
|
| 497 |
assert.strictEqual( model.getTemplateDataQueryTitle(), expected ); |
| 498 |
} ) |
| 499 |
); |
| 500 |
|
| 501 |
[ |
| 502 |
[ {}, false, 'no parameters' ], |
| 503 |
[ { p1: {}, p2: { wt: 'foo' } }, true, 'multiple parameters' ], |
| 504 |
[ { p1: {} }, false, 'undefined' ], |
| 505 |
[ { p1: { wt: null } }, false, 'null' ], |
| 506 |
[ { p1: { wt: '' } }, false, 'empty string' ], |
| 507 |
[ { p1: { wt: ' ' } }, true, 'space' ], |
| 508 |
[ { p1: { wt: '0' } }, true, '0' ], |
| 509 |
[ { p1: { wt: '\nfoo' } }, true, 'newline' ] |
| 510 |
].forEach( ( [ params, expected, message ] ) => QUnit.test( 'containsValuableData: ' + message, ( assert ) => { |
| 511 |
const transclusion = { nextUniquePartId: () => '' }, |
| 512 |
data = { target: {}, params }, |
| 513 |
model = ve.dm.MWTemplateModel.newFromData( transclusion, data ); |
| 514 |
|
| 515 |
assert.strictEqual( model.containsValuableData(), expected ); |
| 516 |
} ) |
| 517 |
); |
| 518 |
|
| 519 |
} |