diff --git a/icu4c/source/test/intltest/localematchertest.cpp b/icu4c/source/test/intltest/localematchertest.cpp index 2996f8aa706d..639b58682209 100644 --- a/icu4c/source/test/intltest/localematchertest.cpp +++ b/icu4c/source/test/intltest/localematchertest.cpp @@ -457,7 +457,7 @@ void LocaleMatcherTest::testResolvedLocale() { namespace { -bool toInvariant(const UnicodeString &s, CharString &inv, ErrorCode &errorCode) { +bool toInvariant(const UnicodeString &s, CharString &inv, IcuTestErrorCode &errorCode) { if (errorCode.isSuccess()) { inv.clear().appendInvariantChars(s, errorCode); return errorCode.isSuccess(); @@ -477,7 +477,7 @@ bool getSuffixAfterPrefix(const UnicodeString &s, int32_t limit, bool getInvariantSuffixAfterPrefix(const UnicodeString &s, int32_t limit, const UnicodeString &prefix, CharString &suffix, - ErrorCode &errorCode) { + IcuTestErrorCode &errorCode) { UnicodeString u_suffix; return getSuffixAfterPrefix(s, limit, prefix, u_suffix) && toInvariant(u_suffix, suffix, errorCode); diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 3244077d796e..c855e3e29277 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -3766,7 +3766,7 @@ void NumberFormatTest::TestMismatchedCurrencyFormatFail() { df->setLenient(false); { Formattable result; - ErrorCode failStatus; + UErrorCode failStatus = U_ZERO_ERROR; df->parse(u"1.23\u20AC", result, failStatus); assertEquals("Should fail to parse", U_INVALID_FORMAT_ERROR, failStatus); } diff --git a/icu4c/source/tools/ctestfw/tstdtmod.cpp b/icu4c/source/tools/ctestfw/tstdtmod.cpp index 649065164b47..5a8e1ef73659 100644 --- a/icu4c/source/tools/ctestfw/tstdtmod.cpp +++ b/icu4c/source/tools/ctestfw/tstdtmod.cpp @@ -25,6 +25,22 @@ IcuTestErrorCode::~IcuTestErrorCode() { } } +UErrorCode IcuTestErrorCode::reset() { + UErrorCode code = errorCode; + errorCode = U_ZERO_ERROR; + return code; +} + +void IcuTestErrorCode::assertSuccess() const { + if(isFailure()) { + handleFailure(); + } +} + +const char* IcuTestErrorCode::errorName() const { + return u_errorName(errorCode); +} + UBool IcuTestErrorCode::errIfFailureAndReset() { if(isFailure()) { errlog(false, u"expected success", nullptr); @@ -103,10 +119,11 @@ UBool IcuTestErrorCode::expectErrorAndReset(UErrorCode expectedError, const char } void IcuTestErrorCode::setScope(const char* message) { - scopeMessage.remove().append({ message, -1, US_INV }); + UnicodeString us(message, -1, US_INV); + scopeMessage = us; } -void IcuTestErrorCode::setScope(const UnicodeString& message) { +void IcuTestErrorCode::setScope(std::u16string_view message) { scopeMessage = message; } @@ -114,12 +131,12 @@ void IcuTestErrorCode::handleFailure() const { errlog(false, u"(handleFailure)", nullptr); } -void IcuTestErrorCode::errlog(UBool dataErr, const UnicodeString& mainMessage, const char* extraMessage) const { +void IcuTestErrorCode::errlog(UBool dataErr, std::u16string_view mainMessage, const char* extraMessage) const { UnicodeString msg(testName, -1, US_INV); msg.append(u' ').append(mainMessage); msg.append(u" but got error: ").append(UnicodeString(errorName(), -1, US_INV)); - if (!scopeMessage.isEmpty()) { + if (!scopeMessage.empty()) { msg.append(u" scope: ").append(scopeMessage); } diff --git a/icu4c/source/tools/ctestfw/unicode/testlog.h b/icu4c/source/tools/ctestfw/unicode/testlog.h index 01d584c6ec81..3951e729b748 100644 --- a/icu4c/source/tools/ctestfw/unicode/testlog.h +++ b/icu4c/source/tools/ctestfw/unicode/testlog.h @@ -13,17 +13,11 @@ #ifndef U_TESTFW_TESTLOG #define U_TESTFW_TESTLOG +#include #include #include "unicode/utypes.h" #include "unicode/testtype.h" -#if U_SHOW_CPLUSPLUS_API - -#include "unicode/errorcode.h" -#include "unicode/unistr.h" - -#endif // U_SHOW_CPLUSPLUS_API - /** Facilitates internal logging of data driven test service * It would be interesting to develop this into a full * fledged control system as in Java. @@ -31,20 +25,34 @@ class T_CTEST_EXPORT_API TestLog { public: virtual ~TestLog(); - virtual void errln(std::u16string_view message ) = 0; - virtual void logln(std::u16string_view message ) = 0; - virtual void dataerrln(std::u16string_view message ) = 0; + virtual void errln(std::u16string_view message) = 0; + virtual void logln(std::u16string_view message) = 0; + virtual void dataerrln(std::u16string_view message) = 0; virtual const char* getTestDataPath(UErrorCode& err) = 0; }; -#if U_SHOW_CPLUSPLUS_API +// Note: The IcuTestErrorCode used to be a subclass of ErrorCode, but that made it not usable for +// unit tests that work without U_SHOW_CPLUSPLUS_API. +// So instead we *copy* the ErrorCode API. -class T_CTEST_EXPORT_API IcuTestErrorCode : public ErrorCode { +class T_CTEST_EXPORT_API IcuTestErrorCode { public: IcuTestErrorCode(TestLog &callingTestClass, const char *callingTestName) - : testClass(callingTestClass), testName(callingTestName), scopeMessage() {} + : errorCode(U_ZERO_ERROR), + testClass(callingTestClass), testName(callingTestName), scopeMessage() {} virtual ~IcuTestErrorCode(); + // ErrorCode API + operator UErrorCode & () { return errorCode; } + operator UErrorCode * () { return &errorCode; } + UBool isSuccess() const { return U_SUCCESS(errorCode); } + UBool isFailure() const { return U_FAILURE(errorCode); } + UErrorCode get() const { return errorCode; } + void set(UErrorCode value) { errorCode=value; } + UErrorCode reset(); + void assertSuccess() const; + const char* errorName() const; + // Returns true if isFailure(). UBool errIfFailureAndReset(); UBool errIfFailureAndReset(const char *fmt, ...); @@ -55,19 +63,18 @@ class T_CTEST_EXPORT_API IcuTestErrorCode : public ErrorCode { /** Sets an additional message string to be appended to failure output. */ void setScope(const char* message); - void setScope(const UnicodeString& message); + void setScope(std::u16string_view message); protected: - virtual void handleFailure() const override; + virtual void handleFailure() const; private: + UErrorCode errorCode; TestLog &testClass; const char *const testName; - UnicodeString scopeMessage; + std::u16string scopeMessage; - void errlog(UBool dataErr, const UnicodeString& mainMessage, const char* extraMessage) const; + void errlog(UBool dataErr, std::u16string_view mainMessage, const char* extraMessage) const; }; -#endif // U_SHOW_CPLUSPLUS_API - #endif