Demonstrate current currency formatting in unit tests.
This CL also adds the number_format_compact_web_test blaze target in
order to automatically run the Dart tests in the file by that name.
PiperOrigin-RevId: 278353158
diff --git a/test/number_format_compact_test.dart b/test/number_format_compact_test.dart
index 4dff6b1..c89aa6a 100644
--- a/test/number_format_compact_test.dart
+++ b/test/number_format_compact_test.dart
@@ -81,6 +81,10 @@
testCurrency('it', 4420000, '4,42\u00A0Mio\u00A0\$', '4\u00A0Mio\u00A0\$',
currency: 'USD');
+ testCurrency('he', 335, '\u200F335 ₪', '\u200F300 ₪',
+ reason: 'TODO(b/36488375): Short format throws away significant digits '
+ 'without good reason.');
+
test('Explicit non-default symbol with compactCurrency', () {
var format = NumberFormat.compactCurrency(locale: 'ja', symbol: '()');
var result = format.format(98765);
@@ -88,19 +92,24 @@
});
}
+/// Tests for [NumberFormat.compactSimpleCurrency] and
+/// [Numberformat.compactCurrency]. For `compactCurrency`, it also passes the
+/// `symbol` parameter after which the result is expected to be the same as for
+/// `compactSimpleCurrency`. The `expectedShort` string is compared to the
+/// output of the formatters with significantDigits set to `1`.
void testCurrency(
String locale, num number, String expected, String expectedShort,
- {String currency}) {
+ {String currency, String reason}) {
test('Compact simple currency for $locale, $number', () {
var format =
NumberFormat.compactSimpleCurrency(locale: locale, name: currency);
var result = format.format(number);
- expect(result, expected);
+ expect(result, expected, reason: '$reason');
var shortFormat =
NumberFormat.compactSimpleCurrency(locale: locale, name: currency);
shortFormat.significantDigits = 1;
var shortResult = shortFormat.format(number);
- expect(shortResult, expectedShort);
+ expect(shortResult, expectedShort, reason: 'shortFormat: $reason');
});
test('Compact currency for $locale, $number', () {
var symbols = {
@@ -108,6 +117,7 @@
'en_US': r'$',
'ru': 'руб.',
'it': '€',
+ 'he': '₪',
'CAD': r'$',
'USD': r'$'
};
@@ -115,12 +125,12 @@
var format = NumberFormat.compactCurrency(
locale: locale, name: currency, symbol: symbol);
var result = format.format(number);
- expect(result, expected);
+ expect(result, expected, reason: '$reason');
var shortFormat = NumberFormat.compactCurrency(
locale: locale, name: currency, symbol: symbol);
shortFormat.significantDigits = 1;
var shortResult = shortFormat.format(number);
- expect(shortResult, expectedShort);
+ expect(shortResult, expectedShort, reason: 'shortFormat: $reason');
});
}
diff --git a/test/number_format_compact_web_test.dart b/test/number_format_compact_web_test.dart
index 8ec50d8..46d608f 100644
--- a/test/number_format_compact_web_test.dart
+++ b/test/number_format_compact_web_test.dart
@@ -1,14 +1,16 @@
-/// Tests for ECMAScript compact format numbers (e.g. 1.2M instead of 1200000).
+/// Tests for compact number formatting in pure Dart and in ECMAScript.
///
-/// These tests check that the test cases match what ECMAScript produces. They
-/// are not testing the package:intl implementation, they only help verify
-/// consistent behaviour across platforms.
+/// TODO(b/36488375): run all these tests against both implementations to prove
+/// consistency when the bug is fixed. Also fix documentation and perhaps
+/// merge tests: these tests currently also touch non-compact currency
+/// formatting.
/// We use @Tags rather than @TestOn to be able to specify something that can be
/// ignored when using a build system that can't read dart_test.yaml. This
/// depends on https://github.com/tc39/proposal-unified-intl-numberformat.
@Tags(const ['unifiedNumberFormat'])
+import 'package:intl/intl.dart' as intl;
import 'package:js/js_util.dart' as js;
import 'package:test/test.dart';
@@ -18,6 +20,59 @@
main() {
testdata35.compactNumberTestData.forEach(validate);
more_testdata.cldr35CompactNumTests.forEach(validateMore);
+
+ test('RTL currency formatting', () {
+ var basic = intl.NumberFormat.currency(locale: 'he');
+ expect(basic.format(1234), '\u200F1,234.00 ILS');
+ basic = intl.NumberFormat.currency(locale: 'he', symbol: '₪');
+ expect(basic.format(1234), '\u200F1,234.00 ₪');
+ expect(ecmaFormatNumber('he', 1234, style: 'currency', currency: 'ILS'),
+ '\u200F1,234.00 ₪');
+
+ var compact = intl.NumberFormat.compactCurrency(locale: 'he');
+ // Awkward:
+ expect(compact.format(1234), 'ILS \u200F1.23K');
+ compact = intl.NumberFormat.compactCurrency(locale: 'he', symbol: '₪');
+ // Awkward:
+ expect(compact.format(1234), '₪ \u200F1.23K');
+ // ECMAScript skips the RTL character for notation:'compact':
+ expect(
+ ecmaFormatNumber('he', 1234,
+ style: 'currency', currency: 'ILS', notation: 'compact'),
+ '₪ 1.2K');
+ // short/long compactDisplay doesn't change anything here:
+ expect(
+ ecmaFormatNumber('he', 1234,
+ style: 'currency',
+ currency: 'ILS',
+ notation: 'compact',
+ compactDisplay: 'short'),
+ '₪ 1.2K');
+ expect(
+ ecmaFormatNumber('he', 1234,
+ style: 'currency',
+ currency: 'ILS',
+ notation: 'compact',
+ compactDisplay: 'long'),
+ '₪ 1.2K');
+
+ var compactSimple = intl.NumberFormat.compactSimpleCurrency(locale: 'he');
+ expect(compactSimple.format(1234), '₪ \u200F1.23K');
+ });
+}
+
+String ecmaFormatNumber(String locale, num number,
+ {String style: null,
+ String currency: null,
+ String notation: null,
+ String compactDisplay: null}) {
+ var options = js.newObject();
+ if (notation != null) js.setProperty(options, 'notation', notation);
+ if (compactDisplay != null)
+ js.setProperty(options, 'compactDisplay', compactDisplay);
+ if (style != null) js.setProperty(options, 'style', style);
+ if (currency != null) js.setProperty(options, 'currency', currency);
+ return js.callMethod(number, 'toLocaleString', [locale, options]);
}
var ecmaProblemLocalesShort = [
@@ -47,14 +102,11 @@
print("Skipping problem locale '$locale' for SHORT compact number tests");
return;
}
- var options = js.newObject();
- js.setProperty(options, 'notation', 'compact');
test('Validate $locale SHORT', () {
for (var data in expected) {
var number = num.parse(data.first);
- expect(
- js.callMethod(number, 'toLocaleString', [locale, options]), data[1]);
+ expect(ecmaFormatNumber(locale, number, notation: 'compact'), data[1]);
}
});
}
@@ -64,15 +116,14 @@
print("Skipping problem locale '$locale' for LONG compact number tests");
return;
}
- var options = js.newObject();
- js.setProperty(options, 'notation', 'compact');
- js.setProperty(options, 'compactDisplay', 'long');
test('Validate $locale LONG', () {
for (var data in expected) {
var number = num.parse(data.first);
expect(
- js.callMethod(number, 'toLocaleString', [locale, options]), data[2]);
+ ecmaFormatNumber(locale, number,
+ notation: 'compact', compactDisplay: 'long'),
+ data[2]);
}
});
}