Skip to content

Commit

Permalink
Change task 3
Browse files Browse the repository at this point in the history
  • Loading branch information
KachalovM committed Sep 28, 2024
1 parent b8b13c3 commit a38757c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
4 changes: 1 addition & 3 deletions src/TimedDoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ DoorTimerAdapter::DoorTimerAdapter(TimedDoor& door) : door(door) {}

void DoorTimerAdapter::Timeout() {
if (door.isDoorOpened()) {
door.throwState();
throw std::runtime_error("Door is not opened after timeout");
}
}

Expand All @@ -24,8 +24,6 @@ bool TimedDoor::isDoorOpened() {

void TimedDoor::unlock() {
isOpened = true;
Timer timer;
timer.tregister(iTimeout, adapter);
}

void TimedDoor::lock() {
Expand Down
84 changes: 54 additions & 30 deletions test/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,80 +7,104 @@
#include <chrono> // NOLINT [build/c++11]
#include "TimedDoor.h"

class TimedDoorTest : public ::testing::Test {
protected:
MockTimedDoor* timedDoor;
class MockTimerClient : public TimerClient {
public:
MOCK_METHOD(void, Timeout, (), (override));
};

class MockDoor : public Door {
public:
MOCK_METHOD(void, lock, (), (override));
MOCK_METHOD(void, unlock, (), (override));
MOCK_METHOD(bool, isDoorOpened, (), (override));
MOCK_METHOD(void, throwState, (), ());
};

class TimedDoorTest : public testing::Test {
protected:
TimedDoor* timedDoor;
Timer* timer;

void SetUp() override {
timedDoor = new MockTimedDoor(2);
timedDoor = new TimedDoor(2);
timer = new Timer();
}

void TearDown() override {
delete timedDoor;
delete timer;
}
};

TEST_F(TimedDoorTest, DoorTimeoutAfterUnlock) {
TEST_F(TimedDoorTest, UnlockingDoorKeepsItOpenAfterTimeout) {
timedDoor->unlock();
std::this_thread::sleep_for(std::chrono::seconds(timedDoor->getTimeOut() + 1));
EXPECT_FALSE(timedDoor->isDoorOpened());
std::this_thread::sleep_for(std::chrono::seconds(
timedDoor->getTimeOut() + 5));
EXPECT_TRUE(timedDoor->isDoorOpened());
}

TEST_F(TimedDoorTest, DoorTimeoutAfterLock) {
TEST_F(TimedDoorTest, LockingDoorClosesItAfterTimeout) {
timedDoor->lock();
std::this_thread::sleep_for(std::chrono::seconds(timedDoor->getTimeOut() + 1));
std::this_thread::sleep_for(std::chrono::seconds(
timedDoor->getTimeOut() + 5));
EXPECT_FALSE(timedDoor->isDoorOpened());
}

TEST_F(TimedDoorTest, DoorTimeoutAfterUnlockAndLock) {
TEST_F(TimedDoorTest, LockingAfterUnlockingClosesTheDoor) {
timedDoor->unlock();
std::this_thread::sleep_for(std::chrono::seconds(timedDoor->getTimeOut() - 1));
std::this_thread::sleep_for(std::chrono::seconds(
timedDoor->getTimeOut() - 1));
timedDoor->lock();
std::this_thread::sleep_for(std::chrono::seconds(timedDoor->getTimeOut() + 1));
std::this_thread::sleep_for(std::chrono::seconds(
timedDoor->getTimeOut() + 1));
EXPECT_FALSE(timedDoor->isDoorOpened());
}

TEST_F(TimedDoorTest, DoorTimeoutAfterMultipleUnlocks) {
for (int i = 0; i < 3; ++i) {
timedDoor->unlock();
std::this_thread::sleep_for(std::chrono::seconds(timedDoor->getTimeOut() + 1));
}
TEST_F(TimedDoorTest, MultipleUnlocksKeepsDoorOpenAfterTimeout) {
timedDoor->unlock();
std::this_thread::sleep_for(std::chrono::seconds(
timedDoor->getTimeOut() + 3));
timedDoor->unlock();
std::this_thread::sleep_for(std::chrono::seconds(
timedDoor->getTimeOut() + 3));
timedDoor->unlock();
std::this_thread::sleep_for(std::chrono::seconds(
timedDoor->getTimeOut() + 3));
EXPECT_TRUE(timedDoor->isDoorOpened());
}

TEST_F(TimedDoorTest, DoorCanBeOpenedAfterUnlock) {
TEST_F(TimedDoorTest, DoorOpensWhenUnlocked) {
timedDoor->unlock();
EXPECT_TRUE(timedDoor->isDoorOpened());
}

TEST_F(TimedDoorTest, DoorCanBeClosedAfterLock) {
TEST_F(TimedDoorTest, DoorClosesWhenLocked) {
timedDoor->lock();
EXPECT_FALSE(timedDoor->isDoorOpened());
}

TEST_F(TimedDoorTest, DoorCanBeOpenedAfterMultipleUnlocks) {
for (int i = 0; i < 2; ++i) {
timedDoor->unlock();
}
TEST_F(TimedDoorTest, MultipleUnlocksKeepsDoorOpen) {
timedDoor->unlock();
timedDoor->unlock();
EXPECT_TRUE(timedDoor->isDoorOpened());
}

TEST_F(TimedDoorTest, DoorCanBeClosedAfterMultipleLocks) {
TEST_F(TimedDoorTest, MultipleLocksKeepsDoorClosed) {
timedDoor->unlock();
for (int i = 0; i < 2; ++i) {
timedDoor->lock();
}
timedDoor->lock();
timedDoor->lock();
EXPECT_FALSE(timedDoor->isDoorOpened());
}

TEST_F(TimedDoorTest, DoorCanBeOpenedAndClosed) {
TEST_F(TimedDoorTest, OpenAndCloseDoorFunctionality) {
timedDoor->unlock();
EXPECT_TRUE(timedDoor->isDoorOpened());
timedDoor->lock();
EXPECT_FALSE(timedDoor->isDoorOpened());
timedDoor->unlock();
EXPECT_TRUE(timedDoor->isDoorOpened());
}

TEST_F(TimedDoorTest, GetTimeoutReturnsCorrectValue) {
TEST_F(TimedDoorTest, CorrectTimeoutValueReturned) {
int expectedTimeout = 2;
EXPECT_EQ(timedDoor->getTimeOut(), expectedTimeout);
}

0 comments on commit a38757c

Please sign in to comment.