forked from Mirrorlandia_minetest/minetest
master #6
@ -330,7 +330,7 @@ std::string TestBase::getTestTempDirectory()
|
|||||||
|
|
||||||
m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
|
m_test_dir = fs::TempPath() + DIR_DELIM "mttest_" + buf;
|
||||||
if (!fs::CreateDir(m_test_dir))
|
if (!fs::CreateDir(m_test_dir))
|
||||||
throw TestFailedException();
|
UASSERT(false);
|
||||||
|
|
||||||
return m_test_dir;
|
return m_test_dir;
|
||||||
}
|
}
|
||||||
@ -343,6 +343,26 @@ std::string TestBase::getTestTempFile()
|
|||||||
return getTestTempDirectory() + DIR_DELIM + buf + ".tmp";
|
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.
|
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
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "irrlichttypes_extrabloated.h"
|
#include "irrlichttypes_extrabloated.h"
|
||||||
@ -27,63 +29,48 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "filesys.h"
|
#include "filesys.h"
|
||||||
#include "mapnode.h"
|
#include "mapnode.h"
|
||||||
|
|
||||||
class TestFailedException : public std::exception {
|
class TestFailedException { // don’t 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
|
// Runs a unit test and reports results
|
||||||
#define TEST(fxn, ...) { \
|
#define TEST(fxn, ...) runTest(#fxn, [&] () { fxn(__VA_ARGS__); });
|
||||||
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; \
|
|
||||||
}
|
|
||||||
|
|
||||||
// Asserts the specified condition is true, or fails the current unit test
|
// Asserts the specified condition is true, or fails the current unit test
|
||||||
#define UASSERT(x) \
|
#define UASSERT(x) \
|
||||||
if (!(x)) { \
|
if (!(x)) { \
|
||||||
rawstream << "Test assertion failed: " #x << std::endl \
|
throw TestFailedException(#x, __FILE__, __LINE__); \
|
||||||
<< " at " << fs::GetFilenameFromPath(__FILE__) \
|
|
||||||
<< ":" << __LINE__ << std::endl; \
|
|
||||||
throw TestFailedException(); \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asserts the specified condition is true, or fails the current unit test
|
// Asserts the specified condition is true, or fails the current unit test
|
||||||
// and prints the format specifier fmt
|
// and prints the format specifier fmt
|
||||||
#define UTEST(x, fmt, ...) \
|
#define UTEST(x, fmt, ...) \
|
||||||
if (!(x)) { \
|
if (!(x)) { \
|
||||||
char utest_buf[1024]; \
|
char utest_buf[1024]; \
|
||||||
snprintf(utest_buf, sizeof(utest_buf), fmt, __VA_ARGS__); \
|
snprintf(utest_buf, sizeof(utest_buf), fmt, __VA_ARGS__); \
|
||||||
rawstream << "Test assertion failed: " << utest_buf << std::endl \
|
throw TestFailedException(utest_buf, __FILE__, __LINE__); \
|
||||||
<< " at " << fs::GetFilenameFromPath(__FILE__) \
|
|
||||||
<< ":" << __LINE__ << std::endl; \
|
|
||||||
throw TestFailedException(); \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asserts the comparison specified by CMP is true, or fails the current unit test
|
// Asserts the comparison specified by CMP is true, or fails the current unit test
|
||||||
#define UASSERTCMP(T, CMP, actual, expected) { \
|
#define UASSERTCMP(T, CMP, actual, expected) { \
|
||||||
T a = (actual); \
|
T a = (actual); \
|
||||||
T e = (expected); \
|
T e = (expected); \
|
||||||
if (!(a CMP e)) { \
|
if (!(a CMP e)) { \
|
||||||
rawstream \
|
std::ostringstream message; \
|
||||||
<< "Test assertion failed: " << #actual << " " << #CMP << " " \
|
message << #actual " " #CMP " " #expected; \
|
||||||
<< #expected << std::endl \
|
message << std::endl << " actual : " << a; \
|
||||||
<< " at " << fs::GetFilenameFromPath(__FILE__) << ":" \
|
message << std::endl << " expected: " << e; \
|
||||||
<< __LINE__ << std::endl \
|
throw TestFailedException(message.str(), __FILE__, __LINE__); \
|
||||||
<< " actual : " << a << std::endl << " expected: " \
|
} \
|
||||||
<< e << std::endl; \
|
|
||||||
throw TestFailedException(); \
|
|
||||||
} \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define UASSERTEQ(T, actual, expected) UASSERTCMP(T, ==, actual, expected)
|
#define UASSERTEQ(T, actual, expected) UASSERTCMP(T, ==, actual, expected)
|
||||||
@ -113,6 +100,8 @@ public:
|
|||||||
u32 num_tests_failed;
|
u32 num_tests_failed;
|
||||||
u32 num_tests_run;
|
u32 num_tests_run;
|
||||||
|
|
||||||
|
void runTest(const char *name, std::function<void()> &&test);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_test_dir;
|
std::string m_test_dir;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user