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

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

Issue 2621033003: [Payments] Currency formatter for order amounts. (Closed)
Patch Set: Initial 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/strings/string_util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "third_party/icu/source/common/unicode/locid.h"
10 #include <ostream> // NOLINT
11
12 namespace payments {
13
14 struct CurrencyFormatterTestCase {
please use gerrit instead 2017/01/11 18:29:30 nit: A shorter name like "TestCase" will make the
Mathieu 2017/01/11 22:22:26 Done.
15 CurrencyFormatterTestCase(const char* amount,
16 const char* currency_code,
17 const char* locale_name,
18 const std::string& expected_amount)
19 : amount(amount),
20 currency_code(currency_code),
21 locale_name(locale_name),
22 expected_amount(expected_amount) {}
23 ~CurrencyFormatterTestCase() {}
24
25 const char* amount;
please use gerrit instead 2017/01/11 18:29:30 const char* const amount; (First "const" says "do
Mathieu 2017/01/11 22:22:26 Done.
26 const char* currency_code;
27 const char* locale_name;
28 std::string expected_amount;
please use gerrit instead 2017/01/11 18:29:30 const std::string expected_amount;
Mathieu 2017/01/11 22:22:26 Thanks.
29 };
30
31 class PaymentsCurrencyFormatterTest
32 : public testing::TestWithParam<CurrencyFormatterTestCase> {};
33
34 TEST_P(PaymentsCurrencyFormatterTest, IsValidCurrencyFormat) {
35 std::string outputAmount =
36 FormatAmountForCurrency(GetParam().amount, GetParam().currency_code,
37 icu::Locale(GetParam().locale_name));
38
39 // Convenience so the test cases can use regular spaces.
40 static const char kSpace[] = " ";
41 static const char kNonBreakingSpace[] = "\xC2\xA0";
42 std::string converted;
43 base::ReplaceChars(GetParam().expected_amount, kSpace, kNonBreakingSpace,
44 &converted);
45
46 EXPECT_EQ(converted, outputAmount)
47 << "Failed to convert " << GetParam().amount << " ("
48 << GetParam().currency_code << ") in " << GetParam().locale_name;
49 }
50
51 INSTANTIATE_TEST_CASE_P(
52 CurrencyAmounts,
53 PaymentsCurrencyFormatterTest,
54 testing::Values(
55 CurrencyFormatterTestCase("55.00", "USD", "en_US", "$55.00"),
56 CurrencyFormatterTestCase("55.00", "USD", "en_CA", "$55.00"),
57 CurrencyFormatterTestCase("55.00", "USD", "fr_CA", "55,00 $"),
58 CurrencyFormatterTestCase("55.00", "USD", "fr_FR", "55,00 $"),
59
60 CurrencyFormatterTestCase("55.5", "USD", "en_US", "$55.50"),
61 CurrencyFormatterTestCase("55", "USD", "en_US", "$55.00"),
62 CurrencyFormatterTestCase("123", "USD", "en_US", "$123.00"),
63 CurrencyFormatterTestCase("1234", "USD", "en_US", "$1,234.00"),
please use gerrit instead 2017/01/11 18:29:30 Also: CurrencyFormatterTestCase("0.1234", "USD",
Mathieu 2017/01/11 22:22:26 ICU is not modern enough to know about micro trans
64
65 CurrencyFormatterTestCase("55.00", "EUR", "en_US", "€55.00"),
66 CurrencyFormatterTestCase("55.00", "EUR", "en_CA", "€55.00"),
67 CurrencyFormatterTestCase("55.00", "EUR", "fr_CA", "55,00 €"),
68 CurrencyFormatterTestCase("55.00", "EUR", "fr_FR", "55,00 €"),
69
70 CurrencyFormatterTestCase("55.00", "CAD", "en_US", "$55.00"),
71 CurrencyFormatterTestCase("55.00", "CAD", "en_CA", "$55.00"),
72 CurrencyFormatterTestCase("55.00", "CAD", "fr_CA", "55,00 $"),
73 CurrencyFormatterTestCase("55.00", "CAD", "fr_FR", "55,00 $"),
please use gerrit instead 2017/01/11 18:29:31 Also: CurrencyFormatterTestCase("1234", "USD", "f
Mathieu 2017/01/11 22:22:26 Done.
74
75 CurrencyFormatterTestCase("55", "JPY", "ja_JP", "¥55"),
76 CurrencyFormatterTestCase("55.0", "JPY", "ja_JP", "¥55"),
77 CurrencyFormatterTestCase("55.00", "JPY", "ja_JP", "¥55"),
78 CurrencyFormatterTestCase("55.12", "JPY", "ja_JP", "¥55"),
79 CurrencyFormatterTestCase("55.49", "JPY", "ja_JP", "¥55"),
80 CurrencyFormatterTestCase("55.50", "JPY", "ja_JP", "¥56"),
81 CurrencyFormatterTestCase("55.99", "JPY", "ja_JP", "¥56"),
82
83 // Unofficial ISO 4217 currency code.
84 CurrencyFormatterTestCase("55.00", "BTX", "en_US", "55.00"),
85 CurrencyFormatterTestCase("-55.00", "BTX", "en_US", "-55.00"),
please use gerrit instead 2017/01/11 18:29:30 Also: CurrencyFormatterTestCase("-55.00", "BTX",
Mathieu 2017/01/11 22:22:26 Done.
86
87 // Any string of at most 2048 characters can be a valid currency code.
88 CurrencyFormatterTestCase("55.00", "", "en_US", "55.00"),
89 CurrencyFormatterTestCase("55.00", "ABCDEF", "en_US", "55.00"),
90
91 // If it doesn't fit in a double primitive type, interesting things
92 // happen.
please use gerrit instead 2017/01/11 18:29:30 Can we avoid this similar to how it's done in Curr
Mathieu 2017/01/11 22:22:26 Thanks for prodding. Used a different API that acc
93 CurrencyFormatterTestCase(
94 "123456789012345678901234567890.123456789012345678901234567890",
95 "USD",
96 "fr_FR",
97 "123 456 789 012 346 000 000 000 000 000,00 $")
98
99 ));
100
101 } // namespace payments
OLDNEW
« components/payments/currency_formatter.cc ('K') | « components/payments/currency_formatter.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698