From e2a4b198ba444871326711a7f1e65db8964e900d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Sun, 6 Oct 2024 19:38:32 +0200 Subject: [PATCH] testrunner: small `ASSERT_THROW*` cleanup (#6853) --- test/fixture.cpp | 5 +- test/fixture.h | 1 - test/testgarbage.cpp | 12 ++-- test/testsymboldatabase.cpp | 9 ++- test/testtokenize.cpp | 110 ++++++++++++++++++------------------ 5 files changed, 72 insertions(+), 65 deletions(-) diff --git a/test/fixture.cpp b/test/fixture.cpp index eac9f613763..f9414f0fd58 100644 --- a/test/fixture.cpp +++ b/test/fixture.cpp @@ -455,8 +455,9 @@ TestFixture::SettingsBuilder& TestFixture::SettingsBuilder::library(const char l if (REDUNDANT_CHECK && std::find(settings.libraries.cbegin(), settings.libraries.cend(), lib) != settings.libraries.cend()) throw std::runtime_error("redundant setting: libraries (" + std::string(lib) + ")"); // TODO: exename is not yet set - if (settings.library.load(fixture.exename.c_str(), lib).errorcode != Library::ErrorCode::OK) - throw std::runtime_error("library '" + std::string(lib) + "' not found"); + const Library::ErrorCode lib_error = settings.library.load(fixture.exename.c_str(), lib).errorcode; + if (lib_error != Library::ErrorCode::OK) + throw std::runtime_error("loading library '" + std::string(lib) + "' failed - " + std::to_string(static_cast(lib_error))); // strip extension std::string lib_s(lib); const std::string ext(".cfg"); diff --git a/test/fixture.h b/test/fixture.h index 38c38a5d890..a2f315aeaae 100644 --- a/test/fixture.h +++ b/test/fixture.h @@ -318,7 +318,6 @@ class TestInstance { #define ASSERT_EQUALS_MSG( EXPECTED, ACTUAL, MSG ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL, MSG) #define ASSERT_EQUALS_ENUM( EXPECTED, ACTUAL ) assertEqualsEnum(__FILE__, __LINE__, (EXPECTED), (ACTUAL)) #define TODO_ASSERT_EQUALS_ENUM( WANTED, CURRENT, ACTUAL ) todoAssertEqualsEnum(__FILE__, __LINE__, WANTED, CURRENT, ACTUAL) -#define ASSERT_THROW( CMD, EXCEPTION ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&) {} catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false) #define ASSERT_THROW_EQUALS( CMD, EXCEPTION, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { assertEquals(__FILE__, __LINE__, EXPECTED, e.errorMessage); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false) #define ASSERT_THROW_EQUALS_2( CMD, EXCEPTION, EXPECTED ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const EXCEPTION&e) { assertEquals(__FILE__, __LINE__, EXPECTED, e.what()); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false) #define ASSERT_THROW_INTERNAL( CMD, TYPE ) do { try { (void)(CMD); assertThrowFail(__FILE__, __LINE__); } catch (const InternalError& e) { assertEqualsEnum(__FILE__, __LINE__, InternalError::TYPE, e.type); } catch (...) { assertThrowFail(__FILE__, __LINE__); } } while (false) diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 652388d917a..674a53def71 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -538,12 +538,12 @@ class TestGarbage : public TestFixture { void garbageCode23() { //garbage code : don't crash (#3481) - ASSERT_THROW_EQUALS(checkCode("{\n" - " if (1) = x\n" - " else abort s[2]\n" - "}"), - InternalError, - "syntax error"); + ASSERT_THROW_INTERNAL_EQUALS(checkCode("{\n" + " if (1) = x\n" + " else abort s[2]\n" + "}"), + SYNTAX, + "syntax error"); } void garbageCode24() { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index f39d0cf2b5f..d52a2b3022e 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -1840,7 +1840,14 @@ class TestSymbolDatabase : public TestFixture { // three elements: varId 0 also counts via a fake-entry ASSERT(v && db->variableList().size() == 3); - ASSERT_THROW(db->getVariableFromVarId(3), std::out_of_range); + // TODO: we should provide our own error message +#ifdef _MSC_VER + ASSERT_THROW_EQUALS_2(db->getVariableFromVarId(3), std::out_of_range, "invalid vector subscript"); +#elif !defined(_LIBCPP_VERSION) + ASSERT_THROW_EQUALS_2(db->getVariableFromVarId(3), std::out_of_range, "vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)"); +#else + ASSERT_THROW_EQUALS_2(db->getVariableFromVarId(3), std::out_of_range, "vector"); +#endif } void hasRegularFunction() { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index e5f6fb22903..51ef2f51812 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -7135,19 +7135,19 @@ class TestTokenizer : public TestFixture { ASSERT_NO_THROW(tokenizeAndStringify("enum { E = int{} };")); ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int a() { b((c)return 0) }"), SYNTAX, "syntax error"); - ASSERT_THROW_EQUALS(tokenizeAndStringify("int f() { MACRO(x) return 0; }"), - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); - - ASSERT_THROW_EQUALS(tokenizeAndStringify("void f(int i) {\n" // #11770 - " if (i == 0) {}\n" - " else if (i == 1) {}\n" - " else\n" - " MACRO(i)\n" - "}\n" - "void g() {}\n"), - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int f() { MACRO(x) return 0; }"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); + + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("void f(int i) {\n" // #11770 + " if (i == 0) {}\n" + " else if (i == 1) {}\n" + " else\n" + " MACRO(i)\n" + "}\n" + "void g() {}\n"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); ASSERT_NO_THROW(tokenizeAndStringify("void f(int i) {\n" " if (i == 0) {}\n" " else if (i == 1) {}\n" @@ -7156,45 +7156,45 @@ class TestTokenizer : public TestFixture { "}\n" "void g() {}\n")); - ASSERT_THROW_EQUALS(tokenizeAndStringify("class C : public QObject {\n" // #11770 - " struct S { static void g() {} };\n" - "private Q_SLOTS:\n" - " void f() { S::g(); }\n" - "};\n"), - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it."); - ASSERT_THROW_EQUALS(tokenizeAndStringify("class C : public QObject {\n" - " struct S { static void g() {} };\n" - "private slots:\n" - " void f() { S::g(); }\n" - "};\n"), - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If slots is a macro then please configure it."); - - ASSERT_THROW_EQUALS(tokenizeAndStringify("namespace U_ICU_ENTRY_POINT_RENAME(icu) { }\n" - "namespace icu = U_ICU_ENTRY_POINT_RENAME(icu);\n" - "namespace U_ICU_ENTRY_POINT_RENAME(icu) {\n" - " class BreakIterator;\n" - "}\n" - "typedef int UStringCaseMapper(icu::BreakIterator* iter);\n"), - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If U_ICU_ENTRY_POINT_RENAME is a macro then please configure it."); - - ASSERT_THROW_EQUALS(tokenizeAndStringify("void f() { MACRO(x(), y(), \"abc\", z(); ok = true); }\n"), // #12006 - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); - - ASSERT_THROW_EQUALS(tokenizeAndStringify("int (*f) MACRO((void *));\n"), // #12010 - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); - - ASSERT_THROW_EQUALS(tokenizeAndStringify("struct S { int a[2] PACKED; };\n"), - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If PACKED is a macro then please configure it."); - - ASSERT_THROW_EQUALS(tokenizeAndStringify("MACRO(a, b,,)\n"), - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("class C : public QObject {\n" // #11770 + " struct S { static void g() {} };\n" + "private Q_SLOTS:\n" + " void f() { S::g(); }\n" + "};\n"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it."); + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("class C : public QObject {\n" + " struct S { static void g() {} };\n" + "private slots:\n" + " void f() { S::g(); }\n" + "};\n"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If slots is a macro then please configure it."); + + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("namespace U_ICU_ENTRY_POINT_RENAME(icu) { }\n" + "namespace icu = U_ICU_ENTRY_POINT_RENAME(icu);\n" + "namespace U_ICU_ENTRY_POINT_RENAME(icu) {\n" + " class BreakIterator;\n" + "}\n" + "typedef int UStringCaseMapper(icu::BreakIterator* iter);\n"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If U_ICU_ENTRY_POINT_RENAME is a macro then please configure it."); + + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("void f() { MACRO(x(), y(), \"abc\", z(); ok = true); }\n"), // #12006 + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); + + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int (*f) MACRO((void *));\n"), // #12010 + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); + + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("struct S { int a[2] PACKED; };\n"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If PACKED is a macro then please configure it."); + + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("MACRO(a, b,,)\n"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If MACRO is a macro then please configure it."); ASSERT_THROW_INTERNAL(tokenizeAndStringify("{ for (()()) }"), SYNTAX); // #11643 @@ -7735,9 +7735,9 @@ class TestTokenizer : public TestFixture { } void checkConfiguration() { - ASSERT_THROW_EQUALS(checkConfig("void f() { DEBUG(x();y()); }"), - InternalError, - "There is an unknown macro here somewhere. Configuration is required. If DEBUG is a macro then please configure it."); + ASSERT_THROW_INTERNAL_EQUALS(checkConfig("void f() { DEBUG(x();y()); }"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If DEBUG is a macro then please configure it."); } void unknownType() { // #8952