Debian Codegraph @

tests/CppUTestExt/GTest1Test.cpp

raw symbols SCIP
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 #ifdef CPPUTEST_INCLUDE_GTEST_TESTS
29 #undef new
30
31 #include "CppUTestExt/GTest.h"
32 #include "CppUTestExt/GMock.h"
33
34 static bool g_GTestEqual_has_been_called = false;
35 TEST(GTestSimpleTest, GTestEqual)
36 {
37 EXPECT_EQ(1, 1);
38 g_GTestEqual_has_been_called = true;
39 }
40
41 TEST(GTestSimpleTest, GTestAssertEq)
42 {
43 ASSERT_EQ(1, 1);
44 }
45
46 TEST(GTestSimpleTest, GTestExpectTrue)
47 {
48 EXPECT_TRUE(true);
49 }
50
51 TEST(GTestSimpleTest, GTestAssertTrue)
52 {
53 ASSERT_TRUE(true);
54 }
55
56 TEST(GTestSimpleTest, GTestExpectFalse)
57 {
58 EXPECT_FALSE(false);
59 }
60
61 TEST(GTestSimpleTest, GTestExpectStreq)
62 {
63 EXPECT_STREQ("hello world", "hello world");
64 }
65
66 /* Death tests are IMHO not a good idea at all. But for compatibility reason, we'll support it */
67
68 static void crashMe ()
69 {
70 fprintf(stderr, "Crash me!");
71 *((int*) 0) = 10;
72 }
73
74 TEST(GTestSimpleTest, GTestDeathTest)
75 {
76 #if defined(GTEST_VERSION_GTEST_1_7)
77 CppuTestGTestIgnoreLeaksInTest();
78 #endif
79 ASSERT_DEATH(crashMe(), "Crash me!");
80 }
81
82 #undef TEST
83
84 #include "CppUTest/TestHarness.h"
85 #include "CppUTest/TestOutput.h"
86 #include "CppUTest/TestTestingFixture.h"
87
88 TEST_GROUP(gtest)
89 {
90 };
91
92 TEST(gtest, SimpleGoogleTestExists)
93 {
94 TestRegistry* registry = TestRegistry::getCurrentRegistry();
95 CHECK(registry->findTestWithName("GTestEqual"));
96 }
97
98 TEST(gtest, SimpleGoogleTestGroupExists)
99 {
100 TestRegistry* registry = TestRegistry::getCurrentRegistry();
101 CHECK(registry->findTestWithGroup("GTestSimpleTest"));
102 }
103
104 TEST(gtest, SimpleGoogleTestGetCalled)
105 {
106 StringBufferTestOutput output;
107 TestResult result(output);
108 TestPlugin plugin("dummy");
109
110 TestRegistry* registry = TestRegistry::getCurrentRegistry();
111 UtestShell * shell = registry->findTestWithName("GTestEqual");
112 g_GTestEqual_has_been_called = false;
113 shell->runOneTest(&plugin, result);
114
115 CHECK(g_GTestEqual_has_been_called);
116 }
117
118 static bool afterCheck;
119
120 static void _failMethodEXPECT_EQ()
121 {
122 EXPECT_EQ(1, 2);
123 afterCheck = true;
124 }
125
126 static void _failMethodASSERT_EQ()
127 {
128 ASSERT_EQ(1, 2);
129 afterCheck = true;
130 }
131
132 static void _failMethodEXPECT_TRUE()
133 {
134 EXPECT_TRUE(false);
135 afterCheck = true;
136 }
137
138 static void _failMethodASSERT_TRUE()
139 {
140 ASSERT_TRUE(false);
141 afterCheck = true;
142 }
143
144 static void _failMethodEXPECT_FALSE()
145 {
146 EXPECT_FALSE(true);
147 afterCheck = true;
148 }
149
150 static void _failMethodEXPECT_STREQ()
151 {
152 EXPECT_STREQ("hello", "world");
153 afterCheck = true;
154 }
155
156 TEST_GROUP(gtestMacros)
157 {
158 TestTestingFixture* fixture;
159 void setup()
160 {
161 fixture = new TestTestingFixture();
162 afterCheck = false;
163 }
164 void teardown()
165 {
166 delete fixture;
167 }
168
169 void testFailureWith(void(*method)())
170 {
171 fixture->setTestFunction(method);
172 fixture->runAllTests();
173 LONGS_EQUAL(1, fixture->getFailureCount());
174 CHECK(!afterCheck);
175 }
176
177 };
178
179 TEST(gtestMacros, EXPECT_EQFails)
180 {
181 testFailureWith(_failMethodEXPECT_EQ);
182 }
183
184 TEST(gtestMacros, EXPECT_TRUEFails)
185 {
186 testFailureWith(_failMethodEXPECT_TRUE);
187 }
188
189 TEST(gtestMacros, EXPECT_FALSEFails)
190 {
191 testFailureWith(_failMethodEXPECT_FALSE);
192 }
193
194 TEST(gtestMacros, EXPECT_STREQFails)
195 {
196 testFailureWith(_failMethodEXPECT_STREQ);
197 }
198
199 TEST(gtestMacros, ASSERT_EQFails)
200 {
201 testFailureWith(_failMethodASSERT_EQ);
202 }
203
204 TEST(gtestMacros, ASSERT_TRUEFails)
205 {
206 testFailureWith(_failMethodASSERT_TRUE);
207 }
208
209 #endif