ICU-5643 ucurrGetName: se uresb_getByKeyWithFallback to lookup Currency code. Add tests for fallback and default warnings, and for unknown currency code.

X-SVN-Rev: 21228
This commit is contained in:
Eric Mader 2007-03-12 19:53:42 +00:00
parent 66e1878970
commit 9818e3785e
2 changed files with 35 additions and 21 deletions

View File

@ -22,6 +22,7 @@
#include "umutex.h"
#include "ucln_in.h"
#include "uenumimp.h"
#include "uresimp.h"
//------------------------------------------------------------
// Constants
@ -479,28 +480,23 @@ ucurr_getName(const UChar* currency,
myUCharsToChars(buf, currency);
const UChar* s = NULL;
ec2 = U_ZERO_ERROR;
UResourceBundle* rb = ures_open(NULL, loc, &ec2);
// Multi-level resource inheritance fallback loop
for (;;) {
ec2 = U_ZERO_ERROR;
UResourceBundle* rb = ures_open(NULL, loc, &ec2);
rb = ures_getByKey(rb, CURRENCIES, rb, &ec2);
rb = ures_getByKey(rb, buf, rb, &ec2);
s = ures_getStringByIndex(rb, choice, len, &ec2);
ures_close(rb);
rb = ures_getByKey(rb, CURRENCIES, rb, &ec2);
// If we've succeeded we're done. Otherwise, try to fallback.
// If that fails (because we are already at root) then exit.
if (U_SUCCESS(ec2) || !fallback(loc)) {
if (ec2 == U_USING_DEFAULT_WARNING
|| (ec2 == U_USING_FALLBACK_WARNING && *ec != U_USING_DEFAULT_WARNING)) {
*ec = ec2;
}
break;
} else if (strlen(loc) == 0) {
*ec = U_USING_DEFAULT_WARNING;
} else if (*ec != U_USING_DEFAULT_WARNING) {
*ec = U_USING_FALLBACK_WARNING;
// Fetch resource with multi-level resource inheritance fallback
rb = ures_getByKeyWithFallback(rb, buf, rb, &ec2);
s = ures_getStringByIndex(rb, choice, len, &ec2);
ures_close(rb);
// If we've succeeded we're done. Otherwise, try to fallback.
// If that fails (because we are already at root) then exit.
if (U_SUCCESS(ec2)) {
if (ec2 == U_USING_DEFAULT_WARNING
|| (ec2 == U_USING_FALLBACK_WARNING && *ec != U_USING_DEFAULT_WARNING)) {
*ec = ec2;
}
}

View File

@ -1482,6 +1482,7 @@ void NumberFormatTest::TestCurrencyNames(void) {
// USD { "US$", "US Dollar" } // 04/04/1792-
UErrorCode ec = U_ZERO_ERROR;
static const UChar USD[] = {0x55, 0x53, 0x44, 0}; /*USD*/
static const UChar USX[] = {0x55, 0x53, 0x58, 0}; /*USX*/
static const UChar CAD[] = {0x43, 0x41, 0x44, 0}; /*CAD*/
static const UChar ITL[] = {0x49, 0x54, 0x4C, 0}; /*ITL*/
UBool isChoiceFormat;
@ -1518,14 +1519,31 @@ void NumberFormatTest::TestCurrencyNames(void) {
UnicodeString(ucurr_getName(CAD, "en_AU",
UCURR_SYMBOL_NAME,
&isChoiceFormat, &len, &ec)));
assertEquals("USX.getName(LONG_NAME)",
UnicodeString("USX"),
UnicodeString(ucurr_getName(USX, "en_US",
UCURR_LONG_NAME,
&isChoiceFormat, &len, &ec)));
assertSuccess("ucurr_getName", ec);
ec = U_ZERO_ERROR;
// Test that a default or fallback warning is being returned. JB 4239.
ucurr_getName(CAD, "en_US", UCURR_LONG_NAME, &isChoiceFormat,
ucurr_getName(CAD, "es_ES", UCURR_LONG_NAME, &isChoiceFormat,
&len, &ec);
assertTrue("ucurr_getName (fallback)",
U_USING_FALLBACK_WARNING == ec, TRUE);
ucurr_getName(CAD, "zh_TW", UCURR_LONG_NAME, &isChoiceFormat,
&len, &ec);
assertTrue("ucurr_getName (fallback)",
U_USING_FALLBACK_WARNING == ec, TRUE);
ucurr_getName(CAD, "en_US", UCURR_LONG_NAME, &isChoiceFormat,
&len, &ec);
assertTrue("ucurr_getName (default)",
U_USING_DEFAULT_WARNING == ec, TRUE);
ucurr_getName(CAD, "vi", UCURR_LONG_NAME, &isChoiceFormat,
&len, &ec);
assertTrue("ucurr_getName (default)",