-
Notifications
You must be signed in to change notification settings - Fork 15
Mb add mocks for unit testing #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ramceb
wants to merge
1
commit into
main
Choose a base branch
from
mb_add_mocks_for_unit_testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+327
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
78 changes: 78 additions & 0 deletions
78
src/lifecycle_client_lib/test/ut/mocks/applicationcontextmock.cpp
This file contains hidden or 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,78 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2025 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| #include "src/lifecycle_client_lib/test/ut/mocks/applicationcontextmock.h" | ||
| #include "src/lifecycle_client_lib/include/applicationcontext.h" | ||
|
|
||
| #include <functional> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| auto& GetConstructorCallback() noexcept | ||
| { | ||
| static std::function<void(const std::int32_t argc, const score::StringLiteral argv[])> constructor_callback{}; | ||
| return constructor_callback; | ||
| } | ||
|
|
||
| auto& GetGetArgumentsCallback() noexcept | ||
| { | ||
| static std::function<const std::vector<std::string>&()> get_arguments_callback{}; | ||
| return get_arguments_callback; | ||
| } | ||
|
|
||
| auto& GetGetArgumentCallback() noexcept | ||
| { | ||
| static std::function<const std::string(const std::string_view)> get_argument_callback{}; | ||
| return get_argument_callback; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| score::mw::lifecycle::ApplicationContextMock::ApplicationContextMock() | ||
| { | ||
| GetConstructorCallback() = [this](const std::int32_t argc, const score::StringLiteral argv[]) { | ||
| ctor(argc, argv); | ||
| }; | ||
| GetGetArgumentsCallback() = [this]() -> decltype(auto) { | ||
| return get_arguments(); | ||
| }; | ||
| GetGetArgumentCallback() = [this](const std::string_view flag) -> decltype(auto) { | ||
| return get_argument(flag); | ||
| }; | ||
| } | ||
|
|
||
| score::mw::lifecycle::ApplicationContextMock::~ApplicationContextMock() | ||
| { | ||
| GetConstructorCallback() = nullptr; | ||
| GetGetArgumentsCallback() = nullptr; | ||
| GetGetArgumentCallback() = nullptr; | ||
| } | ||
|
|
||
| score::mw::lifecycle::ApplicationContext::ApplicationContext(const std::int32_t argc, const score::StringLiteral argv[]) | ||
| { | ||
| auto& constructor_callback = GetConstructorCallback(); | ||
| constructor_callback(argc, argv); | ||
| } | ||
|
|
||
| const std::vector<std::string>& score::mw::lifecycle::ApplicationContext::get_arguments() const noexcept | ||
| { | ||
| auto& get_arguments_callback = GetGetArgumentsCallback(); | ||
| return get_arguments_callback(); | ||
| } | ||
|
|
||
| std::string score::mw::lifecycle::ApplicationContext::get_argument(const std::string_view flag) const noexcept | ||
| { | ||
| auto& get_argument_callback = GetGetArgumentCallback(); | ||
| return get_argument_callback(flag); | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/lifecycle_client_lib/test/ut/mocks/applicationcontextmock.h
This file contains hidden or 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,47 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2025 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| #ifndef SCORE_MW_LIFECYCLE_MOCKS_APPLICATIONCONTEXTMOCK_H_ | ||
| #define SCORE_MW_LIFECYCLE_MOCKS_APPLICATIONCONTEXTMOCK_H_ | ||
|
|
||
| #include "score/memory/string_literal.h" | ||
|
|
||
| #include <gmock/gmock.h> | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| namespace score | ||
| { | ||
| namespace mw | ||
| { | ||
| namespace lifecycle | ||
| { | ||
|
|
||
| class ApplicationContextMock | ||
| { | ||
| public: | ||
| ApplicationContextMock(); | ||
| ~ApplicationContextMock(); | ||
|
|
||
| MOCK_METHOD(const std::vector<std::string>&, get_arguments, (), ()); | ||
| MOCK_METHOD(void, ctor, (const std::int32_t argc, const score::StringLiteral argv[]), ()); | ||
| MOCK_METHOD(std::string, get_argument, (const std::string_view flag), ()); | ||
| }; | ||
|
|
||
| } // namespace lifecycle | ||
| } // namespace mw | ||
| } // namespace score | ||
|
|
||
| #endif // SCORE_MW_LIFECYCLE_MOCKS_APPLICATIONCONTEXTMOCK_H_ |
96 changes: 96 additions & 0 deletions
96
src/lifecycle_client_lib/test/ut/mocks/lifecyclemanagermock.cpp
This file contains hidden or 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,96 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2025 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| #include "src/lifecycle_client_lib/test/ut/mocks/lifecyclemanagermock.h" | ||
| #include "src/lifecycle_client_lib/include/lifecyclemanager.h" | ||
|
|
||
| #include <functional> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| auto& GetConstructorCallback() noexcept | ||
| { | ||
| static std::function<void()> constructor_callback{}; | ||
| return constructor_callback; | ||
| } | ||
|
|
||
| auto& GetDestructorCallback() noexcept | ||
| { | ||
| static std::function<void()> destructor_callback{}; | ||
| return destructor_callback; | ||
| } | ||
|
|
||
| auto& GetRunCallback() noexcept | ||
| { | ||
| static std::function<std::int32_t(score::mw::lifecycle::Application & app, | ||
| const score::mw::lifecycle::ApplicationContext& context)> | ||
| run_callback; | ||
| return run_callback; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| score::mw::lifecycle::LifeCycleManagerMock::LifeCycleManagerMock() | ||
| { | ||
| GetConstructorCallback() = [this] { | ||
| ctor(); | ||
| }; | ||
| GetDestructorCallback() = [this] { | ||
| dtor(); | ||
| }; | ||
| ResetCallbackForRunMethod(); | ||
| } | ||
|
|
||
| score::mw::lifecycle::LifeCycleManagerMock::~LifeCycleManagerMock() | ||
| { | ||
| GetConstructorCallback() = nullptr; | ||
| GetDestructorCallback() = nullptr; | ||
| GetRunCallback() = nullptr; | ||
| } | ||
|
|
||
| void score::mw::lifecycle::LifeCycleManagerMock::SetCallbackForRunMethod( | ||
| std::function<std::int32_t(score::mw::lifecycle::Application& app, const score::mw::lifecycle::ApplicationContext& context)> callback) | ||
| { | ||
| GetRunCallback() = std::move(callback); | ||
| } | ||
|
|
||
| void score::mw::lifecycle::LifeCycleManagerMock::ResetCallbackForRunMethod() | ||
| { | ||
| GetRunCallback() = [this](score::mw::lifecycle::Application& app, | ||
| const score::mw::lifecycle::ApplicationContext& context) { | ||
| return run(app, context); | ||
| }; | ||
| } | ||
|
|
||
| score::mw::lifecycle::LifeCycleManager::LifeCycleManager(std::unique_ptr<score::os::Signal>) noexcept | ||
| { | ||
| auto& constructor_callback = GetConstructorCallback(); | ||
| constructor_callback(); | ||
| } | ||
|
|
||
| score::mw::lifecycle::LifeCycleManager::~LifeCycleManager() noexcept | ||
| { | ||
| auto& destructor_callback = GetDestructorCallback(); | ||
| destructor_callback(); | ||
| } | ||
|
|
||
| std::int32_t score::mw::lifecycle::LifeCycleManager::run(score::mw::lifecycle::Application& app, | ||
| const score::mw::lifecycle::ApplicationContext& context) | ||
| { | ||
| report_running(); | ||
| auto& run_callback = GetRunCallback(); | ||
| const auto result = run_callback(app, context); | ||
| report_shutdown(); | ||
| return result; | ||
| } |
52 changes: 52 additions & 0 deletions
52
src/lifecycle_client_lib/test/ut/mocks/lifecyclemanagermock.h
This file contains hidden or 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,52 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2025 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| #ifndef SCORE_MW_LIFECYCLE_MOCKS_LIFECYCLEMANAGERMOCK_H_ | ||
| #define SCORE_MW_LIFECYCLE_MOCKS_LIFECYCLEMANAGERMOCK_H_ | ||
|
|
||
| #include "score/os/utils/mocklib/signalmock.h" | ||
| #include "score/os/utils/signal.h" | ||
| #include "src/lifecycle_client_lib/include/application.h" | ||
| #include "src/lifecycle_client_lib/test/ut/mocks/applicationcontextmock.h" | ||
|
|
||
| #include <gmock/gmock.h> | ||
|
|
||
| #include <functional> | ||
|
|
||
| namespace score | ||
| { | ||
| namespace mw | ||
| { | ||
| namespace lifecycle | ||
| { | ||
|
|
||
| class LifeCycleManagerMock | ||
| { | ||
| public: | ||
| LifeCycleManagerMock(); | ||
| ~LifeCycleManagerMock(); | ||
|
|
||
| void SetCallbackForRunMethod( | ||
| std::function<std::int32_t(score::mw::lifecycle::Application& app, const score::mw::lifecycle::ApplicationContext& context)> callback); | ||
| void ResetCallbackForRunMethod(); | ||
|
|
||
| MOCK_METHOD(std::int32_t, run, (score::mw::lifecycle::Application & app, const score::mw::lifecycle::ApplicationContext& context), ()); | ||
| MOCK_METHOD(void, ctor, (), ()); | ||
| MOCK_METHOD(void, dtor, (), ()); | ||
| }; | ||
|
|
||
| } // namespace lifecycle | ||
| } // namespace mw | ||
| } // namespace score | ||
|
|
||
| #endif // SCORE_MW_LIFECYCLE_MOCKS_LIFECYCLEMANAGERMOCK_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we need three targets instead one ?