Skip to content

Commit

Permalink
Move definition of UMFErrorHandlingBehavior out of MessageFormatter::…
Browse files Browse the repository at this point in the history
…Builder into MessageFormatter
  • Loading branch information
catamorphism committed Sep 18, 2024
1 parent 9d0039d commit 235e646
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion icu4c/source/i18n/messageformat2_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace message2 {

MessageFormatter::Builder&
MessageFormatter::Builder::setErrorHandlingBehavior(
MessageFormatter::Builder::UMFErrorHandlingBehavior type) {
MessageFormatter::UMFErrorHandlingBehavior type) {
switch (type) {
case U_MF_STRICT: {
signalErrors = true;
Expand Down
48 changes: 25 additions & 23 deletions icu4c/source/i18n/unicode/messageformat2.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ namespace message2 {
*/
const MFDataModel& getDataModel() const;

/**
* Used in conjunction with the
* MessageFormatter::Builder::`setErrorHandlingBehavior()` method.
*
* @internal ICU 76 technology preview
* @deprecated This API is for technology preview only.
*/
typedef enum UMFErrorHandlingBehavior {
/**
* Suppress errors and return best-effort output.
*
* @internal ICU 76 technology preview
* @deprecated This API is for technology preview only.
*/
U_MF_BEST_EFFORT = 0,
/**
* Signal all MessageFormat errors using the UErrorCode
* argument.
*
* @internal ICU 76 technology preview
* @deprecated This API is for technology preview only.
*/
U_MF_STRICT
} UMFErrorHandlingBehavior;

/**
* The mutable Builder class allows each part of the MessageFormatter to be initialized
* separately; calling its `build()` method yields an immutable MessageFormatter.
Expand Down Expand Up @@ -171,29 +196,6 @@ namespace message2 {

void clearState();
public:
/**
* Used in conjunction with the `setErrorHandlingBehavior()` method.
*
* @internal ICU 76 technology preview
* @deprecated This API is for technology preview only.
*/
typedef enum UMFErrorHandlingBehavior {
/**
* Suppress errors and return best-effort output.
*
* @internal ICU 76 technology preview
* @deprecated This API is for technology preview only.
*/
U_MF_BEST_EFFORT = 0,
/**
* Signal all MessageFormat errors using the UErrorCode
* argument.
*
* @internal ICU 76 technology preview
* @deprecated This API is for technology preview only.
*/
U_MF_STRICT
} UMFErrorHandlingBehavior;
/**
* Sets the locale to use for formatting.
*
Expand Down
16 changes: 8 additions & 8 deletions icu4c/source/test/intltest/messageformat2test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ void TestMessageFormat2::testFormatterAPI() {
// if there's a syntax error
UnicodeString pattern = "{{}";
MessageFormatter::Builder mfBuilder(errorCode);
mfBuilder.setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_BEST_EFFORT); // This shouldn't matter, since there's a syntax error
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_BEST_EFFORT); // This shouldn't matter, since there's a syntax error
mfBuilder.setPattern(pattern, parseError, errorCode);
MessageFormatter mf = mfBuilder.build(errorCode);
U_ASSERT(errorCode == U_MF_SYNTAX_ERROR);

/*
Parsing is done when setPattern() is called,
so setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_STRICT) or setSuppressErrors must be called
so setErrorHandlingBehavior(MessageFormatter::U_MF_STRICT) or setSuppressErrors must be called
_before_ setPattern() to get the right behavior,
and if either method is called after setting a pattern,
setPattern() has to be called again.
*/
// Should get the same behavior with strict errors
errorCode.reset();
mfBuilder.setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_STRICT);
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_STRICT);
// Force re-parsing, as above comment
mfBuilder.setPattern(pattern, parseError, errorCode);
mf = mfBuilder.build(errorCode);
Expand All @@ -99,7 +99,7 @@ void TestMessageFormat2::testFormatterAPI() {
pattern = "{{{$x}}}";
errorCode.reset();
// Check that a pattern with a resolution error gives fallback output
mfBuilder.setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_BEST_EFFORT);
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_BEST_EFFORT);
mfBuilder.setPattern(pattern, parseError, errorCode);
mf = mfBuilder.build(errorCode);
U_ASSERT(U_SUCCESS(errorCode));
Expand All @@ -108,7 +108,7 @@ void TestMessageFormat2::testFormatterAPI() {
U_ASSERT(result == "{$x}");

// Check that we do get an error with strict errors
mfBuilder.setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_STRICT);
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_STRICT);
mf = mfBuilder.build(errorCode);
U_ASSERT(U_SUCCESS(errorCode));
result = mf.formatToString(MessageArguments(), errorCode);
Expand All @@ -118,15 +118,15 @@ void TestMessageFormat2::testFormatterAPI() {
errorCode.reset();
pattern = "hello";
mfBuilder.setPattern(pattern, parseError, errorCode);
mfBuilder.setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_BEST_EFFORT);
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_BEST_EFFORT);
mf = mfBuilder.build(errorCode);
U_ASSERT(U_SUCCESS(errorCode));
result = mf.formatToString(MessageArguments(), errorCode);
U_ASSERT(U_SUCCESS(errorCode));
U_ASSERT(result == "hello");

// Check that behavior is the same with strict errors
mfBuilder.setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_STRICT);
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_STRICT);
mf = mfBuilder.build(errorCode);
U_ASSERT(U_SUCCESS(errorCode));
result = mf.formatToString(MessageArguments(), errorCode);
Expand Down Expand Up @@ -286,7 +286,7 @@ void TestMessageFormat2::testAPICustomFunctions() {
MessageFormatter::Builder mfBuilder(errorCode);
UnicodeString result;
// This fails, because we did not provide a function registry:
MessageFormatter mf = mfBuilder.setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_STRICT)
MessageFormatter mf = mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_STRICT)
.setPattern("Hello {$name :person formality=informal}",
parseError, errorCode)
.setLocale(locale)
Expand Down
4 changes: 2 additions & 2 deletions icu4c/source/test/intltest/messageformat2test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class TestUtils {
}
// Initially, set error behavior to strict.
// We'll re-run to check for errors.
mfBuilder.setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_STRICT);
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_STRICT);
MessageFormatter mf = mfBuilder.build(errorCode);
UnicodeString result;

Expand Down Expand Up @@ -279,7 +279,7 @@ class TestUtils {
// Re-run the formatter if there was an error,
// in order to get best-effort output
errorCode.reset();
mfBuilder.setErrorHandlingBehavior(MessageFormatter::Builder::U_MF_BEST_EFFORT);
mfBuilder.setErrorHandlingBehavior(MessageFormatter::U_MF_BEST_EFFORT);
mf = mfBuilder.build(errorCode);
if (U_SUCCESS(errorCode)) {
result = mf.formatToString(MessageArguments(testCase.getArguments(), errorCode), errorCode);
Expand Down

0 comments on commit 235e646

Please sign in to comment.