test treewide: apply test names best practices
1. test suite names are usually related to the function under tests, not
the whole source or header file
2. test case names should be verbose and describe the high level
behavior
This commits also splits test cases when they are for different
functions, and merges test cases when they are very similar.
Reference:
1. https://github.com/google/googletest/tree/main/googletest/samples
2. https://testing.googleblog.com/2007/02/tott-naming-unit-tests-responsibly.html
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: I498cb2076bbaa9f0829f50bca45a2e38a6f4ab7d
diff --git a/redfish-core/lib/ut/service_root_test.cpp b/redfish-core/lib/ut/service_root_test.cpp
index 06a7c21..ab0e660 100644
--- a/redfish-core/lib/ut/service_root_test.cpp
+++ b/redfish-core/lib/ut/service_root_test.cpp
@@ -104,7 +104,7 @@
EXPECT_EQ(json.size(), 21);
}
-TEST(ServiceRootTest, ServiceRootConstructor)
+TEST(HandleServiceRootGet, ServiceRootStaticAttributesAreExpected)
{
auto shareAsyncResp = std::make_shared<bmcweb::AsyncResp>();
diff --git a/redfish-core/ut/configfile_test.cpp b/redfish-core/ut/configfile_test.cpp
index 7e9c785..2c68c0b 100644
--- a/redfish-core/ut/configfile_test.cpp
+++ b/redfish-core/ut/configfile_test.cpp
@@ -10,61 +10,42 @@
namespace ibm_mc
{
-TEST(ConfigFileTest, FileNameValidChar)
+TEST(IsValidConfigFileName, FileNameValidCharReturnsTrue)
{
crow::Response res;
- const std::string fileName = "GoodConfigFile";
- EXPECT_TRUE(isValidConfigFileName(fileName, res));
+ EXPECT_TRUE(isValidConfigFileName("GoodConfigFile", res));
}
-TEST(ConfigFileTest, FileNameInvalidChar)
+TEST(IsValidConfigFileName, FileNameInvalidCharReturnsFalse)
{
crow::Response res;
- const std::string fileName = "Bad@file";
- EXPECT_FALSE(isValidConfigFileName(fileName, res));
+ EXPECT_FALSE(isValidConfigFileName("Bad@file", res));
}
-TEST(ConfigFileTest, FileNameInvalidPath1)
+TEST(IsValidConfigFileName, FileNameInvalidPathReturnsFalse)
{
crow::Response res;
- const std::string fileName = "/../../../../../etc/badpath";
- EXPECT_FALSE(isValidConfigFileName(fileName, res));
+ EXPECT_FALSE(isValidConfigFileName("/../../../../../etc/badpath", res));
+ EXPECT_FALSE(isValidConfigFileName("/../../etc/badpath", res));
+ EXPECT_FALSE(isValidConfigFileName("/mydir/configFile", res));
}
-TEST(ConfigFileTest, FileNameInvalidPath2)
+
+TEST(IsValidConfigFileName, EmptyFileNameReturnsFalse)
{
crow::Response res;
-
- const std::string fileName = "/../../etc/badpath";
- EXPECT_FALSE(isValidConfigFileName(fileName, res));
+ EXPECT_FALSE(isValidConfigFileName("", res));
}
-TEST(ConfigFileTest, FileNameInvalidPath3)
+
+TEST(IsValidConfigFileName, SlashFileNameReturnsFalse)
{
crow::Response res;
-
- const std::string fileName = "/mydir/configFile";
- EXPECT_FALSE(isValidConfigFileName(fileName, res));
+ EXPECT_FALSE(isValidConfigFileName("/", res));
}
-TEST(ConfigFileTest, FileNameNull)
+TEST(IsValidConfigFileName, FileNameMoreThan20CharReturnsFalse)
{
crow::Response res;
-
- const std::string fileName;
- EXPECT_FALSE(isValidConfigFileName(fileName, res));
-}
-TEST(ConfigFileTest, FileNameSlash)
-{
- crow::Response res;
-
- const std::string fileName = "/";
- EXPECT_FALSE(isValidConfigFileName(fileName, res));
-}
-TEST(ConfigFileTest, FileNameMorethan20Char)
-{
- crow::Response res;
-
- const std::string fileName = "BadfileBadfileBadfile";
- EXPECT_FALSE(isValidConfigFileName(fileName, res));
+ EXPECT_FALSE(isValidConfigFileName("BadfileBadfileBadfile", res));
}
} // namespace ibm_mc
diff --git a/redfish-core/ut/hex_utils_test.cpp b/redfish-core/ut/hex_utils_test.cpp
index 05bcb9e..619f6eb 100644
--- a/redfish-core/ut/hex_utils_test.cpp
+++ b/redfish-core/ut/hex_utils_test.cpp
@@ -5,7 +5,7 @@
namespace
{
-TEST(ToHexString, uint64)
+TEST(IntToHexString, ReturnsCorrectHexForUint64)
{
EXPECT_EQ(intToHexString(0xFFFFFFFFFFFFFFFFULL, 16), "FFFFFFFFFFFFFFFF");
@@ -27,12 +27,12 @@
EXPECT_EQ(intToHexString(0xBEEF, 4), "BEEF");
}
-TEST(BytesToHexString, Success)
+TEST(BytesToHexString, OnSuccess)
{
EXPECT_EQ(bytesToHexString({0x1a, 0x2b}), "1A2B");
}
-TEST(HexCharToNibble, chars)
+TEST(HexCharToNibble, ReturnsCorrectNibbleForEveryHexChar)
{
for (char c = 0; c < std::numeric_limits<char>::max(); ++c)
{
diff --git a/redfish-core/ut/ip_utils_test.cpp b/redfish-core/ut/ip_utils_test.cpp
index c27ec99..c7d600b 100644
--- a/redfish-core/ut/ip_utils_test.cpp
+++ b/redfish-core/ut/ip_utils_test.cpp
@@ -9,17 +9,25 @@
using ::boost::asio::ip::make_address;
-TEST(IpToString, v4mapped)
+TEST(IpToString, ReturnsCorrectIpStringForIpv4Addresses)
{
EXPECT_EQ(toString(make_address("127.0.0.1")), "127.0.0.1");
EXPECT_EQ(toString(make_address("192.168.1.1")), "192.168.1.1");
EXPECT_EQ(toString(make_address("::1")), "::1");
+}
+
+TEST(IpToString, ReturnsCorrectIpStringForIpv6Addresses)
+{
EXPECT_EQ(toString(make_address("fd03:f9ab:25de:89ec::0001")),
"fd03:f9ab:25de:89ec::1");
EXPECT_EQ(toString(make_address("fd03:f9ab:25de:89ec::1234:abcd")),
"fd03:f9ab:25de:89ec::1234:abcd");
EXPECT_EQ(toString(make_address("fd03:f9ab:25de:89ec:1234:5678:90ab:cdef")),
"fd03:f9ab:25de:89ec:1234:5678:90ab:cdef");
+}
+
+TEST(IpToString, ReturnsCorrectIpStringForIpv4MappedIpv6Addresses)
+{
EXPECT_EQ(toString(make_address("::ffff:127.0.0.1")), "127.0.0.1");
}
} // namespace
diff --git a/redfish-core/ut/json_utils_test.cpp b/redfish-core/ut/json_utils_test.cpp
index cd5504c..427ed21 100644
--- a/redfish-core/ut/json_utils_test.cpp
+++ b/redfish-core/ut/json_utils_test.cpp
@@ -10,7 +10,7 @@
namespace
{
-TEST(readJson, ValidElements)
+TEST(ReadJson, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly)
{
crow::Response res;
nlohmann::json jsonRequest = {{"integer", 1},
@@ -30,23 +30,26 @@
EXPECT_TRUE((vec == std::vector<uint64_t>{1, 2, 3}));
}
-TEST(readJson, ExtraElements)
+TEST(readJson, ExtraElementsReturnsFalseReponseIsBadRequest)
{
crow::Response res;
- nlohmann::json jsonRequest = {{"integer", 1}, {"string0", "hello"}};
+ nlohmann::json jsonRequest = {{"integer", 1}, {"string", "hello"}};
- int64_t integer = 0;
- std::string str;
+ std::optional<int> integer;
+ std::optional<std::string> str;
+
EXPECT_FALSE(readJson(jsonRequest, res, "integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_EQ(integer, 1);
- EXPECT_FALSE(readJson(jsonRequest, res, "string0", str));
+ EXPECT_FALSE(readJson(jsonRequest, res, "string", str));
EXPECT_EQ(res.result(), boost::beast::http::status::bad_request);
EXPECT_FALSE(res.jsonValue.empty());
+ EXPECT_EQ(str, "hello");
}
-TEST(readJson, WrongElementType)
+TEST(ReadJson, WrongElementTypeReturnsFalseReponseIsBadRequest)
{
crow::Response res;
nlohmann::json jsonRequest = {{"integer", 1}, {"string0", "hello"}};
@@ -67,7 +70,7 @@
EXPECT_FALSE(res.jsonValue.empty());
}
-TEST(readJson, MissingElement)
+TEST(ReadJson, MissingElementReturnsFalseReponseIsBadRequest)
{
crow::Response res;
nlohmann::json jsonRequest = {{"integer", 1}, {"string0", "hello"}};
@@ -87,7 +90,7 @@
EXPECT_FALSE(res.jsonValue.empty());
}
-TEST(readJson, JsonVector)
+TEST(ReadJson, JsonArrayAreUnpackedCorrectly)
{
crow::Response res;
nlohmann::json jsonRequest = R"(
@@ -100,9 +103,10 @@
EXPECT_TRUE(readJson(jsonRequest, res, "TestJson", jsonVec));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_EQ(jsonVec, R"([{"hello": "yes"}, [{"there": "no"}, "nice"]])"_json);
}
-TEST(readJson, JsonSubElementValue)
+TEST(ReadJson, JsonSubElementValueAreUnpackedCorrectly)
{
crow::Response res;
nlohmann::json jsonRequest = R"(
@@ -118,7 +122,7 @@
EXPECT_TRUE(res.jsonValue.empty());
}
-TEST(readJson, JsonSubElementValueDepth2)
+TEST(ReadJson, JsonDeeperSubElementValueAreUnpackedCorrectly)
{
crow::Response res;
nlohmann::json jsonRequest = R"(
@@ -136,7 +140,7 @@
EXPECT_TRUE(res.jsonValue.empty());
}
-TEST(readJson, JsonSubElementValueMultiple)
+TEST(ReadJson, MultipleJsonSubElementValueAreUnpackedCorrectly)
{
crow::Response res;
nlohmann::json jsonRequest = R"(
@@ -161,7 +165,7 @@
EXPECT_TRUE(res.jsonValue.empty());
}
-TEST(readJson, ExtraElement)
+TEST(ReadJson, ExtraElement)
{
crow::Response res;
nlohmann::json jsonRequest = {{"integer", 1}, {"string", "hello"}};
@@ -180,7 +184,7 @@
EXPECT_EQ(str, "hello");
}
-TEST(readJson, ValidMissingElement)
+TEST(ReadJson, ValidMissingElementReturnsTrue)
{
crow::Response res;
nlohmann::json jsonRequest = {{"integer", 1}};
@@ -217,7 +221,7 @@
EXPECT_EQ(str1, std::nullopt);
}
-TEST(readJson, InvalidMissingElement)
+TEST(ReadJson, InvalidMissingElementReturnsFalse)
{
crow::Response res;
nlohmann::json jsonRequest = {{"integer", 1}, {"string", "hello"}};
@@ -245,7 +249,7 @@
EXPECT_FALSE(res.jsonValue.empty());
}
-TEST(readJsonPatch, ValidElements)
+TEST(ReadJsonPatch, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly)
{
crow::Response res;
std::error_code ec;
@@ -259,7 +263,7 @@
EXPECT_TRUE(res.jsonValue.empty());
}
-TEST(readJsonPatch, EmptyObjectDisallowed)
+TEST(ReadJsonPatch, EmptyObjectReturnsFalseResponseBadRequest)
{
crow::Response res;
std::error_code ec;
@@ -273,7 +277,7 @@
EXPECT_FALSE(res.jsonValue.empty());
}
-TEST(readJsonPatch, OdataIgnored)
+TEST(ReadJsonPatch, OdataIgnored)
{
crow::Response res;
std::error_code ec;
@@ -287,7 +291,7 @@
EXPECT_TRUE(res.jsonValue.empty());
}
-TEST(readJsonPatch, OnlyOdataGivesNoOperation)
+TEST(ReadJsonPatch, OnlyOdataGivesNoOperation)
{
crow::Response res;
std::error_code ec;
@@ -301,7 +305,7 @@
EXPECT_FALSE(res.jsonValue.empty());
}
-TEST(readJsonAction, ValidElements)
+TEST(ReadJsonAction, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly)
{
crow::Response res;
std::error_code ec;
@@ -313,9 +317,10 @@
EXPECT_TRUE(readJsonAction(req, res, "integer", integer));
EXPECT_EQ(res.result(), boost::beast::http::status::ok);
EXPECT_TRUE(res.jsonValue.empty());
+ EXPECT_EQ(integer, 1);
}
-TEST(readJsonAction, EmptyObjectAllowed)
+TEST(ReadJsonAction, EmptyObjectReturnsTrueResponseOk)
{
crow::Response res;
std::error_code ec;
diff --git a/redfish-core/ut/registries_test.cpp b/redfish-core/ut/registries_test.cpp
index c324fee..758d8b3 100644
--- a/redfish-core/ut/registries_test.cpp
+++ b/redfish-core/ut/registries_test.cpp
@@ -7,7 +7,7 @@
namespace
{
-TEST(RedfishRegistries, fillMessageArgs)
+TEST(FillMessageArgs, ArgsAreFilledCorrectly)
{
std::string toFill("%1");
fillMessageArgs({{"foo"}}, toFill);
diff --git a/redfish-core/ut/stl_utils_test.cpp b/redfish-core/ut/stl_utils_test.cpp
index 02a9c15..7242200 100644
--- a/redfish-core/ut/stl_utils_test.cpp
+++ b/redfish-core/ut/stl_utils_test.cpp
@@ -8,13 +8,16 @@
{
namespace
{
-TEST(STLUtilesTest, RemoveDuplicates)
+TEST(FirstDuplicate, ReturnsIteratorToFirstDuplicate)
{
std::vector<std::string> strVec = {"s1", "s4", "s1", "s2", "", "s3", "s3"};
-
auto iter = firstDuplicate(strVec.begin(), strVec.end());
EXPECT_EQ(*iter, "s3");
+}
+TEST(RemoveDuplicates, AllDuplicatesAreRempvedInplace)
+{
+ std::vector<std::string> strVec = {"s1", "s4", "s1", "s2", "", "s3", "s3"};
removeDuplicate(strVec);
EXPECT_EQ(strVec.size(), 5);