diff options
author | vladlosev <vladlosev@8415998a-534a-0410-bf83-d39667b30386> | 2009-11-18 00:12:05 +0000 |
---|---|---|
committer | vladlosev <vladlosev@8415998a-534a-0410-bf83-d39667b30386> | 2009-11-18 00:12:05 +0000 |
commit | 201ac161919b2c7f464b4f966a4f1a1a2379c486 (patch) | |
tree | 295fb41b053b7332b9f49c90535d668b33668313 /test/gmock-port_test.cc | |
parent | a070cbd91c2a8bfe7caed64e31387312a1c5df5a (diff) | |
download | googletest-201ac161919b2c7f464b4f966a4f1a1a2379c486.tar.gz googletest-201ac161919b2c7f464b4f966a4f1a1a2379c486.tar.bz2 googletest-201ac161919b2c7f464b4f966a4f1a1a2379c486.zip |
Enables gmock's implicit_cast to work with source types that have a non-const conversion operator (by Zhanyong Wan).
Diffstat (limited to 'test/gmock-port_test.cc')
-rw-r--r-- | test/gmock-port_test.cc | 139 |
1 files changed, 135 insertions, 4 deletions
diff --git a/test/gmock-port_test.cc b/test/gmock-port_test.cc index fe844e72..3d983d52 100644 --- a/test/gmock-port_test.cc +++ b/test/gmock-port_test.cc @@ -36,8 +36,139 @@ #include <gmock/internal/gmock-port.h> #include <gtest/gtest.h> -// This file intentionally contains no tests at this moment. +// NOTE: if this file is left without tests for some reason, put a dummy +// test here to make references to symbols in the gtest library and avoid +// 'undefined symbol' linker errors in gmock_main: +// +// TEST(DummyTest, Dummy) {} + +namespace testing { +namespace internal { +// Needed to avoid name collisions in gmock_all_test.cc. +namespace gmock_port_test { + +class Base { + public: + // Copy constructor and assignment operator do exactly what we need, so we + // use them. + Base() : member_(0) {} + explicit Base(int n) : member_(n) {} + virtual ~Base() {} + int member() { return member_; } + + private: + int member_; +}; + +class Derived : public Base { + public: + explicit Derived(int n) : Base(n) {} +}; + +TEST(ImplicitCastTest, ConvertsPointers) { + Derived derived(0); + EXPECT_TRUE(&derived == ::testing::internal::implicit_cast<Base*>(&derived)); +} + +TEST(ImplicitCastTest, CanUseInheritance) { + Derived derived(1); + Base base = ::testing::internal::implicit_cast<Base>(derived); + EXPECT_EQ(derived.member(), base.member()); +} + +// The non-const version is not enabled for Symbian since the Nokia compiler +// cannot decide which version of the overloaded implicit_cast method to use +// when the source is a const. +#if !GTEST_OS_SYMBIAN +class Castable { + public: + Castable(bool* converted) : converted_(converted) {} + operator Base() { + *converted_ = true; + return Base(); + } + + private: + bool* const converted_; +}; + +TEST(ImplicitCastTest, CanUseNonConstCastOperator) { + bool converted = false; + Castable castable(&converted); + Base base = ::testing::internal::implicit_cast<Base>(castable); + EXPECT_TRUE(converted); +} +#endif // !GTEST_OS_SYMBIAN + +class ConstCastable { + public: + ConstCastable(bool* converted) : converted_(converted) {} + operator Base() const { + *converted_ = true; + return Base(); + } + + private: + bool* const converted_; +}; + +TEST(ImplicitCastTest, CanUseConstCastOperatorOnConstValues) { + bool converted = false; + const ConstCastable const_castable(&converted); + Base base = ::testing::internal::implicit_cast<Base>(const_castable); + EXPECT_TRUE(converted); +} + +// The non-const version is not enabled for Symbian since the Nokia compiler +// cannot decide which version of the overloaded implicit_cast method to use +// when the source is a const. +#if !GTEST_OS_SYMBIAN +class ConstAndNonConstCastable { + public: + ConstAndNonConstCastable(bool* converted, bool* const_converted) + : converted_(converted), const_converted_(const_converted) {} + operator Base() { + *converted_ = true; + return Base(); + } + operator Base() const { + *const_converted_ = true; + return Base(); + } + + private: + bool* const converted_; + bool* const const_converted_; +}; + +TEST(ImplicitCastTest, CanSelectBetweenConstAndNonConstCasrAppropriately) { + bool converted = false; + bool const_converted = false; + ConstAndNonConstCastable castable(&converted, &const_converted); + Base base = ::testing::internal::implicit_cast<Base>(castable); + EXPECT_TRUE(converted); + EXPECT_FALSE(const_converted); + + converted = false; + const_converted = false; + const ConstAndNonConstCastable const_castable(&converted, &const_converted); + base = ::testing::internal::implicit_cast<Base>(const_castable); + EXPECT_FALSE(converted); + EXPECT_TRUE(const_converted); +} +#endif // !GTEST_OS_SYMBIAN + +class To { + public: + To(bool* converted) { *converted = true; } // NOLINT +}; + +TEST(ImplicitCastTest, CanUseImplicitConstructor) { + bool converted = false; + To to = ::testing::internal::implicit_cast<To>(&converted); + EXPECT_TRUE(converted); +} -// Putting a dummy test here makes references to symbols in the gtest -// library and avoids 'undefined symbol' linker errors in gmock_main. -TEST(DummyTest, Dummy) {} +} // namespace gmock_port_test +} // namespace internal +} // namespace testing |