blob: 81ba8b4a91e86a7344e74c4613626cfea85d93eb [file]
#ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_BMC_TEST_UTIL_H_
#define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_BMC_TEST_UTIL_H_
#include <filesystem> // NOLINT(build/c++17)
#include <string>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/util/message_differencer.h"
namespace testing::proto {
namespace internal {
template <typename Proto>
bool CompareIgnoringRepeatedOrdering(const Proto& actual,
const Proto& expected) {
::google::protobuf::util::MessageDifferencer differencer;
differencer.set_repeated_field_comparison(
::google::protobuf::util::MessageDifferencer::AS_SET);
return differencer.Compare(actual, expected);
}
template <typename Proto>
bool CompareIgnoringRepeatedOrdering(const Proto& actual,
absl::string_view expected_str) {
Proto expected;
if (!::google::protobuf::TextFormat::ParseFromString(expected_str, &expected)) {
LOG(FATAL) << "Failed to parse expected proto: " << expected_str;
}
return CompareIgnoringRepeatedOrdering(actual, expected);
}
} // namespace internal
MATCHER_P(EqualsProto, expected, "") {
return ::google::protobuf::util::MessageDifferencer::Equals(arg, expected);
}
MATCHER_P(IgnoringRepeatedFieldOrderingEqualsProto, expected, "") {
return internal::CompareIgnoringRepeatedOrdering(arg, expected);
}
} // namespace testing::proto
namespace testing::status {
// NOTE does not use the message in the status.
MATCHER_P(StatusIs, statusIn, "") {
*result_listener << "returning status code " << arg << " instead of "
<< statusIn;
return (arg.code() == statusIn);
}
namespace internal {
inline const absl::Status& GetStatus(const absl::Status& status) {
return status;
}
template <typename T>
inline const absl::Status& GetStatus(const absl::StatusOr<T>& status) {
return status.status();
}
} // namespace internal
} // namespace testing::status
#ifndef ASSERT_OK
#define ASSERT_OK(expr) \
ASSERT_THAT(testing::status::internal::GetStatus(expr), \
testing::status::StatusIs(absl::StatusCode::kOk))
#endif
#define ASSERT_OK_AND_ASSIGN_CT(lhs, expr, ct) \
decltype(expr) status_or_##ct; \
do { \
status_or_##ct = (expr); \
ASSERT_OK(status_or_##ct); \
} while (0); \
lhs = *std::move(status_or_##ct)
#define ASSERT_OK_AND_ASSIGN_CT_pre(lhs, exp, ct) \
ASSERT_OK_AND_ASSIGN_CT(lhs, exp, ct)
#ifndef ASSERT_OK_AND_ASSIGN
#define ASSERT_OK_AND_ASSIGN(lhs, expr) \
ASSERT_OK_AND_ASSIGN_CT_pre(lhs, expr, __COUNTER__)
#endif
namespace safepower_agent {
inline std::string GetUniqueTempDir() {
const auto* test_info =
::testing::UnitTest::GetInstance()->current_test_info();
std::string path =
absl::StrCat(testing::TempDir(), "/", test_info->test_suite_name(), "_",
test_info->name());
std::filesystem::create_directories(path);
return path;
}
} // namespace safepower_agent
#endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_BMC_TEST_UTIL_H_