From 3a85aaadb82872406ad7d031a27c16bfad777777 Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Wed, 20 Dec 2023 18:46:48 +0530 Subject: [PATCH] Use gmock matchers for more idiomatic assertions/expectations. --- tests/integration/BasicTests.cpp | 12 +++++------ tests/integration/MegaCmdTestingTools.cpp | 14 ++++++++----- tests/integration/MegaCmdTestingTools.h | 25 ----------------------- 3 files changed, 15 insertions(+), 36 deletions(-) diff --git a/tests/integration/BasicTests.cpp b/tests/integration/BasicTests.cpp index b2926df4..2e607f02 100644 --- a/tests/integration/BasicTests.cpp +++ b/tests/integration/BasicTests.cpp @@ -38,13 +38,13 @@ TEST_F(NOINTERACTIVEReadTest, Find) auto r = executeInClient({"find"}); std::vector result_paths = splitByNewline(r.out()); - ASSERT_FALSE(result_paths.empty()); + ASSERT_THAT(result_paths, testing::Not(testing::IsEmpty())); - EXPECT_CONTAINS(result_paths, "."); - EXPECT_CONTAINS(result_paths, "testReadingFolder01"); - EXPECT_CONTAINS(result_paths, "testReadingFolder01/file03.txt"); - EXPECT_CONTAINS(result_paths, "testReadingFolder01/folder01/file03.txt"); - EXPECT_CONTAINS(result_paths, "testReadingFolder01/folder02/subfolder03/file02.txt"); + EXPECT_THAT(result_paths, testing::Contains(".")); + EXPECT_THAT(result_paths, testing::Contains("testReadingFolder01")); + EXPECT_THAT(result_paths, testing::Contains("testReadingFolder01/file03.txt")); + EXPECT_THAT(result_paths, testing::Contains("testReadingFolder01/folder01/file03.txt")); + EXPECT_THAT(result_paths, testing::Contains("testReadingFolder01/folder02/subfolder03/file02.txt")); } TEST_F(NOINTERACTIVELoggedInTest, Whoami) diff --git a/tests/integration/MegaCmdTestingTools.cpp b/tests/integration/MegaCmdTestingTools.cpp index c711f678..4f18656b 100644 --- a/tests/integration/MegaCmdTestingTools.cpp +++ b/tests/integration/MegaCmdTestingTools.cpp @@ -13,6 +13,8 @@ * program. */ +#include + #include "MegaCmdTestingTools.h" std::vector splitByNewline(const std::string& str) @@ -57,14 +59,16 @@ void ensureLoggedIn() const char* user = getenv("MEGACMD_TEST_USER"); const char* pass = getenv("MEGACMD_TEST_PASS"); - if (!user || !pass) - { - std::cerr << "ENSURE MEGACMD_TEST_USER and MEGACMD_TEST_PASS are set!" << std::endl; - ASSERT_FALSE("MISSING USER/PASS env variables"); - } + ASSERT_THAT(user, testing::AllOf(testing::NotNull(), testing::Not(testing::IsEmpty()))) << "Missing testing user env variable. Ensure that MEGACMD_TEST_USER is set!"; + ASSERT_THAT(user, testing::AllOf(testing::NotNull(), testing::Not(testing::IsEmpty()))) + << "Missing testing user password env variable. Ensure that MEGACMD_TEST_PASS is set!"; auto result = executeInClient({"login", user, pass}); ASSERT_EQ(0, result.status()); + + result = executeInClient({"whoami"}); + ASSERT_EQ(0, result.status()); + ASSERT_THAT(result.out(), testing::HasSubstr(user)); } } diff --git a/tests/integration/MegaCmdTestingTools.h b/tests/integration/MegaCmdTestingTools.h index 5858b6f0..081c1e40 100644 --- a/tests/integration/MegaCmdTestingTools.h +++ b/tests/integration/MegaCmdTestingTools.h @@ -27,31 +27,6 @@ #include #include -template -testing::AssertionResult AssertContains(const char* container_expr, const char* item_expr, C container, I item) -{ - if (std::find(container.begin(), container.end(), item) != container.end()) - { - return testing::AssertionSuccess(); - } - return testing::AssertionFailure() << "`" << container_expr << "` does not contain item " << item_expr; -} - -template -testing::AssertionResult AssertNotContains(const char* container_expr, const char* item_expr, C container, I item) -{ - if (std::find(container.begin(), container.end(), item) == container.end()) - { - return testing::AssertionSuccess(); - } - return testing::AssertionFailure() << "`" << container_expr << "` contains item " << item_expr; -} - -#define ASSERT_CONTAINS(container, item) ASSERT_PRED_FORMAT2(AssertContains, container, item) -#define ASSERT_NCONTAINS(container, item) ASSERT_PRED_FORMAT2(AssertNotContains, container, item) -#define EXPECT_CONTAINS(container, item) EXPECT_PRED_FORMAT2(AssertContains, container, item) -#define EXPECT_NCONTAINS(container, item) EXPECT_PRED_FORMAT2(AssertNotContains, container, item) - std::vector splitByNewline(const std::string& str); class ClientResponse