Skip to content

Commit 492764e

Browse files
silabs-borislrzr
authored andcommitted
uic: zpc: UIC-3488: Attribute store test are now in their own class
This will allow other component than command class to still have access to the attribute test helper functions. We made a wrapper for zpc_attribute_store_test_helper to be able to use it in our new test implementation without the need to relink the previous ones. (cherry picked from commit 963e10e49e78e249ee799f76694d4f2c9a937a45) Forwarded: #11 Signed-off-by: Philippe Coval <philippe.coval@silabs.com>
1 parent 7edb2d6 commit 492764e

File tree

7 files changed

+300
-144
lines changed

7 files changed

+300
-144
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
add_library(zpc_attribute_store_test_helper zpc_attribute_store_test_helper.cpp)
1+
add_library(zpc_attribute_store_test_helper
2+
zpc_attribute_store_test_helper.cpp
3+
)
24
target_include_directories(zpc_attribute_store_test_helper PUBLIC .)
35
target_link_libraries(zpc_attribute_store_test_helper
4-
PUBLIC zpc_attribute_store )
6+
PUBLIC zpc_attribute_store)
7+
8+
add_subdirectory(cpp_wrapper)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
add_library(zpc_attribute_store_test_helper_cpp
2+
zpc_attribute_store_test_helper_cpp.cpp
3+
)
4+
target_include_directories(zpc_attribute_store_test_helper_cpp PUBLIC .)
5+
target_link_libraries(zpc_attribute_store_test_helper_cpp
6+
PUBLIC zpc_attribute_store_test_helper unity2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/******************************************************************************
2+
* # License
3+
* <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
4+
******************************************************************************
5+
* The licensor of this software is Silicon Laboratories Inc. Your use of this
6+
* software is governed by the terms of Silicon Labs Master Software License
7+
* Agreement (MSLA) available at
8+
* www.silabs.com/about-us/legal/master-software-license-agreement. This
9+
* software is distributed to you in Source Code format and is governed by the
10+
* sections of the MSLA applicable to Source Code.
11+
*
12+
*****************************************************************************/
13+
// Helper class
14+
#include "zpc_attribute_store_test_helper_cpp.hpp"
15+
#include "attribute_store_defined_attribute_types.h"
16+
17+
#include "sl_log.h"
18+
19+
// C++ includes
20+
#ifdef __cplusplus
21+
#include <string>
22+
#include <boost/format.hpp>
23+
#endif
24+
25+
namespace zpc_attribute_store_test_helper
26+
{
27+
28+
attribute_store::attribute cpp_endpoint_id_node = ATTRIBUTE_STORE_INVALID_NODE; //NOSONAR - false positive
29+
30+
void zpc_attribute_store_test_helper_init() {
31+
// Create base structure
32+
zpc_attribute_store_test_helper_create_network();
33+
// Cpp wrapper for endpoint id node
34+
cpp_endpoint_id_node = endpoint_id_node;
35+
}
36+
37+
void helper_test_node_existence(attribute_store::attribute attribute,
38+
bool should_exists,
39+
const std::string &expected_attribute_name,
40+
const std::string &expected_parent_name)
41+
{
42+
TEST_ASSERT_EQUAL_MESSAGE(
43+
should_exists,
44+
attribute.is_valid(),
45+
(boost::format("Attribute '%1%' should %2% exists under '%3%'")
46+
% expected_attribute_name % (should_exists ? "" : "NOT")
47+
% expected_parent_name)
48+
.str()
49+
.c_str());
50+
}
51+
52+
attribute_store::attribute
53+
helper_test_and_get_node(attribute_store_type_t node_type,
54+
attribute_store::attribute parent)
55+
{
56+
auto attribute = parent.child_by_type(node_type);
57+
58+
helper_test_node_existence(attribute,
59+
true,
60+
attribute_store_get_type_name(node_type),
61+
parent.name());
62+
return attribute;
63+
}
64+
65+
void helper_test_node_exists(attribute_store_type_t node_type,
66+
attribute_store::attribute parent)
67+
{
68+
helper_test_node_existence(parent.child_by_type(node_type),
69+
true,
70+
attribute_store_get_type_name(node_type),
71+
parent.name());
72+
}
73+
void helper_test_node_does_not_exists(attribute_store_type_t node_type,
74+
attribute_store::attribute parent)
75+
{
76+
helper_test_node_existence(parent.child_by_type(node_type),
77+
false,
78+
attribute_store_get_type_name(node_type),
79+
parent.name());
80+
}
81+
82+
83+
////////////////////////////////////////////////////////////////////////////////////
84+
// Version helpers
85+
////////////////////////////////////////////////////////////////////////////////////
86+
void helper_set_command_class_version(zwave_command_class_t command_class_id,
87+
const zwave_cc_version_t &version,
88+
attribute_store::attribute parent)
89+
{
90+
parent.add_node(ZWAVE_CC_VERSION_ATTRIBUTE(command_class_id))
91+
.set_reported(version);
92+
}
93+
94+
zwave_cc_version_t
95+
helper_get_command_class_version(zwave_command_class_t command_class_id)
96+
{
97+
try {
98+
return cpp_endpoint_id_node
99+
.child_by_type(ZWAVE_CC_VERSION_ATTRIBUTE(command_class_id))
100+
.reported<zwave_cc_version_t>();
101+
} catch (const std::exception &e) {
102+
sl_log_error("zpc_attribute_store_test_helper_cpp",
103+
"Command class version not found for %d",
104+
command_class_id);
105+
return 0;
106+
}
107+
}
108+
109+
} // namespace zpc_attribute_store_test_helper
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/******************************************************************************
2+
* # License
3+
* <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
4+
******************************************************************************
5+
* The licensor of this software is Silicon Laboratories Inc. Your use of this
6+
* software is governed by the terms of Silicon Labs Master Software License
7+
* Agreement (MSLA) available at
8+
* www.silabs.com/about-us/legal/master-software-license-agreement. This
9+
* software is distributed to you in Source Code format and is governed by the
10+
* sections of the MSLA applicable to Source Code.
11+
*
12+
*****************************************************************************/
13+
14+
#ifndef ZPC_ATTRIBUTE_STORE_TEST_HELPER_CPP_HPP
15+
#define ZPC_ATTRIBUTE_STORE_TEST_HELPER_CPP_HPP
16+
17+
// Unify cpp
18+
#include "attribute.hpp"
19+
20+
extern "C" {
21+
// Z-Wave types
22+
#include "zwave_generic_types.h"
23+
#include "zwave_command_class_version_types.h"
24+
#include "zpc_attribute_store_test_helper.h"
25+
26+
// Test framework
27+
#include "unity.h"
28+
29+
/**
30+
* @brief Helper namespace for the ZPC attribute store tests
31+
*
32+
* CPP wrapper of the ZPC attribute store test helper.
33+
* This is done in a separate file to avoid breaking linkage to existing tests.
34+
*/
35+
namespace zpc_attribute_store_test_helper
36+
{
37+
38+
////////////////////////////////////////////////////////////////////////////////////
39+
// Global variables
40+
// Must be declared as "extern" and defined in the cpp to avoid multiple definition
41+
// More information : https://stackoverflow.com/questions/11478152/how-to-work-with-variable-in-namespace
42+
////////////////////////////////////////////////////////////////////////////////////
43+
// Endpoint id node wrapper
44+
extern attribute_store::attribute cpp_endpoint_id_node; //NOSONAR - false positive
45+
46+
47+
/**
48+
* @brief Initialize the test helper
49+
*
50+
* Initialize the Z-Wave network and create the base structure for the tests.
51+
*/
52+
void zpc_attribute_store_test_helper_init();
53+
54+
////////////////////////////////////////////////////////////////////////////////////
55+
// Version
56+
////////////////////////////////////////////////////////////////////////////////////
57+
/**
58+
* @brief Set version for current class
59+
*
60+
* @param command_class_id Command class id to set version
61+
* @param version Command class version to set
62+
* @param parent Parent node of the node to get (default to current endpoint)
63+
*/
64+
void helper_set_command_class_version(zwave_command_class_t command_class_id,
65+
const zwave_cc_version_t &version,
66+
attribute_store::attribute parent
67+
= cpp_endpoint_id_node);
68+
69+
/**
70+
* @brief Get version for current class
71+
*
72+
* @param command_class_id Command class id to get version
73+
*
74+
* @return Command class version
75+
*/
76+
zwave_cc_version_t
77+
helper_get_command_class_version(zwave_command_class_t command_class_id);
78+
79+
////////////////////////////////////////////////////////////////////////////////////
80+
// Generic Node/Attribute Test Helpers
81+
////////////////////////////////////////////////////////////////////////////////////
82+
/**
83+
* @brief Get a node and check that it exists
84+
*
85+
* @note Test will fail if node doesn't exists
86+
*
87+
* @param node_type Node type to get
88+
* @param parent Parent node of the node to get (default to current endpoint)
89+
*
90+
* @return attribute_store::attribute Node that was found (garmented to exists)
91+
*/
92+
attribute_store::attribute
93+
helper_test_and_get_node(attribute_store_type_t node_type,
94+
attribute_store::attribute parent
95+
= cpp_endpoint_id_node);
96+
97+
/**
98+
* @brief Test that a node exists
99+
*
100+
* @param node_type Node type to test
101+
* @param parent Parent node of the node to get (default to current endpoint)
102+
*/
103+
void helper_test_node_exists(attribute_store_type_t node_type,
104+
attribute_store::attribute parent
105+
= cpp_endpoint_id_node);
106+
/**
107+
* @brief Test that a node doesn't exists
108+
*
109+
* @param node_type Node type to test
110+
* @param parent Parent node of the node to get (default to current endpoint)
111+
*/
112+
void helper_test_node_does_not_exists(attribute_store_type_t node_type,
113+
attribute_store::attribute parent
114+
= cpp_endpoint_id_node);
115+
} // namespace zpc_attribute_store_test_helper
116+
117+
} // extern "C"
118+
119+
// Cpp template functions
120+
namespace zpc_attribute_store_test_helper
121+
{
122+
template<typename T> attribute_store::attribute helper_test_attribute_value(
123+
attribute_store_type_t node_type,
124+
T expected_value,
125+
attribute_store::attribute parent = cpp_endpoint_id_node,
126+
attribute_store_node_value_state_t state = REPORTED_ATTRIBUTE)
127+
{
128+
auto current_node = helper_test_and_get_node(node_type, parent);
129+
130+
try {
131+
const std::string error_message
132+
= (std::string("Value mismatch for ") + current_node.name_and_id())
133+
.c_str();
134+
135+
if constexpr (std::is_same<T, std::vector<uint8_t>>::value) {
136+
TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(
137+
expected_value.data(),
138+
current_node.reported<std::vector<uint8_t>>().data(),
139+
expected_value.size(),
140+
error_message.c_str());
141+
} else if constexpr (std::is_same<T, std::string>::value) {
142+
TEST_ASSERT_EQUAL_STRING_MESSAGE(
143+
expected_value.c_str(),
144+
current_node.reported<std::string>().c_str(),
145+
error_message.c_str());
146+
} else {
147+
TEST_ASSERT_EQUAL_MESSAGE(expected_value,
148+
current_node.get<T>(state),
149+
error_message.c_str());
150+
}
151+
} catch (std::exception &e) {
152+
TEST_FAIL_MESSAGE(e.what());
153+
}
154+
155+
return current_node;
156+
}
157+
} // namespace zpc_attribute_store_test_helper
158+
#endif // ZPC_ATTRIBUTE_STORE_TEST_HELPER_CPP_HPP

applications/zpc/components/zwave_command_classes/test/helpers/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ target_include_directories(zwave_command_class_test_helpers
99

1010
target_link_libraries(zwave_command_class_test_helpers
1111
# Helpers
12-
zpc_attribute_store_test_helper
12+
zpc_attribute_store_test_helper_cpp
1313
# Mock
1414
zwave_command_handler_mock
1515
uic_attribute_resolver_mock)

0 commit comments

Comments
 (0)