| // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| @TestOn('browser') |
| library; |
| |
| import 'dart:js_interop'; |
| import 'dart:math'; |
| |
| import 'package:intl4x/number_format.dart'; |
| import 'package:test/test.dart'; |
| |
| import '../utils.dart'; |
| |
| @JS('Intl.NumberFormat') |
| extension type NumberFormatJS._(JSObject _) implements JSObject { |
| external factory NumberFormatJS([JSArray<JSString> locale, JSAny options]); |
| external String format(JSAny num); |
| } |
| |
| JSAny generateProperties(Map<String, Object> properties) => properties.jsify()!; |
| |
| void main() { |
| testWithFormatting('Some fuzzy testing', tags: ['ecmaUnsupported'], () { |
| final seed = Random().nextInt(1 << 31); |
| print('Seed: $seed'); |
| final random = Random(seed); |
| |
| final numbers = [3.14, 5, 20000, 3, 4.2214, 3.99999, 20000.0001]; |
| final locales = [ |
| Locale.parse('en-US'), |
| Locale.parse('de-DE'), |
| Locale.parse('zh-TW'), |
| ]; |
| final options = <(Object, NumberFormat Function(Locale locale), JSAny)>[ |
| ( |
| {'minimumFractionDigits': 2}, |
| (locale) => |
| NumberFormat(digits: const Digits.withFractionDigits(minimum: 2)), |
| generateProperties({'minimumFractionDigits': 2}), |
| ), |
| ( |
| 'useGrouping', |
| (locale) => NumberFormat(useGrouping: Grouping.always), |
| generateProperties({'useGrouping': true}), |
| ), |
| ( |
| 'USD', |
| (locale) => NumberFormat.currency(currency: 'USD'), |
| generateProperties({'style': 'currency', 'currency': 'USD'}), |
| ), |
| ( |
| 'percent', |
| (locale) => NumberFormat.percent(), |
| generateProperties({'style': 'percent'}), |
| ), |
| ]; |
| |
| List<(num, Locale, (Object, NumberFormat Function(Locale locale), JSAny))> |
| selectIndicesFrom(int length) => List.generate( |
| length, |
| (index) => ( |
| numbers[random.nextInt(numbers.length)], |
| locales[random.nextInt(locales.length)], |
| options[random.nextInt(options.length)], |
| ), |
| ).toSet().toList(); |
| |
| for (final (number, locale, (desc, formatter, object)) in selectIndicesFrom( |
| 1000, |
| )) { |
| final jsFormat = NumberFormatJS( |
| [locale.toLanguageTag().toJS].toJS, |
| object, |
| ).format(number.toJS); |
| final dartFormat = formatter(locale).format(number); |
| expect( |
| dartFormat, |
| jsFormat, |
| reason: 'With number $number, locale $locale, options $desc', |
| ); |
| } |
| }); |
| } |