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
|
/*
* Keyman is copyright (C) SIL International. MIT License.
*
* Keyman Core - test assertion macros
*/
#pragma once
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include "test_color.h"
#ifdef _test_assert_failed
#undef _test_assert_failed
#endif
#define _test_assert_failed(result, exprText) { \
std::wcerr << console_color::fg(console_color::BRIGHT_RED) \
<< "Test failed with " << (result) \
<< " at " << __FILE__ << ":" << __LINE__ << ":" \
<< console_color::reset() \
<< std::endl \
<< " " << (exprText) << std::endl; \
std::exit(EXIT_FAILURE); \
}
#ifdef try_status
#undef try_status
#endif
#define try_status(expr) { \
auto __s = (expr); \
if (__s != KM_CORE_STATUS_OK) { \
_test_assert_failed(__s, u ## #expr); \
} \
}
#ifdef test_assert
#undef test_assert
#endif
#define test_assert(expr) { \
if (!(expr)) { \
_test_assert_failed(0, u ## #expr); \
} \
}
#ifdef test_assert_equal
#undef test_assert_equal
#endif
#define test_assert_equal(actual, expected) { \
if ((actual) != (expected)) { \
std::wcerr << console_color::fg(console_color::BRIGHT_RED) \
<< "Test failed at " << __FILE__ << ":" << __LINE__ << ":" \
<< console_color::reset() \
<< std::endl \
<< "expected: " << (expected) << std::endl \
<< "actual: " << (actual) << std::endl; \
std::exit(EXIT_FAILURE); \
} \
}
#ifdef test_assert_string_equal
#undef test_assert_string_equal
#endif
#define test_assert_string_equal(actual, expected) { \
if (u16cmp((actual), (expected)) != 0) { \
std::wcerr << console_color::fg(console_color::BRIGHT_RED) \
<< "Test failed at " << __FILE__ << ":" << __LINE__ << ":" \
<< console_color::reset() \
<< std::endl \
<< "expected: " << Debug_UnicodeString((PKMX_WCHAR)(expected)) << std::endl \
<< "actual: " << Debug_UnicodeString((PKMX_WCHAR)(actual)) << std::endl; \
std::exit(EXIT_FAILURE); \
} \
}
|