|
| 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 "gtest/gtest.h" |
| 29 | + |
| 30 | +static bool g_GTestEqual_has_been_called = false; |
| 31 | +TEST(GTestSimpleTest, GTestEqual) |
| 32 | +{ |
| 33 | + EXPECT_EQ(1, 1); |
| 34 | + g_GTestEqual_has_been_called = true; |
| 35 | +} |
| 36 | + |
| 37 | +TEST(GTestSimpleTest, GTestAssertEq) |
| 38 | +{ |
| 39 | + ASSERT_EQ(1, 1); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(GTestSimpleTest, GTestExpectTrue) |
| 43 | +{ |
| 44 | + EXPECT_TRUE(true); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(GTestSimpleTest, GTestAssertTrue) |
| 48 | +{ |
| 49 | + ASSERT_TRUE(true); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(GTestSimpleTest, GTestExpectFalse) |
| 53 | +{ |
| 54 | + EXPECT_FALSE(false); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(GTestSimpleTest, GTestExpectStreq) |
| 58 | +{ |
| 59 | + EXPECT_STREQ("hello world", "hello world"); |
| 60 | +} |
| 61 | + |
| 62 | +class GTestTestingFixtureTest : public testing::Test { |
| 63 | +protected: |
| 64 | + bool setup_was_called; |
| 65 | + char* freed_during_teardown; |
| 66 | + |
| 67 | + void SetUp() |
| 68 | + { |
| 69 | + setup_was_called = true; |
| 70 | + freed_during_teardown = NULL; |
| 71 | + } |
| 72 | + |
| 73 | + void TearDown() |
| 74 | + { |
| 75 | + delete [] freed_during_teardown; |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +TEST_F(GTestTestingFixtureTest, setupBeenCalled) |
| 80 | +{ |
| 81 | + EXPECT_TRUE(setup_was_called); |
| 82 | +} |
| 83 | + |
| 84 | +TEST_F(GTestTestingFixtureTest, teardownMustBeCalledOrElseThisWillLeak) |
| 85 | +{ |
| 86 | + freed_during_teardown = new char[100]; |
| 87 | +} |
| 88 | + |
| 89 | +#undef TEST |
| 90 | +#undef RUN_ALL_TESTS |
| 91 | +#undef FAIL |
| 92 | + |
| 93 | +#include "CppUTest/TestHarness.h" |
| 94 | +#include "CppUTest/TestRegistry.h" |
| 95 | +#include "CppUTest/TestOutput.h" |
| 96 | +#include "CppUTest/TestTestingFixture.h" |
| 97 | +#include "CppUTestExt/GTestConvertor.h" |
| 98 | + |
| 99 | +TEST_GROUP(gtest) |
| 100 | +{ |
| 101 | +}; |
| 102 | + |
| 103 | +TEST(gtest, SimpleGoogleTestExists) |
| 104 | +{ |
| 105 | + TestRegistry* registry = TestRegistry::getCurrentRegistry(); |
| 106 | + CHECK(registry->findTestWithName("GTestEqual")); |
| 107 | +} |
| 108 | + |
| 109 | +TEST(gtest, SimpleGoogleTestGroupExists) |
| 110 | +{ |
| 111 | + TestRegistry* registry = TestRegistry::getCurrentRegistry(); |
| 112 | + CHECK(registry->findTestWithGroup("GTestSimpleTest")); |
| 113 | +} |
| 114 | + |
| 115 | +TEST(gtest, SimpleGoogleTestGetCalled) |
| 116 | +{ |
| 117 | + StringBufferTestOutput output; |
| 118 | + TestResult result(output); |
| 119 | + TestPlugin plugin("dummy"); |
| 120 | + |
| 121 | + TestRegistry* registry = TestRegistry::getCurrentRegistry(); |
| 122 | + UtestShell * shell = registry->findTestWithName("GTestEqual"); |
| 123 | + g_GTestEqual_has_been_called = false; |
| 124 | + shell->runOneTest(&plugin, result); |
| 125 | + |
| 126 | + CHECK(g_GTestEqual_has_been_called); |
| 127 | +} |
| 128 | + |
| 129 | +static bool afterCheck; |
| 130 | + |
| 131 | +static void _failMethodEXPECT_EQ() |
| 132 | +{ |
| 133 | + EXPECT_EQ(1, 2); |
| 134 | + afterCheck = true; |
| 135 | +} |
| 136 | + |
| 137 | +static void _failMethodASSERT_EQ() |
| 138 | +{ |
| 139 | + ASSERT_EQ(1, 2); |
| 140 | + afterCheck = true; |
| 141 | +} |
| 142 | + |
| 143 | +static void _failMethodEXPECT_TRUE() |
| 144 | +{ |
| 145 | + EXPECT_TRUE(false); |
| 146 | + afterCheck = true; |
| 147 | +} |
| 148 | + |
| 149 | +static void _failMethodASSERT_TRUE() |
| 150 | +{ |
| 151 | + ASSERT_TRUE(false); |
| 152 | + afterCheck = true; |
| 153 | +} |
| 154 | + |
| 155 | +static void _failMethodEXPECT_FALSE() |
| 156 | +{ |
| 157 | + EXPECT_FALSE(true); |
| 158 | + afterCheck = true; |
| 159 | +} |
| 160 | + |
| 161 | +static void _failMethodEXPECT_STREQ() |
| 162 | +{ |
| 163 | + EXPECT_STREQ("hello", "world"); |
| 164 | + afterCheck = true; |
| 165 | +} |
| 166 | + |
| 167 | +TEST_GROUP(gtestMacros) |
| 168 | +{ |
| 169 | + TestTestingFixture* fixture; |
| 170 | + void setup() |
| 171 | + { |
| 172 | + fixture = new TestTestingFixture(); |
| 173 | + afterCheck = false; |
| 174 | + } |
| 175 | + void teardown() |
| 176 | + { |
| 177 | + delete fixture; |
| 178 | + } |
| 179 | + |
| 180 | + void testFailureWith(void(*method)()) |
| 181 | + { |
| 182 | + fixture->setTestFunction(method); |
| 183 | + fixture->runAllTests(); |
| 184 | + LONGS_EQUAL(1, fixture->getFailureCount()); |
| 185 | + CHECK(!afterCheck); |
| 186 | + } |
| 187 | + |
| 188 | +}; |
| 189 | + |
| 190 | +TEST(gtestMacros, EXPECT_EQFails) |
| 191 | +{ |
| 192 | + testFailureWith(_failMethodEXPECT_EQ); |
| 193 | +} |
| 194 | + |
| 195 | +TEST(gtestMacros, EXPECT_TRUEFails) |
| 196 | +{ |
| 197 | + testFailureWith(_failMethodEXPECT_TRUE); |
| 198 | +} |
| 199 | + |
| 200 | +TEST(gtestMacros, EXPECT_FALSEFails) |
| 201 | +{ |
| 202 | + testFailureWith(_failMethodEXPECT_FALSE); |
| 203 | +} |
| 204 | + |
| 205 | +TEST(gtestMacros, EXPECT_STREQFails) |
| 206 | +{ |
| 207 | + testFailureWith(_failMethodEXPECT_STREQ); |
| 208 | +} |
| 209 | + |
| 210 | +TEST(gtestMacros, ASSERT_EQFails) |
| 211 | +{ |
| 212 | + testFailureWith(_failMethodASSERT_EQ); |
| 213 | +} |
| 214 | + |
| 215 | +TEST(gtestMacros, ASSERT_TRUEFails) |
| 216 | +{ |
| 217 | + testFailureWith(_failMethodASSERT_TRUE); |
| 218 | +} |
0 commit comments