#include "filesystem_mock.hpp"
#include "hwmonio.hpp"

#include <chrono>
#include <string>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace hwmonio
{
namespace
{

using ::testing::_;
using ::testing::Return;

class HwmonIOTest : public ::testing::Test
{
  protected:
    HwmonIOTest() : _hwmonio(_path, &_mock)
    {
    }

    const int64_t _value = 12;
    const std::string _path = "abcd";
    const std::string _type = "fan";
    const std::string _id = "a";
    const std::string _sensor = "1";
    const size_t _retries = 1;
    const std::chrono::milliseconds _delay = std::chrono::milliseconds{10};

    FileSystemMock _mock;
    HwmonIO _hwmonio;
};

TEST_F(HwmonIOTest, ReadReturnsValue)
{
    EXPECT_CALL(_mock, read(_)).WillOnce(Return(_value));
    EXPECT_THAT(_hwmonio.read(_type, _id, _sensor, _retries, _delay), _value);
}

int64_t SetErrnoExcept(const std::string&)
{
    errno = ETIMEDOUT;
    throw std::runtime_error("bad times");
}

TEST_F(HwmonIOTest, ReadExceptsRetryable)
{
    EXPECT_CALL(_mock, read(_))
        .WillOnce(&SetErrnoExcept)
        .WillOnce(Return(_value));
    EXPECT_THAT(_hwmonio.read(_type, _id, _sensor, _retries, _delay), _value);
}

} // namespace
} // namespace hwmonio