Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranckRJ committed May 12, 2024
1 parent 2bc4623 commit 360c457
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 14 deletions.
16 changes: 14 additions & 2 deletions tests/multiple_translation_units_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ namespace multiple_tu {

void stubFunc(fakeit::Mock<SomeInterface>& mock)
{
fakeit::When(Method(mock, func)).AlwaysReturn(3);
fakeit::When(Method(mock, func)).Return(5);
}

void stubFunc2(fakeit::Mock<SomeInterface>& mock)
{
fakeit::When(Method(mock, func2)).AlwaysReturn("String");
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");
}

}
6 changes: 4 additions & 2 deletions tests/multiple_translation_units_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
namespace multiple_tu {

struct SomeInterface {
virtual int func() = 0;
virtual std::string func2() = 0;
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);

}
112 changes: 102 additions & 10 deletions tests/multiple_translation_units_stub_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ struct MultipleTranslationUnitsStub : tpunit::TestFixture {

MultipleTranslationUnitsStub() :
tpunit::TestFixture(
TEST(MultipleTranslationUnitsStub::NoCollidingIds)
TEST(MultipleTranslationUnitsStub::NoCollidingIdsBasics),
TEST(MultipleTranslationUnitsStub::NoCollidingIdsAlternateBasics),
TEST(MultipleTranslationUnitsStub::NoCollidingIdsMoreFunctionsMocked),
TEST(MultipleTranslationUnitsStub::NoCollidingIdsWhenOverridingMocks)
)
{
}

// TODO: more tests to be sure I didn't broke something by changing stuff:
// - alterning mocked func with when and func.
// - testing return value.
// - multiple mocks on same method.
// - etc ?

void NoCollidingIds() {
void NoCollidingIdsBasics()
{
Mock<SomeInterface> mock;

stubFunc(mock);
Expand All @@ -29,8 +27,102 @@ struct MultipleTranslationUnitsStub : tpunit::TestFixture {
SomeInterface &i = mock.get();

// Uncatchable write access violation if ids collide.
i.func();
i.func2();
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);

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 360c457

Please sign in to comment.