Domanda

when using the Google Test Framework I can have the following code compile, despite there being barewords undefined symbols passed as arguments to 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.

È stato utile?

Soluzione

TEST is a preprocessor macro, and its areguments are not identifiers, the TEST macro just uses them as building blocks to generate code. In this case it generates the class called faketestfixture_faketestname_Test with the method called TestBody. The actual body of that method is what you supply in curly brackets after the TEST macro invocation. 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. :-)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top