From 40d254a53bd56a5104205cae2316a5f024f12c55 Mon Sep 17 00:00:00 2001 From: anton Date: Mon, 18 Mar 2024 17:40:02 +0300 Subject: [PATCH 1/5] TimedDoor.h edited --- README.md | 4 ++-- include/TimedDoor.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f19f864..19caea0 100644 --- a/README.md +++ b/README.md @@ -44,13 +44,13 @@ class TimedDoor : public Door { private: DoorTimerAdapter * adapter; int iTimeout; - bool opened; + bool isOpened; public: explicit TimedDoor(int); bool isDoorOpened(); void unlock(); void lock(); - void DoorTimeOut(); + int getTimeOut(); void throwState(); }; diff --git a/include/TimedDoor.h b/include/TimedDoor.h index e1eade2..04429ad 100644 --- a/include/TimedDoor.h +++ b/include/TimedDoor.h @@ -32,13 +32,13 @@ class TimedDoor : public Door { private: DoorTimerAdapter * adapter; int iTimeout; - bool opened; + bool isOpened; public: explicit TimedDoor(int); bool isDoorOpened(); void unlock(); void lock(); - void DoorTimeOut(); + int getTimeOut() const; void throwState(); }; From 9db0f9df961ab688bdc2eaf1364a850371ec1783 Mon Sep 17 00:00:00 2001 From: KirFedotov Date: Sun, 26 May 2024 14:45:36 +0300 Subject: [PATCH 2/5] first commit --- .gitmodules | 3 ++ CMakeLists.txt | 8 +++++ src/TimedDoor.cpp | 60 +++++++++++++++++++++++++++++++++++ test/tests.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100644 src/TimedDoor.cpp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cca15d8 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "googletest"] + path = googletest + url = https://github.com/google/googletest.git \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 36758e8..65f04a3 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,5 +9,13 @@ set(tool_dest "bin") set(lib_dest "lib") set(include_dest "include/") +include(FetchContent) +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG release-1.10.0 +) +FetchContent_MakeAvailable(googletest) + add_subdirectory(src) add_subdirectory(test) diff --git a/src/TimedDoor.cpp b/src/TimedDoor.cpp new file mode 100644 index 0000000..edea7aa --- /dev/null +++ b/src/TimedDoor.cpp @@ -0,0 +1,60 @@ +// Copyright 2024 Fedotov Kirill + +#include +#include // NOLINT [build/c++11] +#include // 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!"); + } +} \ No newline at end of file diff --git a/test/tests.cpp b/test/tests.cpp index 605e570..348f5e7 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -1,6 +1,83 @@ -// Copyright 2021 GHA Test Team +// Copyright 2024 Fedotov Kirill #include #include #include +#include // NOLINT [build/c++11] +#include // 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()); +} From 3f590935a606a824e3960abd65729baf7c697b34 Mon Sep 17 00:00:00 2001 From: KirFedotov Date: Sun, 26 May 2024 15:00:28 +0300 Subject: [PATCH 3/5] fix --- src/TimedDoor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TimedDoor.cpp b/src/TimedDoor.cpp index edea7aa..8fd7af1 100644 --- a/src/TimedDoor.cpp +++ b/src/TimedDoor.cpp @@ -57,4 +57,4 @@ void TimedDoor::throwState() { } else { throw std::runtime_error("Door is closed!"); } -} \ No newline at end of file +} From 9c0633b249b28787c83ce63ebb07278a4213a33a Mon Sep 17 00:00:00 2001 From: KirFedotov Date: Sun, 26 May 2024 15:04:32 +0300 Subject: [PATCH 4/5] fix --- test/tests.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/tests.cpp b/test/tests.cpp index 348f5e7..05b5de5 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -31,53 +31,53 @@ class TimedDoorTest : public ::testing::Test { } }; -TEST(Fedotov_Test, lock) { +TEST_F(Fedotov_Test, lock) { door.unlock(); door.lock(); EXPECT_FALSE(door.isDoorOpened()); } -TEST(Fedotov_Test, unlock) { +TEST_F(Fedotov_Test, unlock) { door.unlock(); EXPECT_TRUE(door.isDoorOpened()); } -TEST(Fedotov_Test, initialize_lock) { +TEST_F(Fedotov_Test, initialize_lock) { EXPECT_FALSE(door.isDoorOpened()); } -TEST(Fedotov_Test, second_unlock) { +TEST_F(Fedotov_Test, second_unlock) { door.unlock(); EXPECT_THROW(door.unlock(), std::logic_error); } -TEST(Fedotov_Test, opened_exception_throw) { +TEST_F(Fedotov_Test, opened_exception_throw) { door.unlock(); EXPECT_THROW(door.throwState(), std::runtime_error); } -TEST(Fedotov_Test, closed_exception_throw) { +TEST_F(Fedotov_Test, closed_exception_throw) { door.unlock(); door.lock(); EXPECT_THROW(door.throwState(), std::runtime_error); } -TEST(Fedotov_Test, timeout_throws_on_unlocked) { +TEST_F(Fedotov_Test, timeout_throws_on_unlocked) { DoorTimerAdapter adapter(door); door.unlock(); EXPECT_THROW(adapter.Timeout(), std::runtime_error); } -TEST(Fedotov_Test, double_lock) { +TEST_F(Fedotov_Test, double_lock) { EXPECT_THROW(door.lock(), std::logic_error); } -TEST(Fedotov_Test, timer_calls_timeout) { +TEST_F(Fedotov_Test, timer_calls_timeout) { EXPECT_CALL(*mockClient, Timeout()).Times(1); timer.tregister(1, mockClient); } -TEST(Fedotov_Test, timeout_no_throws_on_locked) { +TEST_F(Fedotov_Test, timeout_no_throws_on_locked) { DoorTimerAdapter adapter(door); EXPECT_NO_THROW(adapter.Timeout()); } From 188327357ad5c973d55c3c47f3bbb7c8662bb136 Mon Sep 17 00:00:00 2001 From: KirFedotov Date: Sun, 26 May 2024 15:11:20 +0300 Subject: [PATCH 5/5] fix --- test/tests.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/test/tests.cpp b/test/tests.cpp index 05b5de5..4e6bc79 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -8,7 +8,6 @@ #include "TimedDoor.h" - class MockTimerClient : public TimerClient { public: MOCK_METHOD(void, Timeout, (), (override)); @@ -42,42 +41,42 @@ TEST_F(Fedotov_Test, unlock) { EXPECT_TRUE(door.isDoorOpened()); } -TEST_F(Fedotov_Test, initialize_lock) { - EXPECT_FALSE(door.isDoorOpened()); -} - -TEST_F(Fedotov_Test, second_unlock) { - door.unlock(); - EXPECT_THROW(door.unlock(), std::logic_error); -} - TEST_F(Fedotov_Test, opened_exception_throw) { door.unlock(); EXPECT_THROW(door.throwState(), std::runtime_error); } +TEST_F(Fedotov_Test, init_lock) { + EXPECT_FALSE(door.isDoorOpened()); +} + TEST_F(Fedotov_Test, closed_exception_throw) { door.unlock(); door.lock(); EXPECT_THROW(door.throwState(), std::runtime_error); } -TEST_F(Fedotov_Test, timeout_throws_on_unlocked) { - DoorTimerAdapter adapter(door); +TEST_F(Fedotov_Test, double_unlock) { door.unlock(); - EXPECT_THROW(adapter.Timeout(), std::runtime_error); + EXPECT_THROW(door.unlock(), std::logic_error); } TEST_F(Fedotov_Test, double_lock) { EXPECT_THROW(door.lock(), std::logic_error); } -TEST_F(Fedotov_Test, timer_calls_timeout) { - EXPECT_CALL(*mockClient, Timeout()).Times(1); - timer.tregister(1, mockClient); +TEST_F(Fedotov_Test, timeout_throws_on_unlocked) { + DoorTimerAdapter adapter(door); + door.unlock(); + EXPECT_THROW(adapter.Timeout(), std::runtime_error); } TEST_F(Fedotov_Test, timeout_no_throws_on_locked) { DoorTimerAdapter adapter(door); EXPECT_NO_THROW(adapter.Timeout()); } + +TEST_F(Fedotov_Test, timer_calls_timeout) { + EXPECT_CALL(*mockClient, Timeout()).Times(1); + timer.tregister(1, mockClient); +}