-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test - introduce unit tests for telemetry::File
- Loading branch information
1 parent
afb4e85
commit 0d166c3
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* @file | ||
* @author Lukas Hutak <hutak@cesnet.cz> | ||
* @brief Unit tests of telemetry::File class | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#include <telemetry/directory.hpp> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
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 |