From 201ac161919b2c7f464b4f966a4f1a1a2379c486 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Wed, 18 Nov 2009 00:12:05 +0000 Subject: Enables gmock's implicit_cast to work with source types that have a non-const conversion operator (by Zhanyong Wan). --- test/gmock-port_test.cc | 139 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 135 insertions(+), 4 deletions(-) (limited to 'test/gmock-port_test.cc') 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 #include -// 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(&derived)); +} + +TEST(ImplicitCastTest, CanUseInheritance) { + Derived derived(1); + Base base = ::testing::internal::implicit_cast(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(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(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(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(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(&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 -- cgit v1.2.3