ICU-10675 Documentation wrong on ICU4C ListFormatter

This commit is contained in:
Mihai Nita 2019-02-15 10:42:54 -08:00 committed by Markus Scherer
parent 06a8de0f0a
commit f78e1a53d6
7 changed files with 92 additions and 13 deletions

3
.gitignore vendored
View file

@ -179,6 +179,7 @@ icu4c/source/samples/break/release
icu4c/source/samples/break/x64
icu4c/source/samples/break/x86
icu4c/source/samples/cal/*.d
icu4c/source/samples/cal/*.o
icu4c/source/samples/cal/*.pdb
icu4c/source/samples/cal/*.vcxproj.user
icu4c/source/samples/cal/Debug
@ -226,6 +227,7 @@ icu4c/source/samples/csdet/release
icu4c/source/samples/csdet/x64
icu4c/source/samples/csdet/x86
icu4c/source/samples/date/*.d
icu4c/source/samples/date/*.o
icu4c/source/samples/date/*.pdb
icu4c/source/samples/date/*.vcxproj.user
icu4c/source/samples/date/Debug
@ -656,6 +658,7 @@ icu4c/source/tools/ctestfw/release
icu4c/source/tools/ctestfw/x64
icu4c/source/tools/ctestfw/x86
icu4c/source/tools/escapesrc/*.d
icu4c/source/tools/escapesrc/*.o
icu4c/source/tools/escapesrc/Makefile
icu4c/source/tools/escapesrc/output-*.cpp
icu4c/source/tools/genbrk/*.d

View file

@ -188,7 +188,7 @@ class U_I18N_API ListFormatter : public UObject{
* Creates a ListFormatter appropriate for a locale and style.
*
* @param locale The locale.
* @param style the style, either "standard", "duration", or "duration-short"
* @param style the style, either "standard", "or", "unit", "unit-narrow", or "unit-short"
* @param errorCode ICU error code, set if no data available for the given locale.
* @return A ListFormatter object created from internal data derived from
* CLDR data.

View file

@ -573,6 +573,42 @@ void ListFormatterTest::TestFormattedValue() {
}
}
void ListFormatterTest::DoTheRealListStyleTesting(Locale locale,
UnicodeString items[], int itemCount,
const char* style, const char* expected, IcuTestErrorCode status) {
LocalPointer<ListFormatter> formatter(
ListFormatter::createInstance(locale, style, status));
UnicodeString actualResult;
formatter->format(items, itemCount, actualResult, status);
assertEquals(style, UnicodeString(expected), actualResult);
}
void ListFormatterTest::TestDifferentStyles() {
Locale locale("fr");
UnicodeString input[4] = { u"rouge", u"jaune", u"bleu", u"vert" };
IcuTestErrorCode status(*this, "TestDifferentStyles()");
DoTheRealListStyleTesting(locale, input, 4, "standard", "rouge, jaune, bleu et vert", status);
DoTheRealListStyleTesting(locale, input, 4, "or", "rouge, jaune, bleu ou vert", status);
DoTheRealListStyleTesting(locale, input, 4, "unit", "rouge, jaune, bleu et vert", status);
DoTheRealListStyleTesting(locale, input, 4, "unit-narrow", "rouge jaune bleu vert", status);
DoTheRealListStyleTesting(locale, input, 4, "unit-short", "rouge, jaune, bleu et vert", status);
}
void ListFormatterTest::TestBadStylesFail() {
Locale locale("fr");
const char * badStyles[4] = { "", "duration", "duration-short", "something-clearly-wrong" };
IcuTestErrorCode status(*this, "TestBadStylesFail()");
for (int i = 0; i < 4; ++i) {
LocalPointer<ListFormatter> formatter(ListFormatter::createInstance(locale, badStyles[i], status));
if (!status.expectErrorAndReset(U_MISSING_RESOURCE_ERROR, "style \"%s\"", badStyles[i])) {
// Do nothing, expectErrorAndReset already reports the error
}
}
}
void ListFormatterTest::runIndexedTest(int32_t index, UBool exec,
const char* &name, char* /*par */) {
@ -619,6 +655,12 @@ void ListFormatterTest::runIndexedTest(int32_t index, UBool exec,
case 21: name = "TestFormattedValue";
if (exec) TestFormattedValue();
break;
case 22: name = "TestDifferentStyles";
if (exec) TestDifferentStyles();
break;
case 23: name = "TestBadStylesFail";
if (exec) TestBadStylesFail();
break;
default: name = ""; break;
}
}

View file

@ -53,6 +53,8 @@ class ListFormatterTest : public IntlTestWithFieldPosition {
void TestFieldPositionIteratorWith2ItemsPatternShift();
void TestFieldPositionIteratorWith3ItemsPatternShift();
void TestFormattedValue();
void TestDifferentStyles();
void TestBadStylesFail();
private:
void CheckFormatting(
@ -106,6 +108,13 @@ class ListFormatterTest : public IntlTestWithFieldPosition {
UnicodeString four,
UnicodeString results[4],
const char* testName);
void DoTheRealListStyleTesting(
Locale locale,
UnicodeString items[],
int32_t itemCount,
const char* style,
const char* expected,
IcuTestErrorCode status);
private:
// Reused test data.

View file

@ -47,33 +47,40 @@ final public class ListFormatter {
@Deprecated
public enum Style {
/**
* Standard style.
* Standard, conjunction style.
* @internal
* @deprecated This API is ICU internal only.
*/
@Deprecated
STANDARD("standard"),
/**
* Style for full durations
* Disjunction style.
* @internal
* @deprecated This API is ICU internal only.
*/
@Deprecated
DURATION("unit"),
OR("or"),
/**
* Style for durations in abbrevated form
* Style for full units
* @internal
* @deprecated This API is ICU internal only.
*/
@Deprecated
DURATION_SHORT("unit-short"),
UNIT("unit"),
/**
* Style for durations in narrow form
* Style for units in abbrevated form
* @internal
* @deprecated This API is ICU internal only.
*/
@Deprecated
DURATION_NARROW("unit-narrow");
UNIT_SHORT("unit-short"),
/**
* Style for units in narrow form
* @internal
* @deprecated This API is ICU internal only.
*/
@Deprecated
UNIT_NARROW("unit-narrow");
private final String name;

View file

@ -156,21 +156,21 @@ public class MeasureFormat extends UFormat {
*
* @stable ICU 53
*/
WIDE(ListFormatter.Style.DURATION, UnitWidth.FULL_NAME, UnitWidth.FULL_NAME),
WIDE(ListFormatter.Style.UNIT, UnitWidth.FULL_NAME, UnitWidth.FULL_NAME),
/**
* Abbreviate when possible.
*
* @stable ICU 53
*/
SHORT(ListFormatter.Style.DURATION_SHORT, UnitWidth.SHORT, UnitWidth.ISO_CODE),
SHORT(ListFormatter.Style.UNIT_SHORT, UnitWidth.SHORT, UnitWidth.ISO_CODE),
/**
* Brief. Use only a symbol for the unit when possible.
*
* @stable ICU 53
*/
NARROW(ListFormatter.Style.DURATION_NARROW, UnitWidth.NARROW, UnitWidth.SHORT),
NARROW(ListFormatter.Style.UNIT_NARROW, UnitWidth.NARROW, UnitWidth.SHORT),
/**
* Identical to NARROW except when formatMeasures is called with an hour and minute; minute and
@ -179,7 +179,7 @@ public class MeasureFormat extends UFormat {
*
* @stable ICU 53
*/
NUMERIC(ListFormatter.Style.DURATION_NARROW, UnitWidth.NARROW, UnitWidth.SHORT),
NUMERIC(ListFormatter.Style.UNIT_NARROW, UnitWidth.NARROW, UnitWidth.SHORT),
/**
* The default format width for getCurrencyFormat(), which is to show the symbol for currency
@ -189,7 +189,7 @@ public class MeasureFormat extends UFormat {
* @deprecated ICU 61 This API is ICU internal only.
*/
@Deprecated
DEFAULT_CURRENCY(ListFormatter.Style.DURATION, UnitWidth.FULL_NAME, UnitWidth.SHORT);
DEFAULT_CURRENCY(ListFormatter.Style.UNIT, UnitWidth.FULL_NAME, UnitWidth.SHORT);
private final ListFormatter.Style listFormatterStyle;

View file

@ -187,6 +187,24 @@ public class ListFormatterTest extends TestFmwk {
assertEquals("bug 9946", "{0}, {1}, and {2}", listFormatter.format("{0}", "{1}", "{2}"));
}
void DoTheRealListStyleTesting(ULocale locale, String items[], ListFormatter.Style style, String expected) {
ListFormatter listFormatter = ListFormatter.getInstance(locale, style);
assertEquals("Style \"" + style + "\"", expected, listFormatter.format(items));
}
@Test
public void TestDifferentStyles() {
ULocale locale = ULocale.FRENCH;
String[] input = { "rouge", "jaune", "bleu", "vert" };
DoTheRealListStyleTesting(locale, input, ListFormatter.Style.STANDARD, "rouge, jaune, bleu et vert");
DoTheRealListStyleTesting(locale, input, ListFormatter.Style.OR, "rouge, jaune, bleu ou vert");
DoTheRealListStyleTesting(locale, input, ListFormatter.Style.UNIT, "rouge, jaune, bleu et vert");
DoTheRealListStyleTesting(locale, input, ListFormatter.Style.UNIT_NARROW, "rouge jaune bleu vert");
DoTheRealListStyleTesting(locale, input, ListFormatter.Style.UNIT_SHORT, "rouge, jaune, bleu et vert");
}
private boolean isDefaultLocaleEnglishLike() {
ULocale defaultLocale = ULocale.getDefault(ULocale.Category.FORMAT);
return defaultLocale.equals(ULocale.ENGLISH) || defaultLocale.equals(ULocale.US);