diff --git a/common/include/test_assert.h b/common/include/test_assert.h index 9669c16a02..7a48c9e178 100644 --- a/common/include/test_assert.h +++ b/common/include/test_assert.h @@ -28,25 +28,39 @@ #ifdef try_status #undef try_status #endif +#if defined(EXPECT_EQ) +/* vector to GTest where available */ +#define try_status(expr) EXPECT_EQ((expr), KM_CORE_STATUS_OK) +#else #define try_status(expr) { \ auto __s = (expr); \ if (__s != KM_CORE_STATUS_OK) { \ _test_assert_failed(__s, u ## #expr); \ } \ } +#endif #ifdef test_assert #undef test_assert #endif +#if defined(EXPECT_TRUE) +/* vector to GTest where available */ +# define test_assert(expr) EXPECT_TRUE(expr) << "test_assert failed" +#else #define test_assert(expr) { \ if (!(expr)) { \ _test_assert_failed(0, u ## #expr); \ } \ } +#endif #ifdef test_assert_equal #undef test_assert_equal #endif +#if defined(EXPECT_EQ) +/* vector to GTest where available */ +#define test_assert_equal(actual, expected) EXPECT_EQ(actual, expected) +#else #define test_assert_equal(actual, expected) { \ if ((actual) != (expected)) { \ std::wcerr << console_color::fg(console_color::BRIGHT_RED) \ @@ -58,10 +72,15 @@ std::exit(EXIT_FAILURE); \ } \ } +#endif #ifdef test_assert_string_equal #undef test_assert_string_equal #endif +#if defined(EXPECT_EQ) +/* vector to GTest where available */ +#define test_assert_string_equal(actual, expected) EXPECT_EQ(actual, expected) +#else #define test_assert_string_equal(actual, expected) { \ if (u16cmp((actual), (expected)) != 0) { \ std::wcerr << console_color::fg(console_color::BRIGHT_RED) \ @@ -73,3 +92,4 @@ std::exit(EXIT_FAILURE); \ } \ } +#endif diff --git a/core/tests/unit/ldml/kmx_plus.tests.cpp b/core/tests/unit/ldml/kmx_plus.tests.cpp index eed021664c..be8bd07215 100644 --- a/core/tests/unit/ldml/kmx_plus.tests.cpp +++ b/core/tests/unit/ldml/kmx_plus.tests.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "kmx/kmx_plus.h" #include "kmx/kmx_xstring.h" @@ -12,9 +13,7 @@ using namespace km::core::kmx; -int test_COMP_KMXPLUS_KEYS_KEY() { - std::cout << "== " << __FUNCTION__ << std::endl; - +TEST(KMXPlusTest, test_COMP_KMXPLUS_KEYS_KEY) { COMP_KMXPLUS_KEYS_KEY e[2] = { { 0x00000127, // to = U+0127 = 295 @@ -55,13 +54,9 @@ int test_COMP_KMXPLUS_KEYS_KEY() { test_assert_equal(es1.at(0), 0xD83D); test_assert_equal(es1.at(1), 0xDE40); test_assert(es1 == std::u16string(u"🙀")); - - return EXIT_SUCCESS; } -int test_ldml_vkeys() { - std::cout << "== " << __FUNCTION__ << std::endl; - +TEST(KMXPlusTest, test_ldml_vkeys) { km::core::ldml::vkeys vk; #define ADD_VKEY(k, m) { \ @@ -151,13 +146,9 @@ int test_ldml_vkeys() { "K_E"), RCTRLFLAG|LALTFLAG, found), u"K_E-K_ALTFLAG|RCTRLFLAG"); test_assert_equal(vk.lookup(km::tests::get_vk( "K_E"), RCTRLFLAG|RALTFLAG, found), u"K_E-K_ALTFLAG|RCTRLFLAG"); - - return EXIT_SUCCESS; } -int test_uset() { - std::cout << "== " << __FUNCTION__ << std::endl; - +TEST(KMXPlusTest, test_uset) { const COMP_KMXPLUS_USET_RANGE r[] = { {0x61, 0x7a}, // [a-z] {0x127, 0x127} // [ħ] @@ -171,31 +162,10 @@ int test_uset() { SimpleUSet uempty; test_assert_equal(uempty.contains(0x62), false); test_assert_equal(uempty.contains(0x127), false); - - return EXIT_SUCCESS; } -int -main(int argc, const char *argv[]) { - int rc = EXIT_SUCCESS; - - if (test_COMP_KMXPLUS_KEYS_KEY() != EXIT_SUCCESS) { - rc = EXIT_FAILURE; - } - - if (test_ldml_vkeys() != EXIT_SUCCESS) { - rc = EXIT_FAILURE; - } - - if (test_uset() != EXIT_SUCCESS) { - rc = EXIT_FAILURE; - } - - if (rc == EXIT_FAILURE) { - std::cout << "== FAILURE" << std::endl; - } else { - std::cout << "== PASS" << std::endl; - } - - return rc; +GTEST_API_ int +main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/core/tests/unit/ldml/meson.build b/core/tests/unit/ldml/meson.build index e7a271b91f..21ae49590d 100644 --- a/core/tests/unit/ldml/meson.build +++ b/core/tests/unit/ldml/meson.build @@ -19,6 +19,10 @@ if node.found() subdir('invalid-keyboards') endif +gtest = subproject('gtest') +gtest_dep = gtest.get_variable('gtest_dep') +gmock_dep = gtest.get_variable('gmock_dep') + # Build ldml test executable keyboard_build_path = meson.current_build_dir() / 'keyboards' @@ -87,7 +91,7 @@ e = executable('kmx_plus_tests', 'kmx_plus.tests.cpp', cpp_args: defns + warns, include_directories: [inc, libsrc, '../../../../developer/src/ext/json'], link_args: links + tests_flags, - dependencies: [icu_uc, icu_i18n], + dependencies: [icu_uc, icu_i18n, gtest_dep, gmock_dep], objects: lib.extract_all_objects(recursive: false)) test('kmx_plus_tests', e, suite: 'ldml')