diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ae861015..a532a78b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -10,9 +10,10 @@ add_executable(FakeIt_tests functional.cpp gcc_stubbing_multiple_values_tests.cpp gcc_type_info_tests.cpp + inherited_funcs_tests.cpp miscellaneous_tests.cpp move_only_return_tests.cpp - moving_mocks_around.cpp + moving_mocks_around_tests.cpp msc_stubbing_multiple_values_tests.cpp msc_type_info_tests.cpp multiple_translation_units_stub.cpp diff --git a/tests/inherited_funcs_tests.cpp b/tests/inherited_funcs_tests.cpp new file mode 100644 index 00000000..185b2dd0 --- /dev/null +++ b/tests/inherited_funcs_tests.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2014 Eran Pe'er. + * + * This program is made available under the terms of the MIT License. + * + * Created on Mar 10, 2014 + */ + +#include "tpunit++.hpp" +#include "fakeit.hpp" + +using namespace fakeit; + +struct InheritedFuncsTests : tpunit::TestFixture +{ + + InheritedFuncsTests() : + TestFixture( + TEST(InheritedFuncsTests::mock_base_overloaded_functions), + TEST(InheritedFuncsTests::mock_base_and_child_overloaded_functions) + ) + { + } + + class BaseInterface + { + public: + virtual ~BaseInterface() = default; + + virtual double nonOverloadedMethod() = 0; + virtual double overloadedMethod() = 0; + virtual double overloadedMethod() const = 0; + virtual double overloadedInChildMethod() = 0; + }; + + class Interface : public BaseInterface + { + public: + ~Interface() override = default; + using BaseInterface::overloadedInChildMethod; + virtual double overloadedInChildMethod() const = 0; + }; + + void mock_base_overloaded_functions() + { + Mock mock; + + When(Method(mock, nonOverloadedMethod)).Return(1.5); + When(OverloadedMethod(mock, overloadedMethod, double())).Return(2.5); + When(ConstOverloadedMethod(mock, overloadedMethod, double())).Return(3.5); + + Interface& interface = mock.get(); + const Interface& constInterface = mock.get(); + + EXPECT_EQUAL(interface.nonOverloadedMethod(), 1.5); + EXPECT_EQUAL(interface.overloadedMethod(), 2.5); + EXPECT_EQUAL(constInterface.overloadedMethod(), 3.5); + + Verify(Method(mock, nonOverloadedMethod)).Exactly(1); + Verify(OverloadedMethod(mock, overloadedMethod, double())).Exactly(1); + Verify(ConstOverloadedMethod(mock, overloadedMethod, double())).Exactly(1); + } + + void mock_base_and_child_overloaded_functions() + { + Mock mock; + + When(OverloadedMethod(mock, overloadedInChildMethod, double())).Return(4.5); + When(ConstOverloadedMethod(mock, overloadedInChildMethod, double())).Return(5.5); + + Interface& interface = mock.get(); + const Interface& constInterface = mock.get(); + + EXPECT_EQUAL(interface.overloadedInChildMethod(), 4.5); + EXPECT_EQUAL(constInterface.overloadedInChildMethod(), 5.5); + + Verify(OverloadedMethod(mock, overloadedInChildMethod, double())).Exactly(1); + Verify(ConstOverloadedMethod(mock, overloadedInChildMethod, double())).Exactly(1); + } + +} __InheritedFuncsTests; diff --git a/tests/moving_mocks_around.cpp b/tests/moving_mocks_around_tests.cpp similarity index 89% rename from tests/moving_mocks_around.cpp rename to tests/moving_mocks_around_tests.cpp index 4a0a778c..669bc925 100644 --- a/tests/moving_mocks_around.cpp +++ b/tests/moving_mocks_around_tests.cpp @@ -11,15 +11,15 @@ using namespace fakeit; -struct MovingMocksAround : tpunit::TestFixture +struct MovingMocksAroundTests : tpunit::TestFixture { - MovingMocksAround() : + MovingMocksAroundTests() : TestFixture( - TEST(MovingMocksAround::move_mock), - TEST(MovingMocksAround::move_mock_then_delete), - TEST(MovingMocksAround::create_mock_from_function), - TEST(MovingMocksAround::create_multiple_mocks_from_function) + TEST(MovingMocksAroundTests::move_mock), + TEST(MovingMocksAroundTests::move_mock_then_delete), + TEST(MovingMocksAroundTests::create_mock_from_function), + TEST(MovingMocksAroundTests::create_multiple_mocks_from_function) ) { } @@ -98,4 +98,4 @@ struct MovingMocksAround : tpunit::TestFixture Verify(Method(mock2, function).Using(paramString2)).Exactly(1); } -} __MovingMocksAround; +} __MovingMocksAroundTests; diff --git a/tests/multiple_translation_units_stub.cpp b/tests/multiple_translation_units_stub.cpp index 6f2dc0bb..2fa25bfe 100644 --- a/tests/multiple_translation_units_stub.cpp +++ b/tests/multiple_translation_units_stub.cpp @@ -1,12 +1,27 @@ #include "multiple_translation_units_stub.h" +namespace multiple_tu { -void stubFunc2(fakeit::Mock& mock) -{ - fakeit::When(Method(mock, func2)).AlwaysReturn("String"); -} + void stubFunc(fakeit::Mock& mock) + { + fakeit::When(Method(mock, func)).Return(5); + } + + void stubFunc2(fakeit::Mock& mock) + { + fakeit::When(Method(mock, func2)).Return("String"); + } + + void stubMoreFunc(fakeit::Mock& mock) + { + fakeit::When(Method(mock, func).Using(1)).Return(10); + fakeit::When(Method(mock, func).Using(2)).Return(20); + } + + void stubMoreFunc2(fakeit::Mock& mock) + { + fakeit::When(Method(mock, func2).Using(1)).Return("String1"); + fakeit::When(Method(mock, func2).Using(2)).Return("String2"); + } -void stubFunc(fakeit::Mock& mock) -{ - fakeit::When(Method(mock, func)).AlwaysReturn(3); } diff --git a/tests/multiple_translation_units_stub.h b/tests/multiple_translation_units_stub.h index 084cf17b..28293d63 100644 --- a/tests/multiple_translation_units_stub.h +++ b/tests/multiple_translation_units_stub.h @@ -4,10 +4,16 @@ #include "fakeit.hpp" -struct SomeInterface { - virtual int func() = 0; - virtual std::string func2() = 0; -}; +namespace multiple_tu { -void stubFunc2(fakeit::Mock& mock); -void stubFunc(fakeit::Mock& mock); + struct SomeInterface { + virtual int func(int) = 0; + virtual std::string func2(int) = 0; + }; + + void stubFunc(fakeit::Mock& mock); + void stubFunc2(fakeit::Mock& mock); + void stubMoreFunc(fakeit::Mock& mock); + void stubMoreFunc2(fakeit::Mock& mock); + +} diff --git a/tests/multiple_translation_units_stub_test.cpp b/tests/multiple_translation_units_stub_test.cpp index 75cf43f4..8f954850 100644 --- a/tests/multiple_translation_units_stub_test.cpp +++ b/tests/multiple_translation_units_stub_test.cpp @@ -3,22 +3,126 @@ #include "multiple_translation_units_stub.h" using namespace fakeit; +using namespace multiple_tu; struct MultipleTranslationUnitsStub : tpunit::TestFixture { - MultipleTranslationUnitsStub() - : tpunit::TestFixture( - TEST(MultipleTranslationUnitsStub::NoCollidingIds) - ) - {} - void NoCollidingIds() { + MultipleTranslationUnitsStub() : + tpunit::TestFixture( + TEST(MultipleTranslationUnitsStub::NoCollidingIdsBasics), + TEST(MultipleTranslationUnitsStub::NoCollidingIdsAlternateBasics), + TEST(MultipleTranslationUnitsStub::NoCollidingIdsMoreFunctionsMocked), + TEST(MultipleTranslationUnitsStub::NoCollidingIdsWhenOverridingMocks) + ) + { + } + + void NoCollidingIdsBasics() + { + Mock mock; + + stubFunc(mock); + When(Method(mock, func2)).Return("Something"); + + SomeInterface &i = mock.get(); + + // Uncatchable write access violation if ids collide. + EXPECT_EQUAL(i.func(5), 5); + EXPECT_EQUAL(i.func2(5), "Something"); + + Verify(Method(mock, func).Using(5)).Exactly(1); + Verify(Method(mock, func2).Using(5)).Exactly(1); + } + + void NoCollidingIdsAlternateBasics() + { + Mock mock; + + When(Method(mock, func)).Return(100); + stubFunc2(mock); + + SomeInterface &i = mock.get(); + + // Uncatchable write access violation if ids collide. + EXPECT_EQUAL(i.func(5), 100); + EXPECT_EQUAL(i.func2(5), "String"); + + Verify(Method(mock, func).Using(5)).Exactly(1); + Verify(Method(mock, func2).Using(5)).Exactly(1); + } + + void NoCollidingIdsMoreFunctionsMocked() + { Mock mock; + + stubFunc(mock); + stubFunc2(mock); + + When(Method(mock, func).Using(20)).Return(20); + When(Method(mock, func).Using(50)).Return(50); + + When(Method(mock, func2).Using(20)).Return("Something-20"); + When(Method(mock, func2).Using(50)).Return("Something-50"); + + stubMoreFunc(mock); + stubMoreFunc2(mock); + SomeInterface &i = mock.get(); - + + // Uncatchable write access violation if ids collide. + EXPECT_EQUAL(i.func(1), 10); + EXPECT_EQUAL(i.func(2), 20); + EXPECT_EQUAL(i.func(5), 5); + EXPECT_EQUAL(i.func(20), 20); + EXPECT_EQUAL(i.func(50), 50); + EXPECT_EQUAL(i.func2(1), "String1"); + EXPECT_EQUAL(i.func2(2), "String2"); + EXPECT_EQUAL(i.func2(5), "String"); + EXPECT_EQUAL(i.func2(20), "Something-20"); + EXPECT_EQUAL(i.func2(50), "Something-50"); + + Verify(Method(mock, func).Using(1)).Exactly(1); + Verify(Method(mock, func).Using(2)).Exactly(1); + Verify(Method(mock, func).Using(5)).Exactly(1); + Verify(Method(mock, func).Using(20)).Exactly(1); + Verify(Method(mock, func).Using(50)).Exactly(1); + Verify(Method(mock, func2).Using(1)).Exactly(1); + Verify(Method(mock, func2).Using(2)).Exactly(1); + Verify(Method(mock, func2).Using(5)).Exactly(1); + Verify(Method(mock, func2).Using(20)).Exactly(1); + Verify(Method(mock, func2).Using(50)).Exactly(1); + } + + void NoCollidingIdsWhenOverridingMocks() + { + Mock mock; + + stubFunc(mock); + When(Method(mock, func)).Return(123); + When(Method(mock, func2)).Return("Something"); stubFunc2(mock); - When(Method(mock, func)).Return(1); - mock.get().func2(); // Uncatchable write access violation if ids collide + SomeInterface &i = mock.get(); + + EXPECT_EQUAL(i.func(0), 123); + EXPECT_EQUAL(i.func2(0), "String"); + + try { + i.func(0); + FAIL(); + } catch (const UnexpectedMethodCallException&) { + // There was only one action in the mock for this function. + } + + try { + i.func2(0); + FAIL(); + } catch (const UnexpectedMethodCallException&) { + // There was only one action in the mock for this function. + } + + Verify(Method(mock, func)).Exactly(2); + Verify(Method(mock, func)).Exactly(2); } } __MultipleTranslationUnitsStub;