78 lines
1.8 KiB
C++
78 lines
1.8 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include "SysfsRead.hpp"
|
|
|
|
TEST(SysfsReaderTest, ReturnsEnabledWhenFileContainsOne)
|
|
{
|
|
// Arrange create the file and write "1\n" into it.
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << "1\n";
|
|
// out is closed automatically at the end of this scope
|
|
}
|
|
|
|
// 2) Act: construct the reader and read the status.
|
|
SysfsReader reader{"fake_sysfs_input"};
|
|
SysfsStatus status = reader.read_status();
|
|
// 3) Assert: we expect Enabled.
|
|
EXPECT_EQ(status, SysfsStatus::Enabled);
|
|
}
|
|
|
|
TEST(SysfsReaderTest, ReturnsEmptyWhenFileIsEmpty)
|
|
{
|
|
// Arrange: create the file and don't write anything
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << "";
|
|
}
|
|
SysfsReader reader{"fake_sysfs_input"};
|
|
SysfsStatus status1 = reader.read_status();
|
|
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << " ";
|
|
}
|
|
SysfsReader reader_2{"fake_sysfs_input"};
|
|
SysfsStatus status2 = reader_2.read_status();
|
|
|
|
// Assert
|
|
EXPECT_EQ(status1, SysfsStatus::Empty);
|
|
EXPECT_EQ(status2, SysfsStatus::Empty);
|
|
}
|
|
|
|
TEST(SysfsReaderTest, ReturnsUnexpectedValue)
|
|
{
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << "tdd development";
|
|
}
|
|
SysfsReader reader{"fake_sysfs_input"};
|
|
SysfsStatus status = reader.read_status();
|
|
|
|
// Assert
|
|
EXPECT_EQ(status, SysfsStatus::UnexpectedValue);
|
|
}
|
|
|
|
TEST(SysfsReaderTest, ReturnsErrorTempTooHigh)
|
|
{
|
|
{
|
|
std::ofstream out("fake_sysfs_input");
|
|
out << "error: temp too high";
|
|
}
|
|
SysfsReader reader{"fake_sysfs_input"};
|
|
SysfsStatus status = reader.read_status();
|
|
|
|
// Assert
|
|
EXPECT_EQ(status, SysfsStatus::ErrorTempTooHigh);
|
|
}
|
|
|
|
TEST(SysfsReaderTest, ReturnsUnreachableWhenDoesntExist)
|
|
{
|
|
SysfsReader reader{"nonexistent_sysfs_input"};
|
|
SysfsStatus status = reader.read_status();
|
|
// Assert
|
|
EXPECT_EQ(status, SysfsStatus::Unreachable);
|
|
}
|