Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Side by Side Diff: components/payments/currency_formatter_unittest.cc

Issue 2621033003: [Payments] Currency formatter for order amounts. (Closed)
Patch Set: currency formatter takes locale_name as string Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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* currency_system = kIso4217CurrencySystem)
21 : amount(amount),
22 currency_code(currency_code),
23 locale_name(locale_name),
24 expected_amount(expected_amount),
25 currency_system(currency_system) {}
26 ~TestCase() {}
27
28 const char* const amount;
29 const char* const currency_code;
30 const char* const locale_name;
31 const std::string expected_amount;
32 const base::Optional<std::string> currency_system;
33 };
34
35 class PaymentsCurrencyFormatterTest : public testing::TestWithParam<TestCase> {
36 };
37
38 TEST_P(PaymentsCurrencyFormatterTest, IsValidCurrencyFormat) {
39 CurrencyFormatter formatter(GetParam().currency_code,
40 GetParam().currency_system,
41 GetParam().locale_name);
42 base::string16 output_amount = formatter.Format(GetParam().amount);
43
44 // Convenience so the test cases can use regular spaces.
45 const base::string16 kSpace(base::ASCIIToUTF16(" "));
46 const base::string16 kNonBreakingSpace(base::UTF8ToUTF16("\xC2\xA0"));
47 base::string16 converted;
48 base::ReplaceChars(base::UTF8ToUTF16(GetParam().expected_amount), kSpace,
49 kNonBreakingSpace, &converted);
50
51 EXPECT_EQ(converted, output_amount)
52 << "Failed to convert " << GetParam().amount << " ("
53 << GetParam().currency_code << ") in " << GetParam().locale_name;
54 }
55
56 INSTANTIATE_TEST_CASE_P(
57 CurrencyAmounts,
58 PaymentsCurrencyFormatterTest,
59 testing::Values(
60 TestCase("55.00", "USD", "en_US", "$55.00"),
61 TestCase("55.00", "USD", "en_CA", "$55.00"),
62 TestCase("55.00", "USD", "fr_CA", "55,00 $"),
63 TestCase("55.00", "USD", "fr_FR", "55,00 $"),
64 TestCase("1234", "USD", "fr_FR", "1 234,00 $"),
65
66 TestCase("55.5", "USD", "en_US", "$55.50"),
67 TestCase("55", "USD", "en_US", "$55.00"),
68 TestCase("123", "USD", "en_US", "$123.00"),
69 TestCase("1234", "USD", "en_US", "$1,234.00"),
70 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.
71
72 TestCase("55.00", "EUR", "en_US", "€55.00"),
73 TestCase("55.00", "EUR", "fr_CA", "55,00 €"),
74 TestCase("55.00", "EUR", "fr_FR", "55,00 €"),
75
76 TestCase("55.00", "CAD", "en_US", "$55.00"),
77 TestCase("55.00", "CAD", "en_CA", "$55.00"),
78 TestCase("55.00", "CAD", "fr_CA", "55,00 $"),
79 TestCase("55.00", "CAD", "fr_FR", "55,00 $"),
80
81 TestCase("55.00", "BRL", "en_US", "R$55.00"),
82 TestCase("55.00", "BRL", "fr_CA", "55,00 R$"),
83 TestCase("55.00", "BRL", "pt_BR", "R$55,00"),
84
85 TestCase("55.00", "RUB", "en_US", "55.00"),
86 TestCase("55.00", "RUB", "fr_CA", "55,00"),
87 TestCase("55.00", "RUB", "ru_RU", "55,00 ₽"),
88
89 TestCase("55", "JPY", "ja_JP", "¥55"),
90 TestCase("55.0", "JPY", "ja_JP", "¥55"),
91 TestCase("55.00", "JPY", "ja_JP", "¥55"),
92 TestCase("55.12", "JPY", "ja_JP", "¥55"),
93 TestCase("55.49", "JPY", "ja_JP", "¥55"),
94 TestCase("55.50", "JPY", "ja_JP", "¥56"),
95 TestCase("55.99", "JPY", "ja_JP", "¥56"),
96
97 // Unofficial ISO 4217 currency code.
98 TestCase("55.00", "BTX", "en_US", "55.00"),
99 TestCase("-55.00", "BTX", "en_US", "-55.00"),
100 TestCase("-55.00", "BTX", "fr_FR", "-55,00"),
101
102 // Any string of at most 2048 characters can be a valid currency code.
103 TestCase("55.00", "", "en_US", "55.00"),
104 TestCase("55,00", "", "fr_CA", "55,00"),
105 TestCase("55,00", "", "fr-CA", "55,00"),
106 TestCase("55.00", "ABCDEF", "en_US", "55.00"),
107
108 // Edge cases.
109 TestCase("", "", "", ""),
110 TestCase("-1", "", "", "- 1.00"),
111 TestCase("-1.1255", "", "", "- 1.13"),
112
113 // Handles big numbers.
114 TestCase(
115 "123456789012345678901234567890.123456789012345678901234567890",
116 "USD",
117 "fr_FR",
118 "123 456 789 012 345 678 901 234 567 890,12 $"),
119
120 // When the currency system is not ISO4217, only the amount is formatted
121 // using the locale (there is no other indication of currency).
122 TestCase("55.00", "USD", "en_CA", "55.00", "http://currsystem.com"),
123 TestCase("55.00", "USD", "fr_CA", "55,00", "http://currsystem.com"),
124 TestCase("55.00", "USD", "fr_FR", "55,00", "http://currsystem.com"),
125 TestCase("1234", "USD", "fr_FR", "1 234,00", "http://currsystem.com"),
126 TestCase("55.5", "USD", "en_US", "55.50", "http://currsystem.com"),
127 TestCase("55", "CAD", "en_US", "55.00", "http://currsystem.com"),
128 TestCase("123", "BTX", "en_US", "123.00", "http://currsystem.com"),
129 TestCase("1234", "JPY", "en_US", "1,234.00", "http://currsystem.com"),
130 TestCase("0.1234", "USD", "en_US", "0.12", "http://currsystem.com")));
131
132 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698