gpt4 book ai didi

c++ - 枚举类的 GoogleTest 测试错误打印

转载 作者:可可西里 更新时间:2023-11-01 17:57:40 27 4
gpt4 key购买 nike

我正在使用 GoogleTest 1.7.0 版来测试我的 C++ 应用程序。我有一个枚举定义如下

namespace MyNamespace {
enum class MyEnum {
MyEnumValue,
MyEnumValue2
}
}

GoogleTest 错误地打印它的值,导致测试失败时出现此错误消息:

Value of: MyClass.MyMethodThatReturnsEnum()

Actual: 4-byte object

Expected: MyEnum::MyEnumValue

Which is: 4-byte object <02-00 00-00>

删除 class 关键字会生成带有枚举实际值的正确错误消息。这是 GoogleTest 的已知行为/错误吗?有办法解决这个问题吗?

MyClass my_class;

EXPECT_EQ(MyEnum::MyEnumValue, my_class.MyMethodThatReturnsEnum());

最佳答案

当您在 enum class MyEnum 中定义常量时,您将它们定义为对象属于用户定义类型 MyEnum,其中没有隐式转换任何整数类型。这是 enum class目标,但它不是让 Googletest 假设您想要 MyEnum 类型的对象自动为了插入输出而转换为整数类型流,如果您选择使该类型成为 enum class 而不仅仅是一个枚举

所以您观察到的不是 Googletest 错误。它只是 Googletest 使用其某种类型的用户定义对象的回退表示 T for which

std::ostream & operator<<(std::ostream &, T const &);

未定义。

如果你想查看enum class MyEnum类型对象的整数值出现在 Googletest 的测试诊断中,您至少有两种方法。

一种方法是简单地将测试应用于对象的基础积分值,如:

ma​​in.cpp(1)

#include <gtest/gtest.h>
#include <type_traits>

enum class MyEnum {
Value,
Value2
};

auto as_integral(MyEnum me)
-> std::underlying_type<MyEnum>::type
{
return static_cast<std::underlying_type<MyEnum>::type>(me);
}

TEST(foo,bar)
{
EXPECT_EQ(as_integral(MyEnum::Value),as_integral(MyEnum::Value2));
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

编译、链接和运行:

$ g++ -std=c++11 -Wall -Wextra -o gtester main.cpp -lgtest  -lpthread
$ ./gtester
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from foo
[ RUN ] foo.bar
main.cpp:17: Failure
Expected equality of these values:
as_integral(MyEnum::Value)
Which is: 0
as_integral(MyEnum::Value2)
Which is: 1
[ FAILED ] foo.bar (0 ms)
[----------] 1 test from foo (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (1 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] foo.bar

1 FAILED TEST

另一种更好的方法是为 Googletest 提供以下定义:

std::ostream & operator<<(std::ostream &, MyEnum const &);

适合你。然后 Googletest 将在其诊断中使用它,如:

ma​​in.cpp(2)

#include <gtest/gtest.h>
#include <type_traits>
#include <ostream>

enum class MyEnum {
Value,
Value2
};

auto as_integral(MyEnum me)
-> std::underlying_type<MyEnum>::type
{
return static_cast<std::underlying_type<MyEnum>::type>(me);
}

std::ostream & operator<<(std::ostream & out, MyEnum me)
{
return out << as_integral(me);
}

TEST(foo,bar)
{
EXPECT_EQ(MyEnum::Value,MyEnum::Value2);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

编译、链接和运行:

$ g++ -std=c++11 -Wall -Wextra -o gtester main.cpp -lgtest  -lpthread
$ ./gtester
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from foo
[ RUN ] foo.bar
main.cpp:23: Failure
Expected equality of these values:
MyEnum::Value
Which is: 0
MyEnum::Value2
Which is: 1
[ FAILED ] foo.bar (0 ms)
[----------] 1 test from foo (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] foo.bar

1 FAILED TEST

关于c++ - 枚举类的 GoogleTest 测试错误打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28346182/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com