From 0d166c373543ba63eb217d477dce93ed4798985f Mon Sep 17 00:00:00 2001 From: Pavel Siska Date: Wed, 17 Apr 2024 12:13:53 +0200 Subject: [PATCH] test - introduce unit tests for telemetry::File --- src/telemetry/file.cpp | 4 ++ src/telemetry/tests/testFile.cpp | 111 +++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/telemetry/tests/testFile.cpp diff --git a/src/telemetry/file.cpp b/src/telemetry/file.cpp index eb87367..7130483 100644 --- a/src/telemetry/file.cpp +++ b/src/telemetry/file.cpp @@ -68,3 +68,7 @@ void File::disable() } } // namespace telemetry + +#ifdef TELEMETRY_ENABLE_TESTS +#include "tests/testFile.cpp" +#endif diff --git a/src/telemetry/tests/testFile.cpp b/src/telemetry/tests/testFile.cpp new file mode 100644 index 0000000..4afbfda --- /dev/null +++ b/src/telemetry/tests/testFile.cpp @@ -0,0 +1,111 @@ +/** + * @file + * @author Lukas Hutak + * @brief Unit tests of telemetry::File class + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +#include + +namespace telemetry { + +/** + * @test Test checking availability of read operation. + */ +TEST(TelemetryFile, hasRead) +{ + auto root = Directory::create(); + + auto noOpsFile = root->addFile("no", {}); + EXPECT_FALSE(noOpsFile->hasRead()); + + FileOps ops {}; + ops.read = []() { return Scalar {}; }; + auto opsFile = root->addFile("yes", ops); + EXPECT_TRUE(opsFile->hasRead()); +} + +/** + * @test Test checking availability of clear operation. + */ +TEST(TelemetryFile, hasClear) +{ + auto root = Directory::create(); + + auto noOpsFile = root->addFile("no", {}); + EXPECT_FALSE(noOpsFile->hasClear()); + + FileOps ops {}; + ops.clear = []() {}; + auto opsFile = root->addFile("yes", ops); + EXPECT_TRUE(opsFile->hasClear()); +} + +/** + * @test Test executing read operation. + */ +TEST(TelemetryFile, read) +{ + auto root = Directory::create(); + + auto noOpsFile = root->addFile("no", {}); + EXPECT_THROW(noOpsFile->read(), NodeException); + + FileOps ops {}; + ops.read = []() { return Scalar {"hello"}; }; + auto opsFile = root->addFile("yes", ops); + EXPECT_EQ(Content {Scalar {"hello"}}, opsFile->read()); +} + +/** + * @test Test executing read operation. + */ +TEST(TelemetryFile, clear) +{ + auto root = Directory::create(); + int valueToClear = 1; + + auto noOpsFile = root->addFile("no", {}); + EXPECT_THROW(noOpsFile->clear(), NodeException); + + FileOps ops {}; + ops.clear = [&]() { valueToClear = 0; }; + auto opsFile = root->addFile("yes", ops); + opsFile->clear(); + EXPECT_EQ(0, valueToClear); +} + +/** + * @test Test that disabling of all operations works as expected. + */ +TEST(TelemetryFile, disable) +{ + int64_t counter = 0; + FileOps ops {}; + ops.read = [&]() { return Scalar {counter++}; }; + ops.clear = [&]() { counter = 0; }; + + auto root = Directory::create(); + auto file = root->addFile("file", ops); + + EXPECT_TRUE(file->hasRead()); + EXPECT_TRUE(file->hasClear()); + + EXPECT_EQ(Content {Scalar {int64_t {0}}}, file->read()); + EXPECT_EQ(Content {Scalar {int64_t {1}}}, file->read()); + EXPECT_EQ(Content {Scalar {int64_t {2}}}, file->read()); + EXPECT_NO_THROW(file->clear()); + EXPECT_EQ(Content {Scalar {int64_t {0}}}, file->read()); + + file->disable(); + + EXPECT_FALSE(file->hasRead()); + EXPECT_FALSE(file->hasClear()); + EXPECT_THROW(file->read(), NodeException); + EXPECT_THROW(file->clear(), NodeException); +} + +} // namespace telemetry