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
0 commit comments