Skip to content

Commit

Permalink
ICU-22954 IcuTestErrorCode usable without U_SHOW_CPLUSPLUS_API
Browse files Browse the repository at this point in the history
  • Loading branch information
markusicu committed Dec 12, 2024
1 parent 0592666 commit 984b6ee
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
4 changes: 2 additions & 2 deletions icu4c/source/test/intltest/localematchertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/test/intltest/numfmtst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
25 changes: 21 additions & 4 deletions icu4c/source/tools/ctestfw/tstdtmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -103,23 +119,24 @@ 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;
}

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);
}

Expand Down
45 changes: 26 additions & 19 deletions icu4c/source/tools/ctestfw/unicode/testlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,46 @@
#ifndef U_TESTFW_TESTLOG
#define U_TESTFW_TESTLOG

#include <string>
#include <string_view>
#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.
*/
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, ...);
Expand All @@ -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

0 comments on commit 984b6ee

Please sign in to comment.