/
redgpu
/
ezEngine
Обзор
Документация
Войти
/
redgpu
/
ezEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
Code/UnitTests/FoundationTest/Utility/CommandLineUtilsTest.cpp
250 строк
9 KB
Sanakan8472
Cross-platform tracing system (ETW, LTTNG, Perfetto) (#1815)
15 фев 2026, 10:24
Не верифицирован
15 фев 2026, 10:24
fac0c1f
Код
Авторство
О чём код?
#include <FoundationTest/FoundationTestPCH.h> #include <Foundation/Utilities/CommandLineUtils.h> EZ_CREATE_SIMPLE_TEST(Utility, CommandLineUtils) { EZ_TEST_BLOCK(ezTestBlock::Enabled, "GetParameterCount / GetParameter") { const int argc = 9; const char* argv[argc] = {"bla/blub/myprogram.exe", "-Test1", "true", "-Test2", "off", "-Test3", "-Test4", "on", "-Test5"}; ezCommandLineUtils CmdLn; CmdLn.SetCommandLine(argc, argv); EZ_TEST_INT(CmdLn.GetParameterCount(), 9); EZ_TEST_STRING(CmdLn.GetParameter(0), "bla/blub/myprogram.exe"); EZ_TEST_STRING(CmdLn.GetParameter(1), "-Test1"); EZ_TEST_STRING(CmdLn.GetParameter(2), "true"); EZ_TEST_STRING(CmdLn.GetParameter(3), "-Test2"); EZ_TEST_STRING(CmdLn.GetParameter(4), "off"); EZ_TEST_STRING(CmdLn.GetParameter(5), "-Test3"); EZ_TEST_STRING(CmdLn.GetParameter(6), "-Test4"); EZ_TEST_STRING(CmdLn.GetParameter(7), "on"); EZ_TEST_STRING(CmdLn.GetParameter(8), "-Test5"); CmdLn.InjectCustomArgument("-duh"); EZ_TEST_INT(CmdLn.GetParameterCount(), 10); EZ_TEST_STRING(CmdLn.GetParameter(9), "-duh"); CmdLn.InjectCustomArgument("I need my Space"); EZ_TEST_INT(CmdLn.GetParameterCount(), 11); EZ_TEST_STRING(CmdLn.GetParameter(10), "I need my Space"); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "GetOptionIndex / GetStringOptionArguments / GetStringOption") { const int argc = 15; const char* argv[argc] = {"bla/blub/myprogram.exe", "-opt1", "true", "false", "-opt2", "\"test2\"", "-opt3", "-opt4", "one", "two = three", "four", " five ", " six ", "-opt5", "-opt6"}; ezCommandLineUtils CmdLn; CmdLn.SetCommandLine(argc, argv); EZ_TEST_INT(CmdLn.GetOptionIndex("-opt1"), 1); EZ_TEST_INT(CmdLn.GetOptionIndex("-opt2"), 4); EZ_TEST_INT(CmdLn.GetOptionIndex("-opt3"), 6); EZ_TEST_INT(CmdLn.GetOptionIndex("-opt4"), 7); EZ_TEST_INT(CmdLn.GetOptionIndex("-opt5"), 13); EZ_TEST_INT(CmdLn.GetOptionIndex("-opt6"), 14); EZ_TEST_INT(CmdLn.GetStringOptionArguments("-opt1"), 2); EZ_TEST_INT(CmdLn.GetStringOptionArguments("-opt2"), 1); EZ_TEST_INT(CmdLn.GetStringOptionArguments("-opt3"), 0); EZ_TEST_INT(CmdLn.GetStringOptionArguments("-opt4"), 5); EZ_TEST_INT(CmdLn.GetStringOptionArguments("-opt5"), 0); EZ_TEST_INT(CmdLn.GetStringOptionArguments("-opt6"), 0); EZ_TEST_STRING(CmdLn.GetStringOption("-opt1", 0), "true"); EZ_TEST_STRING(CmdLn.GetStringOption("-opt1", 1), "false"); EZ_TEST_STRING(CmdLn.GetStringOption("-opt1", 2, "end"), "end"); // GetStringOption will trim " at the start and end to match the windows command line parsing behaviour on all platforms. EZ_TEST_STRING(CmdLn.GetStringOption("-opt2", 0), "test2"); EZ_TEST_STRING(CmdLn.GetStringOption("-opt2", 1, "end"), "end"); EZ_TEST_STRING(CmdLn.GetStringOption("-opt3", 0, "end"), "end"); EZ_TEST_STRING(CmdLn.GetStringOption("-opt4", 0), "one"); EZ_TEST_STRING(CmdLn.GetStringOption("-opt4", 1), "two = three"); EZ_TEST_STRING(CmdLn.GetStringOption("-opt4", 2), "four"); EZ_TEST_STRING(CmdLn.GetStringOption("-opt4", 3), " five "); EZ_TEST_STRING(CmdLn.GetStringOption("-opt4", 4), " six "); EZ_TEST_STRING(CmdLn.GetStringOption("-opt4", 5, "end"), "end"); EZ_TEST_STRING(CmdLn.GetStringOption("-opt5", 0), ""); EZ_TEST_STRING(CmdLn.GetStringOption("-opt6", 0), ""); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "GetBoolOption") { const int argc = 9; const char* argv[argc] = {"bla/blub/myprogram.exe", "-Test1", "true", "-Test2", "off", "-Test3", "-Test4", "on", "-Test5"}; ezCommandLineUtils CmdLn; CmdLn.SetCommandLine(argc, argv); // case sensitive and wrong EZ_TEST_BOOL(CmdLn.GetBoolOption("-test1", true, true) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-test1", false, true) == false); EZ_TEST_BOOL(CmdLn.GetBoolOption("-test2", true, true) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-test2", false, true) == false); // case insensitive and wrong EZ_TEST_BOOL(CmdLn.GetBoolOption("-test1", true) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-test1", false) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-test2", true) == false); EZ_TEST_BOOL(CmdLn.GetBoolOption("-test2", false) == false); // case sensitive and correct EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test1", true) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test1", false) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test2", true) == false); EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test2", false) == false); EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test3", true) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test3", false) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test4", true) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test4", false) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test5", true) == true); EZ_TEST_BOOL(CmdLn.GetBoolOption("-Test5", false) == true); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "GetIntOption") { const int argc = 9; const char* argv[argc] = {"bla/blub/myprogram.exe", "-Test1", "23", "-Test2", "42", "-Test3", "-Test4", "-11", "-Test5"}; ezCommandLineUtils CmdLn; CmdLn.SetCommandLine(argc, argv); // case sensitive and wrong EZ_TEST_INT(CmdLn.GetIntOption("-test1", 2, true), 2); EZ_TEST_INT(CmdLn.GetIntOption("-test2", 17, true), 17); // case insensitive and wrong EZ_TEST_INT(CmdLn.GetIntOption("-test1", 2), 23); EZ_TEST_INT(CmdLn.GetIntOption("-test2", 17), 42); // case sensitive and correct EZ_TEST_INT(CmdLn.GetIntOption("-Test1", 2), 23); EZ_TEST_INT(CmdLn.GetIntOption("-Test2", 3), 42); EZ_TEST_INT(CmdLn.GetIntOption("-Test3", 4), 4); EZ_TEST_INT(CmdLn.GetIntOption("-Test4", 5), -11); EZ_TEST_INT(CmdLn.GetIntOption("-Test5"), 0); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "GetUIntOption") { const int argc = 9; const char* argv[argc] = {"bla/blub/myprogram.exe", "-Test1", "23", "-Test2", "42", "-Test3", "-Test4", "-11", "-Test5"}; ezCommandLineUtils CmdLn; CmdLn.SetCommandLine(argc, argv); // case sensitive and wrong EZ_TEST_INT(CmdLn.GetUIntOption("-test1", 2, true), 2); EZ_TEST_INT(CmdLn.GetUIntOption("-test2", 17, true), 17); // case insensitive and wrong EZ_TEST_INT(CmdLn.GetUIntOption("-test1", 2), 23); EZ_TEST_INT(CmdLn.GetUIntOption("-test2", 17), 42); // case sensitive and correct EZ_TEST_INT(CmdLn.GetUIntOption("-Test1", 2), 23); EZ_TEST_INT(CmdLn.GetUIntOption("-Test2", 3), 42); EZ_TEST_INT(CmdLn.GetUIntOption("-Test3", 4), 4); EZ_TEST_INT(CmdLn.GetUIntOption("-Test4", 5), 5); EZ_TEST_INT(CmdLn.GetUIntOption("-Test5"), 0); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "GetFloatOption") { const int argc = 9; const char* argv[argc] = {"bla/blub/myprogram.exe", "-Test1", "23.45", "-Test2", "42.3", "-Test3", "-Test4", "-11", "-Test5"}; ezCommandLineUtils CmdLn; CmdLn.SetCommandLine(argc, argv); // case sensitive and wrong EZ_TEST_DOUBLE(CmdLn.GetFloatOption("-test1", 2.3, true), 2.3, 0.0); EZ_TEST_DOUBLE(CmdLn.GetFloatOption("-test2", 17.8, true), 17.8, 0.0); // case insensitive and wrong EZ_TEST_DOUBLE(CmdLn.GetFloatOption("-test1", 2.3), 23.45, 0.0); EZ_TEST_DOUBLE(CmdLn.GetFloatOption("-test2", 17.8), 42.3, 0.0); // case sensitive and correct EZ_TEST_DOUBLE(CmdLn.GetFloatOption("-Test1", 2.3), 23.45, 0.0); EZ_TEST_DOUBLE(CmdLn.GetFloatOption("-Test2", 3.4), 42.3, 0.0); EZ_TEST_DOUBLE(CmdLn.GetFloatOption("-Test3", 4.5), 4.5, 0.0); EZ_TEST_DOUBLE(CmdLn.GetFloatOption("-Test4", 5.6), -11, 0.0); EZ_TEST_DOUBLE(CmdLn.GetFloatOption("-Test5"), 0, 0.0); } EZ_TEST_BLOCK(ezTestBlock::Enabled, "SplitCommandLineString") { // Simple unquoted arguments. { ezDynamicArray<ezString> args; ezDynamicArray<const char*> argv; ezCommandLineUtils::SplitCommandLineString("-run -noGui -all", false, args, argv); EZ_TEST_INT(args.GetCount(), 3); EZ_TEST_STRING(args[0], "-run"); EZ_TEST_STRING(args[1], "-noGui"); EZ_TEST_STRING(args[2], "-all"); EZ_TEST_INT(argv.GetCount(), 3); } // Quoted argument with spaces. { ezDynamicArray<ezString> args; ezDynamicArray<const char*> argv; ezCommandLineUtils::SplitCommandLineString("-output \"C:/My Folder/file.txt\" -verbose", false, args, argv); EZ_TEST_INT(args.GetCount(), 3); EZ_TEST_STRING(args[0], "-output"); EZ_TEST_STRING(args[1], "C:/My Folder/file.txt"); EZ_TEST_STRING(args[2], "-verbose"); } // Quoted argument without spaces (quotes should still be stripped). { ezDynamicArray<ezString> args; ezDynamicArray<const char*> argv; ezCommandLineUtils::SplitCommandLineString("-name \"hello\" -count 3", false, args, argv); EZ_TEST_INT(args.GetCount(), 4); EZ_TEST_STRING(args[0], "-name"); EZ_TEST_STRING(args[1], "hello"); EZ_TEST_STRING(args[2], "-count"); EZ_TEST_STRING(args[3], "3"); } // Multiple spaces between arguments. { ezDynamicArray<ezString> args; ezDynamicArray<const char*> argv; ezCommandLineUtils::SplitCommandLineString(" -a -b ", false, args, argv); EZ_TEST_INT(args.GetCount(), 2); EZ_TEST_STRING(args[0], "-a"); EZ_TEST_STRING(args[1], "-b"); } // Empty string. { ezDynamicArray<ezString> args; ezDynamicArray<const char*> argv; ezCommandLineUtils::SplitCommandLineString("", false, args, argv); EZ_TEST_INT(args.GetCount(), 0); EZ_TEST_INT(argv.GetCount(), 0); } } }