OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/i18n/message_formatter.h" | 5 #include "base/i18n/message_formatter.h" |
6 | 6 |
| 7 #include <memory> |
| 8 |
7 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "third_party/icu/source/common/unicode/unistr.h" | 15 #include "third_party/icu/source/common/unicode/unistr.h" |
15 #include "third_party/icu/source/i18n/unicode/datefmt.h" | 16 #include "third_party/icu/source/i18n/unicode/datefmt.h" |
16 #include "third_party/icu/source/i18n/unicode/msgfmt.h" | 17 #include "third_party/icu/source/i18n/unicode/msgfmt.h" |
17 | 18 |
18 typedef testing::Test MessageFormatterTest; | 19 typedef testing::Test MessageFormatterTest; |
(...skipping 10 matching lines...) Expand all Loading... |
29 ~MessageFormatterTest() override { | 30 ~MessageFormatterTest() override { |
30 SetICUDefaultLocale(original_locale_); | 31 SetICUDefaultLocale(original_locale_); |
31 } | 32 } |
32 | 33 |
33 private: | 34 private: |
34 std::string original_locale_; | 35 std::string original_locale_; |
35 }; | 36 }; |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 void AppendFormattedDateTime(const scoped_ptr<icu::DateFormat>& df, | 40 void AppendFormattedDateTime(const std::unique_ptr<icu::DateFormat>& df, |
40 const Time& now, std::string* result) { | 41 const Time& now, |
| 42 std::string* result) { |
41 icu::UnicodeString formatted; | 43 icu::UnicodeString formatted; |
42 df->format(static_cast<UDate>(now.ToJsTime()), formatted). | 44 df->format(static_cast<UDate>(now.ToJsTime()), formatted). |
43 toUTF8String(*result); | 45 toUTF8String(*result); |
44 } | 46 } |
45 | 47 |
46 } // namespace | 48 } // namespace |
47 | 49 |
48 TEST_F(MessageFormatterTest, PluralNamedArgs) { | 50 TEST_F(MessageFormatterTest, PluralNamedArgs) { |
49 const string16 pattern = ASCIIToUTF16( | 51 const string16 pattern = ASCIIToUTF16( |
50 "{num_people, plural, " | 52 "{num_people, plural, " |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 114 } |
113 | 115 |
114 TEST_F(MessageFormatterTest, PluralNumberedArgsWithDate) { | 116 TEST_F(MessageFormatterTest, PluralNumberedArgsWithDate) { |
115 const string16 pattern = ASCIIToUTF16( | 117 const string16 pattern = ASCIIToUTF16( |
116 "{1, plural, " | 118 "{1, plural, " |
117 "=1 {The cert for {0} expired yesterday. Today is {2,date,full}}" | 119 "=1 {The cert for {0} expired yesterday. Today is {2,date,full}}" |
118 "other {The cert for {0} expired # days ago. Today is {2,date,full}}}"); | 120 "other {The cert for {0} expired # days ago. Today is {2,date,full}}}"); |
119 | 121 |
120 base::Time now = base::Time::Now(); | 122 base::Time now = base::Time::Now(); |
121 using icu::DateFormat; | 123 using icu::DateFormat; |
122 scoped_ptr<DateFormat> df(DateFormat::createDateInstance(DateFormat::FULL)); | 124 std::unique_ptr<DateFormat> df( |
| 125 DateFormat::createDateInstance(DateFormat::FULL)); |
123 std::string second_sentence = " Today is "; | 126 std::string second_sentence = " Today is "; |
124 AppendFormattedDateTime(df, now, &second_sentence); | 127 AppendFormattedDateTime(df, now, &second_sentence); |
125 | 128 |
126 std::string result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( | 129 std::string result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( |
127 pattern, "example.com", 1, now)); | 130 pattern, "example.com", 1, now)); |
128 EXPECT_EQ("The cert for example.com expired yesterday." + second_sentence, | 131 EXPECT_EQ("The cert for example.com expired yesterday." + second_sentence, |
129 result); | 132 result); |
130 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( | 133 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( |
131 pattern, "example.com", 15, now)); | 134 pattern, "example.com", 15, now)); |
132 EXPECT_EQ("The cert for example.com expired 15 days ago." + second_sentence, | 135 EXPECT_EQ("The cert for example.com expired 15 days ago." + second_sentence, |
133 result); | 136 result); |
134 } | 137 } |
135 | 138 |
136 TEST_F(MessageFormatterTest, DateTimeAndNumber) { | 139 TEST_F(MessageFormatterTest, DateTimeAndNumber) { |
137 // Note that using 'mph' for all locales is not a good i18n practice. | 140 // Note that using 'mph' for all locales is not a good i18n practice. |
138 const string16 pattern = ASCIIToUTF16( | 141 const string16 pattern = ASCIIToUTF16( |
139 "At {0,time, short} on {0,date, medium}, " | 142 "At {0,time, short} on {0,date, medium}, " |
140 "there was {1} at building {2,number,integer}. " | 143 "there was {1} at building {2,number,integer}. " |
141 "The speed of the wind was {3,number,###.#} mph."); | 144 "The speed of the wind was {3,number,###.#} mph."); |
142 | 145 |
143 using icu::DateFormat; | 146 using icu::DateFormat; |
144 scoped_ptr<DateFormat> tf(DateFormat::createTimeInstance(DateFormat::SHORT)); | 147 std::unique_ptr<DateFormat> tf( |
145 scoped_ptr<DateFormat> df(DateFormat::createDateInstance(DateFormat::MEDIUM)); | 148 DateFormat::createTimeInstance(DateFormat::SHORT)); |
| 149 std::unique_ptr<DateFormat> df( |
| 150 DateFormat::createDateInstance(DateFormat::MEDIUM)); |
146 | 151 |
147 base::Time now = base::Time::Now(); | 152 base::Time now = base::Time::Now(); |
148 std::string expected = "At "; | 153 std::string expected = "At "; |
149 AppendFormattedDateTime(tf, now, &expected); | 154 AppendFormattedDateTime(tf, now, &expected); |
150 expected.append(" on "); | 155 expected.append(" on "); |
151 AppendFormattedDateTime(df, now, &expected); | 156 AppendFormattedDateTime(df, now, &expected); |
152 expected.append(", there was an explosion at building 3. " | 157 expected.append(", there was an explosion at building 3. " |
153 "The speed of the wind was 37.4 mph."); | 158 "The speed of the wind was 37.4 mph."); |
154 | 159 |
155 EXPECT_EQ(expected, UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( | 160 EXPECT_EQ(expected, UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( |
(...skipping 15 matching lines...) Expand all Loading... |
171 EXPECT_EQ("Select files to upload.", result); | 176 EXPECT_EQ("Select files to upload.", result); |
172 | 177 |
173 // fallback if a parameter is not selectors specified in the message pattern. | 178 // fallback if a parameter is not selectors specified in the message pattern. |
174 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( | 179 result = UTF16ToASCII(MessageFormatter::FormatWithNumberedArgs( |
175 pattern, "foobar")); | 180 pattern, "foobar")); |
176 EXPECT_EQ("UNUSED", result); | 181 EXPECT_EQ("UNUSED", result); |
177 } | 182 } |
178 | 183 |
179 } // namespace i18n | 184 } // namespace i18n |
180 } // namespace base | 185 } // namespace base |
OLD | NEW |