-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40d254a
commit 9db0f9d
Showing
4 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "googletest"] | ||
path = googletest | ||
url = https://github.com/google/googletest.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2024 Fedotov Kirill | ||
|
||
#include <stdexcept> | ||
#include <thread> // NOLINT [build/c++11] | ||
#include <chrono> // NOLINT [build/c++11] | ||
|
||
#include "TimedDoor.h" | ||
|
||
void Timer::sleep(int seconds) { | ||
std::this_thread::sleep_for(std::chrono::seconds(seconds)); | ||
} | ||
|
||
void Timer::tregister(int seconds, TimerClient* timerClient) { | ||
client = timerClient; | ||
sleep(seconds); | ||
client->Timeout(); | ||
client = nullptr; | ||
} | ||
|
||
DoorTimerAdapter::DoorTimerAdapter(TimedDoor& door) : door(door) {} | ||
|
||
void DoorTimerAdapter::Timeout() { | ||
if (door.isDoorOpened()) { | ||
throw std::runtime_error("Door has been opened for too long!"); | ||
} | ||
} | ||
|
||
TimedDoor::TimedDoor(int timeout) : iTimeout(timeout), isOpened(false) { | ||
adapter = new DoorTimerAdapter(*this); | ||
} | ||
|
||
bool TimedDoor::isDoorOpened() { | ||
return isOpened; | ||
} | ||
|
||
void TimedDoor::unlock() { | ||
if (isOpened) { | ||
throw std::logic_error("Door already unlocked"); | ||
} | ||
isOpened = true; | ||
} | ||
|
||
void TimedDoor::lock() { | ||
if (!isOpened) { | ||
throw std::logic_error("Door already locked"); | ||
} | ||
isOpened = false; | ||
} | ||
|
||
int TimedDoor::getTimeOut() const { | ||
return iTimeout; | ||
} | ||
|
||
void TimedDoor::throwState() { | ||
if (isOpened) { | ||
throw std::runtime_error("Door is opened!"); | ||
} else { | ||
throw std::runtime_error("Door is closed!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,83 @@ | ||
// Copyright 2021 GHA Test Team | ||
// Copyright 2024 Fedotov Kirill | ||
|
||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
#include <cstdint> | ||
#include <thread> // NOLINT [build/c++11] | ||
#include <chrono> // NOLINT [build/c++11] | ||
|
||
#include "TimedDoor.h" | ||
|
||
|
||
class MockTimerClient : public TimerClient { | ||
public: | ||
MOCK_METHOD(void, Timeout, (), (override)); | ||
}; | ||
|
||
class TimedDoorTest : public ::testing::Test { | ||
protected: | ||
TimedDoor door; | ||
MockTimerClient *mockClient; | ||
Timer timer; | ||
public: | ||
TimedDoorTest() : door(1), timer() {} | ||
protected: | ||
void SetUp() override { | ||
mockClient = new MockTimerClient(); | ||
} | ||
|
||
void TearDown() override { | ||
delete mockClient; | ||
} | ||
}; | ||
|
||
TEST(Fedotov_Test, lock) { | ||
door.unlock(); | ||
door.lock(); | ||
EXPECT_FALSE(door.isDoorOpened()); | ||
} | ||
|
||
TEST(Fedotov_Test, unlock) { | ||
door.unlock(); | ||
EXPECT_TRUE(door.isDoorOpened()); | ||
} | ||
|
||
TEST(Fedotov_Test, initialize_lock) { | ||
EXPECT_FALSE(door.isDoorOpened()); | ||
} | ||
|
||
TEST(Fedotov_Test, second_unlock) { | ||
door.unlock(); | ||
EXPECT_THROW(door.unlock(), std::logic_error); | ||
} | ||
|
||
TEST(Fedotov_Test, opened_exception_throw) { | ||
door.unlock(); | ||
EXPECT_THROW(door.throwState(), std::runtime_error); | ||
} | ||
|
||
TEST(Fedotov_Test, closed_exception_throw) { | ||
door.unlock(); | ||
door.lock(); | ||
EXPECT_THROW(door.throwState(), std::runtime_error); | ||
} | ||
|
||
TEST(Fedotov_Test, timeout_throws_on_unlocked) { | ||
DoorTimerAdapter adapter(door); | ||
door.unlock(); | ||
EXPECT_THROW(adapter.Timeout(), std::runtime_error); | ||
} | ||
|
||
TEST(Fedotov_Test, double_lock) { | ||
EXPECT_THROW(door.lock(), std::logic_error); | ||
} | ||
|
||
TEST(Fedotov_Test, timer_calls_timeout) { | ||
EXPECT_CALL(*mockClient, Timeout()).Times(1); | ||
timer.tregister(1, mockClient); | ||
} | ||
|
||
TEST(Fedotov_Test, timeout_no_throws_on_locked) { | ||
DoorTimerAdapter adapter(door); | ||
EXPECT_NO_THROW(adapter.Timeout()); | ||
} |