#include "Game/IW4/Menu/MenuWriterIW4.h" #include "Game/IW4/IW4.h" #include "Parsing/Menu/MenuFileReader.h" #include "SearchPath/MockSearchPath.h" #include #include #include #include using namespace IW4; namespace { expressionEntry Operator(const int operation) { expressionEntry entry{}; entry.type = EET_OPERATOR; entry.data.op = operation; return entry; } expressionEntry IntOperand(const int value) { expressionEntry entry{}; entry.type = EET_OPERAND; entry.data.operand.dataType = VAL_INT; entry.data.operand.internals.intVal = value; return entry; } TEST_CASE("MenuWriterIW4: Can reload grouped function arguments", "[iw4][menu][assetdumper]") { std::vector entries; std::string expectedExpression; SECTION("Parentheses around an entire argument list") { entries = { Operator(EXP_FUNC_MAX), Operator(OP_LEFTPAREN), IntOperand(3), Operator(OP_COMMA), IntOperand(4), Operator(OP_RIGHTPAREN), Operator(OP_RIGHTPAREN), Operator(OP_DIVIDE), IntOperand(2), }; expectedExpression = "max(3, 4) / 2"; } SECTION("Multiple parentheses around an entire argument list") { entries = { Operator(EXP_FUNC_MAX), Operator(OP_LEFTPAREN), Operator(OP_LEFTPAREN), IntOperand(3), Operator(OP_COMMA), IntOperand(4), Operator(OP_RIGHTPAREN), Operator(OP_RIGHTPAREN), Operator(OP_RIGHTPAREN), }; expectedExpression = "max(3, 4)"; } SECTION("Nested function calls") { entries = { Operator(EXP_FUNC_MAX), Operator(OP_LEFTPAREN), Operator(EXP_FUNC_MIN), Operator(OP_LEFTPAREN), IntOperand(3), Operator(OP_COMMA), IntOperand(4), Operator(OP_RIGHTPAREN), Operator(OP_RIGHTPAREN), Operator(OP_COMMA), IntOperand(5), Operator(OP_RIGHTPAREN), Operator(OP_RIGHTPAREN), Operator(OP_DIVIDE), IntOperand(2), }; expectedExpression = "max(min(3, 4), 5) / 2"; } SECTION("Grouping inside an argument is preserved") { entries = { Operator(EXP_FUNC_MAX), Operator(OP_LEFTPAREN), Operator(OP_LEFTPAREN), IntOperand(1), Operator(OP_ADD), IntOperand(2), Operator(OP_RIGHTPAREN), Operator(OP_MULTIPLY), IntOperand(3), Operator(OP_COMMA), IntOperand(4), Operator(OP_RIGHTPAREN), Operator(OP_RIGHTPAREN), }; expectedExpression = "max((1 + 2) * 3, 4)"; } SECTION("An argument list without extra parentheses") { entries = { Operator(EXP_FUNC_MAX), Operator(OP_LEFTPAREN), IntOperand(1), Operator(OP_ADD), IntOperand(2), Operator(OP_RIGHTPAREN), Operator(OP_MULTIPLY), IntOperand(3), Operator(OP_COMMA), IntOperand(4), Operator(OP_RIGHTPAREN), }; expectedExpression = "max((1 + 2) * 3, 4)"; } Statement_s statement{static_cast(entries.size()), entries.data()}; menuDef_t menu{}; menu.window.name = "test"; menu.visibleExp = &statement; std::ostringstream output; const auto writer = menu::CreateMenuWriterIW4(output); writer->Start(); writer->WriteMenu(menu); writer->End(); INFO(output.str()); CHECK(output.str().find("when(" + expectedExpression + ");") != std::string::npos); MockSearchPath searchPath; menu::MenuAssetZoneState zoneState; std::istringstream input(output.str()); menu::MenuFileReader reader(input, "ui_mp/test.menu", menu::FeatureLevel::IW4, searchPath); reader.IncludeZoneState(zoneState); const auto parsed = reader.ReadMenuFile(); REQUIRE(parsed); REQUIRE(parsed->m_menus.size() == 1); CHECK(parsed->m_menus[0]->m_visible_expression); } TEST_CASE("MenuWriterIW4: Writes the dvar of a text item without edit field data", "[iw4][menu]") { itemDef_s item{}; item.window.name = "tip"; item.type = ITEM_TYPE_TEXT; item.dvar = "didyouknow"; itemDef_s* items[]{&item}; menuDef_t menu{}; menu.window.name = "connect"; menu.items = items; menu.itemCount = 1; std::ostringstream ss; const auto writer = menu::CreateMenuWriterIW4(ss); writer->Start(); writer->WriteMenu(menu); writer->End(); REQUIRE(ss.str().find("dvar \"didyouknow\"") != std::string::npos); } } // namespace