🌟 | 現在、 鉄壁ヘッドショットには対応済みです。 鉄壁HSは通常HSと同じダメージになります。LMG及びDMR、チャージライフル、ハンマーポイント弾を除き、すべてのダメージ値が一致していることを確認しています。 |
「モジュール:Utility/TestHelper」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「local TestHelper = {} local function execute(testcases) local result = {} for name, testcase in pairs(testcases) do local status, retval = pcall(testcase.func) --l…」) |
(型 "table" に対応) |
||
1行目: | 1行目: | ||
local TestHelper = {} | local TestHelper = {} | ||
local StringBuilder = require('Module:Utility/StringBuilder') | |||
local tostringTable = nil | |||
local function tostringObject(val, builder, offset) | |||
if type(val) == 'table' then | |||
tostringTable(val, builder, offset + 1) | |||
elseif type(val) == 'string' then | |||
builder:appendFormat('"%s"', val) | |||
else | |||
builder:append(val) | |||
end | |||
end | |||
function tostringTable(tbl, builder, offset) | |||
offset = offset + 1 | |||
builder:appendFormat(' %s{\n', string.rep(' ', offset - 1)) | |||
-- index | |||
if #tbl > 0 then | |||
builder:appendFormat(' %s', string.rep(' ', offset)) | |||
for i = 1, #tbl do | |||
local val = tbl[i] | |||
tostringObject(val, builder, offset) | |||
builder:append(', ') | |||
end | |||
builder:append('\n') | |||
end | |||
-- key | |||
for key, val in pairs(tbl) do | |||
if type(key) ~= 'number' then | |||
builder:appendFormat(' %s["%s"] = ', string.rep(' ', offset), key) | |||
tostringObject(val, builder, offset) | |||
builder:append(',\n') | |||
end | |||
end | |||
builder:appendFormat(' %s}', string.rep(' ', offset - 1)) | |||
end | |||
local function tostringInternal(val) | |||
if type(val) == 'table' then | |||
local builder = StringBuilder.new() | |||
builder:append('\n') | |||
tostringTable(val, builder, 0) | |||
builder:append('\n') | |||
return tostring(builder) | |||
end | |||
return tostring(val) | |||
end | |||
-- Memo: no check metatable!! | |||
local function tableEquals(obj1, obj2) | |||
if obj1 == obj2 then | |||
return true | |||
end | |||
local typename1 = type(obj1) | |||
local typename2 = type(obj2) | |||
if typename1 ~= typename2 or typename1 ~= 'table' then | |||
return false | |||
end | |||
local keyset = {} | |||
for key, val in pairs(obj1) do | |||
local val2 = obj2[key] | |||
if val2 == nil or tableEquals(val, val2) == false then | |||
return false | |||
end | |||
keyset[key] = true | |||
end | |||
for key2, _ in pairs(obj2) do | |||
if not keyset[key2] then | |||
return false | |||
end | |||
end | |||
return true | |||
end | |||
local function execute(testcases) | local function execute(testcases) | ||
7行目: | 90行目: | ||
--local retval = testcase.func() | --local retval = testcase.func() | ||
if status then | if status then | ||
if testcase.expected | if tableEquals(testcase.expected, retval) then | ||
result[name] = { | result[name] = { | ||
succeed = true, | succeed = true, | ||
56行目: | 139行目: | ||
:done() | :done() | ||
for name, value in pairs(result) do | for name, value in pairs(result) do | ||
local expectedString = tostringInternal(value.expected) | |||
local actualString = tostringInternal(value.actual) | |||
tbl:tag('tr') | tbl:tag('tr') | ||
:tag('td'):wikitext(name):done() | :tag('td'):wikitext(name):done() | ||
62行目: | 148行目: | ||
:wikitext(value.succeed and '✔️' or '❌') | :wikitext(value.succeed and '✔️' or '❌') | ||
:done() | :done() | ||
:tag('td'):wikitext( | :tag('td'):wikitext(expectedString):done() | ||
:tag('td'):wikitext( | :tag('td'):wikitext(actualString):done() | ||
:tag('td'):wikitext(value.message) | :tag('td'):wikitext(value.message) | ||
end | end |
2021年8月20日 (金) 12:41時点における版
このモジュールについての説明文ページを モジュール:Utility/TestHelper/doc に作成できます
local TestHelper = {} local StringBuilder = require('Module:Utility/StringBuilder') local tostringTable = nil local function tostringObject(val, builder, offset) if type(val) == 'table' then tostringTable(val, builder, offset + 1) elseif type(val) == 'string' then builder:appendFormat('"%s"', val) else builder:append(val) end end function tostringTable(tbl, builder, offset) offset = offset + 1 builder:appendFormat(' %s{\n', string.rep(' ', offset - 1)) -- index if #tbl > 0 then builder:appendFormat(' %s', string.rep(' ', offset)) for i = 1, #tbl do local val = tbl[i] tostringObject(val, builder, offset) builder:append(', ') end builder:append('\n') end -- key for key, val in pairs(tbl) do if type(key) ~= 'number' then builder:appendFormat(' %s["%s"] = ', string.rep(' ', offset), key) tostringObject(val, builder, offset) builder:append(',\n') end end builder:appendFormat(' %s}', string.rep(' ', offset - 1)) end local function tostringInternal(val) if type(val) == 'table' then local builder = StringBuilder.new() builder:append('\n') tostringTable(val, builder, 0) builder:append('\n') return tostring(builder) end return tostring(val) end -- Memo: no check metatable!! local function tableEquals(obj1, obj2) if obj1 == obj2 then return true end local typename1 = type(obj1) local typename2 = type(obj2) if typename1 ~= typename2 or typename1 ~= 'table' then return false end local keyset = {} for key, val in pairs(obj1) do local val2 = obj2[key] if val2 == nil or tableEquals(val, val2) == false then return false end keyset[key] = true end for key2, _ in pairs(obj2) do if not keyset[key2] then return false end end return true end local function execute(testcases) local result = {} for name, testcase in pairs(testcases) do local status, retval = pcall(testcase.func) --local retval = testcase.func() if status then if tableEquals(testcase.expected, retval) then result[name] = { succeed = true, actual = retval, expected = testcase.expected, message = 'Test result is passed.', } else result[name] = { succeed = false, actual = retval, expected = testcase.expected, message = 'Test result isn\'t passed.', } end else result[name] = { succeed = false, actual = '', expected = testcase.expected, message = retval, } end end return result end function TestHelper.executeAsNode(testcases) local result = execute(testcases) local tbl = mw.html.create('table') :addClass('wikitable') :tag('tr') :tag('th') :wikitext('Test name<br>テスト名') :done() :tag('th') :wikitext('Status<br>成否') :done() :tag('th') :wikitext('Expected value<br>期待値') :done() :tag('th') :wikitext('Result<br>結果') :done() :tag('th') :wikitext('Message<br>メッセージ') :done() :done() for name, value in pairs(result) do local expectedString = tostringInternal(value.expected) local actualString = tostringInternal(value.actual) tbl:tag('tr') :tag('td'):wikitext(name):done() :tag('td') :attr('align', 'center') :wikitext(value.succeed and '✔️' or '❌') :done() :tag('td'):wikitext(expectedString):done() :tag('td'):wikitext(actualString):done() :tag('td'):wikitext(value.message) end return tbl end return TestHelper