- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设一个模板类,我们在编译时断言整数模板参数必须大于零:
template<int N>
class A
{
public:
A() {
static_assert(N > 0, "N needs to be greater 0.");
}
};
是否可以创建一个 编译 但报告运行时错误 的 googletest 单元测试?例如:
TEST(TestA, ConstructionNotAllowedWithZero)
{
ASSERT_DOES_NOT_COMPILE(
{
A< 0 > a;
}
);
}
最佳答案
有一种方法,但遗憾的是它可能不是您想要的方式。
我的第一个想法是尝试让 SFINAE 通过在未计算的上下文中扩展无效的 lambda 来减少过载。不幸的是(在你的情况下),这是特别不允许的......
#define CODE { \
utter garbage \
}
struct test
{
template<class T>
static std::false_type try_compile(...) { return{}; }
template<class T>
static auto try_compile(int)
-> decltype([]() CODE, void(), std::true_type());
{ return {}; }
};
struct tag {};
using does_compile = decltype(test::try_compile<tag>(0));
输出:
./maybe_compile.cpp:88:17: error: lambda expression in an unevaluated operand
-> decltype([]() CODE, void(), std::true_type());
所以它又回到了绘图板和一个很好的旧系统调用来调出编译器......
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <sstream>
struct temp_file {
temp_file()
: filename(std::tmpnam(nullptr))
{}
~temp_file() {
std::remove(filename.c_str());
}
std::string filename;
};
bool compiles(const std::string code, std::ostream& reasons)
{
using namespace std::string_literals;
temp_file capture_file;
temp_file cpp_file;
std::ofstream of(cpp_file.filename);
std::copy(std::begin(code), std::end(code), std::ostream_iterator<char>(of));
of.flush();
of.close();
const auto cmd_line = "c++ -x c++ -o /dev/null "s + cpp_file.filename + " 2> " + capture_file.filename;
auto val = system(cmd_line.c_str());
std::ifstream ifs(capture_file.filename);
reasons << ifs.rdbuf();
ifs.close();
return val == 0;
}
auto main() -> int
{
std::stringstream reasons1;
const auto code1 =
R"code(
#include <iostream>
int main() {
return 0;
}
)code";
std::cout << "compiles: " << compiles(code1, reasons1) << std::endl;
std::stringstream reasons2;
const auto code2 =
R"code(
#include <iostream>
int main() {
FOO!!!!XC@£$%^&*()VBNMYGHH
return 0;
}
)code";
std::cout << "compiles: " << compiles(code2, reasons2) << std::endl;
std::cout << "\nAnd here's why...\n";
std::cout << reasons2.str() << std::endl;
return 0;
}
在我的例子中给出了以下示例输出:
compiles: 1
compiles: 0
And here's why...
/var/tmp/tmp.3.2dADZ7:4:9: error: use of undeclared identifier 'FOO'
FOO!!!!XC@£$%^&*()VBNMYGHH
^
/var/tmp/tmp.3.2dADZ7:4:19: error: non-ASCII characters are not allowed outside of literals and identifiers
FOO!!!!XC@£$%^&*()VBNMYGHH
^
2 errors generated.
当然,您可以在对 compiles()
的调用周围添加所有必要的宏,以便对其进行 GTESTify。您当然必须在 c 编译器调用上设置命令行选项以设置正确的路径和定义。
关于c++ - 是否可以使用 GTest 进行 ASSERT_DOES_NOT_COMPILE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31607928/
我在尝试使用 cmake 构建项目时遇到错误。 我在我的电脑上手动下载并安装了 gtest(即/usr/include 中提供了 gtest 头文件)和/usr/lib 中的 gtest、gtest_
我在构建我的项目时遇到以下错误 C:\gtest\gtest-1.6.0\include\gtest/gtest-printers.h(327) : error C2220: warning trea
你好, 我目前正在研究 CMake,以便在工作中使用 ExternalProject_add。因为我想测试 googletest,所以我尝试同时测试两者。我在编译单元测试代码时遇到问题。 所以我就按照
我一直在为这个问题苦苦挣扎一段时间:我在 Windows 10 下使用 CMake 和 GTest,但我得到一个 (Link target) -> LINK : fatal error LNK1104
如果内存不足,C++ 中的 new 关键字将抛出异常,但下面的代码会在 new 失败时尝试返回“NO_MEMORY”。这很糟糕,因为它会引发 std::bad_alloc 异常。 我正在写一个单元测试
我正在尝试运行一些Google测试,并且在每个测试装置中都有很多代码需要重复,因此我想使代码尽可能简短,我想使用的子类的SetUp方法Testing::test父类,但是TEST_F固定装置无法识别S
我正在尝试使用 gtest 类型参数化来确保我的类的基本功能。但是我被困在测试设置中。以下是示例: 我有两个名为 CatTest 和 DogTest 的类,以及一个名为 AnimalTest 的基类,
我想应用参数化测试并具有以下夹具: class MyTest: public ::testing::TestWithParam {...}; 我想设置两个参数化测试用例,其中一个小参数失败,但大参数成
如果我使用 g(oogle)Test,这些像 FRIEND_TEST(Test, Proc) 这样的小谷歌宏在我的代码中随处可见。是否有自动机制将它们从构建中排除,或者我真的必须用 #ifdefs 包
我有一个项目,我添加了一个 gtest 项目,然后将原始项目转换为 .lib 项目,并从第三个新项目调用其 main() 函数。这样,我就可以用项目 .exe 构建测试 .exe。除了,它不起作用。
注意:我知道这个问题以不同的方式被问到,但我无法根据我的限制解决它: 我正在使用谷歌测试版 1.7 使用平台工具集 V100 在 Visual Studio 2010 中编译 我正在为我创建的库编写
我的项目中有一个目录树: /project /build /src main.cpp student.cpp /include
在我们所知的 gtets 中,当控件找到 TEST 或 TEST_F 函数时,它会注册 测试用例 进入 gtest。但是按照我的要求,gtest注册完所有的测试用例之后我需要搜索 testcasena
我有一大套单元测试和一些集成测试是用谷歌测试框架或 gtest 实现的。 由于没有标记,我使用禁用约定将测试分成组或在它们前面加上 GROUPA_、GROUPB_ 等前缀。 这很好用。我可以过滤不同的
我想混合参数化测试和类型化测试。这是我的尝试: struct X {}; struct Y {}; template struct MyTestFixture: public ::testing::
我已阅读 this official document ,了解如何进行二进制比较和字符串比较。 ASSERT_EQ 和 ASSERT_STREQ 在数组比较情况下无法工作。 例如 li@li:~/po
我正在测试我的功能是否正确运行 bool Core::IsMeta(void) { return mProc->GetCode(mPC)->Meta; } 使用说明 EXPECT_EQ(true,
我想用 gtest 测试一个模板类。我在 Google Test manual 中阅读了有关 TYPED_TEST 的信息并查看了official example他们引用了,但我仍然无法全神贯注地获取
这是我的: /* Can't change 'base' struct. */ struct base { public: template void printVal() {
我想要等于 2 个对象,正好是卡片(使用 gtest 进行单元测试)。这是我的代码: #include "stdafx.h" #include #include class Card { p
我是一名优秀的程序员,十分优秀!