Skip to content

Commit 3f98d9e

Browse files
Add tests for status_item
Signed-off-by: Mahmoud Almasri <mahm.al.masri@gmail.com>
1 parent 5266c96 commit 3f98d9e

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

diagnostic_aggregator/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,13 @@ if(BUILD_TESTING)
7878
set(ament_cmake_copyright_FOUND TRUE)
7979
ament_lint_auto_find_test_dependencies()
8080

81+
find_package(ament_cmake_gtest REQUIRED)
8182
find_package(ament_cmake_pytest REQUIRED)
8283
find_package(launch_testing_ament_cmake REQUIRED)
8384

85+
ament_add_gtest(test_status_item test/test_status_item.cpp)
86+
target_link_libraries(test_status_item ${PROJECT_NAME})
87+
8488
file(TO_CMAKE_PATH "${CMAKE_INSTALL_PREFIX}/lib/${PROJECT_NAME}/aggregator_node" AGGREGATOR_NODE)
8589
file(TO_CMAKE_PATH "${CMAKE_INSTALL_PREFIX}/lib/${PROJECT_NAME}/add_analyzer" ADD_ANALYZER)
8690
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test/test_listener.py" TEST_LISTENER)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*********************************************************************
2+
* Software License Agreement (BSD License)
3+
*
4+
* Copyright (c) 2025, Mahmoud Almasri
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* * Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* * Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
*********************************************************************/
23+
24+
#include <gtest/gtest.h>
25+
26+
#include <string>
27+
#include <vector>
28+
29+
#include "diagnostic_aggregator/status_item.hpp"
30+
#include "diagnostic_msgs/msg/key_value.hpp"
31+
#include "rclcpp/rclcpp.hpp"
32+
33+
using diagnostic_aggregator::StatusItem;
34+
using diagnostic_aggregator::Level_OK;
35+
using diagnostic_aggregator::Level_Stale;
36+
using diagnostic_msgs::msg::KeyValue;
37+
38+
namespace
39+
{
40+
KeyValue kv(const std::string & k, const std::string & v)
41+
{
42+
KeyValue out;
43+
out.key = k;
44+
out.value = v;
45+
return out;
46+
}
47+
} // namespace
48+
49+
TEST(StatusItem, constructorWithValuesInitializesAllFields)
50+
{
51+
std::vector<KeyValue> values{kv("a", "1"), kv("b", "2")};
52+
StatusItem item("sensor", values, "ok-msg", Level_OK);
53+
54+
EXPECT_EQ(item.getName(), "sensor");
55+
EXPECT_EQ(item.getMessage(), "ok-msg");
56+
EXPECT_EQ(item.getLevel(), Level_OK);
57+
EXPECT_EQ(item.getValue("a"), "1");
58+
EXPECT_EQ(item.getValue("b"), "2");
59+
}
60+
61+
TEST(StatusItem, addValueNewKeyAppendsEntry)
62+
{
63+
StatusItem item("sensor");
64+
ASSERT_FALSE(item.hasKey("k"));
65+
66+
item.addValue("k", "v");
67+
68+
EXPECT_TRUE(item.hasKey("k"));
69+
EXPECT_EQ(item.getValue("k"), "v");
70+
}
71+
72+
TEST(StatusItem, addValueExistingKeyUpdatesInPlace)
73+
{
74+
std::vector<KeyValue> values{kv("k", "old")};
75+
StatusItem item("sensor", values);
76+
77+
item.addValue("k", "new");
78+
79+
EXPECT_EQ(item.getValue("k"), "new");
80+
81+
// values_ is private; check via the serialized message that no duplicate was added.
82+
auto msg = item.toStatusMsg("path");
83+
ASSERT_EQ(msg->values.size(), 1u);
84+
EXPECT_EQ(msg->values[0].key, "k");
85+
EXPECT_EQ(msg->values[0].value, "new");
86+
}
87+
88+
TEST(StatusItem, hasKey)
89+
{
90+
std::vector<KeyValue> values{kv("a", "1"), kv("b", "2")};
91+
StatusItem item("sensor", values);
92+
93+
EXPECT_FALSE(item.hasKey("nope"));
94+
EXPECT_TRUE(item.hasKey("a"));
95+
EXPECT_TRUE(item.hasKey("b"));
96+
}
97+
98+
int main(int argc, char ** argv)
99+
{
100+
testing::InitGoogleTest(&argc, argv);
101+
rclcpp::init(argc, argv);
102+
103+
return RUN_ALL_TESTS();
104+
}

0 commit comments

Comments
 (0)