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