| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/payments/currency_formatter.h" | |
| 6 | |
| 7 #include "base/optional.h" | |
| 8 #include "base/strings/string16.h" | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 namespace payments { | |
| 14 | |
| 15 struct TestCase { | |
| 16 TestCase(const char* amount, | |
| 17 const char* currency_code, | |
| 18 const char* locale_name, | |
| 19 const std::string& expected_amount, | |
| 20 const char* expected_currency_code, | |
| 21 const char* currency_system = kIso4217CurrencySystem) | |
| 22 : amount(amount), | |
| 23 currency_code(currency_code), | |
| 24 locale_name(locale_name), | |
| 25 expected_amount(expected_amount), | |
| 26 expected_currency_code(expected_currency_code), | |
| 27 currency_system(currency_system) {} | |
| 28 ~TestCase() {} | |
| 29 | |
| 30 const char* const amount; | |
| 31 const char* const currency_code; | |
| 32 const char* const locale_name; | |
| 33 const std::string expected_amount; | |
| 34 const char* const expected_currency_code; | |
| 35 const char* const currency_system; | |
| 36 }; | |
| 37 | |
| 38 class PaymentsCurrencyFormatterTest : public testing::TestWithParam<TestCase> { | |
| 39 }; | |
| 40 | |
| 41 TEST_P(PaymentsCurrencyFormatterTest, IsValidCurrencyFormat) { | |
| 42 CurrencyFormatter formatter(GetParam().currency_code, | |
| 43 GetParam().currency_system, | |
| 44 GetParam().locale_name); | |
| 45 base::string16 output_amount = formatter.Format(GetParam().amount); | |
| 46 | |
| 47 // Convenience so the test cases can use regular spaces. | |
| 48 const base::string16 kSpace(base::ASCIIToUTF16(" ")); | |
| 49 const base::string16 kNonBreakingSpace(base::UTF8ToUTF16("\xC2\xA0")); | |
| 50 base::string16 converted; | |
| 51 base::ReplaceChars(base::UTF8ToUTF16(GetParam().expected_amount), kSpace, | |
| 52 kNonBreakingSpace, &converted); | |
| 53 | |
| 54 EXPECT_EQ(converted, output_amount) | |
| 55 << "Failed to convert " << GetParam().amount << " (" | |
| 56 << GetParam().currency_code << ") in " << GetParam().locale_name; | |
| 57 EXPECT_EQ(GetParam().expected_currency_code, | |
| 58 formatter.formatted_currency_code()); | |
| 59 } | |
| 60 | |
| 61 INSTANTIATE_TEST_CASE_P( | |
| 62 CurrencyAmounts, | |
| 63 PaymentsCurrencyFormatterTest, | |
| 64 testing::Values( | |
| 65 TestCase("55.00", "USD", "en_US", "$55.00", "USD"), | |
| 66 TestCase("55.00", "USD", "en_CA", "$55.00", "USD"), | |
| 67 TestCase("55.00", "USD", "fr_CA", "55,00 $", "USD"), | |
| 68 TestCase("55.00", "USD", "fr_FR", "55,00 $", "USD"), | |
| 69 TestCase("1234", "USD", "fr_FR", "1 234,00 $", "USD"), | |
| 70 | |
| 71 TestCase("55.5", "USD", "en_US", "$55.50", "USD"), | |
| 72 TestCase("55", "USD", "en_US", "$55.00", "USD"), | |
| 73 TestCase("123", "USD", "en_US", "$123.00", "USD"), | |
| 74 TestCase("1234", "USD", "en_US", "$1,234.00", "USD"), | |
| 75 TestCase("0.1234", "USD", "en_US", "$0.1234", "USD"), | |
| 76 | |
| 77 TestCase("55.00", "EUR", "en_US", "€55.00", "EUR"), | |
| 78 TestCase("55.00", "EUR", "fr_CA", "55,00 €", "EUR"), | |
| 79 TestCase("55.00", "EUR", "fr_FR", "55,00 €", "EUR"), | |
| 80 | |
| 81 TestCase("55.00", "CAD", "en_US", "$55.00", "CAD"), | |
| 82 TestCase("55.00", "CAD", "en_CA", "$55.00", "CAD"), | |
| 83 TestCase("55.00", "CAD", "fr_CA", "55,00 $", "CAD"), | |
| 84 TestCase("55.00", "CAD", "fr_FR", "55,00 $", "CAD"), | |
| 85 | |
| 86 TestCase("55.00", "BRL", "en_US", "R$55.00", "BRL"), | |
| 87 TestCase("55.00", "BRL", "fr_CA", "55,00 R$", "BRL"), | |
| 88 TestCase("55.00", "BRL", "pt_BR", "R$55,00", "BRL"), | |
| 89 | |
| 90 TestCase("55.00", "RUB", "en_US", "55.00", "RUB"), | |
| 91 TestCase("55.00", "RUB", "fr_CA", "55,00", "RUB"), | |
| 92 TestCase("55.00", "RUB", "ru_RU", "55,00 ₽", "RUB"), | |
| 93 | |
| 94 TestCase("55", "JPY", "ja_JP", "¥55", "JPY"), | |
| 95 TestCase("55.0", "JPY", "ja_JP", "¥55", "JPY"), | |
| 96 TestCase("55.00", "JPY", "ja_JP", "¥55", "JPY"), | |
| 97 TestCase("55.12", "JPY", "ja_JP", "¥55.12", "JPY"), | |
| 98 TestCase("55.49", "JPY", "ja_JP", "¥55.49", "JPY"), | |
| 99 TestCase("55.50", "JPY", "ja_JP", "¥55.5", "JPY"), | |
| 100 TestCase("55.9999", "JPY", "ja_JP", "¥55.9999", "JPY"), | |
| 101 | |
| 102 // Unofficial ISO 4217 currency code. | |
| 103 TestCase("55.00", "BTC", "en_US", "55.00", "BTC"), | |
| 104 TestCase("-0.0000000001", "BTC", "en_US", "-0.0000000001", "BTC"), | |
| 105 TestCase("-55.00", "BTC", "fr_FR", "-55,00", "BTC"), | |
| 106 | |
| 107 // Any string of at most 2048 characters can be a valid currency code. | |
| 108 TestCase("55.00", "", "en_US", "55.00", ""), | |
| 109 TestCase("55,00", "", "fr_CA", "55,00", ""), | |
| 110 TestCase("55,00", "", "fr-CA", "55,00", ""), | |
| 111 TestCase("55.00", "ABCDEF", "en_US", "55.00", "ABCDE\xE2\x80\xA6"), | |
| 112 | |
| 113 // Edge cases. | |
| 114 TestCase("", "", "", "", ""), | |
| 115 TestCase("-1", "", "", "- 1.00", ""), | |
| 116 TestCase("-1.1255", "", "", "- 1.1255", ""), | |
| 117 | |
| 118 // Handles big numbers. | |
| 119 TestCase( | |
| 120 "123456789012345678901234567890.123456789012345678901234567890", | |
| 121 "USD", | |
| 122 "fr_FR", | |
| 123 "123 456 789 012 345 678 901 234 567 890,123456789 $", | |
| 124 "USD"), | |
| 125 | |
| 126 // When the currency system is not ISO4217, only the amount is formatted | |
| 127 // using the locale (there is no other indication of currency). | |
| 128 TestCase("55.00", | |
| 129 "USD", | |
| 130 "en_CA", | |
| 131 "55.00", | |
| 132 "USD", | |
| 133 "http://currsystem.com"), | |
| 134 TestCase("55.00", | |
| 135 "USD", | |
| 136 "fr_CA", | |
| 137 "55,00", | |
| 138 "USD", | |
| 139 "http://currsystem.com"), | |
| 140 TestCase("55.00", | |
| 141 "USD", | |
| 142 "fr_FR", | |
| 143 "55,00", | |
| 144 "USD", | |
| 145 "http://currsystem.com"), | |
| 146 TestCase("1234", | |
| 147 "USD", | |
| 148 "fr_FR", | |
| 149 "1 234,00", | |
| 150 "USD", | |
| 151 "http://currsystem.com"), | |
| 152 TestCase("55.5", | |
| 153 "USD", | |
| 154 "en_US", | |
| 155 "55.50", | |
| 156 "USD", | |
| 157 "http://currsystem.com"), | |
| 158 TestCase("55", "CAD", "en_US", "55.00", "CAD", "http://currsystem.com"), | |
| 159 TestCase("123", | |
| 160 "BTC", | |
| 161 "en_US", | |
| 162 "123.00", | |
| 163 "BTC", | |
| 164 "http://currsystem.com"), | |
| 165 TestCase("1234", | |
| 166 "JPY", | |
| 167 "en_US", | |
| 168 "1,234.00", | |
| 169 "JPY", | |
| 170 "http://currsystem.com"), | |
| 171 TestCase("0.1234", | |
| 172 "USD", | |
| 173 "en_US", | |
| 174 "0.1234", | |
| 175 "USD", | |
| 176 "http://currsystem.com"))); | |
| 177 | |
| 178 } // namespace payments | |
| OLD | NEW |