Changeset View
Standalone View
source/ps/GameSetup/tests/test_CmdLineArgs.h
/* Copyright (C) 2018 Wildfire Games. | /* Copyright (C) 2018 Wildfire Games. | ||||
Lint: Inaccurate Copyright Year: Inaccurate Copyright Year | |||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 9 Lines | |||||
#include "ps/GameSetup/CmdLineArgs.h" | #include "ps/GameSetup/CmdLineArgs.h" | ||||
class TestCmdLineArgs : public CxxTest::TestSuite | class TestCmdLineArgs : public CxxTest::TestSuite | ||||
{ | { | ||||
public: | public: | ||||
void test_has() | void test_has() | ||||
{ | { | ||||
const char* argv[] = { "program", "-test2" }; | constexpr std::array argv = { "program", "-test2" }; | ||||
StanUnsubmitted Done Inline Actionsinclude std::array Stan: include std::array | |||||
CmdLineArgs c(ARRAY_SIZE(argv), argv); | CmdLineArgs c(argv); | ||||
TS_ASSERT(!c.Has("test1")); | TS_ASSERT(!c.Has("test1")); | ||||
TS_ASSERT(c.Has("test2")); | TS_ASSERT(c.Has("test2")); | ||||
TS_ASSERT(!c.Has("test3")); | TS_ASSERT(!c.Has("test3")); | ||||
TS_ASSERT(!c.Has("")); | TS_ASSERT(!c.Has("")); | ||||
} | } | ||||
void test_get() | void test_get() | ||||
{ | { | ||||
const char* argv[] = { "program", "-test1=", "--test2=x", "-test3=-y=y-", "-=z" }; | constexpr std::array argv = { "program", "-test1=", "--test2=x", "-test3=-y=y-", "-=z" }; | ||||
CmdLineArgs c(ARRAY_SIZE(argv), argv); | CmdLineArgs c(argv); | ||||
Done Inline ActionsIs the std::string type inferred from usage or should it be specified for correctness? Stan: Is the std::string type inferred from usage or should it be specified for correctness? | |||||
Done Inline ActionsThis is a std::array<const char*, 5>. I didn't specify the template because this way it is easier to maintain (its easy to add an argument). phosit: This is a `std::array<const char*, 5>`. I didn't specify the template because this way it is… | |||||
Done Inline ActionsThe fact that I didn't know whether it was a std::string or a const char* might point to yes. @wraitii and @vladislavbelov might have more input. Should probably be added to the https://trac.wildfiregames.com/wiki/Coding_Conventions after decision. Stan: The fact that I didn't know whether it was a std::string or a const char* might point to yes. | |||||
Not Done Inline ActionsI don't have a strong opinion on that. Generally I prefer consistency and explicit relations. So as Stan said it might be similar to the auto rule - use as rare as possible and only when type is obvious. In that case the type is simple and it's a test, so a pure std::array might be enough if it compiles on all platforms. vladislavbelov: I don't have a strong opinion on that. Generally I prefer consistency and explicit relations. | |||||
TS_ASSERT(!c.Has("program")); | TS_ASSERT(!c.Has("program")); | ||||
TS_ASSERT_STR_EQUALS(c.Get("test0"), ""); | TS_ASSERT_STR_EQUALS(c.Get("test0"), ""); | ||||
TS_ASSERT_STR_EQUALS(c.Get("test1"), ""); | TS_ASSERT_STR_EQUALS(c.Get("test1"), ""); | ||||
TS_ASSERT_STR_EQUALS(c.Get("test2"), "x"); | TS_ASSERT_STR_EQUALS(c.Get("test2"), "x"); | ||||
TS_ASSERT_STR_EQUALS(c.Get("test3"), "-y=y-"); | TS_ASSERT_STR_EQUALS(c.Get("test3"), "-y=y-"); | ||||
TS_ASSERT_STR_EQUALS(c.Get(""), "z"); | TS_ASSERT_STR_EQUALS(c.Get(""), "z"); | ||||
} | } | ||||
void test_multiple() | void test_multiple() | ||||
{ | { | ||||
const char* argv[] = { "program", "-test1=one", "--test1=two", "-test2=none", "-test1=three" }; | constexpr std::array argv = | ||||
CmdLineArgs c(ARRAY_SIZE(argv), argv); | { | ||||
"program", "-test1=one", "--test1=two", "-test2=none", "-test1=three" | |||||
}; | |||||
CmdLineArgs c(argv); | |||||
TS_ASSERT_STR_EQUALS(c.Get("test1"), "one"); | TS_ASSERT_STR_EQUALS(c.Get("test1"), "one"); | ||||
TS_ASSERT_STR_EQUALS(c.Get("test2"), "none"); | TS_ASSERT_STR_EQUALS(c.Get("test2"), "none"); | ||||
std::vector<CStr> test1 = c.GetMultiple("test1"); | std::vector<CStr> test1 = c.GetMultiple("test1"); | ||||
CStr expected1[] = { "one", "two", "three" }; | CStr expected1[] = { "one", "two", "three" }; | ||||
TS_ASSERT_VECTOR_EQUALS_ARRAY(test1, expected1); | TS_ASSERT_VECTOR_EQUALS_ARRAY(test1, expected1); | ||||
std::vector<CStr> test2 = c.GetMultiple("test2"); | std::vector<CStr> test2 = c.GetMultiple("test2"); | ||||
CStr expected2[] = { "none" }; | CStr expected2[] = { "none" }; | ||||
TS_ASSERT_VECTOR_EQUALS_ARRAY(test2, expected2); | TS_ASSERT_VECTOR_EQUALS_ARRAY(test2, expected2); | ||||
std::vector<CStr> test3 = c.GetMultiple("test3"); | std::vector<CStr> test3 = c.GetMultiple("test3"); | ||||
TS_ASSERT_EQUALS(test3.size(), (size_t)0); | TS_ASSERT_EQUALS(test3.size(), (size_t)0); | ||||
} | } | ||||
void test_get_invalid() | void test_get_invalid() | ||||
{ | { | ||||
const char* argv[] = { | constexpr std::array argv = { | ||||
"-test1", "--test2", "test3-", " -test4", "--", "-==" | "-test1", "--test2", "test3-", " -test4", "--", "-==" | ||||
}; | }; | ||||
CmdLineArgs c(ARRAY_SIZE(argv), argv); | CmdLineArgs c(argv); | ||||
TS_ASSERT(!c.Has("test1")); | TS_ASSERT(!c.Has("test1")); | ||||
TS_ASSERT(c.Has("test2")); | TS_ASSERT(c.Has("test2")); | ||||
TS_ASSERT(!c.Has("test3")); | TS_ASSERT(!c.Has("test3")); | ||||
TS_ASSERT(!c.Has("test4")); | TS_ASSERT(!c.Has("test4")); | ||||
} | } | ||||
void test_arg0() | void test_arg0() | ||||
{ | { | ||||
const char* argv[] = { "program" }; | constexpr std::array argv = { "program" }; | ||||
CmdLineArgs c(ARRAY_SIZE(argv), argv); | CmdLineArgs c(argv); | ||||
TS_ASSERT_WSTR_EQUALS(c.GetArg0().string(), L"program"); | TS_ASSERT_WSTR_EQUALS(c.GetArg0().string(), L"program"); | ||||
CmdLineArgs c2(0, NULL); | CmdLineArgs c2(PS::span<const char* const>{}); | ||||
TS_ASSERT_WSTR_EQUALS(c2.GetArg0().string(), L""); | TS_ASSERT_WSTR_EQUALS(c2.GetArg0().string(), L""); | ||||
const char* argv3[] = { "ab/cd/ef/gh/../ij" }; | const std::array argv3 = { "ab/cd/ef/gh/../ij" }; | ||||
CmdLineArgs c3(ARRAY_SIZE(argv3), argv3); | CmdLineArgs c3(argv3); | ||||
#if OS_WIN | if constexpr(OS_WIN) | ||||
Not Done Inline ActionsThat's bad, because specifying platforms means some platform-dependent code. And outside of a template, all branches should be compiled. vladislavbelov: That's bad, because specifying platforms means some platform-dependent code. And outside of a… | |||||
Done Inline Actionswith if constexpr all branches will be compiled. Sometime there is a platform specific function but that does not apply here. phosit: with `if constexpr` all branches will be compiled. Sometime there is a platform specific… | |||||
Not Done Inline ActionsInconsistency is bad, we should try to use the same style for the whole project. vladislavbelov: Inconsistency is bad, we should try to use the same style for the whole project. | |||||
Not Done Inline ActionsThere is also a problem with unused variables and other warnings. vladislavbelov: There is also a problem with unused variables and other warnings. | |||||
{ | |||||
TS_ASSERT_WSTR_EQUALS(c3.GetArg0().string(), L"ab\\cd\\ef\\gh\\..\\ij"); | TS_ASSERT_WSTR_EQUALS(c3.GetArg0().string(), L"ab\\cd\\ef\\gh\\..\\ij"); | ||||
#else | } | ||||
else | |||||
{ | |||||
TS_ASSERT_WSTR_EQUALS(c3.GetArg0().string(), L"ab/cd/ef/gh/../ij"); | TS_ASSERT_WSTR_EQUALS(c3.GetArg0().string(), L"ab/cd/ef/gh/../ij"); | ||||
#endif | } | ||||
} | } | ||||
void test_get_without_names() | void test_get_without_names() | ||||
{ | { | ||||
const char* argv[] = { "program", "test0", "-test1", "test2", "test3", "--test4=test5" }; | constexpr std::array argv = | ||||
CmdLineArgs c(ARRAY_SIZE(argv), argv); | { | ||||
"program", "test0", "-test1", "test2", "test3", "--test4=test5" | |||||
}; | |||||
CmdLineArgs c(argv); | |||||
TS_ASSERT(c.Has("test1")); | TS_ASSERT(c.Has("test1")); | ||||
TS_ASSERT_STR_EQUALS(c.Get("test4"), "test5"); | TS_ASSERT_STR_EQUALS(c.Get("test4"), "test5"); | ||||
CStr expected_args[] = { "test0", "test2", "test3" }; | CStr expected_args[] = { "test0", "test2", "test3" }; | ||||
TS_ASSERT_VECTOR_EQUALS_ARRAY(c.GetArgsWithoutName(), expected_args); | TS_ASSERT_VECTOR_EQUALS_ARRAY(c.GetArgsWithoutName(), expected_args); | ||||
} | } | ||||
}; | }; |
Inaccurate Copyright Year