| 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 21 matching lines...) Expand all Loading... |
| 32 // Google Mock - a framework for writing C++ mock classes. | 32 // Google Mock - a framework for writing C++ mock classes. |
| 33 // | 33 // |
| 34 // This file tests the internal cross-platform support utilities. | 34 // This file tests the internal cross-platform support utilities. |
| 35 | 35 |
| 36 #include <gmock/internal/gmock-port.h> | 36 #include <gmock/internal/gmock-port.h> |
| 37 #include <gtest/gtest.h> | 37 #include <gtest/gtest.h> |
| 38 | 38 |
| 39 // NOTE: if this file is left without tests for some reason, put a dummy | 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 | 40 // test here to make references to symbols in the gtest library and avoid |
| 41 // 'undefined symbol' linker errors in gmock_main: | 41 // 'undefined symbol' linker errors in gmock_main: |
| 42 // | |
| 43 // TEST(DummyTest, Dummy) {} | |
| 44 | 42 |
| 45 namespace testing { | 43 TEST(DummyTest, Dummy) {} |
| 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 class Castable { | |
| 80 public: | |
| 81 Castable(bool* converted) : converted_(converted) {} | |
| 82 operator Base() { | |
| 83 *converted_ = true; | |
| 84 return Base(); | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 bool* converted_; | |
| 89 }; | |
| 90 | |
| 91 TEST(ImplicitCastTest, CanUseNonConstCastOperator) { | |
| 92 bool converted = false; | |
| 93 Castable castable(&converted); | |
| 94 Base base = ::testing::internal::implicit_cast<Base>(castable); | |
| 95 EXPECT_TRUE(converted); | |
| 96 } | |
| 97 | |
| 98 class ConstCastable { | |
| 99 public: | |
| 100 ConstCastable(bool* converted) : converted_(converted) {} | |
| 101 operator Base() const { | |
| 102 *converted_ = true; | |
| 103 return Base(); | |
| 104 } | |
| 105 | |
| 106 private: | |
| 107 bool* converted_; | |
| 108 }; | |
| 109 | |
| 110 TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) { | |
| 111 bool converted = false; | |
| 112 const ConstCastable const_castable(&converted); | |
| 113 Base base = ::testing::internal::implicit_cast<Base>(const_castable); | |
| 114 EXPECT_TRUE(converted); | |
| 115 } | |
| 116 | |
| 117 class ConstAndNonConstCastable { | |
| 118 public: | |
| 119 ConstAndNonConstCastable(bool* converted, bool* const_converted) | |
| 120 : converted_(converted), const_converted_(const_converted) {} | |
| 121 operator Base() { | |
| 122 *converted_ = true; | |
| 123 return Base(); | |
| 124 } | |
| 125 operator Base() const { | |
| 126 *const_converted_ = true; | |
| 127 return Base(); | |
| 128 } | |
| 129 | |
| 130 private: | |
| 131 bool* converted_; | |
| 132 bool* const_converted_; | |
| 133 }; | |
| 134 | |
| 135 TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) { | |
| 136 bool converted = false; | |
| 137 bool const_converted = false; | |
| 138 ConstAndNonConstCastable castable(&converted, &const_converted); | |
| 139 Base base = ::testing::internal::implicit_cast<Base>(castable); | |
| 140 EXPECT_TRUE(converted); | |
| 141 EXPECT_FALSE(const_converted); | |
| 142 | |
| 143 converted = false; | |
| 144 const_converted = false; | |
| 145 const ConstAndNonConstCastable const_castable(&converted, &const_converted); | |
| 146 base = ::testing::internal::implicit_cast<Base>(const_castable); | |
| 147 EXPECT_FALSE(converted); | |
| 148 EXPECT_TRUE(const_converted); | |
| 149 } | |
| 150 | |
| 151 class To { | |
| 152 public: | |
| 153 To(bool* converted) { *converted = true; } // NOLINT | |
| 154 }; | |
| 155 | |
| 156 TEST(ImplicitCastTest, CanUseImplicitConstructor) { | |
| 157 bool converted = false; | |
| 158 To to = ::testing::internal::implicit_cast<To>(&converted); | |
| 159 EXPECT_TRUE(converted); | |
| 160 } | |
| 161 | |
| 162 } // namespace gmock_port_test | |
| 163 } // namespace internal | |
| 164 } // namespace testing | |
| OLD | NEW |