| 1 |
local testframework = require 'Module:TestFramework' |
| 2 |
|
| 3 |
local title, title_copy, title2, title3, title4, title5, title6u, title6s, title4p |
| 4 |
if mw.title.testPageId then |
| 5 |
title = mw.title.getCurrentTitle() |
| 6 |
title_copy = mw.title.getCurrentTitle() |
| 7 |
title2 = mw.title.new( 'Module:TestFramework' ) |
| 8 |
title3 = mw.title.new( 'interwikiprefix:Module:TestFramework' ) |
| 9 |
title4 = mw.title.new( 'Talk:Has/A/Subpage' ) |
| 10 |
title5 = mw.title.new( 'Not/A/Subpage' ) |
| 11 |
title4.fragment = 'frag' |
| 12 |
|
| 13 |
title4p = mw.title.new( 'Talk:Has/A' ) |
| 14 |
|
| 15 |
title6u = mw.title.new( 'Module_talk:Test_Framework' ) |
| 16 |
title6u.fragment = '__frag__frag__' |
| 17 |
|
| 18 |
title6s = mw.title.new( 'Module talk:Test Framework' ) |
| 19 |
title6s.fragment = ' frag frag ' |
| 20 |
end |
| 21 |
|
| 22 |
local function prop_foreach( prop ) |
| 23 |
return title[prop], title2[prop], title3[prop], title4[prop], title5[prop], title6u[prop], title6s[prop] |
| 24 |
end |
| 25 |
|
| 26 |
local function func_foreach( func, ... ) |
| 27 |
return title[func]( title, ... ), |
| 28 |
title2[func]( title2, ... ), |
| 29 |
title3[func]( title3, ... ), |
| 30 |
title4[func]( title4, ... ), |
| 31 |
title5[func]( title5, ... ), |
| 32 |
title6u[func]( title6u, ... ), |
| 33 |
title6s[func]( title6s, ... ) |
| 34 |
end |
| 35 |
|
| 36 |
local function identity( ... ) |
| 37 |
return ... |
| 38 |
end |
| 39 |
|
| 40 |
local function test_space_normalization( s ) |
| 41 |
local title = mw.title.new( s ) |
| 42 |
return tostring( title ), tostring( title.fragment ) |
| 43 |
end |
| 44 |
|
| 45 |
local function test_expensive_10() |
| 46 |
for i = 1, 10 do |
| 47 |
local _ = mw.title.new( tostring( i ) ).id |
| 48 |
end |
| 49 |
return 'did not error' |
| 50 |
end |
| 51 |
|
| 52 |
local function test_expensive_11() |
| 53 |
for i = 1, 11 do |
| 54 |
local _ = mw.title.new( tostring( i ) ).id |
| 55 |
end |
| 56 |
return 'did not error' |
| 57 |
end |
| 58 |
|
| 59 |
local function test_expensive_cached() |
| 60 |
for i = 1, 100 do |
| 61 |
local _ = mw.title.new( 'Title' ).id |
| 62 |
end |
| 63 |
return 'did not error' |
| 64 |
end |
| 65 |
|
| 66 |
local function test_inexpensive() |
| 67 |
for i = 1, 100 do |
| 68 |
local _ = mw.title.new( 'Title' ).prefixedText |
| 69 |
end |
| 70 |
return 'did not error' |
| 71 |
end |
| 72 |
|
| 73 |
local function test_getContent() |
| 74 |
return mw.title.new( 'ScribuntoTestPage' ):getContent(), |
| 75 |
mw.title.new( 'ScribuntoTestNonExistingPage' ):getContent() |
| 76 |
end |
| 77 |
|
| 78 |
local function test_content() |
| 79 |
return mw.title.new( 'ScribuntoTestPage' ).content, |
| 80 |
mw.title.new( 'ScribuntoTestNonExistingPage' ).content |
| 81 |
end |
| 82 |
|
| 83 |
local function test_redirectTarget() |
| 84 |
local targets = {} |
| 85 |
local titles = { |
| 86 |
'ScribuntoTestRedirect', |
| 87 |
'ScribuntoTestNonRedirect', |
| 88 |
'ScribuntoTestNonExistingPage' |
| 89 |
} |
| 90 |
for _, title in ipairs( titles ) do |
| 91 |
local target = mw.title.new( title ).redirectTarget |
| 92 |
if title.prefixedText ~= nil then |
| 93 |
target = title.prefixedText |
| 94 |
end |
| 95 |
table.insert( targets, target ) |
| 96 |
end |
| 97 |
return unpack( targets ) |
| 98 |
end |
| 99 |
|
| 100 |
local function test_getCurrentTitle_fragment() |
| 101 |
mw.title.getCurrentTitle().fragment = 'bad' |
| 102 |
return mw.title.getCurrentTitle().fragment |
| 103 |
end |
| 104 |
|
| 105 |
local function test_pageLang() |
| 106 |
local l = mw.title.getCurrentTitle().pageLang |
| 107 |
if l:getCode() ~= mw.language.getContentLanguage():getCode() then |
| 108 |
return 'error' |
| 109 |
end |
| 110 |
return 'did not error' |
| 111 |
end |
| 112 |
|
| 113 |
|
| 114 |
local tests = { |
| 115 |
{ name = 'tostring', func = identity, type = 'ToString', |
| 116 |
args = { title, title2, title3, title4, title5, title6u, title6s }, |
| 117 |
expect = { |
| 118 |
'Main Page', 'Module:TestFramework', 'interwikiprefix:Module:TestFramework', |
| 119 |
'Talk:Has/A/Subpage', 'Not/A/Subpage', |
| 120 |
'Module talk:Test Framework', 'Module talk:Test Framework' |
| 121 |
} |
| 122 |
}, |
| 123 |
|
| 124 |
{ name = 'title.equal', func = mw.title.equals, |
| 125 |
args = { title, title }, |
| 126 |
expect = { true } |
| 127 |
}, |
| 128 |
{ name = 'title.equal (2)', func = mw.title.equals, |
| 129 |
args = { title, title_copy }, |
| 130 |
expect = { true } |
| 131 |
}, |
| 132 |
{ name = 'title.equal (3)', func = mw.title.equals, |
| 133 |
args = { title, title2 }, |
| 134 |
expect = { false } |
| 135 |
}, |
| 136 |
{ name = '==', func = function () |
| 137 |
return rawequal( title, title_copy ), title == title, title == title_copy, title == title2 |
| 138 |
end, |
| 139 |
expect = { false, true, true, false } |
| 140 |
}, |
| 141 |
|
| 142 |
{ name = 'title.compare', func = mw.title.compare, |
| 143 |
args = { title, title }, |
| 144 |
expect = { 0 } |
| 145 |
}, |
| 146 |
{ name = 'title.compare (2)', func = mw.title.compare, |
| 147 |
args = { title, title_copy }, |
| 148 |
expect = { 0 } |
| 149 |
}, |
| 150 |
{ name = 'title.compare (3)', func = mw.title.compare, |
| 151 |
args = { title, title2 }, |
| 152 |
expect = { -1 } |
| 153 |
}, |
| 154 |
{ name = 'title.compare (4)', func = mw.title.compare, |
| 155 |
args = { title2, title }, |
| 156 |
expect = { 1 } |
| 157 |
}, |
| 158 |
{ name = 'title.compare (5)', func = mw.title.compare, |
| 159 |
args = { title2, title3 }, |
| 160 |
expect = { -1 } |
| 161 |
}, |
| 162 |
{ name = 'title.compare (6)', func = mw.title.compare, |
| 163 |
args = { title6s, title6u }, |
| 164 |
expect = { 0 } |
| 165 |
}, |
| 166 |
{ name = '<', func = function () |
| 167 |
return title < title, title < title_copy, title < title2, title2 < title |
| 168 |
end, |
| 169 |
expect = { false, false, true, false } |
| 170 |
}, |
| 171 |
{ name = '<=', func = function () |
| 172 |
return title <= title, title <= title_copy, title <= title2, title2 <= title |
| 173 |
end, |
| 174 |
expect = { true, true, true, false } |
| 175 |
}, |
| 176 |
|
| 177 |
{ name = 'title.new with namespace', func = mw.title.new, type = 'ToString', |
| 178 |
args = { 'TestFramework', 'Module' }, |
| 179 |
expect = { 'Module:TestFramework' } |
| 180 |
}, |
| 181 |
{ name = 'title.new with namespace (2)', func = mw.title.new, type = 'ToString', |
| 182 |
args = { 'TestFramework', mw.site.namespaces.Module.id }, |
| 183 |
expect = { 'Module:TestFramework' } |
| 184 |
}, |
| 185 |
{ name = 'title.new with namespace (3)', func = mw.title.new, type = 'ToString', |
| 186 |
args = { 'Template:TestFramework', 'Module' }, |
| 187 |
expect = { 'Template:TestFramework' } |
| 188 |
}, |
| 189 |
{ name = 'title.new space normalization', func = test_space_normalization, |
| 190 |
args = { ' __ Template __ : __ Test _ Framework __ # _ frag _ frag _ ' }, |
| 191 |
expect = { 'Template:Test Framework', ' frag frag' } |
| 192 |
}, |
| 193 |
{ name = 'title.new with invalid title', func = mw.title.new, |
| 194 |
args = { '<bad title>' }, |
| 195 |
expect = { nil } |
| 196 |
}, |
| 197 |
{ name = 'title.new with nonexistent pageid', func = mw.title.new, |
| 198 |
args = { -1 }, |
| 199 |
expect = { nil } |
| 200 |
}, |
| 201 |
{ name = 'title.new with pageid 0', func = mw.title.new, |
| 202 |
args = { 0 }, |
| 203 |
expect = { nil } |
| 204 |
}, |
| 205 |
{ name = 'title.new with existing pageid', func = mw.title.new, type = 'ToString', |
| 206 |
args = { mw.title.testPageId }, |
| 207 |
expect = { 'ScribuntoTestPage' } |
| 208 |
}, |
| 209 |
|
| 210 |
{ name = 'title.makeTitle', func = mw.title.makeTitle, type = 'ToString', |
| 211 |
args = { 'Module', 'TestFramework' }, |
| 212 |
expect = { 'Module:TestFramework' } |
| 213 |
}, |
| 214 |
{ name = 'title.makeTitle (2)', func = mw.title.makeTitle, type = 'ToString', |
| 215 |
args = { mw.site.namespaces.Module.id, 'TestFramework' }, |
| 216 |
expect = { 'Module:TestFramework' } |
| 217 |
}, |
| 218 |
{ name = 'title.makeTitle (3)', func = mw.title.makeTitle, type = 'ToString', |
| 219 |
args = { mw.site.namespaces.Module.id, 'Template:TestFramework' }, |
| 220 |
expect = { 'Module:Template:TestFramework' } |
| 221 |
}, |
| 222 |
|
| 223 |
{ name = '.isLocal', func = prop_foreach, |
| 224 |
args = { 'isLocal' }, |
| 225 |
expect = { true, true, false, true, true, true, true } |
| 226 |
}, |
| 227 |
{ name = '.isTalkPage', func = prop_foreach, |
| 228 |
args = { 'isTalkPage' }, |
| 229 |
expect = { false, false, nil, true, false, true, true } |
| 230 |
}, |
| 231 |
{ name = '.isSubpage', func = prop_foreach, |
| 232 |
args = { 'isSubpage' }, |
| 233 |
expect = { false, false, false, true, false, false, false } |
| 234 |
}, |
| 235 |
{ name = '.text', func = prop_foreach, |
| 236 |
args = { 'text' }, |
| 237 |
expect = { |
| 238 |
'Main Page', 'TestFramework', 'Module:TestFramework', 'Has/A/Subpage', 'Not/A/Subpage', |
| 239 |
'Test Framework', 'Test Framework' |
| 240 |
} |
| 241 |
}, |
| 242 |
{ name = '.prefixedText', func = prop_foreach, |
| 243 |
args = { 'prefixedText' }, |
| 244 |
expect = { |
| 245 |
'Main Page', 'Module:TestFramework', 'interwikiprefix:Module:TestFramework', |
| 246 |
'Talk:Has/A/Subpage', 'Not/A/Subpage', 'Module talk:Test Framework', 'Module talk:Test Framework', |
| 247 |
} |
| 248 |
}, |
| 249 |
{ name = '.rootText', func = prop_foreach, |
| 250 |
args = { 'rootText' }, |
| 251 |
expect = { |
| 252 |
'Main Page', 'TestFramework', 'Module:TestFramework', 'Has', 'Not/A/Subpage', |
| 253 |
'Test Framework', 'Test Framework' |
| 254 |
} |
| 255 |
}, |
| 256 |
{ name = '.baseText', func = prop_foreach, |
| 257 |
args = { 'baseText' }, |
| 258 |
expect = { |
| 259 |
'Main Page', 'TestFramework', 'Module:TestFramework', 'Has/A', 'Not/A/Subpage', |
| 260 |
'Test Framework', 'Test Framework' |
| 261 |
} |
| 262 |
}, |
| 263 |
{ name = '.subpageText', func = prop_foreach, |
| 264 |
args = { 'subpageText' }, |
| 265 |
expect = { |
| 266 |
'Main Page', 'TestFramework', 'Module:TestFramework', 'Subpage', 'Not/A/Subpage', |
| 267 |
'Test Framework', 'Test Framework' |
| 268 |
} |
| 269 |
}, |
| 270 |
{ name = '.fullText', func = prop_foreach, |
| 271 |
args = { 'fullText' }, |
| 272 |
expect = { |
| 273 |
'Main Page', 'Module:TestFramework', 'interwikiprefix:Module:TestFramework', |
| 274 |
'Talk:Has/A/Subpage#frag', 'Not/A/Subpage', |
| 275 |
'Module talk:Test Framework# frag frag', 'Module talk:Test Framework# frag frag' |
| 276 |
} |
| 277 |
}, |
| 278 |
{ name = '.subjectNsText', func = prop_foreach, |
| 279 |
args = { 'subjectNsText' }, |
| 280 |
expect = { '', 'Module', nil, '', '', 'Module', 'Module' } |
| 281 |
}, |
| 282 |
{ name = '.canTalk', func = prop_foreach, |
| 283 |
args = { 'canTalk' }, |
| 284 |
expect = { true, true, nil, true, true, true, true } |
| 285 |
}, |
| 286 |
{ name = '.talkNsText', func = prop_foreach, |
| 287 |
args = { 'talkNsText' }, |
| 288 |
expect = { 'Talk', 'Module talk', nil, 'Talk', 'Talk', 'Module talk', 'Module talk' } |
| 289 |
}, |
| 290 |
{ name = '.fragment', func = prop_foreach, |
| 291 |
args = { 'fragment' }, |
| 292 |
expect = { '', '', '', 'frag', '', ' frag frag', ' frag frag' } |
| 293 |
}, |
| 294 |
{ name = '.interwiki', func = prop_foreach, |
| 295 |
args = { 'interwiki' }, |
| 296 |
expect = { '', '', 'interwikiprefix', '', '', '', '' } |
| 297 |
}, |
| 298 |
{ name = '.namespace', func = prop_foreach, |
| 299 |
args = { 'namespace' }, |
| 300 |
expect = { |
| 301 |
0, mw.site.namespaces.Module.id, 0, 1, 0, |
| 302 |
mw.site.namespaces.Module_talk.id, mw.site.namespaces.Module_talk.id |
| 303 |
} |
| 304 |
}, |
| 305 |
{ name = '.protectionLevels', func = prop_foreach, |
| 306 |
args = { 'protectionLevels' }, |
| 307 |
expect = { |
| 308 |
{ edit = {}, move = {} }, { edit = { 'sysop', 'bogus' }, move = { 'sysop', 'bogus' } }, |
| 309 |
{}, { create = { 'sysop' } }, { edit = { 'autoconfirmed' }, move = { 'sysop' } }, |
| 310 |
{ edit = {}, move = { 'sysop' } }, { edit = {}, move = { 'sysop' } } |
| 311 |
} |
| 312 |
}, |
| 313 |
{ name = '.cascadingProtection', func = prop_foreach, |
| 314 |
args = { 'cascadingProtection' }, |
| 315 |
expect = { |
| 316 |
{ restrictions = { edit = { 'sysop' } }, sources = { 'Lockbox', 'Lockbox2' } }, { restrictions = {}, sources = {} }, |
| 317 |
{ restrictions = {}, sources = {} }, { restrictions = {}, sources = {} }, { restrictions = {}, sources = {} }, |
| 318 |
{ restrictions = {}, sources = {} }, { restrictions = {}, sources = {} } |
| 319 |
} |
| 320 |
}, |
| 321 |
{ name = '.inNamespace()', func = func_foreach, |
| 322 |
args = { 'inNamespace', 'Module' }, |
| 323 |
expect = { false, true, false, false, false, false, false } |
| 324 |
}, |
| 325 |
{ name = '.inNamespace() 2', func = func_foreach, |
| 326 |
args = { 'inNamespace', mw.site.namespaces.Module.id }, |
| 327 |
expect = { false, true, false, false, false, false, false } |
| 328 |
}, |
| 329 |
{ name = '.inNamespaces()', func = func_foreach, |
| 330 |
args = { 'inNamespaces', 0, 1 }, |
| 331 |
expect = { true, false, true, true, true, false, false } |
| 332 |
}, |
| 333 |
{ name = '.hasSubjectNamespace()', func = func_foreach, |
| 334 |
args = { 'hasSubjectNamespace', 0 }, |
| 335 |
expect = { true, false, true, true, true, false, false } |
| 336 |
}, |
| 337 |
{ name = '.isSubpageOf() 1', func = func_foreach, |
| 338 |
args = { 'isSubpageOf', title }, |
| 339 |
expect = { false, false, false, false, false, false, false } |
| 340 |
}, |
| 341 |
{ name = '.isSubpageOf() 2', func = func_foreach, |
| 342 |
args = { 'isSubpageOf', title4p }, |
| 343 |
expect = { false, false, false, true, false, false, false } |
| 344 |
}, |
| 345 |
{ name = '.partialUrl()', func = func_foreach, |
| 346 |
args = { 'partialUrl' }, |
| 347 |
expect = { |
| 348 |
'Main_Page', 'TestFramework', 'Module:TestFramework', 'Has/A/Subpage', 'Not/A/Subpage', |
| 349 |
'Test_Framework', 'Test_Framework' |
| 350 |
} |
| 351 |
}, |
| 352 |
{ name = '.fullUrl()', func = func_foreach, |
| 353 |
args = { 'fullUrl' }, |
| 354 |
expect = { |
| 355 |
'//wiki.local/wiki/Main_Page', |
| 356 |
'//wiki.local/wiki/Module:TestFramework', |
| 357 |
'//test.wikipedia.org/wiki/Module:TestFramework', |
| 358 |
'//wiki.local/wiki/Talk:Has/A/Subpage#frag', |
| 359 |
'//wiki.local/wiki/Not/A/Subpage', |
| 360 |
'//wiki.local/wiki/Module_talk:Test_Framework#_frag_frag', |
| 361 |
'//wiki.local/wiki/Module_talk:Test_Framework#_frag_frag', |
| 362 |
} |
| 363 |
}, |
| 364 |
{ name = '.fullUrl() 2', func = func_foreach, |
| 365 |
args = { 'fullUrl', { action = 'test' } }, |
| 366 |
expect = { |
| 367 |
'//wiki.local/w/index.php?title=Main_Page&action=test', |
| 368 |
'//wiki.local/w/index.php?title=Module:TestFramework&action=test', |
| 369 |
'//test.wikipedia.org/wiki/Module:TestFramework?action=test', |
| 370 |
'//wiki.local/w/index.php?title=Talk:Has/A/Subpage&action=test#frag', |
| 371 |
'//wiki.local/w/index.php?title=Not/A/Subpage&action=test', |
| 372 |
'//wiki.local/w/index.php?title=Module_talk:Test_Framework&action=test#_frag_frag', |
| 373 |
'//wiki.local/w/index.php?title=Module_talk:Test_Framework&action=test#_frag_frag', |
| 374 |
} |
| 375 |
}, |
| 376 |
{ name = '.fullUrl() 3', func = func_foreach, |
| 377 |
args = { 'fullUrl', nil, 'http' }, |
| 378 |
expect = { |
| 379 |
'http://wiki.local/wiki/Main_Page', |
| 380 |
'http://wiki.local/wiki/Module:TestFramework', |
| 381 |
'http://test.wikipedia.org/wiki/Module:TestFramework', |
| 382 |
'http://wiki.local/wiki/Talk:Has/A/Subpage#frag', |
| 383 |
'http://wiki.local/wiki/Not/A/Subpage', |
| 384 |
'http://wiki.local/wiki/Module_talk:Test_Framework#_frag_frag', |
| 385 |
'http://wiki.local/wiki/Module_talk:Test_Framework#_frag_frag', |
| 386 |
} |
| 387 |
}, |
| 388 |
{ name = '.localUrl()', func = func_foreach, |
| 389 |
args = { 'localUrl' }, |
| 390 |
expect = { |
| 391 |
'/wiki/Main_Page', |
| 392 |
'/wiki/Module:TestFramework', |
| 393 |
'//test.wikipedia.org/wiki/Module:TestFramework', |
| 394 |
'/wiki/Talk:Has/A/Subpage', |
| 395 |
'/wiki/Not/A/Subpage', |
| 396 |
'/wiki/Module_talk:Test_Framework', |
| 397 |
'/wiki/Module_talk:Test_Framework', |
| 398 |
} |
| 399 |
}, |
| 400 |
{ name = '.canonicalUrl()', func = func_foreach, |
| 401 |
args = { 'canonicalUrl' }, |
| 402 |
expect = { |
| 403 |
'http://wiki.local/wiki/Main_Page', |
| 404 |
'http://wiki.local/wiki/Module:TestFramework', |
| 405 |
'http://test.wikipedia.org/wiki/Module:TestFramework', |
| 406 |
'http://wiki.local/wiki/Talk:Has/A/Subpage#frag', |
| 407 |
'http://wiki.local/wiki/Not/A/Subpage', |
| 408 |
'http://wiki.local/wiki/Module_talk:Test_Framework#_frag_frag', |
| 409 |
'http://wiki.local/wiki/Module_talk:Test_Framework#_frag_frag', |
| 410 |
} |
| 411 |
}, |
| 412 |
|
| 413 |
{ name = '.getContent()', func = test_getContent, |
| 414 |
expect = { |
| 415 |
'{{int:mainpage}}<includeonly>...</includeonly><noinclude>...</noinclude>', |
| 416 |
nil, |
| 417 |
} |
| 418 |
}, |
| 419 |
|
| 420 |
{ name = '.content', func = test_content, |
| 421 |
expect = { |
| 422 |
'{{int:mainpage}}<includeonly>...</includeonly><noinclude>...</noinclude>', |
| 423 |
false, |
| 424 |
} |
| 425 |
}, |
| 426 |
|
| 427 |
{ name = '.redirectTarget', func = test_redirectTarget, type = 'ToString', |
| 428 |
expect = { 'ScribuntoTestTarget', false, false } |
| 429 |
}, |
| 430 |
|
| 431 |
{ name = 'not quite too many expensive functions', func = test_expensive_10, |
| 432 |
expect = { 'did not error' } |
| 433 |
}, |
| 434 |
{ name = 'too many expensive functions', func = test_expensive_11, |
| 435 |
expect = 'too many expensive function calls' |
| 436 |
}, |
| 437 |
{ name = "previously cached titles shouldn't count as expensive", func = test_expensive_cached, |
| 438 |
expect = { 'did not error' } |
| 439 |
}, |
| 440 |
{ name = "inexpensive actions shouldn't count as expensive", func = test_inexpensive, |
| 441 |
expect = { 'did not error' } |
| 442 |
}, |
| 443 |
{ name = "fragments don't leak via getCurrentTitle()", func = test_getCurrentTitle_fragment, |
| 444 |
expect = { '' } |
| 445 |
}, |
| 446 |
{ name = "sample page language returns current content language", func = test_pageLang, |
| 447 |
expect = { 'did not error' } |
| 448 |
}, |
| 449 |
} |
| 450 |
|
| 451 |
return testframework.getTestProvider( tests ) |