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

Unified 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 side-by-side diff with in-line comments
Download patch
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..0343cdc76e9c62e18981372c002b216d754d1751
--- /dev/null
+++ b/components/payments/currency_formatter_unittest.cc
@@ -0,0 +1,101 @@
+// 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/strings/string_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/icu/source/common/unicode/locid.h"
+#include <ostream> // NOLINT
+
+namespace payments {
+
+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.
+ CurrencyFormatterTestCase(const char* amount,
+ const char* currency_code,
+ const char* locale_name,
+ const std::string& expected_amount)
+ : amount(amount),
+ currency_code(currency_code),
+ locale_name(locale_name),
+ expected_amount(expected_amount) {}
+ ~CurrencyFormatterTestCase() {}
+
+ 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.
+ const char* currency_code;
+ const char* locale_name;
+ 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.
+};
+
+class PaymentsCurrencyFormatterTest
+ : public testing::TestWithParam<CurrencyFormatterTestCase> {};
+
+TEST_P(PaymentsCurrencyFormatterTest, IsValidCurrencyFormat) {
+ std::string outputAmount =
+ FormatAmountForCurrency(GetParam().amount, GetParam().currency_code,
+ icu::Locale(GetParam().locale_name));
+
+ // Convenience so the test cases can use regular spaces.
+ static const char kSpace[] = " ";
+ static const char kNonBreakingSpace[] = "\xC2\xA0";
+ std::string converted;
+ base::ReplaceChars(GetParam().expected_amount, kSpace, kNonBreakingSpace,
+ &converted);
+
+ EXPECT_EQ(converted, outputAmount)
+ << "Failed to convert " << GetParam().amount << " ("
+ << GetParam().currency_code << ") in " << GetParam().locale_name;
+}
+
+INSTANTIATE_TEST_CASE_P(
+ CurrencyAmounts,
+ PaymentsCurrencyFormatterTest,
+ testing::Values(
+ CurrencyFormatterTestCase("55.00", "USD", "en_US", "$55.00"),
+ CurrencyFormatterTestCase("55.00", "USD", "en_CA", "$55.00"),
+ CurrencyFormatterTestCase("55.00", "USD", "fr_CA", "55,00 $"),
+ CurrencyFormatterTestCase("55.00", "USD", "fr_FR", "55,00 $"),
+
+ CurrencyFormatterTestCase("55.5", "USD", "en_US", "$55.50"),
+ CurrencyFormatterTestCase("55", "USD", "en_US", "$55.00"),
+ CurrencyFormatterTestCase("123", "USD", "en_US", "$123.00"),
+ 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
+
+ CurrencyFormatterTestCase("55.00", "EUR", "en_US", "€55.00"),
+ CurrencyFormatterTestCase("55.00", "EUR", "en_CA", "€55.00"),
+ CurrencyFormatterTestCase("55.00", "EUR", "fr_CA", "55,00 €"),
+ CurrencyFormatterTestCase("55.00", "EUR", "fr_FR", "55,00 €"),
+
+ CurrencyFormatterTestCase("55.00", "CAD", "en_US", "$55.00"),
+ CurrencyFormatterTestCase("55.00", "CAD", "en_CA", "$55.00"),
+ CurrencyFormatterTestCase("55.00", "CAD", "fr_CA", "55,00 $"),
+ 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.
+
+ CurrencyFormatterTestCase("55", "JPY", "ja_JP", "¥55"),
+ CurrencyFormatterTestCase("55.0", "JPY", "ja_JP", "¥55"),
+ CurrencyFormatterTestCase("55.00", "JPY", "ja_JP", "¥55"),
+ CurrencyFormatterTestCase("55.12", "JPY", "ja_JP", "¥55"),
+ CurrencyFormatterTestCase("55.49", "JPY", "ja_JP", "¥55"),
+ CurrencyFormatterTestCase("55.50", "JPY", "ja_JP", "¥56"),
+ CurrencyFormatterTestCase("55.99", "JPY", "ja_JP", "¥56"),
+
+ // Unofficial ISO 4217 currency code.
+ CurrencyFormatterTestCase("55.00", "BTX", "en_US", "55.00"),
+ 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.
+
+ // Any string of at most 2048 characters can be a valid currency code.
+ CurrencyFormatterTestCase("55.00", "", "en_US", "55.00"),
+ CurrencyFormatterTestCase("55.00", "ABCDEF", "en_US", "55.00"),
+
+ // If it doesn't fit in a double primitive type, interesting things
+ // 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
+ CurrencyFormatterTestCase(
+ "123456789012345678901234567890.123456789012345678901234567890",
+ "USD",
+ "fr_FR",
+ "123 456 789 012 346 000 000 000 000 000,00 $")
+
+ ));
+
+} // namespace payments
« 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