Skip to content

Commit

Permalink
Refactor unit test temp file helper
Browse files Browse the repository at this point in the history
  • Loading branch information
timower committed May 31, 2024
1 parent d13854e commit fc4303e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
24 changes: 24 additions & 0 deletions test/unit/TempFiles.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <catch2/catch_test_macros.hpp>

#include <filesystem>

class TemporaryDirectory {
public:
TemporaryDirectory() : dir("/tmp/unit-tests") {
REQUIRE_NOTHROW(std::filesystem::create_directory(dir));
}
TemporaryDirectory(const TemporaryDirectory&) = delete;
TemporaryDirectory(TemporaryDirectory&&) = default;
TemporaryDirectory& operator=(const TemporaryDirectory&) = delete;
TemporaryDirectory& operator=(TemporaryDirectory&&) = default;

~TemporaryDirectory() {
if (!dir.empty() && std::filesystem::is_directory(dir)) {
REQUIRE_NOTHROW(std::filesystem::remove_all(dir));
}
}

std::filesystem::path dir;
};
21 changes: 4 additions & 17 deletions test/unit/TestTilem.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <catch2/catch_test_macros.hpp>

#include "TempFiles.h"
#include "rMLibTestHelper.h"

#include "Calculator.h"
Expand All @@ -11,24 +12,9 @@
using namespace rmlib;
using namespace tilem;

class TemporaryDirectory {
public:
TemporaryDirectory() : dir("/tmp/tilem") {
REQUIRE_NOTHROW(std::filesystem::create_directory(dir));
}

~TemporaryDirectory() {
if (!dir.empty() && std::filesystem::is_directory(dir)) {
REQUIRE_NOTHROW(std::filesystem::remove_all(dir));
}
}

std::string dir;
};

TEST_CASE("Tilem", "[tilem][ui]") {
TemporaryDirectory tmp;
const auto romPath = tmp.dir + "/unit_test.rom";
const auto romPath = tmp.dir / "unit_test.rom";

auto ctx = TestContext::make();

Expand All @@ -54,7 +40,8 @@ TEST_CASE("Tilem", "[tilem][ui]") {

// Wait for the download to finish.
ctx.pump();
while (!ctx.findByText("Downloading ROM '" + romPath + "' ...").empty()) {
while (!ctx.findByText("Downloading ROM '" + romPath.string() + "' ...")
.empty()) {
ctx.pump();
}
ctx.pump(std::chrono::milliseconds(500));
Expand Down
4 changes: 2 additions & 2 deletions test/unit/assets/tilem-error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fc4303e

Please sign in to comment.