Files
phantom-gladiator/tools/test_unit_rules.lua
T

132 lines
7.4 KiB
Lua

package.path='Mods/PhantomGladiator/Scripts/?.lua;'..package.path
local Rules=require('unit_rules')
local function filesystem(files)
local fs={files=files or {}}
function fs.open(path,mode)
if mode:find('r',1,true) then
local text=fs.files[path]
if not text then return nil end
return {read=function()return text end,close=function()return true end}
end
if fs.deny then return nil end
local out={}
return {write=function(_,s)out[#out+1]=s;return true end,
close=function()if not fs.lose then fs.files[path]=table.concat(out) end;return true end}
end
return fs
end
local FILE='memory/unit-rules.tsv'
local function new(fs,lines)
return Rules.new({path='memory',io=fs,log=function(s)if lines then lines[#lines+1]=s end end})
end
local function actor(class)
return {IsValid=function()return true end,GetClass=function()
return {IsValid=function()return true end,GetFName=function()return {ToString=function()return class end}end}
end}
end
do
local fs=filesystem();local rules=new(fs)
for _,id in ipairs({'BP_Monster_HuaShan_Blue','BP_Monster_HuaShan_RedTroop_BigBro','BP_Monster_HuaShan_JianOu','BP_Monster_EMei_Sword_JianZhen','BP_Monster_JadeMaskMan','BP_Monster_FemaleVessel_YuZan','BP_Monster_XiaoXue','BP_Monster_Danghui','BP_Monster_CanQu_BiHai'})do
assert(rules:get(id,'entourage')=='off' and rules:set(id,'entourage','on').ok)
assert(new(fs):get(id,'entourage')=='on' and rules:set(id,'entourage','off').ok)
end
assert(not rules:set('BP_Monster_Dog','entourage','on').ok)
end
-- Defaults, a choice saved and read back, the default again takes the row away.
do
local fs,lines=filesystem(),{}
local rules=new(fs,lines)
assert(rules:get('BP_Monster_MoTian','lock')=='execute' and rules:get('BP_Monster_MoTian','phantom')=='off')
assert(rules:status().text=='')
local result=rules:set('BP_Monster_MoTian','lock','kill')
assert(result.ok and result.spawned_count==0 and result.message=='锁血后打死')
assert(fs.files[FILE]=='UnitRulesV1\nBP_Monster_MoTian\tlock\tkill\n',fs.files[FILE])
assert(rules:get('BP_Monster_MoTian','lock')=='kill' and rules:get(actor('BP_Monster_MoTian_C'),'lock')=='kill','by id or by an actor')
assert(rules:get(actor('BP_Monster_AHua_C'),'lock')=='execute','others keep the default')
assert(rules:set('BP_Monster_YanQianSui','phantom','keep').ok)
assert(rules:status().text=='BP_Monster_MoTian:lock=kill;BP_Monster_YanQianSui:phantom=keep',rules:status().text)
-- Saved rules come back at the next start.
local again=new(fs)
assert(again:get('BP_Monster_MoTian','lock')=='kill' and again:get('BP_Monster_YanQianSui','phantom')=='keep')
assert(rules:set('BP_Monster_MoTian','lock','execute').ok)
assert(fs.files[FILE]=='UnitRulesV1\nBP_Monster_YanQianSui\tphantom\tkeep\n',fs.files[FILE])
assert(lines[1]:find('[UnitRules] 设置 单位=BP_Monster_MoTian lock=kill 保存读回=true',1,true),lines[1])
end
-- Refused: an unknown unit, rule or value; a value of the other rule. Nothing changes.
do
local fs=filesystem()
local rules=new(fs)
for _,bad in ipairs({{'../x','lock','kill'},{'','lock','kill'},{'BP_Monster_MoTian','size','kill'},{'BP_Monster_MoTian','lock','keep'},{'BP_Monster_MoTian','phantom','kill'},{nil,'lock','kill'}})do
assert(not rules:set(bad[1],bad[2],bad[3]).ok)
end
assert(fs.files[FILE]==nil and rules:status().text=='')
end
-- A save that does not read back keeps the rule as it was and says so.
do
local fs,lines=filesystem(),{}
local rules=new(fs,lines)
fs.lose=true
local result=rules:set('BP_Monster_MoTian','lock','kill')
assert(not result.ok and result.message=='单位规则没有保存' and rules:get('BP_Monster_MoTian','lock')=='execute')
assert(lines[1]:find('保存读回=false',1,true))
fs.lose=false;fs.deny=true
assert(not rules:set('BP_Monster_MoTian','lock','kill').ok)
end
-- A damaged file: only what is valid counts, defaults are not kept.
do
local fs=filesystem({[FILE]='UnitRulesV1\nBP_Monster_MoTian\tlock\tkill\nBP_Bad/x\tlock\tkill\nBP_Monster_AHua\tlock\tboom\nBP_Monster_X\tsize\tkill\nBP_Monster_Y\tlock\texecute\n'})
local rules=new(fs)
assert(rules:status().text=='BP_Monster_MoTian:lock=kill',rules:status().text)
assert(new(filesystem({[FILE]='OldV0\nBP_Monster_MoTian\tlock\tkill\n'})):status().text=='')
end
-- XuKong keeps his passive by default, and an explicit 'off' survives a restart.
do
local fs=filesystem();local rules=new(fs);local id='BP_Monster_MengHua'
assert(rules:get(id,'menghua_stage')=='keep')
assert(rules:set(id,'menghua_stage','skip').ok and new(fs):get(id,'menghua_stage')=='skip')
assert(rules:set(id,'menghua_stage','keep').ok and rules:status().text=='')
fs.deny=true;assert(not rules:set(id,'menghua_stage','skip').ok and rules:get(id,'menghua_stage')=='keep')
assert(not rules:set('BP_Monster_XuKong','menghua_stage','skip').ok)
local damaged=new(filesystem({[FILE]='UnitRulesV1\nBP_Monster_XuKong\tmenghua_stage\tskip\nBP_Monster_MengHua\tmenghua_stage\tskip\n'}))
assert(damaged:status().text==id..':menghua_stage=skip')
end
do
local fs=filesystem();local rules=new(fs);local id='BP_Monster_XianZhe'
assert(rules:get(id,'phantom')=='keep')
assert(rules:set(id,'phantom','off').ok and new(fs):get(id,'phantom')=='off')
assert(rules:set(id,'phantom','keep').ok and rules:status().text=='')
end
do
local fs=filesystem()
local rules=new(fs)
local id='BP_Monster_XuKong'
assert(rules:get(id,'phantom')=='keep' and rules:get(actor(id..'_C'),'phantom')=='keep')
assert(rules:set(id,'phantom','off').ok)
assert(rules:status().text==id..':phantom=off')
assert(new(fs):get(id,'phantom')=='off')
assert(rules:set(id,'phantom','keep').ok)
assert(rules:status().text=='' and new(fs):get(id,'phantom')=='keep')
end
do
local fs=filesystem();local rules=new(fs)
assert(not rules:hasExecutionOverrides()and rules:get('BP_Monster_MoTian','execution_minions')=='on_100')
assert(rules:set('BP_Monster_MoTian','execution_minions','off_37').ok and rules:hasExecutionOverrides())
local reloaded=new(fs)
assert(reloaded:hasExecutionOverrides()and reloaded:get('BP_Monster_MoTian','execution_minions')=='off_37')
for _,bad in ipairs({'on_101','off_-1','on_nan','on_50.5','50'})do assert(not rules:set('BP_Monster_MoTian','execution_minions',bad).ok)end
fs.deny=true;assert(not rules:set('BP_Monster_MoTian','execution_minions','on_25').ok)
assert(rules:get('BP_Monster_MoTian','execution_minions')=='off_37')
fs.deny=false;assert(rules:set('BP_Monster_MoTian','execution_minions','on_100').ok and not rules:hasExecutionOverrides())
end
print('unit_rules: PASS')
do
local fs=filesystem();local rules=new(fs)
assert(rules:get('BP_Monster_JingYe','remote_reflect')=='keep')
assert(rules:set('BP_Monster_JingYe','remote_reflect','off').ok)
assert(new(fs):get('BP_Monster_JingYe','remote_reflect')=='off')
assert(rules:set('BP_Monster_JingYe','remote_reflect','keep').ok and rules:status().text=='')
assert(not rules:set('BP_Monster_MBoss','remote_reflect','off').ok,'only thorns shields support this rule')
local loaded=new(filesystem({[FILE]='UnitRulesV1\nBP_Monster_MBoss\tremote_reflect\toff\nBP_Monster_JingYe\tremote_reflect\toff\n'}))
assert(loaded:get('BP_Monster_MBoss','remote_reflect')=='keep' and loaded:get('BP_Monster_JingYe','remote_reflect')=='off')
end