shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: vladl@google.com (Vlad Losev) |
| 31 | |
| 32 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file tests the internal cross-platform support utilities. |
| 35 | |
| 36 | #include <gmock/internal/gmock-port.h> |
| 37 | #include <gtest/gtest.h> |
| 38 | |
| 39 | TEST(GmockCheckSyntaxTest, BehavesLikeASingleStatement) { |
| 40 | if (false) |
| 41 | GMOCK_CHECK_(false) << "This should never be executed; " |
| 42 | "It's a compilation test only."; |
| 43 | |
| 44 | if (true) |
| 45 | GMOCK_CHECK_(true); |
| 46 | else |
| 47 | ; |
| 48 | |
| 49 | if (false) |
| 50 | ; |
| 51 | else |
| 52 | GMOCK_CHECK_(true) << ""; |
| 53 | } |
| 54 | |
| 55 | TEST(GmockCheckSyntaxTest, WorksWithSwitch) { |
| 56 | switch (0) { |
| 57 | case 1: |
| 58 | break; |
| 59 | default: |
| 60 | GMOCK_CHECK_(true); |
| 61 | } |
| 62 | |
| 63 | switch(0) |
| 64 | case 0: |
| 65 | GMOCK_CHECK_(true) << "Check failed in switch case"; |
| 66 | } |
| 67 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 68 | TEST(GmockCheckDeathTest, DiesWithCorrectOutputOnFailure) { |
| 69 | const bool a_false_condition = false; |
zhanyong.wan | 93ad355 | 2009-03-06 01:21:45 +0000 | [diff] [blame] | 70 | // MSVC and gcc use different formats to print source file locations. |
| 71 | // Google Mock's failure messages use the same format as used by the |
| 72 | // compiler, in order for the IDE to recognize them. Therefore we look |
| 73 | // for different patterns here depending on the compiler. |
| 74 | const char regex[] = |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 75 | #ifdef _MSC_VER |
zhanyong.wan | 93ad355 | 2009-03-06 01:21:45 +0000 | [diff] [blame] | 76 | "gmock-port_test\\.cc\\(\\d+\\):" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 77 | #else |
zhanyong.wan | 93ad355 | 2009-03-06 01:21:45 +0000 | [diff] [blame] | 78 | "gmock-port_test\\.cc:[0-9]+" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 79 | #endif // _MSC_VER |
zhanyong.wan | 93ad355 | 2009-03-06 01:21:45 +0000 | [diff] [blame] | 80 | ".*a_false_condition.*Extra info"; |
| 81 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 82 | EXPECT_DEATH_IF_SUPPORTED(GMOCK_CHECK_(a_false_condition) << "Extra info", |
| 83 | regex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 84 | } |
| 85 | |
zhanyong.wan | 04d6ed8 | 2009-09-11 07:01:08 +0000 | [diff] [blame] | 86 | #if GTEST_HAS_DEATH_TEST |
| 87 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 88 | TEST(GmockCheckDeathTest, LivesSilentlyOnSuccess) { |
| 89 | EXPECT_EXIT({ |
| 90 | GMOCK_CHECK_(true) << "Extra info"; |
| 91 | ::std::cerr << "Success\n"; |
| 92 | exit(0); }, |
| 93 | ::testing::ExitedWithCode(0), "Success"); |
| 94 | } |
| 95 | |
| 96 | #endif // GTEST_HAS_DEATH_TEST |