output: LedSysfsDriver
Add a simple driver for manipulating with LEDs through sysfs.
Change-Id: Ib4219f8593e934cf4d94fc04d68336fa81c47bac
diff --git a/tests/configure.cmake.h.in b/tests/configure.cmake.h.in
new file mode 100644
index 0000000..6462dc2
--- /dev/null
+++ b/tests/configure.cmake.h.in
@@ -0,0 +1,2 @@
+#define CMAKE_CURRENT_SOURCE_DIR "@CMAKE_CURRENT_SOURCE_DIR@"
+#define CMAKE_CURRENT_BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@"
diff --git a/tests/output-led.cpp b/tests/output-led.cpp
new file mode 100644
index 0000000..86bb159
--- /dev/null
+++ b/tests/output-led.cpp
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
+ *
+ * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz>
+ *
+*/
+
+#include "trompeloeil_doctest.h"
+#include <filesystem>
+#include <fstream>
+#include "outputs/LedSysfsDriver.h"
+#include "test_log_setup.h"
+#include "tests/configure.cmake.h"
+
+namespace {
+
+uint32_t readFile(const std::filesystem::path& path)
+{
+ std::ifstream ifs(path);
+ uint32_t ret;
+ if (!(ifs >> ret)) {
+ throw std::invalid_argument("Failed reading '" + std::string(path) + "'.");
+ }
+ return ret;
+}
+
+/** @short Remove directory tree at 'rootDir' path (if exists) */
+void removeDirectoryTreeIfExists(const std::filesystem::path& rootDir)
+{
+ if (std::filesystem::exists(rootDir)) {
+ std::filesystem::remove_all(rootDir);
+ }
+}
+
+}
+
+TEST_CASE("SysFS LED driver")
+{
+ using namespace std::literals;
+ TEST_INIT_LOGS;
+
+ auto fakeSysfsDir = std::filesystem::path {CMAKE_CURRENT_BINARY_DIR + "/tests/led/"s};
+ auto fakeBrightnessFile = fakeSysfsDir / "brightness";
+ removeDirectoryTreeIfExists(fakeSysfsDir);
+
+ SECTION("Basic usage test")
+ {
+ std::filesystem::copy(std::string(CMAKE_CURRENT_SOURCE_DIR) + "/tests/sysfs/led/1/"s, fakeSysfsDir, std::filesystem::copy_options::recursive);
+ velia::LedSysfsDriver led(fakeSysfsDir);
+
+ led.set(0);
+ REQUIRE(readFile(fakeBrightnessFile) == 0);
+
+ led.set(1);
+ REQUIRE(readFile(fakeBrightnessFile) == 1);
+
+ led.set(42);
+ REQUIRE(readFile(fakeBrightnessFile) == 42);
+
+ led.set(0);
+ REQUIRE(readFile(fakeBrightnessFile) == 0);
+ }
+
+ SECTION("Invalid directory")
+ {
+ std::filesystem::copy(std::string(CMAKE_CURRENT_SOURCE_DIR) + "/tests/sysfs/led/2/"s, fakeSysfsDir, std::filesystem::copy_options::recursive);
+ REQUIRE_THROWS_AS(velia::LedSysfsDriver(fakeSysfsDir), std::invalid_argument);
+ }
+}
diff --git a/tests/sysfs/led/1/brightness b/tests/sysfs/led/1/brightness
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/tests/sysfs/led/1/brightness
@@ -0,0 +1 @@
+1
diff --git a/tests/sysfs/led/1/max_brightness b/tests/sysfs/led/1/max_brightness
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/tests/sysfs/led/1/max_brightness
@@ -0,0 +1 @@
+1
diff --git a/tests/sysfs/led/2/max_brightness b/tests/sysfs/led/2/max_brightness
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/tests/sysfs/led/2/max_brightness
@@ -0,0 +1 @@
+1