| 1 | local testframework = require 'Module:TestFramework' |
| 2 | |
| 3 | local function nsTest( ... ) |
| 4 | local args = { ... } |
| 5 | local t = mw.site.namespaces |
| 6 | local path = 'mw.site.namespaces' |
| 7 | for i = 1, #args do |
| 8 | t = t[args[i]] |
| 9 | path = path .. string.format( '[%q]', args[i] ) |
| 10 | if t == nil then |
| 11 | error( path .. ' is nil!' ) |
| 12 | end |
| 13 | end |
| 14 | return t |
| 15 | end |
| 16 | |
| 17 | local function isNonEmptyString( val ) |
| 18 | return type( val ) == 'string' and val ~= '' |
| 19 | end |
| 20 | |
| 21 | local function isValidInterwikiMap( map ) |
| 22 | assert( type( map ) == 'table', "mw.site.interwikiMap did not return a table" ) |
| 23 | local stringKeys = { 'prefix', 'url' } |
| 24 | local boolKeys = { |
| 25 | 'isProtocolRelative', |
| 26 | 'isLocal', |
| 27 | 'isTranscludable', |
| 28 | 'isCurrentWiki', |
| 29 | 'isExtraLanguageLink' |
| 30 | } |
| 31 | local maybeStringKeys = { 'displayText', 'tooltip' } |
| 32 | for prefix, data in pairs( map ) do |
| 33 | for _, key in ipairs( stringKeys ) do |
| 34 | assert( isNonEmptyString( data[key] ), |
| 35 | key .. " is not a string or is the empty string" |
| 36 | ) |
| 37 | end |
| 38 | assert( prefix == data.prefix, string.format( |
| 39 | "table key '%s' and prefix '%s' do not match", |
| 40 | tostring( prefix ), tostring( data.prefix ) |
| 41 | ) ) |
| 42 | for _, key in ipairs( boolKeys ) do |
| 43 | assert( type( data[key] ) == 'boolean', key .. " is not a boolean" ) |
| 44 | end |
| 45 | for _, key in ipairs( maybeStringKeys ) do |
| 46 | assert( data[key] == nil or isNonEmptyString( data[key] ), |
| 47 | key .. " is not a string or is the empty string, and is not nil" |
| 48 | ) |
| 49 | end |
| 50 | end |
| 51 | return true |
| 52 | end |
| 53 | |
| 54 | return testframework.getTestProvider( { |
| 55 | { name = 'parameter: siteName', |
| 56 | func = type, args = { mw.site.siteName }, |
| 57 | expect = { 'string' } |
| 58 | }, |
| 59 | { name = 'parameter: server', |
| 60 | func = type, args = { mw.site.server }, |
| 61 | expect = { 'string' } |
| 62 | }, |
| 63 | { name = 'parameter set: scriptPath', |
| 64 | func = type, args = { mw.site.scriptPath }, |
| 65 | expect = { 'string' } |
| 66 | }, |
| 67 | { name = 'parameter set: stylePath', |
| 68 | func = type, args = { mw.site.stylePath }, |
| 69 | expect = { 'string' } |
| 70 | }, |
| 71 | { name = 'parameter set: currentVersion', |
| 72 | func = type, args = { mw.site.currentVersion }, |
| 73 | expect = { 'string' } |
| 74 | }, |
| 75 | |
| 76 | { name = 'parameter set: stats.pages', |
| 77 | func = type, args = { mw.site.stats.pages }, |
| 78 | expect = { 'number' } |
| 79 | }, |
| 80 | |
| 81 | { name = 'pagesInCategory', |
| 82 | func = type, args = { mw.site.stats.pagesInCategory( "Example" ) }, |
| 83 | expect = { 'number' } |
| 84 | }, |
| 85 | |
| 86 | { name = 'pagesInNamespace', |
| 87 | func = type, args = { mw.site.stats.pagesInNamespace( 0 ) }, |
| 88 | expect = { 'number' } |
| 89 | }, |
| 90 | |
| 91 | { name = 'usersInGroup', |
| 92 | func = type, args = { mw.site.stats.usersInGroup( 'sysop' ) }, |
| 93 | expect = { 'number' } |
| 94 | }, |
| 95 | |
| 96 | { name = 'Project namespace by number', |
| 97 | func = nsTest, args = { 4, 'canonicalName' }, |
| 98 | expect = { 'Project' } |
| 99 | }, |
| 100 | |
| 101 | { name = 'Project namespace by name', |
| 102 | func = nsTest, args = { 'Project', 'id' }, |
| 103 | expect = { 4 } |
| 104 | }, |
| 105 | |
| 106 | { name = 'Project namespace by name (2)', |
| 107 | func = nsTest, args = { 'PrOjEcT', 'canonicalName' }, |
| 108 | expect = { 'Project' } |
| 109 | }, |
| 110 | |
| 111 | { name = 'Project namespace subject is itself', |
| 112 | func = nsTest, args = { 'Project', 'subject', 'canonicalName' }, |
| 113 | expect = { 'Project' } |
| 114 | }, |
| 115 | |
| 116 | { name = 'Project talk namespace via Project', |
| 117 | func = nsTest, args = { 'Project', 'talk', 'canonicalName' }, |
| 118 | expect = { 'Project talk' } |
| 119 | }, |
| 120 | |
| 121 | { name = 'Project namespace via Project talk', |
| 122 | func = nsTest, args = { 'Project_talk', 'subject', 'canonicalName' }, |
| 123 | expect = { 'Project' } |
| 124 | }, |
| 125 | |
| 126 | { name = 'Project talk namespace via Project (associated)', |
| 127 | func = nsTest, args = { 'Project', 'associated', 'canonicalName' }, |
| 128 | expect = { 'Project talk' } |
| 129 | }, |
| 130 | |
| 131 | { name = 'Project talk namespace by name (standard caps, no underscores)', |
| 132 | func = nsTest, args = { 'Project talk', 'id' }, |
| 133 | expect = { 5 } |
| 134 | }, |
| 135 | |
| 136 | { name = 'Project talk namespace by name (standard caps, underscores)', |
| 137 | func = nsTest, args = { 'Project_talk', 'id' }, |
| 138 | expect = { 5 } |
| 139 | }, |
| 140 | |
| 141 | { name = 'Project talk namespace by name (odd caps, no underscores)', |
| 142 | func = nsTest, args = { 'pRoJeCt tAlK', 'id' }, |
| 143 | expect = { 5 } |
| 144 | }, |
| 145 | |
| 146 | { name = 'Project talk namespace by name (odd caps, underscores)', |
| 147 | func = nsTest, args = { 'pRoJeCt_tAlK', 'id' }, |
| 148 | expect = { 5 } |
| 149 | }, |
| 150 | |
| 151 | { name = 'Project talk namespace by name (extraneous spaces and underscores)', |
| 152 | func = nsTest, args = { '_ _ _Project_ _talk_ _ _', 'id' }, |
| 153 | expect = { 5 } |
| 154 | }, |
| 155 | |
| 156 | { name = 'interwikiMap (all prefixes)', |
| 157 | func = isValidInterwikiMap, args = { mw.site.interwikiMap() }, |
| 158 | expect = { true } |
| 159 | }, |
| 160 | |
| 161 | { name = 'interwikiMap (local prefixes)', |
| 162 | func = isValidInterwikiMap, args = { mw.site.interwikiMap( 'local' ) }, |
| 163 | expect = { true } |
| 164 | }, |
| 165 | |
| 166 | { name = 'interwikiMap (non-local prefixes)', |
| 167 | func = isValidInterwikiMap, args = { mw.site.interwikiMap( '!local' ) }, |
| 168 | expect = { true } |
| 169 | }, |
| 170 | |
| 171 | { name = 'interwikiMap (type error 1)', |
| 172 | func = mw.site.interwikiMap, args = { 123 }, |
| 173 | expect = "bad argument #1 to 'interwikiMap' (string expected, got number)" |
| 174 | }, |
| 175 | |
| 176 | { name = 'interwikiMap (type error 2)', |
| 177 | func = mw.site.interwikiMap, args = { false }, |
| 178 | expect = "bad argument #1 to 'interwikiMap' (string expected, got boolean)" |
| 179 | }, |
| 180 | |
| 181 | { name = 'interwikiMap (unknown filter 1)', |
| 182 | func = mw.site.interwikiMap, args = { '' }, |
| 183 | expect = "bad argument #1 to 'interwikiMap' (unknown filter '')" |
| 184 | }, |
| 185 | |
| 186 | { name = 'interwikiMap (unknown filter 2)', |
| 187 | func = mw.site.interwikiMap, args = { 'foo' }, |
| 188 | expect = "bad argument #1 to 'interwikiMap' (unknown filter 'foo')" |
| 189 | }, |
| 190 | } ) |