简体   繁体   English

谷歌测试框架允许使用未定义的TEST参数

[英]undefined arguments to TEST allowed with google test framework

when using the Google Test Framework I can have the following code compile, despite there being barewords undefined symbols passed as arguments to TEST. 使用Google Test Framework时 ,尽管有未定义的裸词作为参数传递给TEST,但我仍可以编译以下代码。

#include <gtest/gtest.h>

TEST(faketestfixture,faketestname){
  ASSERT_EQ(1,1);
}

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

Why/how does this compile? 为什么/如何编译? What magic are they using? 他们在使用什么魔术?

I began to peek around the source, but quickly realized I was out of my depth and don't even know where to start. 我开始窥视资源,但很快意识到我已经超出了深度,甚至不知道从哪里开始。

TEST is a preprocessor macro, and its areguments are not identifiers, the TEST macro just uses them as building blocks to generate code. TEST是一个预处理器宏,它的参数不是标识符,TEST宏仅将它们用作生成代码的构建块。 In this case it generates the class called faketestfixture_faketestname_Test with the method called TestBody. 在这种情况下,它将使用称为TestBody的方法生成一个名为faketestfixture_faketestname_Test的类。 The actual body of that method is what you supply in curly brackets after the TEST macro invocation. 该方法的实际主体是在TEST宏调用之后在大括号中提供的内容。 So the generated code looks approximately this way: 因此,生成的代码大致如下所示:

class faketestfixture_faketestname_Test : public testing::Test {
 public:
  void TestBody();

  // ... more stuff ...
}

void faketestfixture_faketestname_Test::TestBody() {
  // This is the test body you supplied.
  ASSERT_EQ(1,1);
}

So that's relatively simple. 因此,这相对简单。 The real magic is in how it all is hooked together and invoked. 真正的魔力在于如何将所有这些联系在一起并加以调用。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM