|
| 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