mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-07 06:25:30 +00:00
Merge pull request #56 from hugovdm/errorhandling
Explore Usage-related error codes, address #36
This commit is contained in:
commit
d5d7fdccfe
4 changed files with 27 additions and 4 deletions
|
@ -230,7 +230,8 @@ NumberFormatterImpl::macrosToMicroGenerator(const MacroProps& macros, bool safe,
|
|||
// Unit Preferences and Conversions as our first step
|
||||
if (macros.usage.isSet()) {
|
||||
if (!isCldrUnit) {
|
||||
// We only support "usage" when the input unit is a CLDR Unit.
|
||||
// We only support "usage" when the input unit is specified, and is
|
||||
// a CLDR Unit.
|
||||
status = U_ILLEGAL_ARGUMENT_ERROR;
|
||||
return nullptr;
|
||||
}
|
||||
|
|
|
@ -293,9 +293,7 @@ int32_t getPreferenceMetadataIndex(const MaybeStackVector<UnitPreferenceMetadata
|
|||
} else if (uprv_strcmp(desired.usage.data(), "default") != 0) {
|
||||
desired.usage.truncate(0).append("default", status);
|
||||
} else {
|
||||
// TODO(icu-units/icu#36): reconsider consistency of errors.
|
||||
// Currently this U_MISSING_RESOURCE_ERROR propagates when a
|
||||
// U_NUMBER_SKELETON_SYNTAX_ERROR might be much more intuitive.
|
||||
// "default" is not supposed to be missing for any valid category.
|
||||
status = U_MISSING_RESOURCE_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
@ -310,6 +308,7 @@ int32_t getPreferenceMetadataIndex(const MaybeStackVector<UnitPreferenceMetadata
|
|||
idx = binarySearch(metadata, desired, &foundCategory, &foundUsage, &foundRegion, status);
|
||||
}
|
||||
if (!foundRegion) {
|
||||
// "001" is not supposed to be missing for any valid usage.
|
||||
status = U_MISSING_RESOURCE_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -55,6 +55,7 @@ class NumberFormatterApiTest : public IntlTestWithFieldPosition {
|
|||
void unitMeasure();
|
||||
void unitCompoundMeasure();
|
||||
void unitUsage();
|
||||
void unitUsageErrorCodes();
|
||||
void unitCurrency();
|
||||
void unitPercent();
|
||||
void percentParity();
|
||||
|
|
|
@ -76,6 +76,7 @@ void NumberFormatterApiTest::runIndexedTest(int32_t index, UBool exec, const cha
|
|||
TESTCASE_AUTO(unitMeasure);
|
||||
TESTCASE_AUTO(unitCompoundMeasure);
|
||||
TESTCASE_AUTO(unitUsage);
|
||||
TESTCASE_AUTO(unitUsageErrorCodes);
|
||||
TESTCASE_AUTO(unitCurrency);
|
||||
TESTCASE_AUTO(unitPercent);
|
||||
if (!quick) {
|
||||
|
@ -750,6 +751,27 @@ void NumberFormatterApiTest::unitUsage() {
|
|||
u"0 ft");
|
||||
}
|
||||
|
||||
void NumberFormatterApiTest::unitUsageErrorCodes() {
|
||||
IcuTestErrorCode status(*this, "unitUsageErrorCodes()");
|
||||
UnlocalizedNumberFormatter unloc_formatter;
|
||||
|
||||
unloc_formatter = NumberFormatter::forSkeleton(u"unit/foobar", status);
|
||||
// This gives an error, because foobar is an invalid unit:
|
||||
status.expectErrorAndReset(U_NUMBER_SKELETON_SYNTAX_ERROR);
|
||||
|
||||
unloc_formatter = NumberFormatter::forSkeleton(u"usage/foobar", status);
|
||||
// This does not give an error, because usage is not looked up yet.
|
||||
status.errIfFailureAndReset("Expected behaviour: no immediate error for invalid usage");
|
||||
unloc_formatter.locale("en-GB").formatInt(1, status);
|
||||
// Lacking a unit results in a failure. The skeleton is "incomplete", but we
|
||||
// support adding the unit via the fluent API, so it is not an error until
|
||||
// we build the formatting pipeline itself.
|
||||
status.expectErrorAndReset(U_ILLEGAL_ARGUMENT_ERROR);
|
||||
// Adding the unit as part of the fluent chain leads to success.
|
||||
unloc_formatter.unit(MeasureUnit::getMeter()).locale("en-GB").formatInt(1, status);
|
||||
status.assertSuccess();
|
||||
}
|
||||
|
||||
void NumberFormatterApiTest::unitCompoundMeasure() {
|
||||
assertFormatDescending(
|
||||
u"Meters Per Second Short (unit that simplifies) and perUnit method",
|
||||
|
|
Loading…
Add table
Reference in a new issue