From ff30b27f0d042972b4777c43dbdfccb7641be0f2 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 9 Nov 2022 13:33:50 +1100 Subject: First test cases :) --- src/drivers/test/test_storage.cpp | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/drivers/test/test_storage.cpp (limited to 'src/drivers/test/test_storage.cpp') diff --git a/src/drivers/test/test_storage.cpp b/src/drivers/test/test_storage.cpp new file mode 100644 index 00000000..178ec815 --- /dev/null +++ b/src/drivers/test/test_storage.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +#include "gpio-expander.hpp" +#include "storage.hpp" +#include "i2c.hpp" +#include "spi.hpp" + +#include "catch2/catch.hpp" + +namespace drivers { + +static const std::string kTestFilename = "test"; +static const std::string kTestFilePath = + std::string(kStoragePath) + "/" + kTestFilename; + +TEST_CASE("sd card storage", "[integration]") { + REQUIRE( drivers::init_i2c() == ESP_OK ); + REQUIRE( drivers::init_spi() == ESP_OK ); + GpioExpander expander; + + { + std::unique_ptr result = SdStorage::create(&expander).value(); + + SECTION("write to a file") { + + { + std::ofstream test_file; + test_file.open(kTestFilePath.c_str()); + test_file << "hello here is some test"; + test_file.close(); + } + + SECTION("read from a file") { + std::ifstream test_file; + test_file.open(kTestFilePath.c_str()); + + std::string line; + REQUIRE(std::getline(test_file, line)); + REQUIRE(line == "hello here is some test"); + + test_file.close(); + } + + SECTION("list files") { + DIR* dir; + struct dirent* ent; + + dir = opendir(kStoragePath); + REQUIRE(dir != nullptr); + + bool found_test_file = false; + while (ent = readdir(dir)) { + if (ent->d_name == kTestFilename) { + found_test_file = true; + } + } + closedir(dir); + + REQUIRE(found_test_file); + + } + + REQUIRE(remove(kTestFilePath.c_str()) == 0); + } + } + + REQUIRE( drivers::deinit_i2c() == ESP_OK ); + REQUIRE( drivers::deinit_spi() == ESP_OK ); +} + +} // namespace drivers -- cgit v1.2.3