From e00ff4ba6a5bebcc47ec87ee9152d6e4367408b4 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 5 Mar 2022 17:19:25 +0100 Subject: [PATCH] Test: Change expect macros to support optional parameters --- test/macro.h | 61 ++++++++++++++++++++++++++++++++------- test/unit/testdotfile.cpp | 22 +++++++------- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/test/macro.h b/test/macro.h index 11ec29b..f1bd24c 100644 --- a/test/macro.h +++ b/test/macro.h @@ -4,35 +4,76 @@ #include // fprintf #include // cerr -#define VERIFY(x, result) \ - if (!(x)) { \ - fprintf(stderr, " \033[31;1mFAIL:\033[0m %s:%d: VERIFY(%s) failed\n", \ - __FILE__, __LINE__, #x); \ - Test::TestSuite::the().currentTestCaseFailed(); \ - result; \ - } +#define GET_2TH_ARG(arg1, arg2, ...) arg2 +#define GET_3TH_ARG(arg1, arg2, arg3, ...) arg3 +#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4 +#define MACRO_CHOOSER_1(macro, ...) \ + GET_2TH_ARG(__VA_ARGS__, macro##_1, ) +#define MACRO_CHOOSER_2(macro, ...) \ + GET_3TH_ARG(__VA_ARGS__, macro##_2, macro##_1, ) +#define MACRO_CHOOSER_3(macro, ...) \ + GET_4TH_ARG(__VA_ARGS__, macro##_3, macro##_2, macro##_1, ) -#define EXPECT(x) \ +// ----------------------------------------- + +#define EXPECT_IMPL(x, result) \ if (!(x)) { \ fprintf(stderr, " \033[31;1mFAIL:\033[0m %s:%d: EXPECT(%s) failed\n", \ __FILE__, __LINE__, #x); \ Test::TestSuite::the().currentTestCaseFailed(); \ + result; \ } -#define EXPECT_EQ(a, b) \ +#define EXPECT_1(x) \ + EXPECT_IMPL(x, (void)0) + +#define EXPECT_2(x, result) \ + EXPECT_IMPL(x, result) + +#define EXPECT(...) \ + MACRO_CHOOSER_2(EXPECT, __VA_ARGS__) \ + (__VA_ARGS__) + +// ----------------------------------------- + +#define EXPECT_EQ_IMPL(a, b, result) \ if (a != b) { \ std::cerr << " \033[31;1mFAIL:\033[0m " << __FILE__ << ":" << __LINE__ \ << ": EXPECT_EQ(" << #a << ", " << #b ") failed with" \ << " lhs='" << a << "' and rhs='" << b << "'" << std::endl; \ Test::TestSuite::the().currentTestCaseFailed(); \ + result; \ } -#define EXPECT_NE(a, b) \ +#define EXPECT_EQ_2(a, b) \ + EXPECT_EQ_IMPL(a, b, (void)0) + +#define EXPECT_EQ_3(a, b, result) \ + EXPECT_EQ_IMPL(a, b, result) + +#define EXPECT_EQ(...) \ + MACRO_CHOOSER_3(EXPECT_EQ, __VA_ARGS__) \ + (__VA_ARGS__) + +// ----------------------------------------- + +#define EXPECT_NE_IMPL(a, b, result) \ if (a == b) { \ std::cerr << " \033[31;1mFAIL:\033[0m " << __FILE__ << ":" << __LINE__ \ << ": EXPECT_NE(" << #a << ", " << #b ") failed with" \ << " lhs='" << a << "' and rhs='" << b << "'" << std::endl; \ Test::TestSuite::the().currentTestCaseFailed(); \ + result; \ } +#define EXPECT_NE_2(a, b) \ + EXPECT_NE_IMPL(a, b, (void)0) + +#define EXPECT_NE_3(a, b, result) \ + EXPECT_NE_IMPL(a, b, result) + +#define EXPECT_NE(...) \ + MACRO_CHOOSER_3(EXPECT_NE, __VA_ARGS__) \ + (__VA_ARGS__) + #endif // TEST_H diff --git a/test/unit/testdotfile.cpp b/test/unit/testdotfile.cpp index 29a20bb..f8db6f3 100644 --- a/test/unit/testdotfile.cpp +++ b/test/unit/testdotfile.cpp @@ -27,7 +27,7 @@ const size_t homeDirectorySize = homeDirectory.string().size(); void createTestDotfiles(const std::vector& fileNames, const std::vector& fileContents, bool asRoot = false) { - VERIFY(fileNames.size() == fileContents.size(), return); + EXPECT(fileNames.size() == fileContents.size(), return ); if (root && !asRoot) { setegid(Machine::the().gid()); @@ -100,8 +100,8 @@ TEST_CASE(AddDotfiles) Dotfile::the().add(fileNames); for (const auto& file : fileNames) { - VERIFY(std::filesystem::exists(file), continue); - VERIFY(std::filesystem::exists(file.substr(homeDirectorySize + 1)), continue); + EXPECT(std::filesystem::exists(file), continue); + EXPECT(std::filesystem::exists(file.substr(homeDirectorySize + 1)), continue); Util::File lhs(file); Util::File rhs(file.substr(homeDirectorySize + 1)); @@ -156,8 +156,8 @@ TEST_CASE(PullDotfiles) Dotfile::the().pull(fileNames); for (size_t i = 0; i < fileNames.size(); ++i) { - VERIFY(std::filesystem::exists(homeFileNames.at(i)), continue); - VERIFY(std::filesystem::exists(fileNames.at(i)), continue); + EXPECT(std::filesystem::exists(homeFileNames.at(i)), continue); + EXPECT(std::filesystem::exists(fileNames.at(i)), continue); Util::File lhs(homeFileNames.at(i)); Util::File rhs(fileNames.at(i)); @@ -189,8 +189,8 @@ TEST_CASE(PushDotfiles) Dotfile::the().push(fileNames); for (const auto& file : fileNames) { - VERIFY(std::filesystem::exists(file), continue); - VERIFY(std::filesystem::exists(homeDirectory / file), continue); + EXPECT(std::filesystem::exists(file), continue); + EXPECT(std::filesystem::exists(homeDirectory / file), continue); Util::File lhs(file); Util::File rhs(homeDirectory / file); @@ -580,8 +580,8 @@ test data /**/ uncomment for (size_t i = 0; i < fileNames.size(); ++i) { const auto& file = fileNames.at(i); - VERIFY(std::filesystem::exists(file), continue); - VERIFY(std::filesystem::exists(homeDirectory / file), continue); + EXPECT(std::filesystem::exists(file), continue); + EXPECT(std::filesystem::exists(homeDirectory / file), continue); Util::File lhs(homeDirectory / file); EXPECT_EQ(lhs.data(), pushedFileContents.at(i)); @@ -592,7 +592,7 @@ test data /**/ uncomment TEST_CASE(AddSystemDotfiles) { - VERIFY(geteuid() == 0, return); + EXPECT(geteuid() == 0, return); Config::the().setSystemDirectories({ "/etc", "/usr/lib" }); Dotfile::the().add({ "/etc/group", "/usr/lib/os-release" }); @@ -607,7 +607,7 @@ TEST_CASE(AddSystemDotfiles) TEST_CASE(PullSystemDotfiles) { - VERIFY(geteuid() == 0, return); + EXPECT(geteuid() == 0, return); createTestDotfiles({ "etc/group" }, { "" }, true);