test: Add assert mock to avoid death tests (#1947)

Fixes #1750
This commit is contained in:
Sergey Kuznetsov
2025-03-07 18:11:52 +00:00
committed by GitHub
parent 8a08c5e6ce
commit c57fe1e6e4
30 changed files with 411 additions and 295 deletions

View File

@@ -18,6 +18,7 @@
//==============================================================================
#include "util/LoggerFixtures.hpp"
#include "util/MockAssert.hpp"
#include "util/newconfig/ConfigConstraints.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/Error.hpp"
@@ -34,8 +35,7 @@
using namespace util::config;
struct ConfigValueTest : NoLoggerFixture {};
struct ConfigValueDeathTest : ConfigValueTest {};
struct ConfigValueTest : common::util::WithMockAssert, NoLoggerFixture {};
TEST_F(ConfigValueTest, construct)
{
@@ -69,9 +69,9 @@ TEST_F(ConfigValueTest, defaultValueWithDescription)
EXPECT_EQ(cv.type(), ConfigType::String);
}
TEST_F(ConfigValueDeathTest, invalidDefaultValue)
TEST_F(ConfigValueTest, invalidDefaultValue)
{
EXPECT_DEATH({ [[maybe_unused]] auto const a = ConfigValue{ConfigType::String}.defaultValue(33); }, ".*");
EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto const a = ConfigValue{ConfigType::String}.defaultValue(33); });
}
TEST_F(ConfigValueTest, setValueWrongType)
@@ -128,19 +128,13 @@ TEST_F(ConfigValueConstraintTest, defaultValueWithConstraint)
EXPECT_EQ(cv.getValue(), Value{123});
}
struct ConfigValueConstraintDeathTest : ConfigValueConstraintTest {};
TEST_F(ConfigValueConstraintDeathTest, defaultValueWithConstraintCheckError)
TEST_F(ConfigValueConstraintTest, defaultValueWithConstraintCheckError)
{
EXPECT_DEATH(
{
EXPECT_CALL(constraint, checkTypeImpl).WillOnce(testing::Return(std::nullopt));
EXPECT_CALL(constraint, checkValueImpl).WillOnce(testing::Return(Error{"value error"}));
[[maybe_unused]] auto const cv =
ConfigValue{ConfigType::Integer}.defaultValue(123).withConstraint(constraint);
},
".*"
);
EXPECT_CLIO_ASSERT_FAIL({
EXPECT_CALL(constraint, checkTypeImpl).WillOnce(testing::Return(std::nullopt));
EXPECT_CALL(constraint, checkValueImpl).WillOnce(testing::Return(Error{"value error"}));
[[maybe_unused]] auto const cv = ConfigValue{ConfigType::Integer}.defaultValue(123).withConstraint(constraint);
});
}
// A test for each constraint so it's easy to change in the future
@@ -259,11 +253,11 @@ struct ConstraintTestBundle {
Constraint const& constraint;
};
struct ConstraintDeathTest : testing::TestWithParam<ConstraintTestBundle> {};
struct ConstraintAssertTest : common::util::WithMockAssert, testing::WithParamInterface<ConstraintTestBundle> {};
INSTANTIATE_TEST_SUITE_P(
EachConstraints,
ConstraintDeathTest,
ConstraintAssertTest,
testing::Values(
ConstraintTestBundle{"logTagConstraint", gValidateLogTag},
ConstraintTestBundle{"portConstraint", gValidatePort},
@@ -281,43 +275,28 @@ INSTANTIATE_TEST_SUITE_P(
[](testing::TestParamInfo<ConstraintTestBundle> const& info) { return info.param.name; }
);
TEST_P(ConstraintDeathTest, TestEachConstraint)
TEST_P(ConstraintAssertTest, TestEachConstraint)
{
EXPECT_DEATH(
{
[[maybe_unused]] auto const a =
ConfigValue{ConfigType::Boolean}.defaultValue(true).withConstraint(GetParam().constraint);
},
".*"
);
EXPECT_CLIO_ASSERT_FAIL({
[[maybe_unused]] auto const a =
ConfigValue{ConfigType::Boolean}.defaultValue(true).withConstraint(GetParam().constraint);
});
}
TEST(ConstraintDeathTest, SetInvalidValueTypeStringAndBool)
TEST_F(ConstraintAssertTest, SetInvalidValueTypeStringAndBool)
{
EXPECT_DEATH(
{
[[maybe_unused]] auto a =
ConfigValue{ConfigType::String}.defaultValue(33).withConstraint(gValidateLoadMode);
},
".*"
);
EXPECT_DEATH({ [[maybe_unused]] auto a = ConfigValue{ConfigType::Boolean}.defaultValue(-66); }, ".*");
EXPECT_CLIO_ASSERT_FAIL({
[[maybe_unused]] auto a = ConfigValue{ConfigType::String}.defaultValue(33).withConstraint(gValidateLoadMode);
});
EXPECT_CLIO_ASSERT_FAIL({ [[maybe_unused]] auto a = ConfigValue{ConfigType::Boolean}.defaultValue(-66); });
}
TEST(ConstraintDeathTest, OutOfBounceIntegerConstraint)
TEST_F(ConstraintAssertTest, OutOfBounceIntegerConstraint)
{
EXPECT_DEATH(
{
[[maybe_unused]] auto a =
ConfigValue{ConfigType::Integer}.defaultValue(999999).withConstraint(gValidateUint16);
},
".*"
);
EXPECT_DEATH(
{
[[maybe_unused]] auto a =
ConfigValue{ConfigType::Integer}.defaultValue(-66).withConstraint(gValidateUint32);
},
".*"
);
EXPECT_CLIO_ASSERT_FAIL({
[[maybe_unused]] auto a = ConfigValue{ConfigType::Integer}.defaultValue(999999).withConstraint(gValidateUint16);
});
EXPECT_CLIO_ASSERT_FAIL({
[[maybe_unused]] auto a = ConfigValue{ConfigType::Integer}.defaultValue(-66).withConstraint(gValidateUint32);
});
}