Skip to content

Commit

Permalink
Add back tests from previous implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranckRJ committed May 26, 2024
1 parent e91d6b8 commit 2b51c7c
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 30 deletions.
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions tests/inherited_funcs_tests.cpp
Original file line number Diff line number Diff line change
@@ -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<Interface> 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<Interface> 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;
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
{
}
Expand Down Expand Up @@ -98,4 +98,4 @@ struct MovingMocksAround : tpunit::TestFixture
Verify(Method(mock2, function).Using(paramString2)).Exactly(1);
}

} __MovingMocksAround;
} __MovingMocksAroundTests;
29 changes: 22 additions & 7 deletions tests/multiple_translation_units_stub.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#include "multiple_translation_units_stub.h"

namespace multiple_tu {

void stubFunc2(fakeit::Mock<SomeInterface>& mock)
{
fakeit::When(Method(mock, func2)).AlwaysReturn<std::string>("String");
}
void stubFunc(fakeit::Mock<SomeInterface>& mock)
{
fakeit::When(Method(mock, func)).Return(5);
}

void stubFunc2(fakeit::Mock<SomeInterface>& mock)
{
fakeit::When(Method(mock, func2)).Return("String");
}

void stubMoreFunc(fakeit::Mock<SomeInterface>& mock)
{
fakeit::When(Method(mock, func).Using(1)).Return(10);
fakeit::When(Method(mock, func).Using(2)).Return(20);
}

void stubMoreFunc2(fakeit::Mock<SomeInterface>& mock)
{
fakeit::When(Method(mock, func2).Using(1)).Return("String1");
fakeit::When(Method(mock, func2).Using(2)).Return("String2");
}

void stubFunc(fakeit::Mock<SomeInterface>& mock)
{
fakeit::When(Method(mock, func)).AlwaysReturn<int>(3);
}
18 changes: 12 additions & 6 deletions tests/multiple_translation_units_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<SomeInterface>& mock);
void stubFunc(fakeit::Mock<SomeInterface>& mock);
struct SomeInterface {
virtual int func(int) = 0;
virtual std::string func2(int) = 0;
};

void stubFunc(fakeit::Mock<SomeInterface>& mock);
void stubFunc2(fakeit::Mock<SomeInterface>& mock);
void stubMoreFunc(fakeit::Mock<SomeInterface>& mock);
void stubMoreFunc2(fakeit::Mock<SomeInterface>& mock);

}
122 changes: 113 additions & 9 deletions tests/multiple_translation_units_stub_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SomeInterface> 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<SomeInterface> 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<SomeInterface> 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<SomeInterface> mock;

stubFunc(mock);
When(Method(mock, func)).Return(123);
When(Method(mock, func2)).Return("Something");
stubFunc2(mock);
When(Method(mock, func)).Return<int>(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;

0 comments on commit 2b51c7c

Please sign in to comment.