Skip to content

Commit dbc6a81

Browse files
authored
Merge pull request #78 from eclipse-score/mb_add_mocks_for_unit_testing
Mb add mocks for unit testing
2 parents 562630e + d03a115 commit dbc6a81

5 files changed

Lines changed: 327 additions & 0 deletions

File tree

src/lifecycle_client_lib/BUILD

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,60 @@ cc_library(
154154
]
155155
]
156156

157+
cc_library(
158+
name = "applicationcontext_mock",
159+
testonly = True,
160+
srcs = [
161+
"test/ut/mocks/applicationcontextmock.cpp",
162+
],
163+
hdrs = [
164+
"include/applicationcontext.h",
165+
"test/ut/mocks/applicationcontextmock.h",
166+
],
167+
features = COMPILER_WARNING_FEATURES,
168+
deps = [
169+
"@googletest//:gtest",
170+
"@score_baselibs//score/memory:string_literal",
171+
"@score_baselibs//score/os/utils:signal",
172+
],
173+
)
174+
175+
cc_library(
176+
name = "application_mock",
177+
testonly = True,
178+
srcs = [
179+
"src/application.cpp",
180+
],
181+
hdrs = [
182+
"include/application.h",
183+
],
184+
features = COMPILER_WARNING_FEATURES,
185+
deps = [
186+
":applicationcontext_mock",
187+
"@score_baselibs//score/os/utils:signal",
188+
],
189+
)
190+
191+
cc_library(
192+
name = "lifecycle_mock",
193+
testonly = True,
194+
srcs = [
195+
"test/ut/mocks/lifecyclemanagermock.cpp",
196+
],
197+
hdrs = [
198+
"include/lifecyclemanager.h",
199+
"test/ut/mocks/lifecyclemanagermock.h",
200+
],
201+
features = COMPILER_WARNING_FEATURES,
202+
deps = [
203+
":application_mock",
204+
# "//platform/aas/lib/os/utils/mocklib:signal_mock",
205+
"@score_baselibs//score/os/utils/mocklib:signal_mock",
206+
"@googletest//:gtest",
207+
"@score_baselibs//score/os/utils:signal",
208+
],
209+
)
210+
157211
filegroup(
158212
name = "all_sources",
159213
srcs = glob([
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
#include "src/lifecycle_client_lib/test/ut/mocks/applicationcontextmock.h"
15+
#include "src/lifecycle_client_lib/include/applicationcontext.h"
16+
17+
#include <functional>
18+
19+
namespace
20+
{
21+
22+
auto& GetConstructorCallback() noexcept
23+
{
24+
static std::function<void(const std::int32_t argc, const score::StringLiteral argv[])> constructor_callback{};
25+
return constructor_callback;
26+
}
27+
28+
auto& GetGetArgumentsCallback() noexcept
29+
{
30+
static std::function<const std::vector<std::string>&()> get_arguments_callback{};
31+
return get_arguments_callback;
32+
}
33+
34+
auto& GetGetArgumentCallback() noexcept
35+
{
36+
static std::function<const std::string(const std::string_view)> get_argument_callback{};
37+
return get_argument_callback;
38+
}
39+
40+
} // namespace
41+
42+
score::mw::lifecycle::ApplicationContextMock::ApplicationContextMock()
43+
{
44+
GetConstructorCallback() = [this](const std::int32_t argc, const score::StringLiteral argv[]) {
45+
ctor(argc, argv);
46+
};
47+
GetGetArgumentsCallback() = [this]() -> decltype(auto) {
48+
return get_arguments();
49+
};
50+
GetGetArgumentCallback() = [this](const std::string_view flag) -> decltype(auto) {
51+
return get_argument(flag);
52+
};
53+
}
54+
55+
score::mw::lifecycle::ApplicationContextMock::~ApplicationContextMock()
56+
{
57+
GetConstructorCallback() = nullptr;
58+
GetGetArgumentsCallback() = nullptr;
59+
GetGetArgumentCallback() = nullptr;
60+
}
61+
62+
score::mw::lifecycle::ApplicationContext::ApplicationContext(const std::int32_t argc, const score::StringLiteral argv[])
63+
{
64+
auto& constructor_callback = GetConstructorCallback();
65+
constructor_callback(argc, argv);
66+
}
67+
68+
const std::vector<std::string>& score::mw::lifecycle::ApplicationContext::get_arguments() const noexcept
69+
{
70+
auto& get_arguments_callback = GetGetArgumentsCallback();
71+
return get_arguments_callback();
72+
}
73+
74+
std::string score::mw::lifecycle::ApplicationContext::get_argument(const std::string_view flag) const noexcept
75+
{
76+
auto& get_argument_callback = GetGetArgumentCallback();
77+
return get_argument_callback(flag);
78+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
#ifndef SCORE_MW_LIFECYCLE_MOCKS_APPLICATIONCONTEXTMOCK_H_
15+
#define SCORE_MW_LIFECYCLE_MOCKS_APPLICATIONCONTEXTMOCK_H_
16+
17+
#include "score/memory/string_literal.h"
18+
19+
#include <gmock/gmock.h>
20+
21+
#include <string>
22+
#include <string_view>
23+
#include <vector>
24+
25+
namespace score
26+
{
27+
namespace mw
28+
{
29+
namespace lifecycle
30+
{
31+
32+
class ApplicationContextMock
33+
{
34+
public:
35+
ApplicationContextMock();
36+
~ApplicationContextMock();
37+
38+
MOCK_METHOD(const std::vector<std::string>&, get_arguments, (), ());
39+
MOCK_METHOD(void, ctor, (const std::int32_t argc, const score::StringLiteral argv[]), ());
40+
MOCK_METHOD(std::string, get_argument, (const std::string_view flag), ());
41+
};
42+
43+
} // namespace lifecycle
44+
} // namespace mw
45+
} // namespace score
46+
47+
#endif // SCORE_MW_LIFECYCLE_MOCKS_APPLICATIONCONTEXTMOCK_H_
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
#include "src/lifecycle_client_lib/test/ut/mocks/lifecyclemanagermock.h"
15+
#include "src/lifecycle_client_lib/include/lifecyclemanager.h"
16+
17+
#include <functional>
18+
19+
namespace
20+
{
21+
22+
auto& GetConstructorCallback() noexcept
23+
{
24+
static std::function<void()> constructor_callback{};
25+
return constructor_callback;
26+
}
27+
28+
auto& GetDestructorCallback() noexcept
29+
{
30+
static std::function<void()> destructor_callback{};
31+
return destructor_callback;
32+
}
33+
34+
auto& GetRunCallback() noexcept
35+
{
36+
static std::function<std::int32_t(score::mw::lifecycle::Application & app,
37+
const score::mw::lifecycle::ApplicationContext& context)>
38+
run_callback;
39+
return run_callback;
40+
}
41+
42+
} // namespace
43+
44+
score::mw::lifecycle::LifeCycleManagerMock::LifeCycleManagerMock()
45+
{
46+
GetConstructorCallback() = [this] {
47+
ctor();
48+
};
49+
GetDestructorCallback() = [this] {
50+
dtor();
51+
};
52+
ResetCallbackForRunMethod();
53+
}
54+
55+
score::mw::lifecycle::LifeCycleManagerMock::~LifeCycleManagerMock()
56+
{
57+
GetConstructorCallback() = nullptr;
58+
GetDestructorCallback() = nullptr;
59+
GetRunCallback() = nullptr;
60+
}
61+
62+
void score::mw::lifecycle::LifeCycleManagerMock::SetCallbackForRunMethod(
63+
std::function<std::int32_t(score::mw::lifecycle::Application& app, const score::mw::lifecycle::ApplicationContext& context)> callback)
64+
{
65+
GetRunCallback() = std::move(callback);
66+
}
67+
68+
void score::mw::lifecycle::LifeCycleManagerMock::ResetCallbackForRunMethod()
69+
{
70+
GetRunCallback() = [this](score::mw::lifecycle::Application& app,
71+
const score::mw::lifecycle::ApplicationContext& context) {
72+
return run(app, context);
73+
};
74+
}
75+
76+
score::mw::lifecycle::LifeCycleManager::LifeCycleManager(std::unique_ptr<score::os::Signal>) noexcept
77+
{
78+
auto& constructor_callback = GetConstructorCallback();
79+
constructor_callback();
80+
}
81+
82+
score::mw::lifecycle::LifeCycleManager::~LifeCycleManager() noexcept
83+
{
84+
auto& destructor_callback = GetDestructorCallback();
85+
destructor_callback();
86+
}
87+
88+
std::int32_t score::mw::lifecycle::LifeCycleManager::run(score::mw::lifecycle::Application& app,
89+
const score::mw::lifecycle::ApplicationContext& context)
90+
{
91+
report_running();
92+
auto& run_callback = GetRunCallback();
93+
const auto result = run_callback(app, context);
94+
report_shutdown();
95+
return result;
96+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* SPDX-License-Identifier: Apache-2.0
12+
********************************************************************************/
13+
14+
#ifndef SCORE_MW_LIFECYCLE_MOCKS_LIFECYCLEMANAGERMOCK_H_
15+
#define SCORE_MW_LIFECYCLE_MOCKS_LIFECYCLEMANAGERMOCK_H_
16+
17+
#include "score/os/utils/mocklib/signalmock.h"
18+
#include "score/os/utils/signal.h"
19+
#include "src/lifecycle_client_lib/include/application.h"
20+
#include "src/lifecycle_client_lib/test/ut/mocks/applicationcontextmock.h"
21+
22+
#include <gmock/gmock.h>
23+
24+
#include <functional>
25+
26+
namespace score
27+
{
28+
namespace mw
29+
{
30+
namespace lifecycle
31+
{
32+
33+
class LifeCycleManagerMock
34+
{
35+
public:
36+
LifeCycleManagerMock();
37+
~LifeCycleManagerMock();
38+
39+
void SetCallbackForRunMethod(
40+
std::function<std::int32_t(score::mw::lifecycle::Application& app, const score::mw::lifecycle::ApplicationContext& context)> callback);
41+
void ResetCallbackForRunMethod();
42+
43+
MOCK_METHOD(std::int32_t, run, (score::mw::lifecycle::Application & app, const score::mw::lifecycle::ApplicationContext& context), ());
44+
MOCK_METHOD(void, ctor, (), ());
45+
MOCK_METHOD(void, dtor, (), ());
46+
};
47+
48+
} // namespace lifecycle
49+
} // namespace mw
50+
} // namespace score
51+
52+
#endif // SCORE_MW_LIFECYCLE_MOCKS_LIFECYCLEMANAGERMOCK_H_

0 commit comments

Comments
 (0)