模块:IconLink/tests
此模块的文档可以在模块:IconLink/tests/doc创建
local scm = require 'Module:Schema'
local data = require('Module:IconLink/data')
local p = {}
function p.validate_data_sites()
local UrlWithPlaceholder = scm.String{pattern='^https?://.-%%s'}
local NonEmptyString = scm.String{min_len=1}
local SitesSchema = scm.Table{
[scm.Integer] = scm.Table{
name = NonEmptyString,
icon = scm.Table{
file = NonEmptyString,
size = NonEmptyString / nil,
},
url = UrlWithPlaceholder / scm.Function
/ scm.Table{validator=function(t)
local Item = scm.Table{NonEmptyString, UrlWithPlaceholder}
local LastItem = scm.Table{NonEmptyString / nil, UrlWithPlaceholder}
for i = 1, #t - 1 do
local valid, msg = Item:test(t[i])
if not valid then return false, msg end
end
return LastItem:test(t[#t])
end},
}
}
local valid, msg = SitesSchema:test(data.sites)
if valid then
return '<code>sites</code>表验证通过。'
end
return '<strong class="error"><code>sites</code>有误。</strong>\n<pre>'
..mw.text.nowiki(msg or '')
..'</pre>[[Category:有脚本错误的页面]]'
end
function p.validate_data_domains()
local DomainSegment = scm.String{pattern='^[%w%-]+$'}
local SitesKey = scm.String{validator=function(v)
return data.sites[v]
end}
local DomainsSchema
DomainsSchema = scm.Table{validator=function(t)
for k, v in pairs(t) do
-- 所有key需要是域名的片段
local valid, msg = DomainSegment:test(k)
if not valid then return false, msg end
-- 所有value都是DomainsSchema或SitesKey
valid, msg = (DomainsSchema / SitesKey):test(v)
if not valid then return false, msg end
end
return true
end}
local valid, msg = DomainsSchema:test(data.domains)
if valid then
return '<code>domains</code>表验证通过。'
end
return '<strong class="error"><code>domains</code>有误。</strong>\n<pre>'
..mw.text.nowiki(msg or '')
..'</pre>[[Category:有脚本错误的页面]]'
end
return p