Reduce test framework macrosity

This commit is contained in:
Vitaliy 2023-12-15 12:23:32 +03:00 committed by GitHub
parent bd06466d3a
commit 64b59184d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 46 deletions

@ -330,7 +330,7 @@ std::string TestBase::getTestTempDirectory()
m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
if (!fs::CreateDir(m_test_dir))
throw TestFailedException();
UASSERT(false);
return m_test_dir;
}
@ -343,6 +343,26 @@ std::string TestBase::getTestTempFile()
return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
}
void TestBase::runTest(const char *name, std::function<void()> &&test)
{
u64 t1 = porting::getTimeMs();
try {
test();
rawstream << "[PASS] ";
} catch (TestFailedException &e) {
rawstream << "Test assertion failed: " << e.message << std::endl;
rawstream << " at " << e.file << ":" << e.line << std::endl;
rawstream << "[FAIL] ";
num_tests_failed++;
} catch (std::exception &e) {
rawstream << "Caught unhandled exception: " << e.what() << std::endl;
rawstream << "[FAIL] ";
num_tests_failed++;
}
num_tests_run++;
u64 tdiff = porting::getTimeMs() - t1;
rawstream << name << " - " << tdiff << "ms" << std::endl;
}
/*
NOTE: These tests became non-working then NodeContainer was removed.

@ -19,7 +19,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <functional>
#include <exception>
#include <sstream>
#include <vector>
#include "irrlichttypes_extrabloated.h"
@ -27,35 +29,26 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "filesys.h"
#include "mapnode.h"
class TestFailedException : public std::exception {
class TestFailedException { // dont derive from std::exception to avoid accidental catch
public:
TestFailedException(std::string in_message, const char *in_file, int in_line)
: message(std::move(in_message))
, file(fs::GetFilenameFromPath(in_file))
, line(in_line)
{}
const std::string message;
const std::string file;
const int line;
};
// Runs a unit test and reports results
#define TEST(fxn, ...) { \
u64 t1 = porting::getTimeMs(); \
try { \
fxn(__VA_ARGS__); \
rawstream << "[PASS] "; \
} catch (TestFailedException &e) { \
rawstream << "[FAIL] "; \
num_tests_failed++; \
} catch (std::exception &e) { \
rawstream << "Caught unhandled exception: " << e.what() << std::endl; \
rawstream << "[FAIL] "; \
num_tests_failed++; \
} \
num_tests_run++; \
u64 tdiff = porting::getTimeMs() - t1; \
rawstream << #fxn << " - " << tdiff << "ms" << std::endl; \
}
#define TEST(fxn, ...) runTest(#fxn, [&] () { fxn(__VA_ARGS__); });
// Asserts the specified condition is true, or fails the current unit test
#define UASSERT(x) \
if (!(x)) { \
rawstream << "Test assertion failed: " #x << std::endl \
<< " at " << fs::GetFilenameFromPath(__FILE__) \
<< ":" << __LINE__ << std::endl; \
throw TestFailedException(); \
throw TestFailedException(#x, __FILE__, __LINE__); \
}
// Asserts the specified condition is true, or fails the current unit test
@ -64,10 +57,7 @@ class TestFailedException : public std::exception {
if (!(x)) { \
char utest_buf[1024]; \
snprintf(utest_buf, sizeof(utest_buf), fmt, __VA_ARGS__); \
rawstream << "Test assertion failed: " << utest_buf << std::endl \
<< " at " << fs::GetFilenameFromPath(__FILE__) \
<< ":" << __LINE__ << std::endl; \
throw TestFailedException(); \
throw TestFailedException(utest_buf, __FILE__, __LINE__); \
}
// Asserts the comparison specified by CMP is true, or fails the current unit test
@ -75,14 +65,11 @@ class TestFailedException : public std::exception {
T a = (actual); \
T e = (expected); \
if (!(a CMP e)) { \
rawstream \
<< "Test assertion failed: " << #actual << " " << #CMP << " " \
<< #expected << std::endl \
<< " at " << fs::GetFilenameFromPath(__FILE__) << ":" \
<< __LINE__ << std::endl \
<< " actual : " << a << std::endl << " expected: " \
<< e << std::endl; \
throw TestFailedException(); \
std::ostringstream message; \
message << #actual " " #CMP " " #expected; \
message << std::endl << " actual : " << a; \
message << std::endl << " expected: " << e; \
throw TestFailedException(message.str(), __FILE__, __LINE__); \
} \
}
@ -113,6 +100,8 @@ public:
u32 num_tests_failed;
u32 num_tests_run;
void runTest(const char *name, std::function<void()> &&test);
private:
std::string m_test_dir;
};