Chromium Code Reviews| Index: components/payments/currency_formatter_unittest.cc |
| diff --git a/components/payments/currency_formatter_unittest.cc b/components/payments/currency_formatter_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6d7220bbbe060b38f4880380e946873e996e259a |
| --- /dev/null |
| +++ b/components/payments/currency_formatter_unittest.cc |
| @@ -0,0 +1,132 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/payments/currency_formatter.h" |
| + |
| +#include "base/optional.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace payments { |
| + |
| +struct TestCase { |
| + TestCase(const char* amount, |
| + const char* currency_code, |
| + const char* locale_name, |
| + const std::string& expected_amount, |
| + const char* currency_system = kIso4217CurrencySystem) |
| + : amount(amount), |
| + currency_code(currency_code), |
| + locale_name(locale_name), |
| + expected_amount(expected_amount), |
| + currency_system(currency_system) {} |
| + ~TestCase() {} |
| + |
| + const char* const amount; |
| + const char* const currency_code; |
| + const char* const locale_name; |
| + const std::string expected_amount; |
| + const base::Optional<std::string> currency_system; |
| +}; |
| + |
| +class PaymentsCurrencyFormatterTest : public testing::TestWithParam<TestCase> { |
| +}; |
| + |
| +TEST_P(PaymentsCurrencyFormatterTest, IsValidCurrencyFormat) { |
| + CurrencyFormatter formatter(GetParam().currency_code, |
| + GetParam().currency_system, |
| + GetParam().locale_name); |
| + base::string16 output_amount = formatter.Format(GetParam().amount); |
| + |
| + // Convenience so the test cases can use regular spaces. |
| + const base::string16 kSpace(base::ASCIIToUTF16(" ")); |
| + const base::string16 kNonBreakingSpace(base::UTF8ToUTF16("\xC2\xA0")); |
| + base::string16 converted; |
| + base::ReplaceChars(base::UTF8ToUTF16(GetParam().expected_amount), kSpace, |
| + kNonBreakingSpace, &converted); |
| + |
| + EXPECT_EQ(converted, output_amount) |
| + << "Failed to convert " << GetParam().amount << " (" |
| + << GetParam().currency_code << ") in " << GetParam().locale_name; |
| +} |
| + |
| +INSTANTIATE_TEST_CASE_P( |
| + CurrencyAmounts, |
| + PaymentsCurrencyFormatterTest, |
| + testing::Values( |
| + TestCase("55.00", "USD", "en_US", "$55.00"), |
| + TestCase("55.00", "USD", "en_CA", "$55.00"), |
| + TestCase("55.00", "USD", "fr_CA", "55,00 $"), |
| + TestCase("55.00", "USD", "fr_FR", "55,00 $"), |
| + TestCase("1234", "USD", "fr_FR", "1 234,00 $"), |
| + |
| + TestCase("55.5", "USD", "en_US", "$55.50"), |
| + TestCase("55", "USD", "en_US", "$55.00"), |
| + TestCase("123", "USD", "en_US", "$123.00"), |
| + TestCase("1234", "USD", "en_US", "$1,234.00"), |
| + TestCase("0.1234", "USD", "en_US", "$0.12"), |
|
please use gerrit instead
2017/01/12 19:23:24
I'm still a bit concerned that users won't know th
Mathieu
2017/01/12 20:29:57
Using a max of 10 now.
|
| + |
| + TestCase("55.00", "EUR", "en_US", "€55.00"), |
| + TestCase("55.00", "EUR", "fr_CA", "55,00 €"), |
| + TestCase("55.00", "EUR", "fr_FR", "55,00 €"), |
| + |
| + TestCase("55.00", "CAD", "en_US", "$55.00"), |
| + TestCase("55.00", "CAD", "en_CA", "$55.00"), |
| + TestCase("55.00", "CAD", "fr_CA", "55,00 $"), |
| + TestCase("55.00", "CAD", "fr_FR", "55,00 $"), |
| + |
| + TestCase("55.00", "BRL", "en_US", "R$55.00"), |
| + TestCase("55.00", "BRL", "fr_CA", "55,00 R$"), |
| + TestCase("55.00", "BRL", "pt_BR", "R$55,00"), |
| + |
| + TestCase("55.00", "RUB", "en_US", "55.00"), |
| + TestCase("55.00", "RUB", "fr_CA", "55,00"), |
| + TestCase("55.00", "RUB", "ru_RU", "55,00 ₽"), |
| + |
| + TestCase("55", "JPY", "ja_JP", "¥55"), |
| + TestCase("55.0", "JPY", "ja_JP", "¥55"), |
| + TestCase("55.00", "JPY", "ja_JP", "¥55"), |
| + TestCase("55.12", "JPY", "ja_JP", "¥55"), |
| + TestCase("55.49", "JPY", "ja_JP", "¥55"), |
| + TestCase("55.50", "JPY", "ja_JP", "¥56"), |
| + TestCase("55.99", "JPY", "ja_JP", "¥56"), |
| + |
| + // Unofficial ISO 4217 currency code. |
| + TestCase("55.00", "BTX", "en_US", "55.00"), |
| + TestCase("-55.00", "BTX", "en_US", "-55.00"), |
| + TestCase("-55.00", "BTX", "fr_FR", "-55,00"), |
| + |
| + // Any string of at most 2048 characters can be a valid currency code. |
| + TestCase("55.00", "", "en_US", "55.00"), |
| + TestCase("55,00", "", "fr_CA", "55,00"), |
| + TestCase("55,00", "", "fr-CA", "55,00"), |
| + TestCase("55.00", "ABCDEF", "en_US", "55.00"), |
| + |
| + // Edge cases. |
| + TestCase("", "", "", ""), |
| + TestCase("-1", "", "", "- 1.00"), |
| + TestCase("-1.1255", "", "", "- 1.13"), |
| + |
| + // Handles big numbers. |
| + TestCase( |
| + "123456789012345678901234567890.123456789012345678901234567890", |
| + "USD", |
| + "fr_FR", |
| + "123 456 789 012 345 678 901 234 567 890,12 $"), |
| + |
| + // When the currency system is not ISO4217, only the amount is formatted |
| + // using the locale (there is no other indication of currency). |
| + TestCase("55.00", "USD", "en_CA", "55.00", "http://currsystem.com"), |
| + TestCase("55.00", "USD", "fr_CA", "55,00", "http://currsystem.com"), |
| + TestCase("55.00", "USD", "fr_FR", "55,00", "http://currsystem.com"), |
| + TestCase("1234", "USD", "fr_FR", "1 234,00", "http://currsystem.com"), |
| + TestCase("55.5", "USD", "en_US", "55.50", "http://currsystem.com"), |
| + TestCase("55", "CAD", "en_US", "55.00", "http://currsystem.com"), |
| + TestCase("123", "BTX", "en_US", "123.00", "http://currsystem.com"), |
| + TestCase("1234", "JPY", "en_US", "1,234.00", "http://currsystem.com"), |
| + TestCase("0.1234", "USD", "en_US", "0.12", "http://currsystem.com"))); |
| + |
| +} // namespace payments |