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..6261ef5 --- /dev/null +++ b/src/telemetry/tests/testHolder.cpp @@ -0,0 +1,76 @@ +/** + * @file + * @author Lukas Hutak + * @brief Unit tests of telemetry::Holder class + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +#include + +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