diff --git a/src/core/SysfsRead.cxx b/src/core/SysfsRead.cxx new file mode 100644 index 0000000..962dbef --- /dev/null +++ b/src/core/SysfsRead.cxx @@ -0,0 +1,15 @@ +// SysfsRead.cxx + +// SPDX-License-Identifier: GPL-3.0-or-later +// Author: Unai Blazquez + +#include "SysfsRead.hpp" + +SysfsReader::SysfsReader(const std::filesystem::path& input_path) + : m_path(input_path) +{ +} +SysfsStatus SysfsReader::read_status() const +{ + return SysfsStatus::UnexpectedValue; +} diff --git a/tests/test_sysfs_read.cxx b/tests/test_sysfs_read.cxx index 0294220..417c006 100644 --- a/tests/test_sysfs_read.cxx +++ b/tests/test_sysfs_read.cxx @@ -3,3 +3,75 @@ #include #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); +}