Files
EscapistsReintroduced/tests/test_faction_menu.py
T

64 lines
3.3 KiB
Python

"""Execute the actual faction button handler, including Random-all submission."""
from policy_harness import ROOT, body, run
p = ROOT/'addons/core/Scripts/Game/ERR/00_Core/ERR_FactionMenu.c'
code = body(p, 'protected void OnFactionButton(')
code = (code.replace('protected ', '').replace('SCR_ButtonBaseComponent button', 'SCR_ButtonBaseComponent* button')
.replace('button.GetRootWidget().GetName()', 'button->name')
.replace('foreach (', 'for (').replace('ERR_FactionChoices.Roles()', 'Roles()')
.replace('SCR_ComboBoxComponent.GetComboBoxComponent(', 'GetCombo(')
.replace(').SetCurrentItem(', ')->SetCurrentItem(').replace(').GetCurrentIndex()', ')->GetCurrentIndex()')
.replace('m_Controller.ERR_AdminSubmit', 'm_Controller->ERR_AdminSubmit'))
run(r'''
#include <cassert>
#include <string>
#include <vector>
#include <map>
using string=std::string;
struct IDs:std::vector<string>{using std::vector<string>::vector;int Count(){return size();}};
struct Combo{int index=1;void SetCurrentItem(int i){index=i;}int GetCurrentIndex(){return index;}};
struct SCR_ButtonBaseComponent{string name;};
struct Controller{std::vector<string> commands;void ERR_AdminSubmit(string cmd,bool snapshot){assert(snapshot);commands.push_back(cmd);}};
struct Menu {
bool m_Ready=true,focused=true,m_FactionFieldsInitialized=true;
float m_Age=0,m_ClickDelay=0;
Controller controller;Controller* m_Controller=&controller;
IDs m_FactionIds={"random","us","ussr","fia"};
std::map<string,Combo> combos;
string feedback;
static IDs Roles(){return {"escapists","enemy","independent"};}
bool IsFocused(){return focused;}int GetRootWidget(){return 0;}
Combo* GetCombo(string name,int){return &combos[name];}
void SetText(string field,string value){assert(field=="Feedback");feedback=value;}
'''+code+r'''
};
int main(){
SCR_ButtonBaseComponent random{"FactionsRandomAll"}, apply{"FactionsApply"}, other{"Other"};
Menu m;
m.OnFactionButton(&random);
assert(m.controller.commands.size()==1&&m.controller.commands[0]=="factions set random random random");
for(auto role:Menu::Roles())assert(m.combos["FactionSelect_"+role].index==0);
assert(m.feedback=="Saving faction selections..."&&m.m_ClickDelay>0);
m.OnFactionButton(&random);assert(m.controller.commands.size()==1); // debounce
for(int guard=0;guard<6;guard++){
Menu refused;
if(guard==0)refused.m_Ready=false;
if(guard==1)refused.focused=false;
if(guard==2)refused.m_Controller=nullptr;
if(guard==3)refused.m_Age=7;
if(guard==4)refused.m_ClickDelay=1;
if(guard==5)refused.m_FactionFieldsInitialized=false;
refused.OnFactionButton(&random);assert(refused.controller.commands.empty()&&refused.combos.empty());
}
Menu manual;
manual.combos["FactionSelect_escapists"].index=1;
manual.combos["FactionSelect_enemy"].index=0;
manual.combos["FactionSelect_independent"].index=3;
manual.OnFactionButton(&apply);
assert(manual.controller.commands.size()==1&&manual.controller.commands[0]=="factions set us random fia");
Menu invalid;invalid.combos["FactionSelect_enemy"].index=99;
invalid.OnFactionButton(&apply);invalid.OnFactionButton(&other);invalid.OnFactionButton(nullptr);
assert(invalid.controller.commands.empty());
}
''')
print('PASS: Random all submits persistent random roles immediately; manual mixed choices, stale/unauthorized UI guards and debounce preserved')