From 5f5863794e7e807e7a60bfb7f00020b527273134 Mon Sep 17 00:00:00 2001 From: Pavel Siska Date: Wed, 17 Apr 2024 12:14:13 +0200 Subject: [PATCH] test - introduce unit tests for telemetry::Holder --- src/telemetry/holder.cpp | 4 ++ src/telemetry/tests/testHolder.cpp | 81 ++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/telemetry/tests/testHolder.cpp diff --git a/src/telemetry/holder.cpp b/src/telemetry/holder.cpp index e256bad..2011fdb 100644 --- a/src/telemetry/holder.cpp +++ b/src/telemetry/holder.cpp @@ -34,3 +34,7 @@ void Holder::disableFiles() } } // namespace telemetry + +#ifdef TELEMETRY_ENABLE_TESTS +#include "tests/testHolder.cpp" +#endif diff --git a/src/telemetry/tests/testHolder.cpp b/src/telemetry/tests/testHolder.cpp new file mode 100644 index 0000000..7a69744 --- /dev/null +++ b/src/telemetry/tests/testHolder.cpp @@ -0,0 +1,81 @@ +/** + * @file + * @author Lukas Hutak + * @brief Unit tests of telemetry::Holder class + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +#include + +TEST(sample_test_case, sample_test) +{ + EXPECT_EQ(1, 1); +} + +namespace telemetry { + +/** + * @test Test checking availability of read operation. + */ +TEST(TelemetryHolder, create) +{ + Holder holder; +} + +/** + * @test Test adding various entries to the holder. + */ +TEST(TelemetryHolder, add) +{ + auto root = Directory::create(); + + { + Holder holder; + + { + auto file = root->addFile("file", {}); + auto dir = root->addDir("dir"); + + holder.add(file); + holder.add(dir); + } + + // Check if entries still exists + EXPECT_NE(nullptr, root->getEntry("file")); + EXPECT_NE(nullptr, root->getEntry("dir")); + } + + // After holder destruction, entries should be gone... + EXPECT_EQ(nullptr, root->getEntry("file")); + EXPECT_EQ(nullptr, root->getEntry("dir")); +} + +/** + * @test Test disabling callbacks of held files. + */ +TEST(TelemetryHolder, disableFiles) +{ + auto root = Directory::create(); + FileOps ops = {}; + ops.read = []() { return Scalar {"value"}; }; + ops.clear = []() {}; + + Holder holder; + + auto dir = root->addDir("dir"); + auto file = root->addFile("file", ops); + holder.add(file); + + EXPECT_TRUE(file->hasRead()); + EXPECT_TRUE(file->hasClear()); + + holder.disableFiles(); + + EXPECT_FALSE(file->hasRead()); + EXPECT_FALSE(file->hasClear()); +} + +} // namespace telemetry