protot/3rdparty/bx/tests/tokenizecmd_test.cpp

83 lines
1.7 KiB
C++
Raw Normal View History

2016-11-10 21:53:08 +01:00
/*
2018-02-03 17:39:28 +01:00
* Copyright 2012-2018 Branimir Karadzic. All rights reserved.
2016-11-10 21:53:08 +01:00
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
*/
#include "test.h"
#include <bx/commandline.h>
2017-04-11 08:16:10 +02:00
#include <bx/string.h>
2016-11-10 21:53:08 +01:00
2017-04-11 08:16:10 +02:00
TEST_CASE("commandLine", "")
2016-11-10 21:53:08 +01:00
{
const char* args[] =
{
"-s",
"--long",
2017-04-11 08:16:10 +02:00
"--platform",
"x",
2018-02-03 17:39:28 +01:00
"--num", "1389",
"--foo",
"--", // it should not parse arguments after argument terminator
"--bar",
2016-11-10 21:53:08 +01:00
};
bx::CommandLine cmdLine(BX_COUNTOF(args), args);
2018-02-03 17:39:28 +01:00
REQUIRE( cmdLine.hasArg("long") );
REQUIRE( cmdLine.hasArg('s') );
int32_t num;
REQUIRE(cmdLine.hasArg(num, '\0', "num") );
REQUIRE(1389 == num);
// test argument terminator
REQUIRE( cmdLine.hasArg("foo") );
REQUIRE(!cmdLine.hasArg("bar") );
2016-11-10 21:53:08 +01:00
// non-existing argument
2017-04-11 08:16:10 +02:00
REQUIRE(!cmdLine.hasArg('x') );
REQUIRE(!cmdLine.hasArg("preprocess") );
2016-11-10 21:53:08 +01:00
}
2017-04-11 08:16:10 +02:00
static bool test(const char* _input, int32_t _argc, ...)
2016-11-10 21:53:08 +01:00
{
2017-04-11 08:16:10 +02:00
char buffer[1024];
uint32_t len = sizeof(buffer);
char* argv[32];
int32_t argc;
bx::tokenizeCommandLine(_input, buffer, len, argc, argv, BX_COUNTOF(argv) );
2016-11-10 21:53:08 +01:00
2017-04-11 08:16:10 +02:00
if (_argc != argc)
2016-11-10 21:53:08 +01:00
{
2017-04-11 08:16:10 +02:00
return false;
}
2016-11-10 21:53:08 +01:00
2017-04-11 08:16:10 +02:00
va_list argList;
va_start(argList, _argc);
2016-11-10 21:53:08 +01:00
2017-04-11 08:16:10 +02:00
for (int32_t ii = 0; ii < _argc; ++ii)
2016-11-10 21:53:08 +01:00
{
2017-04-11 08:16:10 +02:00
const char* arg = va_arg(argList, const char*);
2018-02-03 17:39:28 +01:00
if (0 != bx::strCmp(argv[ii], arg) )
2016-11-10 21:53:08 +01:00
{
2017-04-11 08:16:10 +02:00
return false;
2016-11-10 21:53:08 +01:00
}
}
2017-04-11 08:16:10 +02:00
va_end(argList);
return true;
}
TEST_CASE("tokenizeCommandLine", "")
{
REQUIRE(test(" ", 0, NULL) );
REQUIRE(test("\\", 0, NULL) );
REQUIRE(test("a b v g d", 5, "a", "b", "v", "g", "d") );
REQUIRE(test("\"ab\\\"v\" \"\\\\\" g", 3, "ab\"v", "\\", "g") );
REQUIRE(test("a\\\\\\\"b v g", 3, "a\\\"b", "v", "g") );
REQUIRE(test("a\\\\\\\\\"b v\" g d", 3, "a\\\\b v", "g", "d") );
2016-11-10 21:53:08 +01:00
}