OLD | NEW |
1 // Copyright 2008, Google Inc. | 1 // Copyright 2008, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 #include <string> | 34 #include <string> |
35 #include <gmock/gmock.h> | 35 #include <gmock/gmock.h> |
36 #include <gtest/gtest.h> | 36 #include <gtest/gtest.h> |
37 #include <gtest/gtest-spi.h> | 37 #include <gtest/gtest-spi.h> |
38 | 38 |
39 // This must not be defined inside the ::testing namespace, or it will | 39 // This must not be defined inside the ::testing namespace, or it will |
40 // clash with ::testing::Mock. | 40 // clash with ::testing::Mock. |
41 class Mock { | 41 class Mock { |
42 public: | 42 public: |
| 43 Mock() {} |
| 44 |
43 MOCK_METHOD0(DoThis, void()); | 45 MOCK_METHOD0(DoThis, void()); |
| 46 |
| 47 private: |
| 48 GTEST_DISALLOW_COPY_AND_ASSIGN_(Mock); |
44 }; | 49 }; |
45 | 50 |
46 namespace testing { | 51 namespace testing { |
47 namespace gmock_nice_strict_test { | 52 namespace gmock_nice_strict_test { |
48 | 53 |
49 using testing::internal::string; | 54 using testing::internal::string; |
50 using testing::GMOCK_FLAG(verbose); | 55 using testing::GMOCK_FLAG(verbose); |
51 using testing::HasSubstr; | 56 using testing::HasSubstr; |
52 using testing::NiceMock; | 57 using testing::NiceMock; |
53 using testing::StrictMock; | 58 using testing::StrictMock; |
54 | 59 |
55 // Defines some mock classes needed by the tests. | 60 // Defines some mock classes needed by the tests. |
56 | 61 |
57 class Foo { | 62 class Foo { |
58 public: | 63 public: |
59 virtual ~Foo() {} | 64 virtual ~Foo() {} |
60 | 65 |
61 virtual void DoThis() = 0; | 66 virtual void DoThis() = 0; |
62 virtual int DoThat(bool flag) = 0; | 67 virtual int DoThat(bool flag) = 0; |
63 }; | 68 }; |
64 | 69 |
65 class MockFoo : public Foo { | 70 class MockFoo : public Foo { |
66 public: | 71 public: |
| 72 MockFoo() {} |
67 void Delete() { delete this; } | 73 void Delete() { delete this; } |
68 | 74 |
69 MOCK_METHOD0(DoThis, void()); | 75 MOCK_METHOD0(DoThis, void()); |
70 MOCK_METHOD1(DoThat, int(bool flag)); | 76 MOCK_METHOD1(DoThat, int(bool flag)); |
| 77 |
| 78 private: |
| 79 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo); |
71 }; | 80 }; |
72 | 81 |
73 class MockBar { | 82 class MockBar { |
74 public: | 83 public: |
75 explicit MockBar(const string& s) : str_(s) {} | 84 explicit MockBar(const string& s) : str_(s) {} |
76 | 85 |
77 MockBar(char a1, char a2, string a3, string a4, int a5, int a6, | 86 MockBar(char a1, char a2, string a3, string a4, int a5, int a6, |
78 const string& a7, const string& a8, bool a9, bool a10) { | 87 const string& a7, const string& a8, bool a9, bool a10) { |
79 str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) + | 88 str_ = string() + a1 + a2 + a3 + a4 + static_cast<char>(a5) + |
80 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F'); | 89 static_cast<char>(a6) + a7 + a8 + (a9 ? 'T' : 'F') + (a10 ? 'T' : 'F'); |
81 } | 90 } |
82 | 91 |
83 virtual ~MockBar() {} | 92 virtual ~MockBar() {} |
84 | 93 |
85 const string& str() const { return str_; } | 94 const string& str() const { return str_; } |
86 | 95 |
87 MOCK_METHOD0(This, int()); | 96 MOCK_METHOD0(This, int()); |
88 MOCK_METHOD2(That, string(int, bool)); | 97 MOCK_METHOD2(That, string(int, bool)); |
89 | 98 |
90 private: | 99 private: |
91 string str_; | 100 string str_; |
| 101 |
| 102 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockBar); |
92 }; | 103 }; |
93 | 104 |
94 // TODO(wan@google.com): find a way to re-enable these tests. | 105 // TODO(wan@google.com): find a way to re-enable these tests. |
95 #if 0 | 106 #if 0 |
96 | 107 |
97 // Tests that a nice mock generates no warning for uninteresting calls. | 108 // Tests that a nice mock generates no warning for uninteresting calls. |
98 TEST(NiceMockTest, NoWarningForUninterestingCall) { | 109 TEST(NiceMockTest, NoWarningForUninterestingCall) { |
99 NiceMock<MockFoo> nice_foo; | 110 NiceMock<MockFoo> nice_foo; |
100 | 111 |
101 CaptureTestStdout(); | 112 CaptureTestStdout(); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 // non-default constructor. | 177 // non-default constructor. |
167 TEST(NiceMockTest, NonDefaultConstructor10) { | 178 TEST(NiceMockTest, NonDefaultConstructor10) { |
168 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', | 179 NiceMock<MockBar> nice_bar('a', 'b', "c", "d", 'e', 'f', |
169 "g", "h", true, false); | 180 "g", "h", true, false); |
170 EXPECT_EQ("abcdefghTF", nice_bar.str()); | 181 EXPECT_EQ("abcdefghTF", nice_bar.str()); |
171 | 182 |
172 nice_bar.This(); | 183 nice_bar.This(); |
173 nice_bar.That(5, true); | 184 nice_bar.That(5, true); |
174 } | 185 } |
175 | 186 |
176 #if !GTEST_OS_SYMBIAN | 187 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
177 // Tests that NiceMock<Mock> compiles where Mock is a user-defined | 188 // Tests that NiceMock<Mock> compiles where Mock is a user-defined |
178 // class (as opposed to ::testing::Mock). We had to workaround an | 189 // class (as opposed to ::testing::Mock). We had to workaround an |
179 // MSVC 8.0 bug that caused the symbol Mock used in the definition of | 190 // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
180 // NiceMock to be looked up in the wrong context, and this test | 191 // NiceMock to be looked up in the wrong context, and this test |
181 // ensures that our fix works. | 192 // ensures that our fix works. |
182 // | 193 // |
183 // We have to skip this test on Symbian, as it causes the program to | 194 // We have to skip this test on Symbian and Windows Mobile, as it |
184 // crash there, for reasons unclear to us yet. | 195 // causes the program to crash there, for reasons unclear to us yet. |
185 TEST(NiceMockTest, AcceptsClassNamedMock) { | 196 TEST(NiceMockTest, AcceptsClassNamedMock) { |
186 NiceMock< ::Mock> nice; | 197 NiceMock< ::Mock> nice; |
187 EXPECT_CALL(nice, DoThis()); | 198 EXPECT_CALL(nice, DoThis()); |
188 nice.DoThis(); | 199 nice.DoThis(); |
189 } | 200 } |
190 #endif // !GTEST_OS_SYMBIAN | 201 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
191 | 202 |
192 // Tests that a strict mock allows expected calls. | 203 // Tests that a strict mock allows expected calls. |
193 TEST(StrictMockTest, AllowsExpectedCall) { | 204 TEST(StrictMockTest, AllowsExpectedCall) { |
194 StrictMock<MockFoo> strict_foo; | 205 StrictMock<MockFoo> strict_foo; |
195 | 206 |
196 EXPECT_CALL(strict_foo, DoThis()); | 207 EXPECT_CALL(strict_foo, DoThis()); |
197 strict_foo.DoThis(); | 208 strict_foo.DoThis(); |
198 } | 209 } |
199 | 210 |
200 // Tests that an unexpected call on a strict mock fails. | 211 // Tests that an unexpected call on a strict mock fails. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 // non-default constructor. | 251 // non-default constructor. |
241 TEST(StrictMockTest, NonDefaultConstructor10) { | 252 TEST(StrictMockTest, NonDefaultConstructor10) { |
242 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', | 253 StrictMock<MockBar> strict_bar('a', 'b', "c", "d", 'e', 'f', |
243 "g", "h", true, false); | 254 "g", "h", true, false); |
244 EXPECT_EQ("abcdefghTF", strict_bar.str()); | 255 EXPECT_EQ("abcdefghTF", strict_bar.str()); |
245 | 256 |
246 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), | 257 EXPECT_NONFATAL_FAILURE(strict_bar.That(5, true), |
247 "Uninteresting mock function call"); | 258 "Uninteresting mock function call"); |
248 } | 259 } |
249 | 260 |
250 #if !GTEST_OS_SYMBIAN | 261 #if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
251 // Tests that StrictMock<Mock> compiles where Mock is a user-defined | 262 // Tests that StrictMock<Mock> compiles where Mock is a user-defined |
252 // class (as opposed to ::testing::Mock). We had to workaround an | 263 // class (as opposed to ::testing::Mock). We had to workaround an |
253 // MSVC 8.0 bug that caused the symbol Mock used in the definition of | 264 // MSVC 8.0 bug that caused the symbol Mock used in the definition of |
254 // StrictMock to be looked up in the wrong context, and this test | 265 // StrictMock to be looked up in the wrong context, and this test |
255 // ensures that our fix works. | 266 // ensures that our fix works. |
256 // | 267 // |
257 // We have to skip this test on Symbian, as it causes the program to | 268 // We have to skip this test on Symbian and Windows Mobile, as it |
258 // crash there, for reasons unclear to us yet. | 269 // causes the program to crash there, for reasons unclear to us yet. |
259 TEST(StrictMockTest, AcceptsClassNamedMock) { | 270 TEST(StrictMockTest, AcceptsClassNamedMock) { |
260 StrictMock< ::Mock> strict; | 271 StrictMock< ::Mock> strict; |
261 EXPECT_CALL(strict, DoThis()); | 272 EXPECT_CALL(strict, DoThis()); |
262 strict.DoThis(); | 273 strict.DoThis(); |
263 } | 274 } |
264 #endif // !GTEST_OS_SYMBIAN | 275 #endif // !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE |
265 | 276 |
266 } // namespace gmock_nice_strict_test | 277 } // namespace gmock_nice_strict_test |
267 } // namespace testing | 278 } // namespace testing |
OLD | NEW |