Merge pull request #56 from hugovdm/errorhandling

Explore Usage-related error codes, address #36
This commit is contained in:
Hugo van der Merwe 2020-08-11 21:09:20 +02:00 committed by GitHub
commit d5d7fdccfe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 4 deletions

View file

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

View file

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

View file

@ -55,6 +55,7 @@ class NumberFormatterApiTest : public IntlTestWithFieldPosition {
void unitMeasure();
void unitCompoundMeasure();
void unitUsage();
void unitUsageErrorCodes();
void unitCurrency();
void unitPercent();
void percentParity();

View file

@ -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",