问题描述
有没有办法测试编译时错误,但实际上不产生错误?例如,如果我创建一个不可复制的类,我想测试以下事实:尝试复制它会产生编译器错误,但我仍然想执行其他运行时测试.
Is there a way to test compile-time errors, but without actually generating the error? For example, if I create a class which is non-copyable, I'd like to test the fact that trying to copy it will generate a compiler error, but I'd still like to execute the other runtime tests.
struct Foo {
int value_;
Foo(int value) : value_(value) {}
private:
Foo(const Foo&);
const Foo& operator=(const Foo&);
};
int main()
{
Foo f(12);
assert(f.value_ == 12);
assert(IS_COMPILER_ERROR(Foo copy(f);));
} // Would like this to compile and run fine.
我想这不能像那样简单地完成,但是有没有一种惯用的方法来做到这一点,或者我应该推出自己的解决方案(也许使用脚本编译单独的测试文件并测试结果?)?
I guess this can't be done as simply as that, but is there an idiomatic way to do this, or should I roll my own solution (maybe using scripts compiling separate tests files and testing the results?)?
注意:我采用不可复制只是为了说明我的观点,所以我对有关使用 boost::noncopyable 等的答案不感兴趣.
N.B.: I took non-copyable only to illustrate my point, so I'm not interested in answers about using boost::noncopyable and such.
推荐答案
您可以使用 make 来完成.每个测试都是一个代码片段.这是一个带有 2 个 VC++ 测试的工作示例.(我使用了 2 个批处理文件进行通过测试和失败测试).我在这里使用 GNU make.
You can do it using make. Each test will be a code snippet. Here's a working example with 2 tests for VC++. (I've used 2 batch files for pass test and fail test). I'm using GNU make here.
生成文件:
FAILTEST = .failtest.bat
PASSTEST = .passtest.bat
tests: must_fail_but_passes
must_pass_but_fails
must_fail_but_passes:
@$(FAILTEST) $@.cpp
must_pass_but_fails:
@$(PASSTEST) $@.cpp
must_pass_but_fails.cpp
must_pass_but_fails.cpp
struct Foo {
int value_;
Foo(void) : value_(0) {}
private:
Foo(const Foo&);
const Foo& operator=(const Foo&);
};
int main(){富 f(12);返回0;}
int main() { Foo f(12); return 0; }
must_fail_but_passes.cpp
must_fail_but_passes.cpp
struct Foo {
int value_;
Foo(int value) : value_(value) {}
private:
Foo(const Foo&);
const Foo& operator=(const Foo&);
};
int main(){富 f(12);返回0;}
int main() { Foo f(12); return 0; }
通过测试.bat
@echo off
cl /nologo %1 >NUL
if %errorlevel% == 0 goto pass
@echo %1 FAILED
:pass
失败测试.bat
@echo off
cl /nologo %1 >NUL
if not %errorlevel% == 0 goto pass
@echo %1 FAILED
:pass
请注意 cl.exe(即 Visual Studio 编译器)需要在您的路径中才能正常工作"
Note that cl.exe (i.e. Visual Studio compiler) need to be in your path for this to "just work"
玩得开心!
附言我怀疑这会让我出名:-)
P.S. I doubt that this would make me famous though :-)
这篇关于单元测试一个类是不可复制的,以及其他编译时属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!