CppUTest
GTestInterface.h
1 /*
2  * Copyright (c) 2011, Michael Feathers, James Grenning and Bas Vodde
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "CppUTest/CppUTestConfig.h"
29 #include "CppUTest/Utest.h"
30 #include "CppUTest/TestResult.h"
31 #include "CppUTest/TestFailure.h"
32 
33 #define TEST(testGroup, testName) \
34  /* external declaration */ \
35  class TEST_##testGroup##_##testName##_TestShell; \
36  extern TEST_##testGroup##_##testName##_TestShell TEST_##testGroup##_##testName##_TestShell_Instance; \
37  class TEST_##testGroup##_##testName##_Test : public Utest \
38 { public: TEST_##testGroup##_##testName##_Test () : Utest () {} \
39  void testBody(); }; \
40  class TEST_##testGroup##_##testName##_TestShell : public UtestShell \
41 { public: virtual Utest* createTest() { return new TEST_##testGroup##_##testName##_Test; } \
42  } TEST_##testGroup##_##testName##_TestShell_Instance; \
43  static TestInstaller TEST_##testGroup##_##testName##_Installer(TEST_##testGroup##_##testName##_TestShell_Instance, #testGroup, #testName, __FILE__,__LINE__); \
44  void TEST_##testGroup##_##testName##_Test::testBody()
45 
46 #define TEST_F(testGroup, testName) \
47 /* external declaration */ \
48  class TEST_##testGroup##_##testName##_TestShell; \
49  extern TEST_##testGroup##_##testName##_TestShell TEST_##testGroup##_##testName##_TestShell_instance; \
50  class TEST_##testGroup##_##testName##_Test : public testGroup \
51 { public: TEST_##testGroup##_##testName##_Test () : testGroup () {} \
52  void testBody(); }; \
53  class TEST_##testGroup##_##testName##_TestShell : public UtestShell { \
54  virtual Utest* createTest() { return new TEST_##testGroup##_##testName##_Test; } \
55  } TEST_##testGroup##_##testName##_TestShell_instance; \
56  static TestInstaller TEST_##testGroup##_##testName##_Installer(TEST_##testGroup##_##testName##_TestShell_instance, #testGroup, #testName, __FILE__,__LINE__); \
57  void TEST_##testGroup##_##testName##_Test::testBody()
58 
59 /*
60  * NOTICE:
61  *
62  * Code duplicated from UtestMacros.h. Its hard to share as don't want to include the CppUTest
63  * macros in the gtest interface.
64  *
65  */
66 
67 #define EXPECT_EQ(expected, actual) \
68  if ((expected) != (actual))\
69  {\
70  { \
71  UtestShell::getTestResult()->countCheck();\
72  CheckEqualFailure _f(UtestShell::getCurrent(), __FILE__, __LINE__, StringFrom(expected), StringFrom(actual)); \
73  UtestShell::getTestResult()->addFailure(_f);\
74  } \
75  UtestShell::getCurrent()->exitCurrentTest(); \
76  }\
77  else\
78  UtestShell::getTestResult()->countCheck();
79 
80 #define EXPECT_TRUE(condition) \
81  { UtestShell::getCurrent()->assertTrue((condition) != 0, "EXPECT_TRUE", #condition, __FILE__, __LINE__); }
82 
83 #define EXPECT_FALSE(condition) \
84  { UtestShell::getCurrent()->assertTrue((condition) == 0, "EXPECT_FALSE", #condition, __FILE__, __LINE__); }
85 
86 #define EXPECT_STREQ(expected, actual) \
87  { UtestShell::getCurrent()->assertCstrEqual(expected, actual, __FILE__, __LINE__); }
88 
89 #define ASSERT_EQ(expected, actual) EXPECT_EQ(expected, actual)
90 
91 #define ASSERT_TRUE(condition) EXPECT_TRUE(condition)
92 
93 namespace testing
94 {
95  class Test : public Utest
96  {
97  virtual void SetUp(){}
98  virtual void TearDown(){}
99 
100  void setup()
101  {
102  SetUp();
103  }
104 
105  void teardown()
106  {
107  TearDown();
108  }
109 
110  };
111 }
112 
113 
114 
Definition: GTestInterface.h:93
Definition: Utest.h:47
Definition: GTestInterface.h:95