| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
local testframework = require 'Module:TestFramework' |
| 9 |
|
| 10 |
local function getEmptyTestDiv() |
| 11 |
return mw.html.create( 'div' ) |
| 12 |
end |
| 13 |
|
| 14 |
local function testHelper( obj, method, ... ) |
| 15 |
return obj[method]( obj, ... ) |
| 16 |
end |
| 17 |
|
| 18 |
|
| 19 |
local testAttrs = { foo = 'bar', ab = 'cd' } |
| 20 |
setmetatable( testAttrs, { __pairs = function ( t ) |
| 21 |
local keys = { 'ab', 'foo' } |
| 22 |
local i = 0 |
| 23 |
return function() |
| 24 |
i = i + 1 |
| 25 |
if i <= #keys then |
| 26 |
return keys[i], t[keys[i]] |
| 27 |
end |
| 28 |
end |
| 29 |
end } ) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
local function testMultiAddClass() |
| 35 |
return getEmptyTestDiv():addClass( 'foo' ):addClass( 'bar' ) |
| 36 |
end |
| 37 |
|
| 38 |
local function testCssAndCssText() |
| 39 |
return getEmptyTestDiv():css( 'foo', 'bar' ):cssText( 'abc:def' ):css( 'g', 'h' ) |
| 40 |
end |
| 41 |
|
| 42 |
local function testTagDone() |
| 43 |
return getEmptyTestDiv():tag( 'span' ):done() |
| 44 |
end |
| 45 |
|
| 46 |
local function testNodeDone() |
| 47 |
return getEmptyTestDiv():node( getEmptyTestDiv() ):done() |
| 48 |
end |
| 49 |
|
| 50 |
local function testTagNodeAllDone() |
| 51 |
return getEmptyTestDiv():tag( 'p' ):node( getEmptyTestDiv() ):allDone() |
| 52 |
end |
| 53 |
|
| 54 |
local function testAttributeOverride() |
| 55 |
return getEmptyTestDiv():attr( 'good', 'MediaWiki' ):attr( 'good', 'Wikibase' ) |
| 56 |
end |
| 57 |
|
| 58 |
local function testAttributeRemoval() |
| 59 |
return getEmptyTestDiv():attr( 'a', 'b' ):attr( 'a', nil ) |
| 60 |
end |
| 61 |
|
| 62 |
local function testGetAttribute() |
| 63 |
return getEmptyTestDiv():attr( 'town', 'Berlin' ):getAttr( 'town' ) |
| 64 |
end |
| 65 |
|
| 66 |
local function testGetAttributeEscaping() |
| 67 |
return getEmptyTestDiv():attr( 'foo', '<ble"&rgh>' ):getAttr( 'foo' ) |
| 68 |
end |
| 69 |
|
| 70 |
local function testNodeSelfClosingDone() |
| 71 |
return getEmptyTestDiv():node( mw.html.create( 'br' ) ):done() |
| 72 |
end |
| 73 |
|
| 74 |
local function testNodeAppendToSelfClosing() |
| 75 |
return mw.html.create( 'img' ):node( getEmptyTestDiv() ) |
| 76 |
end |
| 77 |
|
| 78 |
local function testWikitextAppendToSelfClosing() |
| 79 |
return mw.html.create( 'hr' ):wikitext( 'foo' ) |
| 80 |
end |
| 81 |
|
| 82 |
local function testCreateWithValue( val ) |
| 83 |
return mw.html.create( val ):wikitext( 'foo' ):tag( 'div' ):attr( 'a', 'b' ):allDone() |
| 84 |
end |
| 85 |
|
| 86 |
local function testCssRemoval() |
| 87 |
return getEmptyTestDiv():css( 'color', 'red' ):css( 'color', nil ) |
| 88 |
end |
| 89 |
|
| 90 |
local function testComplex() |
| 91 |
local builder = getEmptyTestDiv() |
| 92 |
|
| 93 |
builder:addClass( 'firstClass' ):attr( 'what', 'ever' ) |
| 94 |
|
| 95 |
builder:tag( 'meh' ):attr( 'whynot', 'Русский' ):tag( 'hr' ):attr( 'a', 'b' ) |
| 96 |
|
| 97 |
builder:node( mw.html.create( 'hr' ) ) |
| 98 |
|
| 99 |
builder:node( getEmptyTestDiv():attr( 'abc', 'def' ):css( 'width', '-1px' ) ) |
| 100 |
|
| 101 |
return builder |
| 102 |
end |
| 103 |
|
| 104 |
local function testStripMarker() |
| 105 |
local expect = '<div foo="' .. mw.html.stripMarkers.nowiki .. '"></div>' |
| 106 |
local actual = tostring( getEmptyTestDiv():attr( 'foo', mw.html.stripMarkers.nowiki ) ) |
| 107 |
if actual ~= expect then |
| 108 |
error( actual .. ' ~= ' .. expect ) |
| 109 |
end |
| 110 |
return 'ok' |
| 111 |
end |
| 112 |
|
| 113 |
|
| 114 |
local tests = { |
| 115 |
|
| 116 |
{ name = 'mw.html.create', func = mw.html.create, type='ToString', |
| 117 |
args = { 'table' }, |
| 118 |
expect = { '<table></table>' } |
| 119 |
}, |
| 120 |
{ name = 'mw.html.create (self closing)', func = mw.html.create, type='ToString', |
| 121 |
args = { 'br' }, |
| 122 |
expect = { '<br />' } |
| 123 |
}, |
| 124 |
{ name = 'mw.html.create (self closing - forced)', func = mw.html.create, type='ToString', |
| 125 |
args = { 'div', { selfClosing = true } }, |
| 126 |
expect = { '<div />' } |
| 127 |
}, |
| 128 |
{ name = 'mw.html.create (invalid tag 1)', func = mw.html.create, type='ToString', |
| 129 |
args = { '$$$$' }, |
| 130 |
expect = "invalid tag name '$$$$'" |
| 131 |
}, |
| 132 |
{ name = 'mw.html.create (invalid tag 2)', func = mw.html.create, type='ToString', |
| 133 |
args = { {} }, |
| 134 |
expect = "bad argument #1 to 'mw.html.create' (string expected, got table)" |
| 135 |
}, |
| 136 |
{ name = 'mw.html.wikitext', func = testHelper, type='ToString', |
| 137 |
args = { getEmptyTestDiv(), 'wikitext', 'Plain text' }, |
| 138 |
expect = { '<div>Plain text</div>' } |
| 139 |
}, |
| 140 |
{ name = 'mw.html.wikitext (invalid input)', func = testHelper, type='ToString', |
| 141 |
args = { getEmptyTestDiv(), 'wikitext', 'Plain text', {} }, |
| 142 |
expect = "bad argument #2 to 'wikitext' (string or number expected, got table)" |
| 143 |
}, |
| 144 |
{ name = 'mw.html.newline', func = testHelper, type='ToString', |
| 145 |
args = { getEmptyTestDiv(), 'newline' }, |
| 146 |
expect = { '<div>\n</div>' } |
| 147 |
}, |
| 148 |
{ name = 'mw.html.tag', func = testHelper, type='ToString', |
| 149 |
args = { getEmptyTestDiv(), 'tag', 'span' }, |
| 150 |
|
| 151 |
expect = { '<span></span>' } |
| 152 |
}, |
| 153 |
{ name = 'mw.html.attr', func = testHelper, type='ToString', |
| 154 |
args = { getEmptyTestDiv(), 'attr', 'foo', 'bar' }, |
| 155 |
expect = { '<div foo="bar"></div>' } |
| 156 |
}, |
| 157 |
{ name = 'mw.html.attr (nil noop)', func = testHelper, type='ToString', |
| 158 |
args = { getEmptyTestDiv(), 'attr', 'foo', nil }, |
| 159 |
expect = { '<div></div>' } |
| 160 |
}, |
| 161 |
{ name = 'mw.html.attr (table 1)', func = testHelper, type='ToString', |
| 162 |
args = { getEmptyTestDiv(), 'attr', { foo = 'bar' } }, |
| 163 |
expect = { '<div foo="bar"></div>' } |
| 164 |
}, |
| 165 |
{ name = 'mw.html.attr (table 2)', func = testHelper, type='ToString', |
| 166 |
args = { getEmptyTestDiv(), 'attr', testAttrs }, |
| 167 |
expect = { '<div ab="cd" foo="bar"></div>' } |
| 168 |
}, |
| 169 |
{ name = 'mw.html.attr (invalid name 1)', func = testHelper, type='ToString', |
| 170 |
args = { getEmptyTestDiv(), 'attr', 123, 'bar' }, |
| 171 |
expect = "bad argument #1 to 'attr' (string expected, got number)" |
| 172 |
}, |
| 173 |
{ name = 'mw.html.attr (invalid name 2)', func = testHelper, |
| 174 |
args = { getEmptyTestDiv(), 'attr', '§§§§', 'foo' }, |
| 175 |
expect = "bad argument #1 to 'attr' (invalid attribute name '§§§§')" |
| 176 |
}, |
| 177 |
{ name = 'mw.html.attr (table no value)', func = testHelper, |
| 178 |
args = { getEmptyTestDiv(), 'attr', { foo = 'bar' }, 'foo' }, |
| 179 |
expect = "bad argument #2 to 'attr' (if argument #1 is a table, argument #2 must be left empty)" |
| 180 |
}, |
| 181 |
{ name = 'mw.html.attr (invalid value)', func = testHelper, type='ToString', |
| 182 |
args = { getEmptyTestDiv(), 'attr', 'foo', true }, |
| 183 |
expect = "bad argument #2 to 'attr' (string, number or nil expected, got boolean)" |
| 184 |
}, |
| 185 |
{ name = 'mw.html.attr (invalid table 1)', func = testHelper, type='ToString', |
| 186 |
args = { getEmptyTestDiv(), 'attr', { foo = {} } }, |
| 187 |
expect = "bad argument #1 to 'attr' " .. |
| 188 |
'(table keys must be strings, and values must be strings or numbers)' |
| 189 |
}, |
| 190 |
{ name = 'mw.html.attr (invalid table 2)', func = testHelper, type='ToString', |
| 191 |
args = { getEmptyTestDiv(), 'attr', { 1, 2 ,3 } }, |
| 192 |
expect = "bad argument #1 to 'attr' " .. |
| 193 |
'(table keys must be strings, and values must be strings or numbers)' |
| 194 |
}, |
| 195 |
{ name = 'mw.html.attr (invalid table 3)', func = testHelper, type='ToString', |
| 196 |
args = { getEmptyTestDiv(), 'attr', { foo = 'bar', blah = true } }, |
| 197 |
expect = "bad argument #1 to 'attr' " .. |
| 198 |
'(table keys must be strings, and values must be strings or numbers)' |
| 199 |
}, |
| 200 |
{ name = 'mw.html.attr (invalid table 4)', func = testHelper, type='ToString', |
| 201 |
args = { getEmptyTestDiv(), 'attr', { [{}] = 'foo' } }, |
| 202 |
expect = "bad argument #1 to 'attr' " .. |
| 203 |
'(table keys must be strings, and values must be strings or numbers)' |
| 204 |
}, |
| 205 |
{ name = 'mw.html.getAttr (nil)', func = testHelper, |
| 206 |
args = { getEmptyTestDiv(), 'getAttr', 'foo' }, |
| 207 |
expect = { nil } |
| 208 |
}, |
| 209 |
{ name = 'mw.html.getAttr (invalid name)', func = testHelper, |
| 210 |
args = { getEmptyTestDiv(), 'getAttr', 123 }, |
| 211 |
expect = "bad argument #1 to 'getAttr' (string expected, got number)" |
| 212 |
}, |
| 213 |
{ name = 'mw.html.addClass', func = testHelper, type='ToString', |
| 214 |
args = { getEmptyTestDiv(), 'addClass', 'foo' }, |
| 215 |
expect = { '<div class="foo"></div>' } |
| 216 |
}, |
| 217 |
{ name = 'mw.html.addClass (numeric argument)', func = testHelper, type='ToString', |
| 218 |
args = { getEmptyTestDiv(), 'addClass', 123 }, |
| 219 |
expect = { '<div class="123"></div>' } |
| 220 |
}, |
| 221 |
{ name = 'mw.html.addClass (invalid value)', func = testHelper, type='ToString', |
| 222 |
args = { getEmptyTestDiv(), 'addClass', {} }, |
| 223 |
expect = "bad argument #1 to 'addClass' (string, number or nil expected, got table)" |
| 224 |
}, |
| 225 |
{ name = 'mw.html.css', func = testHelper, type='ToString', |
| 226 |
args = { getEmptyTestDiv(), 'css', 'foo', 'bar' }, |
| 227 |
expect = { '<div style="foo:bar"></div>' } |
| 228 |
}, |
| 229 |
{ name = 'mw.html.css (numeric arguments)', func = testHelper, type='ToString', |
| 230 |
args = { getEmptyTestDiv(), 'css', 123, 456 }, |
| 231 |
expect = { '<div style="123:456"></div>' } |
| 232 |
}, |
| 233 |
{ name = 'mw.html.css (nil noop)', func = testHelper, type='ToString', |
| 234 |
args = { getEmptyTestDiv(), 'css', 'foo', nil }, |
| 235 |
expect = { '<div></div>' } |
| 236 |
}, |
| 237 |
{ name = 'mw.html.css (invalid name 1)', func = testHelper, type='ToString', |
| 238 |
args = { getEmptyTestDiv(), 'css', function() end, 'bar' }, |
| 239 |
expect = "bad argument #1 to 'css' (string or number expected, got function)" |
| 240 |
}, |
| 241 |
{ name = 'mw.html.css (table no value)', func = testHelper, type='ToString', |
| 242 |
args = { getEmptyTestDiv(), 'css', {}, 'bar' }, |
| 243 |
expect = "bad argument #2 to 'css' (if argument #1 is a table, argument #2 must be left empty)" |
| 244 |
}, |
| 245 |
{ name = 'mw.html.css (invalid value)', func = testHelper, type='ToString', |
| 246 |
args = { getEmptyTestDiv(), 'css', 'foo', {} }, |
| 247 |
expect = "bad argument #2 to 'css' (string, number or nil expected, got table)" |
| 248 |
}, |
| 249 |
{ name = 'mw.html.css (table)', func = testHelper, type='ToString', |
| 250 |
args = { getEmptyTestDiv(), 'css', testAttrs }, |
| 251 |
expect = { '<div style="ab:cd;foo:bar"></div>' } |
| 252 |
}, |
| 253 |
{ name = 'mw.html.css (invalid table)', func = testHelper, type='ToString', |
| 254 |
args = { getEmptyTestDiv(), 'css', { foo = 'bar', ab = true } }, |
| 255 |
expect = "bad argument #1 to 'css' " .. |
| 256 |
'(table keys and values must be strings or numbers)' |
| 257 |
}, |
| 258 |
{ name = 'mw.html.cssText', func = testHelper, type='ToString', |
| 259 |
args = { getEmptyTestDiv(), 'cssText', 'Unit tests, ftw' }, |
| 260 |
expect = { '<div style="Unit tests, ftw"></div>' } |
| 261 |
}, |
| 262 |
{ name = 'mw.html.cssText (numeric argument)', func = testHelper, type='ToString', |
| 263 |
args = { getEmptyTestDiv(), 'cssText', 123 }, |
| 264 |
expect = { '<div style="123"></div>' } |
| 265 |
}, |
| 266 |
{ name = 'mw.html.cssText (invalid value)', func = testHelper, type='ToString', |
| 267 |
args = { getEmptyTestDiv(), 'cssText', {} }, |
| 268 |
expect = "bad argument #1 to 'cssText' (string, number or nil expected, got table)" |
| 269 |
}, |
| 270 |
{ name = 'mw.html attribute escaping (value with double quotes)', func = testHelper, type='ToString', |
| 271 |
args = { getEmptyTestDiv(), 'attr', 'foo', 'ble"rgh' }, |
| 272 |
expect = { '<div foo="ble"rgh"></div>' } |
| 273 |
}, |
| 274 |
{ name = 'mw.html attribute escaping 1', func = testHelper, type='ToString', |
| 275 |
args = { getEmptyTestDiv(), 'attr', 'foo', 'ble<rgh' }, |
| 276 |
expect = { '<div foo="ble<rgh"></div>' } |
| 277 |
}, |
| 278 |
{ name = 'mw.html attribute escaping 2', func = testHelper, type='ToString', |
| 279 |
args = { getEmptyTestDiv(), 'attr', 'foo', '<ble"&rgh>' }, |
| 280 |
expect = { '<div foo="<ble"&rgh>"></div>' } |
| 281 |
}, |
| 282 |
{ name = 'mw.html attribute escaping (CSS)', func = testHelper, type='ToString', |
| 283 |
args = { getEmptyTestDiv(), 'css', 'mu"ha', 'ha"ha' }, |
| 284 |
expect = { '<div style="mu"ha:ha"ha"></div>' } |
| 285 |
}, |
| 286 |
{ name = 'mw.html attribute escaping (CSS raw)', func = testHelper, type='ToString', |
| 287 |
args = { getEmptyTestDiv(), 'cssText', 'mu"ha:-ha"ha' }, |
| 288 |
expect = { '<div style="mu"ha:-ha"ha"></div>' } |
| 289 |
}, |
| 290 |
{ name = 'mw.html.addClass (nil)', func = testHelper, type='ToString', |
| 291 |
args = { getEmptyTestDiv(), 'addClass' }, |
| 292 |
expect = { '<div></div>' } |
| 293 |
}, |
| 294 |
{ name = 'mw.html.cssText (nil)', func = testHelper, type='ToString', |
| 295 |
args = { getEmptyTestDiv(), 'cssText' }, |
| 296 |
expect = { '<div></div>' } |
| 297 |
}, |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
{ name = 'mw.html.addClass (twice) ', func = testMultiAddClass, type='ToString', |
| 302 |
expect = { '<div class="foo bar"></div>' } |
| 303 |
}, |
| 304 |
{ name = 'mw.html.css.cssText.css', func = testCssAndCssText, type='ToString', |
| 305 |
expect = { '<div style="foo:bar;abc:def;g:h"></div>' } |
| 306 |
}, |
| 307 |
{ name = 'mw.html.tag (using done)', func = testTagDone, type='ToString', |
| 308 |
expect = { '<div><span></span></div>' } |
| 309 |
}, |
| 310 |
{ name = 'mw.html.node (using done)', func = testNodeDone, type='ToString', |
| 311 |
expect = { '<div><div></div></div>' } |
| 312 |
}, |
| 313 |
{ name = 'mw.html.node (self closing, using done)', func = testNodeSelfClosingDone, type='ToString', |
| 314 |
expect = { '<div><br /></div>' } |
| 315 |
}, |
| 316 |
{ name = 'mw.html.node (append to self closing)', func = testNodeAppendToSelfClosing, type='ToString', |
| 317 |
expect = "self-closing tags can't have child nodes" |
| 318 |
}, |
| 319 |
{ name = 'mw.html.wikitext (append to self closing)', func = testWikitextAppendToSelfClosing, type='ToString', |
| 320 |
expect = "self-closing tags can't have child nodes" |
| 321 |
}, |
| 322 |
{ name = 'mw.html.tag.node (using allDone)', func = testTagNodeAllDone, type='ToString', |
| 323 |
expect = { '<div><p><div></div></p></div>' } |
| 324 |
}, |
| 325 |
{ name = 'mw.html.attr (overrides)', func = testAttributeOverride, type='ToString', |
| 326 |
expect = { '<div good="Wikibase"></div>' } |
| 327 |
}, |
| 328 |
{ name = 'mw.html.attr (removal)', func = testAttributeRemoval, type='ToString', |
| 329 |
expect = { '<div></div>' } |
| 330 |
}, |
| 331 |
{ name = 'mw.html.getAttr', func = testGetAttribute, type='ToString', |
| 332 |
expect = { 'Berlin' } |
| 333 |
}, |
| 334 |
{ name = 'mw.html.getAttr (escaping)', func = testGetAttributeEscaping, type='ToString', |
| 335 |
expect = { '<ble"&rgh>' } |
| 336 |
}, |
| 337 |
{ name = 'mw.html.create (empty string)', func = testCreateWithValue, type='ToString', |
| 338 |
args = {''}, |
| 339 |
expect = { 'foo<div a="b"></div>' } |
| 340 |
}, |
| 341 |
{ name = 'mw.html.create (nil)', func = testCreateWithValue, type='ToString', |
| 342 |
args = {nil}, |
| 343 |
expect = { 'foo<div a="b"></div>' } |
| 344 |
}, |
| 345 |
{ name = 'mw.html.css (removal)', func = testCssRemoval, type='ToString', |
| 346 |
expect = { '<div></div>' } |
| 347 |
}, |
| 348 |
{ name = 'mw.html complex test', func = testComplex, type='ToString', |
| 349 |
expect = { |
| 350 |
'<div class="firstClass" what="ever"><meh whynot="Русский"><hr a="b" /></meh>' .. |
| 351 |
'<hr /><div abc="def" style="width:-1px"></div></div>' |
| 352 |
} |
| 353 |
}, |
| 354 |
{ name = 'mw.html strip marker test', func = testStripMarker, type='ToString', |
| 355 |
expect = { 'ok' } |
| 356 |
}, |
| 357 |
} |
| 358 |
|
| 359 |
return testframework.getTestProvider( tests ) |