File: currency_formatter_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (145 lines) | stat: -rw-r--r-- 6,391 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/payments/core/currency_formatter.h"

#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace payments {
namespace {

struct CurrencyTestCase {
  CurrencyTestCase(const char* amount,
                   const char* currency_code,
                   const char* locale_name,
                   const std::string& expected_amount,
                   const char* expected_currency_code)
      : amount(amount),
        currency_code(currency_code),
        locale_name(locale_name),
        expected_amount(expected_amount),
        expected_currency_code(expected_currency_code) {}
  ~CurrencyTestCase() = default;

  const char* const amount;
  const char* const currency_code;
  const char* const locale_name;
  const std::string expected_amount;
  const char* const expected_currency_code;
};

class PaymentsCurrencyFormatterTest
    : public testing::TestWithParam<CurrencyTestCase> {};

TEST_P(PaymentsCurrencyFormatterTest, IsValidCurrencyFormat) {
  CurrencyFormatter formatter(GetParam().currency_code, GetParam().locale_name);
  std::u16string actual_output = formatter.Format(GetParam().amount);

  // Convenience so the test cases can use regular spaces.
  const std::u16string kSpace(u" ");
  const std::u16string kNonBreakingSpace(u"\u00a0");
  const std::u16string kNarrowNonBreakingSpace(u"\u202f");
  base::ReplaceChars(actual_output, kNonBreakingSpace, kSpace, &actual_output);
  base::ReplaceChars(actual_output, kNarrowNonBreakingSpace, kSpace,
                     &actual_output);
  std::u16string expected_output =
      base::UTF8ToUTF16(GetParam().expected_amount);

  EXPECT_EQ(expected_output, actual_output)
      << "Failed to convert " << GetParam().amount << " ("
      << GetParam().currency_code << ") in " << GetParam().locale_name;
  EXPECT_EQ(GetParam().expected_currency_code,
            formatter.formatted_currency_code());
}

INSTANTIATE_TEST_SUITE_P(
    CurrencyAmounts,
    PaymentsCurrencyFormatterTest,
    testing::Values(
        CurrencyTestCase("55.00", "USD", "en_US", "$55.00", "USD"),
        CurrencyTestCase("55.00", "USD", "en_CA", "$55.00", "USD"),
        CurrencyTestCase("55.00", "USD", "fr_CA", "55,00 $", "USD"),
        CurrencyTestCase("55.00", "USD", "fr_FR", "55,00 $", "USD"),
        CurrencyTestCase("1234", "USD", "fr_FR", "1 234,00 $", "USD"),
        // Known oddity about the en_AU formatting in ICU. It will strip the
        // currency symbol in non-AUD currencies. Useful to document in tests.
        // See crbug.com/739812.
        CurrencyTestCase("55.00", "AUD", "en_AU", "$55.00", "AUD"),
        CurrencyTestCase("55.00", "USD", "en_AU", "55.00", "USD"),
        CurrencyTestCase("55.00", "CAD", "en_AU", "55.00", "CAD"),
        CurrencyTestCase("55.00", "JPY", "en_AU", "55", "JPY"),
        CurrencyTestCase("-55.00", "USD", "en_AU", "-55.00", "USD"),

        CurrencyTestCase("55.5", "USD", "en_US", "$55.50", "USD"),
        CurrencyTestCase("55", "USD", "en_US", "$55.00", "USD"),
        CurrencyTestCase("123", "USD", "en_US", "$123.00", "USD"),
        CurrencyTestCase("1234", "USD", "en_US", "$1,234.00", "USD"),
        CurrencyTestCase("0.1234", "USD", "en_US", "$0.1234", "USD"),

        CurrencyTestCase("55.00", "EUR", "en_US", "€55.00", "EUR"),
        CurrencyTestCase("55.00", "EUR", "fr_CA", "55,00 €", "EUR"),
        CurrencyTestCase("55.00", "EUR", "fr_FR", "55,00 €", "EUR"),

        CurrencyTestCase("55.00", "CAD", "en_US", "$55.00", "CAD"),
        CurrencyTestCase("55.00", "CAD", "en_CA", "$55.00", "CAD"),
        CurrencyTestCase("55.00", "CAD", "fr_CA", "55,00 $", "CAD"),
        CurrencyTestCase("55.00", "CAD", "fr_FR", "55,00 $", "CAD"),

        CurrencyTestCase("55.00", "AUD", "en_US", "$55.00", "AUD"),
        CurrencyTestCase("55.00", "AUD", "en_CA", "$55.00", "AUD"),
        CurrencyTestCase("55.00", "AUD", "fr_CA", "55,00 $", "AUD"),
        CurrencyTestCase("55.00", "AUD", "fr_FR", "55,00 $", "AUD"),

        CurrencyTestCase("55.00", "BRL", "en_US", "R$55.00", "BRL"),
        CurrencyTestCase("55.00", "BRL", "fr_CA", "55,00 R$", "BRL"),
        CurrencyTestCase("55.00", "BRL", "pt_BR", "R$ 55,00", "BRL"),

        CurrencyTestCase("55.00", "RUB", "en_US", "55.00", "RUB"),
        CurrencyTestCase("55.00", "RUB", "fr_CA", "55,00", "RUB"),
        CurrencyTestCase("55.00", "RUB", "ru_RU", "55,00 ₽", "RUB"),

        CurrencyTestCase("55", "JPY", "ja_JP", "¥55", "JPY"),
        CurrencyTestCase("55.0", "JPY", "ja_JP", "¥55", "JPY"),
        CurrencyTestCase("55.00", "JPY", "ja_JP", "¥55", "JPY"),
        CurrencyTestCase("55.12", "JPY", "ja_JP", "¥55.12", "JPY"),
        CurrencyTestCase("55.49", "JPY", "ja_JP", "¥55.49", "JPY"),
        CurrencyTestCase("55.50", "JPY", "ja_JP", "¥55.5", "JPY"),
        CurrencyTestCase("55.9999", "JPY", "ja_JP", "¥55.9999", "JPY"),

        // Unofficial ISO 4217 currency code.
        CurrencyTestCase("55.00", "BTC", "en_US", "55.00", "BTC"),
        CurrencyTestCase("-0.0000000001",
                         "BTC",
                         "en_US",
                         "-0.0000000001",
                         "BTC"),
        CurrencyTestCase("-55.00", "BTC", "fr_FR", "-55,00", "BTC"),

        // Any string of at most 2048 characters can be a valid currency code.
        CurrencyTestCase("55.00", "", "en_US", "55.00", ""),
        CurrencyTestCase("55,00", "", "fr_CA", "55,00", ""),
        CurrencyTestCase("55,00", "", "fr-CA", "55,00", ""),
        CurrencyTestCase("55.00",
                         "ABCDEF",
                         "en_US",
                         "55.00",
                         "ABCDE\xE2\x80\xA6"),

        // Edge cases.
        CurrencyTestCase("", "", "", "", ""),
        CurrencyTestCase("-1", "", "", "-1.00", ""),
        CurrencyTestCase("-1.1255", "", "", "-1.1255", ""),

        // Handles big numbers.
        CurrencyTestCase(
            "123456789012345678901234567890.123456789012345678901234567890",
            "USD",
            "fr_FR",
            "123 456 789 012 345 678 901 234 567 890,123456789 $",
            "USD")));

}  // namespace
}  // namespace payments