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 | |
vladlosev | 201ac16 | 2009-11-18 00:12:05 +0000 | [diff] [blame] | 39 | // NOTE: if this file is left without tests for some reason, put a dummy |
| 40 | // test here to make references to symbols in the gtest library and avoid |
| 41 | // 'undefined symbol' linker errors in gmock_main: |
| 42 | // |
| 43 | // TEST(DummyTest, Dummy) {} |
zhanyong.wan | 2e7c475 | 2009-11-06 23:43:30 +0000 | [diff] [blame] | 44 | |
vladlosev | 201ac16 | 2009-11-18 00:12:05 +0000 | [diff] [blame] | 45 | namespace testing { |
| 46 | namespace internal { |
| 47 | // Needed to avoid name collisions in gmock_all_test.cc. |
| 48 | namespace gmock_port_test { |
| 49 | |
| 50 | class Base { |
| 51 | public: |
| 52 | // Copy constructor and assignment operator do exactly what we need, so we |
| 53 | // use them. |
| 54 | Base() : member_(0) {} |
| 55 | explicit Base(int n) : member_(n) {} |
| 56 | virtual ~Base() {} |
| 57 | int member() { return member_; } |
| 58 | |
| 59 | private: |
| 60 | int member_; |
| 61 | }; |
| 62 | |
| 63 | class Derived : public Base { |
| 64 | public: |
| 65 | explicit Derived(int n) : Base(n) {} |
| 66 | }; |
| 67 | |
| 68 | TEST(ImplicitCastTest, ConvertsPointers) { |
| 69 | Derived derived(0); |
| 70 | EXPECT_TRUE(&derived == ::testing::internal::implicit_cast<Base*>(&derived)); |
| 71 | } |
| 72 | |
| 73 | TEST(ImplicitCastTest, CanUseInheritance) { |
| 74 | Derived derived(1); |
| 75 | Base base = ::testing::internal::implicit_cast<Base>(derived); |
| 76 | EXPECT_EQ(derived.member(), base.member()); |
| 77 | } |
| 78 | |
| 79 | // The non-const version is not enabled for Symbian since the Nokia compiler |
| 80 | // cannot decide which version of the overloaded implicit_cast method to use |
| 81 | // when the source is a const. |
| 82 | #if !GTEST_OS_SYMBIAN |
| 83 | class Castable { |
| 84 | public: |
| 85 | Castable(bool* converted) : converted_(converted) {} |
| 86 | operator Base() { |
| 87 | *converted_ = true; |
| 88 | return Base(); |
| 89 | } |
| 90 | |
| 91 | private: |
| 92 | bool* const converted_; |
| 93 | }; |
| 94 | |
| 95 | TEST(ImplicitCastTest, CanUseNonConstCastOperator) { |
| 96 | bool converted = false; |
| 97 | Castable castable(&converted); |
| 98 | Base base = ::testing::internal::implicit_cast<Base>(castable); |
| 99 | EXPECT_TRUE(converted); |
| 100 | } |
| 101 | #endif // !GTEST_OS_SYMBIAN |
| 102 | |
| 103 | class ConstCastable { |
| 104 | public: |
| 105 | ConstCastable(bool* converted) : converted_(converted) {} |
| 106 | operator Base() const { |
| 107 | *converted_ = true; |
| 108 | return Base(); |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | bool* const converted_; |
| 113 | }; |
| 114 | |
| 115 | TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) { |
| 116 | bool converted = false; |
| 117 | const ConstCastable const_castable(&converted); |
| 118 | Base base = ::testing::internal::implicit_cast<Base>(const_castable); |
| 119 | EXPECT_TRUE(converted); |
| 120 | } |
| 121 | |
| 122 | // The non-const version is not enabled for Symbian since the Nokia compiler |
| 123 | // cannot decide which version of the overloaded implicit_cast method to use |
| 124 | // when the source is a const. |
| 125 | #if !GTEST_OS_SYMBIAN |
| 126 | class ConstAndNonConstCastable { |
| 127 | public: |
| 128 | ConstAndNonConstCastable(bool* converted, bool* const_converted) |
| 129 | : converted_(converted), const_converted_(const_converted) {} |
| 130 | operator Base() { |
| 131 | *converted_ = true; |
| 132 | return Base(); |
| 133 | } |
| 134 | operator Base() const { |
| 135 | *const_converted_ = true; |
| 136 | return Base(); |
| 137 | } |
| 138 | |
| 139 | private: |
| 140 | bool* const converted_; |
| 141 | bool* const const_converted_; |
| 142 | }; |
| 143 | |
| 144 | TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) { |
| 145 | bool converted = false; |
| 146 | bool const_converted = false; |
| 147 | ConstAndNonConstCastable castable(&converted, &const_converted); |
| 148 | Base base = ::testing::internal::implicit_cast<Base>(castable); |
| 149 | EXPECT_TRUE(converted); |
| 150 | EXPECT_FALSE(const_converted); |
| 151 | |
| 152 | converted = false; |
| 153 | const_converted = false; |
| 154 | const ConstAndNonConstCastable const_castable(&converted, &const_converted); |
| 155 | base = ::testing::internal::implicit_cast<Base>(const_castable); |
| 156 | EXPECT_FALSE(converted); |
| 157 | EXPECT_TRUE(const_converted); |
| 158 | } |
| 159 | #endif // !GTEST_OS_SYMBIAN |
| 160 | |
| 161 | class To { |
| 162 | public: |
| 163 | To(bool* converted) { *converted = true; } // NOLINT |
| 164 | }; |
| 165 | |
| 166 | TEST(ImplicitCastTest, CanUseImplicitConstructor) { |
| 167 | bool converted = false; |
| 168 | To to = ::testing::internal::implicit_cast<To>(&converted); |
| 169 | EXPECT_TRUE(converted); |
| 170 | } |
| 171 | |
| 172 | } // namespace gmock_port_test |
| 173 | } // namespace internal |
| 174 | } // namespace testing |