I need some explanation about the following piece of code
Hello,
I am trying to understand the following code written in unix platform about unitTest:
/****************************************/
#define TEST(name,classUnderTest)
class classUnderTest##name##Test : public Test
{
public:
classUnderTest##name##Test () : Test (#name "Test") {}
void setup() {};
void teardown() {};
void runTest (TestResult& result_);
} classUnderTest##name##Instance;
void classUnderTest##name##Test::runTest (TestResult& result_)
/*****************************************************/
Could anyone explain what does ##name## stand for? I would like to use in visual studio 2010.
Please provide some suggestion as I am a newbie in writing unitTest.
Thanks,
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
jenny_wui
Hello,
I am trying to understand the following code written in unix platform about unitTest:
Look up how to use the C++ preprocessor and token pasting.
Regards,
Paul McKenzie
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
Paul McKenzie
Look up how to use the C++ preprocessor and token pasting.
Regards,
Paul McKenzie
Thanks for the reply.But the following piece of code when I tried to paste in visual studio 2010, it showed error.
Code:
#define TESTWITHSETUP(name,classUnderTest)
class classUnderTest##name##Test : public Test, name##Setup
{
public:
classUnderTest##name##Test () : Test (#name "Test") {}
void setup() {name##Setup::setup();}
void teardown() {name##Setup::teardown();}
void runTest (TestResult& result_);
} classUnderTest##name##Instance;
void classUnderTest##name##Test::runTest (TestResult& result_)
In the following piece of code,
"class classUnderTest##name##Test : public Test, name##Setup" , where name##Setup has not been define yet, how can it be used. Please give some clarification regarding this kind of syntax. I would like to run and compile some of this kind of code in visual studio 2010. It would be good, if I am referred to some tutorial website.
Thanks.
{
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
jenny_wui
... the following piece of code when I tried to paste in visual studio 2010, it showed error.
Code:
#define TESTWITHSETUP(name,classUnderTest)
...
What is the error code and error message?
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
jenny_wui
Thanks for the reply.But the following piece of code when I tried to paste in visual studio 2010, it showed error.
That is not a full program. You don't even show usage of the macro.
Quote:
Please give some clarification regarding this kind of syntax.
Do you understand what "token pasting" is and what the C++ preprocessor handles token pasting? That is all the information you need to look up to figure out what ## is supposed to do.
Regards,
Paul McKenzie
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
jenny_wui
Thanks for the reply.But the following piece of code when I tried to paste in visual studio 2010, it showed error.
From MSDN:
Quote:
Use line concatenation — place a backslash (\) immediately before the newline character — for long directives on multiple source lines.
1 Attachment(s)
Re: I need some explanation about the following piece of code
Hi, I am familiar with C++, I went through the preprocessor and token pasting, I have a few questions in the light of the picture attached, it would cbe a great help in order to clarify my conception:
1. “name##Setup” class has not yet been defined. How that class can be inherited by “class classUnderTest##name##Test”? Please give some clarification.
2. What does the encircled line 2 (i.e. #name “Test”) mean?
3. What does the encircled line 3 imply? (name##Setup::setup()) as name##Setup class has notyet been defined?
4. At marking 4, “classUnderTest##name##Instance”, is it an instance of “classclassUnderTest##name##Test”, usually this kind of definition is used while writing stuct.
5. In 5, the function “runTest” has again been defined, why?
Thanks in advance.
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
jenny_wui
Hi, I am familiar with C++, I went through the preprocessor and token pasting, I have a few questions in the light of the picture attached, it would cbe a great help in order to clarify my conception:
You didn't circle the most important part of the whole thing, and that is the first line.
Do you know what that #define does? What those parameters are? If you do, then all you need to do is see what happens if you invoked the macro with a sample line:
Code:
TESTWITHSETUP(name1, name2)
Take that line, apply what you learned about token pasting, and expand that macro with those parameters. What do you ultimately end up with once the macro is expanded?
Regards,
Paul McKenzie
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
jenny_wui
1. “name##Setup” class has not yet been defined. How that class can be inherited by “class classUnderTest##name##Test”? Please give some clarification.
A macro need not have the class predefined. When the macro is expanded (something I asked you to do in the previous post, so that you see what it does), then the compiler will expect that the class is already defined, but not before. That macro is perfectly valid.
Regards,
Paul McKenzie
1 Attachment(s)
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
Paul McKenzie
A macro need not have the class predefined. When the macro is expanded (something I asked you to do in the previous post, so that you see what it does), then the compiler will expect that the class is already defined, but not before. That macro is perfectly valid.
Regards,
Paul McKenzie
Hi, thanks for the reply. Actually I am trying to run a simple unit test project (that I have downloaded from the net) to test how it works as I need to write some code for unit test in C++. I have attached the zipped folder. I can compile the project in visual studio 2010, but while running, i get the following error:
/******************************************************************************************************/
fixture_test.obj : error LNK2019: unresolved external symbol "public: __thiscall Test::Test(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Test@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall Test1MyFixtureTest::Test1MyFixtureTest(void)" (??0Test1MyFixtureTest@@QAE@XZ)
1>simplest_test.obj : error LNK2001: unresolved external symbol "public: __thiscall Test::Test(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0Test@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>fixture_test.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Test::run(class TestResult &)" (?run@Test@@UAEXAAVTestResult@@@Z)
1>simplest_test.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Test::run(class TestResult &)" (?run@Test@@UAEXAAVTestResult@@@Z)
1>C:\Users\MYG741\Documents\Visual Studio 2010\Projects\Unit_test\Debug\Unit_test.exe : fatal error LNK1120: 2 unresolved externals
/******************************************************************************************************/
I don't understand what the problem is. It would be very kind, it some help me figure out what's going wrong. My goal is to learn to write unit test in C++. I don't want to use any external libraries. Please provide me some help regarding this.
Thanks in advance.
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
jenny_wui
I can compile the project in visual studio 2010, but while running, i get the following error:
Those are linker errors, not runtime errors.
Those errors are telling you that you are calling functions that do not exist. Where is the implementation for the Test constructor? Where is the Test::run function implemented?
Also, the following compiles:
Code:
#include <string>
#define TESTWITHSETUP(name,classUnderTest)\
class classUnderTest##name##Test : public Test, name##Setup\
{ \
public: \
classUnderTest##name##Test () : Test (#name "Test") {} \
void setup() {name##Setup::setup();} \
void teardown() {name##Setup::teardown();} \
void runTest (TestResult& result_); \
};\
void classUnderTest##name##Test::runTest (TestResult& result_) {}
class Test
{
public:
Test(const std::string& s) {}
};
class Name1Setup
{
public:
void setup() {}
void teardown() {}
};
typedef int TestResult;
TESTWITHSETUP(Name1,Name2)
int main()
{ }
Regards,
Paul McKenzie
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
Paul McKenzie
Those are linker errors, not runtime errors.
Those errors are telling you that you are calling functions that do not exist. Where is the implementation for the Test constructor? Where is the Test::run function implemented?
Also, the following compiles:
Code:
#include <string>
#define TESTWITHSETUP(name,classUnderTest)\
class classUnderTest##name##Test : public Test, name##Setup\
{ \
public: \
classUnderTest##name##Test () : Test (#name "Test") {} \
void setup() {name##Setup::setup();} \
void teardown() {name##Setup::teardown();} \
void runTest (TestResult& result_); \
};\
void classUnderTest##name##Test::runTest (TestResult& result_) {}
class Test
{
public:
Test(const std::string& s) {}
};
class Name1Setup
{
public:
void setup() {}
void teardown() {}
};
typedef int TestResult;
TESTWITHSETUP(Name1,Name2)
int main()
{ }
Regards,
Paul McKenzie
Thank you very much for the reply. Actually I would like to know how unit test works. I don't understand how unit test. I need some full program in unit test which i can run and understand. Could you provide me with some "HelloWorld" type example for unit_test? Thanks in advance.
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
jenny_wui
Thank you very much for the reply. Actually I would like to know how unit test works. I don't understand how unit test. I need some full program in unit test which i can run and understand. Could you provide me with some "HelloWorld" type example for unit_test? Thanks in advance.
If you need further information about undertaking 'unit testing' I suggest you do a web search for info. There's a lot out there. A couple of sites that you may find of interest are
http://stackoverflow.com/questions/3...isual-studio-c
http://en.wikipedia.org/wiki/List_of...ing_frameworks
http://msdn.microsoft.com/en-us/library/dd264975.aspx
http://visualstudiomagazine.com/arti...plus-plus.aspx
Re: I need some explanation about the following piece of code
Quote:
Originally Posted by
Paul McKenzie
Those are linker errors, not runtime errors.
Those errors are telling you that you are calling functions that do not exist. Where is the implementation for the Test constructor? Where is the Test::run function implemented?
Also, the following compiles:
Code:
#include <string>
#define TESTWITHSETUP(name,classUnderTest)\
class classUnderTest##name##Test : public Test, name##Setup\
{ \
public: \
classUnderTest##name##Test () : Test (#name "Test") {} \
void setup() {name##Setup::setup();} \
void teardown() {name##Setup::teardown();} \
void runTest (TestResult& result_); \
};\
void classUnderTest##name##Test::runTest (TestResult& result_) {}
class Test
{
public:
Test(const std::string& s) {}
};
class Name1Setup
{
public:
void setup() {}
void teardown() {}
};
typedef int TestResult;
TESTWITHSETUP(Name1,Name2)
int main()
{ }
Regards,
Paul McKenzie
Hello, I am still a bit in the dark about unit test. Could you explain in your code, how tests are added and run. Also why TEST and TESTWITHSETUP these two macros are defined? What is the difference between these two macros.
#define TEST(name,classUnderTest)\
class classUnderTest##name##Test : public Test\
{ \
public: \
classUnderTest##name##Test () : Test (#name "Test") {} \
void setup() {}; \
void teardown() {}; \
void runTest (TestResult& result_); \
} classUnderTest##name##Instance; \
void classUnderTest##name##Test::runTest (TestResult& result_) \
#define TESTWITHSETUP(name,classUnderTest)\
class classUnderTest##name##Test : public Test, name##Setup\
{ \
public: \
classUnderTest##name##Test () : Test (#name "Test") {} \
void setup() {name##Setup::setup();} \
void teardown() {name##Setup::teardown();} \
void runTest (TestResult& result_); \
} classUnderTest##name##Instance; \
void classUnderTest##name##Test::runTest (TestResult& result_) \
Also at the last line of the two macros why only runTest function has been defined? Please explain a bit more.
Thanks in advance.