1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
/*
* TestToolsLib.cpp
* ----------------
* Purpose: Unit test framework for libopenmpt.
* Notes : Currently somewhat unreadable :/
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "TestToolsLib.h"
#ifdef ENABLE_TESTS
#ifndef MODPLUG_TRACKER
#include <exception>
#include <iostream>
#include <cstdlib>
OPENMPT_NAMESPACE_BEGIN
namespace Test {
int fail_count = 0;
static std::string remove_newlines(std::string str)
{
return mpt::String::Replace(mpt::String::Replace(str, "\n", " "), "\r", " ");
}
Testcase::Testcase(Fatality fatality, Verbosity verbosity, const char * const desc, const mpt::source_location &loc)
: fatality(fatality)
, verbosity(verbosity)
, desc(desc)
, loc(loc)
{
return;
}
std::string Testcase::AsString() const
{
return mpt::format(std::string("%1(%2): %3"))(loc.file_name() ? loc.file_name() : "", loc.line(), remove_newlines(desc));
}
void Testcase::ShowStart() const
{
switch(verbosity)
{
case VerbosityQuiet:
break;
case VerbosityNormal:
#if !MPT_OS_DJGPP
std::cout << "TEST..: " << AsString() << ": " << std::endl;
#endif
break;
case VerbosityVerbose:
#if !MPT_OS_DJGPP
std::cout << "TEST..: " << AsString() << ": " << std::endl;
#endif
break;
}
}
void Testcase::ShowProgress(const char * text) const
{
switch(verbosity)
{
case VerbosityQuiet:
break;
case VerbosityNormal:
break;
case VerbosityVerbose:
#if !MPT_OS_DJGPP
std::cout << "TEST..: " << AsString() << ": " << text << std::endl;
#else
MPT_UNUSED_VARIABLE(text);
#endif
break;
}
}
void Testcase::ShowPass() const
{
switch(verbosity)
{
case VerbosityQuiet:
break;
case VerbosityNormal:
#if !MPT_OS_DJGPP
std::cout << "RESULT: PASS" << std::endl;
#endif
break;
case VerbosityVerbose:
#if !MPT_OS_DJGPP
std::cout << "PASS..: " << AsString() << std::endl;
#endif
break;
}
}
void Testcase::ShowFail(bool exception, const char * const text) const
{
switch(verbosity)
{
case VerbosityQuiet:
break;
case VerbosityNormal:
std::cout << "RESULT: FAIL" << std::endl;
break;
case VerbosityVerbose:
std::cout << "FAIL..: " << AsString() << std::endl;
break;
}
std::cout.flush();
if(!exception)
{
if(!text || (text && std::string(text).empty()))
{
std::cerr << "FAIL: " << AsString() << std::endl;
} else
{
std::cerr << "FAIL: " << AsString() << " : " << text << std::endl;
}
} else
{
if(!text || (text && std::string(text).empty()))
{
std::cerr << "FAIL: " << AsString() << " EXCEPTION!" << std::endl;
} else
{
std::cerr << "FAIL: " << AsString() << " EXCEPTION: " << text << std::endl;
}
}
std::cerr.flush();
}
void Testcase::ReportPassed()
{
ShowPass();
}
void Testcase::ReportFailed()
{
fail_count++;
ReportException();
}
void Testcase::ReportException()
{
try
{
throw; // get the exception
} catch(TestFailed & e)
{
ShowFail(false, e.values.c_str());
if(fatality == FatalityStop)
{
throw; // rethrow
}
} catch(std::exception & e)
{
ShowFail(true, e.what());
throw; // rethrow
} catch(...)
{
ShowFail(true);
throw; // rethrow
}
}
} // namespace Test
#if defined(MPT_ASSERT_HANDLER_NEEDED)
MPT_NOINLINE void AssertHandler(const mpt::source_location &loc, const char *expr, const char *msg)
{
Test::fail_count++;
if(msg)
{
mpt::log::Logger().SendLogMessage(loc, LogError, "ASSERT",
U_("ASSERTION FAILED: ") + mpt::ToUnicode(mpt::CharsetASCII, msg) + U_(" (") + mpt::ToUnicode(mpt::CharsetASCII, expr) + U_(")")
);
} else
{
mpt::log::Logger().SendLogMessage(loc, LogError, "ASSERT",
U_("ASSERTION FAILED: ") + mpt::ToUnicode(mpt::CharsetASCII, expr)
);
}
#if defined(MPT_BUILD_FATAL_ASSERTS)
std::abort();
#endif // MPT_BUILD_FATAL_ASSERTS
}
#endif // MPT_ASSERT_HANDLER_NEEDED
OPENMPT_NAMESPACE_END
#endif // !MODPLUG_TRACKER
#endif // ENABLE_TESTS
|