xref: /aosp_15_r20/external/fmtlib/test/xchar-test.cc (revision 5c90c05cd622c0a81b57953a4d343e0e489f2e08)
1 // Formatting library for C++ - formatting library tests
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 
8 #include "fmt/xchar.h"
9 
10 #include <algorithm>
11 #include <complex>
12 #include <cwchar>
13 #include <vector>
14 
15 #include "fmt/chrono.h"
16 #include "fmt/color.h"
17 #include "fmt/ostream.h"
18 #include "fmt/ranges.h"
19 #include "fmt/std.h"
20 #include "gtest-extra.h"  // Contains
21 #include "util.h"         // get_locale
22 
23 using fmt::detail::max_value;
24 using testing::Contains;
25 
26 #if defined(__MINGW32__) && !defined(_UCRT)
27 // Only C89 conversion specifiers when using MSVCRT instead of UCRT
28 #  define FMT_HAS_C99_STRFTIME 0
29 #else
30 #  define FMT_HAS_C99_STRFTIME 1
31 #endif
32 
33 struct non_string {};
34 
35 template <typename T> class has_to_string_view_test : public testing::Test {};
36 
37 using string_char_types = testing::Types<char, wchar_t, char16_t, char32_t>;
38 TYPED_TEST_SUITE(has_to_string_view_test, string_char_types);
39 
40 template <typename Char>
41 struct derived_from_string_view : fmt::basic_string_view<Char> {};
42 
TYPED_TEST(has_to_string_view_test,has_to_string_view)43 TYPED_TEST(has_to_string_view_test, has_to_string_view) {
44   EXPECT_TRUE(fmt::detail::has_to_string_view<TypeParam*>::value);
45   EXPECT_TRUE(fmt::detail::has_to_string_view<const TypeParam*>::value);
46   EXPECT_TRUE(fmt::detail::has_to_string_view<TypeParam[2]>::value);
47   EXPECT_TRUE(fmt::detail::has_to_string_view<const TypeParam[2]>::value);
48   EXPECT_TRUE(
49       fmt::detail::has_to_string_view<std::basic_string<TypeParam>>::value);
50   EXPECT_TRUE(fmt::detail::has_to_string_view<
51               fmt::basic_string_view<TypeParam>>::value);
52   EXPECT_TRUE(fmt::detail::has_to_string_view<
53               derived_from_string_view<TypeParam>>::value);
54   using fmt_string_view = fmt::detail::std_string_view<TypeParam>;
55   EXPECT_TRUE(std::is_empty<fmt_string_view>::value !=
56               fmt::detail::has_to_string_view<fmt_string_view>::value);
57   EXPECT_FALSE(fmt::detail::has_to_string_view<non_string>::value);
58 }
59 
60 // std::is_constructible is broken in MSVC until version 2015.
61 #if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1900
62 struct explicitly_convertible_to_wstring_view {
operator fmt::wstring_viewexplicitly_convertible_to_wstring_view63   explicit operator fmt::wstring_view() const { return L"foo"; }
64 };
65 
TEST(xchar_test,format_explicitly_convertible_to_wstring_view)66 TEST(xchar_test, format_explicitly_convertible_to_wstring_view) {
67   // Types explicitly convertible to wstring_view are not formattable by
68   // default because it may introduce ODR violations.
69   static_assert(
70       !fmt::is_formattable<explicitly_convertible_to_wstring_view>::value, "");
71 }
72 #endif
73 
TEST(xchar_test,format)74 TEST(xchar_test, format) {
75   EXPECT_EQ(L"42", fmt::format(L"{}", 42));
76   EXPECT_EQ(L"4.2", fmt::format(L"{}", 4.2));
77   EXPECT_EQ(L"abc", fmt::format(L"{}", L"abc"));
78   EXPECT_EQ(L"z", fmt::format(L"{}", L'z'));
79   EXPECT_THROW(fmt::format(fmt::runtime(L"{:*\x343E}"), 42), fmt::format_error);
80   EXPECT_EQ(L"true", fmt::format(L"{}", true));
81   EXPECT_EQ(L"a", fmt::format(L"{0}", L'a'));
82   EXPECT_EQ(L"Cyrillic letter \x42e",
83             fmt::format(L"Cyrillic letter {}", L'\x42e'));
84   EXPECT_EQ(L"abc1", fmt::format(L"{}c{}", L"ab", 1));
85 }
86 
TEST(xchar_test,is_formattable)87 TEST(xchar_test, is_formattable) {
88   static_assert(!fmt::is_formattable<const wchar_t*>::value, "");
89 }
90 
TEST(xchar_test,compile_time_string)91 TEST(xchar_test, compile_time_string) {
92   EXPECT_EQ(fmt::format(fmt::wformat_string<int>(L"{}"), 42), L"42");
93 #if defined(FMT_USE_STRING_VIEW) && FMT_CPLUSPLUS >= 201703L
94   EXPECT_EQ(fmt::format(FMT_STRING(std::wstring_view(L"{}")), 42), L"42");
95 #endif
96 }
97 
TEST(xchar_test,format_to)98 TEST(xchar_test, format_to) {
99   auto buf = std::vector<wchar_t>();
100   fmt::format_to(std::back_inserter(buf), L"{}{}", 42, L'\0');
101   EXPECT_STREQ(buf.data(), L"42");
102 }
103 
TEST(xchar_test,compile_time_string_format_to)104 TEST(xchar_test, compile_time_string_format_to) {
105   std::wstring ws;
106   fmt::format_to(std::back_inserter(ws), FMT_STRING(L"{}"), 42);
107   EXPECT_EQ(L"42", ws);
108 }
109 
TEST(xchar_test,vformat_to)110 TEST(xchar_test, vformat_to) {
111   int n = 42;
112   auto args = fmt::make_wformat_args(n);
113   auto w = std::wstring();
114   fmt::vformat_to(std::back_inserter(w), L"{}", args);
115   EXPECT_EQ(L"42", w);
116 }
117 
118 namespace test {
119 struct struct_as_wstring_view {};
format_as(struct_as_wstring_view)120 auto format_as(struct_as_wstring_view) -> fmt::wstring_view { return L"foo"; }
121 }  // namespace test
122 
TEST(xchar_test,format_as)123 TEST(xchar_test, format_as) {
124   EXPECT_EQ(fmt::format(L"{}", test::struct_as_wstring_view()), L"foo");
125 }
126 
TEST(format_test,wide_format_to_n)127 TEST(format_test, wide_format_to_n) {
128   wchar_t buffer[4];
129   buffer[3] = L'x';
130   auto result = fmt::format_to_n(buffer, 3, L"{}", 12345);
131   EXPECT_EQ(5u, result.size);
132   EXPECT_EQ(buffer + 3, result.out);
133   EXPECT_EQ(L"123x", fmt::wstring_view(buffer, 4));
134   buffer[0] = L'x';
135   buffer[1] = L'x';
136   buffer[2] = L'x';
137   result = fmt::format_to_n(buffer, 3, L"{}", L'A');
138   EXPECT_EQ(1u, result.size);
139   EXPECT_EQ(buffer + 1, result.out);
140   EXPECT_EQ(L"Axxx", fmt::wstring_view(buffer, 4));
141   result = fmt::format_to_n(buffer, 3, L"{}{} ", L'B', L'C');
142   EXPECT_EQ(3u, result.size);
143   EXPECT_EQ(buffer + 3, result.out);
144   EXPECT_EQ(L"BC x", fmt::wstring_view(buffer, 4));
145 }
146 
TEST(xchar_test,named_arg_udl)147 TEST(xchar_test, named_arg_udl) {
148   using namespace fmt::literals;
149   auto udl_a =
150       fmt::format(L"{first}{second}{first}{third}", L"first"_a = L"abra",
151                   L"second"_a = L"cad", L"third"_a = 99);
152   EXPECT_EQ(
153       fmt::format(L"{first}{second}{first}{third}", fmt::arg(L"first", L"abra"),
154                   fmt::arg(L"second", L"cad"), fmt::arg(L"third", 99)),
155       udl_a);
156 }
157 
TEST(xchar_test,print)158 TEST(xchar_test, print) {
159   // Check that the wide print overload compiles.
160   if (fmt::detail::const_check(false)) {
161     fmt::print(L"test");
162     fmt::println(L"test");
163   }
164 }
165 
TEST(xchar_test,join)166 TEST(xchar_test, join) {
167   int v[3] = {1, 2, 3};
168   EXPECT_EQ(fmt::format(L"({})", fmt::join(v, v + 3, L", ")), L"(1, 2, 3)");
169   auto t = std::tuple<wchar_t, int, float>('a', 1, 2.0f);
170   EXPECT_EQ(fmt::format(L"({})", fmt::join(t, L", ")), L"(a, 1, 2)");
171 }
172 
173 enum streamable_enum {};
174 
operator <<(std::wostream & os,streamable_enum)175 std::wostream& operator<<(std::wostream& os, streamable_enum) {
176   return os << L"streamable_enum";
177 }
178 
179 namespace fmt {
180 template <>
181 struct formatter<streamable_enum, wchar_t> : basic_ostream_formatter<wchar_t> {
182 };
183 }  // namespace fmt
184 
185 enum unstreamable_enum {};
format_as(unstreamable_enum e)186 auto format_as(unstreamable_enum e) -> int { return e; }
187 
TEST(xchar_test,enum)188 TEST(xchar_test, enum) {
189   EXPECT_EQ(L"streamable_enum", fmt::format(L"{}", streamable_enum()));
190   EXPECT_EQ(L"0", fmt::format(L"{}", unstreamable_enum()));
191 }
192 
193 struct streamable_and_unformattable {};
194 
operator <<(std::wostream & os,streamable_and_unformattable)195 auto operator<<(std::wostream& os, streamable_and_unformattable)
196     -> std::wostream& {
197   return os << L"foo";
198 }
199 
TEST(xchar_test,streamed)200 TEST(xchar_test, streamed) {
201   EXPECT_FALSE(fmt::is_formattable<streamable_and_unformattable>());
202   EXPECT_EQ(fmt::format(L"{}", fmt::streamed(streamable_and_unformattable())),
203             L"foo");
204 }
205 
TEST(xchar_test,sign_not_truncated)206 TEST(xchar_test, sign_not_truncated) {
207   wchar_t format_str[] = {
208       L'{', L':',
209       '+' | static_cast<wchar_t>(1 << fmt::detail::num_bits<char>()), L'}', 0};
210   EXPECT_THROW(fmt::format(fmt::runtime(format_str), 42), fmt::format_error);
211 }
212 
TEST(xchar_test,chrono)213 TEST(xchar_test, chrono) {
214   auto tm = std::tm();
215   tm.tm_year = 116;
216   tm.tm_mon = 3;
217   tm.tm_mday = 25;
218   tm.tm_hour = 11;
219   tm.tm_min = 22;
220   tm.tm_sec = 33;
221   EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
222             "The date is 2016-04-25 11:22:33.");
223   EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds(42)));
224   EXPECT_EQ(fmt::format(L"{:%F}", tm), L"2016-04-25");
225   EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33");
226 }
227 
system_wcsftime(const std::wstring & format,const std::tm * timeptr,std::locale * locptr=nullptr)228 std::wstring system_wcsftime(const std::wstring& format, const std::tm* timeptr,
229                              std::locale* locptr = nullptr) {
230   auto loc = locptr ? *locptr : std::locale::classic();
231   auto& facet = std::use_facet<std::time_put<wchar_t>>(loc);
232   std::wostringstream os;
233   os.imbue(loc);
234   facet.put(os, os, L' ', timeptr, format.c_str(),
235             format.c_str() + format.size());
236 #ifdef _WIN32
237   // Workaround a bug in older versions of Universal CRT.
238   auto str = os.str();
239   if (str == L"-0000") str = L"+0000";
240   return str;
241 #else
242   return os.str();
243 #endif
244 }
245 
TEST(chrono_test_wchar,time_point)246 TEST(chrono_test_wchar, time_point) {
247   auto t1 = std::chrono::time_point_cast<std::chrono::seconds>(
248       std::chrono::system_clock::now());
249 
250   std::vector<std::wstring> spec_list = {
251       L"%%",  L"%n",  L"%t",  L"%Y",  L"%EY", L"%y",  L"%Oy", L"%Ey", L"%C",
252       L"%EC", L"%G",  L"%g",  L"%b",  L"%h",  L"%B",  L"%m",  L"%Om", L"%U",
253       L"%OU", L"%W",  L"%OW", L"%V",  L"%OV", L"%j",  L"%d",  L"%Od", L"%e",
254       L"%Oe", L"%a",  L"%A",  L"%w",  L"%Ow", L"%u",  L"%Ou", L"%H",  L"%OH",
255       L"%I",  L"%OI", L"%M",  L"%OM", L"%S",  L"%OS", L"%x",  L"%Ex", L"%X",
256       L"%EX", L"%D",  L"%F",  L"%R",  L"%T",  L"%p"};
257 #ifndef _WIN32
258   // Disabled on Windows, because these formats is not consistent among
259   // platforms.
260   spec_list.insert(spec_list.end(), {L"%c", L"%Ec", L"%r"});
261 #elif !FMT_HAS_C99_STRFTIME
262   // Only C89 conversion specifiers when using MSVCRT instead of UCRT
263   spec_list = {L"%%", L"%Y", L"%y", L"%b", L"%B", L"%m", L"%U",
264                L"%W", L"%j", L"%d", L"%a", L"%A", L"%w", L"%H",
265                L"%I", L"%M", L"%S", L"%x", L"%X", L"%p"};
266 #endif
267   spec_list.push_back(L"%Y-%m-%d %H:%M:%S");
268 
269   for (const auto& spec : spec_list) {
270     auto t = std::chrono::system_clock::to_time_t(t1);
271     auto tm = *std::gmtime(&t);
272 
273     auto sys_output = system_wcsftime(spec, &tm);
274 
275     auto fmt_spec = fmt::format(L"{{:{}}}", spec);
276     EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1));
277     EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
278   }
279 
280   // Timezone formatters tests makes sense for localtime.
281 #if FMT_HAS_C99_STRFTIME
282   spec_list = {L"%z", L"%Z"};
283 #else
284   spec_list = {L"%Z"};
285 #endif
286   for (const auto& spec : spec_list) {
287     auto t = std::chrono::system_clock::to_time_t(t1);
288     auto tm = *std::localtime(&t);
289 
290     auto sys_output = system_wcsftime(spec, &tm);
291 
292     auto fmt_spec = fmt::format(L"{{:{}}}", spec);
293     EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
294 
295     if (spec == L"%z") {
296       sys_output.insert(sys_output.end() - 2, 1, L':');
297       EXPECT_EQ(sys_output, fmt::format(L"{:%Ez}", tm));
298       EXPECT_EQ(sys_output, fmt::format(L"{:%Oz}", tm));
299     }
300   }
301 
302   // Separate tests for UTC, since std::time_put can use local time and ignoring
303   // the timezone in std::tm (if it presents on platform).
304   if (fmt::detail::has_member_data_tm_zone<std::tm>::value) {
305     auto t = std::chrono::system_clock::to_time_t(t1);
306     auto tm = *std::gmtime(&t);
307 
308     std::vector<std::wstring> tz_names = {L"GMT", L"UTC"};
309     EXPECT_THAT(tz_names, Contains(fmt::format(L"{:%Z}", t1)));
310     EXPECT_THAT(tz_names, Contains(fmt::format(L"{:%Z}", tm)));
311   }
312 
313   if (fmt::detail::has_member_data_tm_gmtoff<std::tm>::value) {
314     auto t = std::chrono::system_clock::to_time_t(t1);
315     auto tm = *std::gmtime(&t);
316 
317     EXPECT_EQ(L"+0000", fmt::format(L"{:%z}", t1));
318     EXPECT_EQ(L"+0000", fmt::format(L"{:%z}", tm));
319 
320     EXPECT_EQ(L"+00:00", fmt::format(L"{:%Ez}", t1));
321     EXPECT_EQ(L"+00:00", fmt::format(L"{:%Ez}", tm));
322 
323     EXPECT_EQ(L"+00:00", fmt::format(L"{:%Oz}", t1));
324     EXPECT_EQ(L"+00:00", fmt::format(L"{:%Oz}", tm));
325   }
326 }
327 
TEST(xchar_test,color)328 TEST(xchar_test, color) {
329   EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), L"rgb(255,20,30) wide"),
330             L"\x1b[38;2;255;020;030mrgb(255,20,30) wide\x1b[0m");
331 }
332 
TEST(xchar_test,ostream)333 TEST(xchar_test, ostream) {
334 #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 409
335   {
336     std::wostringstream wos;
337     fmt::print(wos, L"Don't {}!", L"panic");
338     EXPECT_EQ(wos.str(), L"Don't panic!");
339   }
340 
341   {
342     std::wostringstream wos;
343     fmt::println(wos, L"Don't {}!", L"panic");
344     EXPECT_EQ(wos.str(), L"Don't panic!\n");
345   }
346 #endif
347 }
348 
TEST(xchar_test,format_map)349 TEST(xchar_test, format_map) {
350   auto m = std::map<std::wstring, int>{{L"one", 1}, {L"t\"wo", 2}};
351   EXPECT_EQ(fmt::format(L"{}", m), L"{\"one\": 1, \"t\\\"wo\": 2}");
352 }
353 
TEST(xchar_test,escape_string)354 TEST(xchar_test, escape_string) {
355   using vec = std::vector<std::wstring>;
356   EXPECT_EQ(fmt::format(L"{}", vec{L"\n\r\t\"\\"}), L"[\"\\n\\r\\t\\\"\\\\\"]");
357   EXPECT_EQ(fmt::format(L"{}", vec{L"понедельник"}), L"[\"понедельник\"]");
358 }
359 
TEST(xchar_test,to_wstring)360 TEST(xchar_test, to_wstring) { EXPECT_EQ(L"42", fmt::to_wstring(42)); }
361 
362 #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
363 
364 template <typename Char> struct numpunct : std::numpunct<Char> {
365  protected:
do_decimal_pointnumpunct366   Char do_decimal_point() const override { return '?'; }
do_groupingnumpunct367   std::string do_grouping() const override { return "\03"; }
do_thousands_sepnumpunct368   Char do_thousands_sep() const override { return '~'; }
369 };
370 
371 template <typename Char> struct no_grouping : std::numpunct<Char> {
372  protected:
do_decimal_pointno_grouping373   Char do_decimal_point() const override { return '.'; }
do_groupingno_grouping374   std::string do_grouping() const override { return ""; }
do_thousands_sepno_grouping375   Char do_thousands_sep() const override { return ','; }
376 };
377 
378 template <typename Char> struct special_grouping : std::numpunct<Char> {
379  protected:
do_decimal_pointspecial_grouping380   Char do_decimal_point() const override { return '.'; }
do_groupingspecial_grouping381   std::string do_grouping() const override { return "\03\02"; }
do_thousands_sepspecial_grouping382   Char do_thousands_sep() const override { return ','; }
383 };
384 
385 template <typename Char> struct small_grouping : std::numpunct<Char> {
386  protected:
do_decimal_pointsmall_grouping387   Char do_decimal_point() const override { return '.'; }
do_groupingsmall_grouping388   std::string do_grouping() const override { return "\01"; }
do_thousands_sepsmall_grouping389   Char do_thousands_sep() const override { return ','; }
390 };
391 
TEST(locale_test,localized_double)392 TEST(locale_test, localized_double) {
393   auto loc = std::locale(std::locale(), new numpunct<char>());
394   EXPECT_EQ(fmt::format(loc, "{:L}", 1.23), "1?23");
395   EXPECT_EQ(fmt::format(loc, "{:Lf}", 1.23), "1?230000");
396   EXPECT_EQ(fmt::format(loc, "{:L}", 1234.5), "1~234?5");
397   EXPECT_EQ(fmt::format(loc, "{:L}", 12000.0), "12~000");
398   EXPECT_EQ(fmt::format(loc, "{:8L}", 1230.0), "   1~230");
399   EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 0.1), "       0?100000");
400   EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 1.0), "       1?000000");
401   EXPECT_EQ(fmt::format(loc, "{:15.6Lf}", 1e3), "   1~000?000000");
402 }
403 
TEST(locale_test,format)404 TEST(locale_test, format) {
405   auto loc = std::locale(std::locale(), new numpunct<char>());
406   EXPECT_EQ("1234567", fmt::format(std::locale(), "{:L}", 1234567));
407   EXPECT_EQ("1~234~567", fmt::format(loc, "{:L}", 1234567));
408   EXPECT_EQ("-1~234~567", fmt::format(loc, "{:L}", -1234567));
409   EXPECT_EQ("-256", fmt::format(loc, "{:L}", -256));
410   auto n = 1234567;
411   EXPECT_EQ("1~234~567", fmt::vformat(loc, "{:L}", fmt::make_format_args(n)));
412   auto s = std::string();
413   fmt::format_to(std::back_inserter(s), loc, "{:L}", 1234567);
414   EXPECT_EQ("1~234~567", s);
415 
416   auto no_grouping_loc = std::locale(std::locale(), new no_grouping<char>());
417   EXPECT_EQ("1234567", fmt::format(no_grouping_loc, "{:L}", 1234567));
418 
419   auto special_grouping_loc =
420       std::locale(std::locale(), new special_grouping<char>());
421   EXPECT_EQ("1,23,45,678", fmt::format(special_grouping_loc, "{:L}", 12345678));
422   EXPECT_EQ("12,345", fmt::format(special_grouping_loc, "{:L}", 12345));
423 
424   auto small_grouping_loc =
425       std::locale(std::locale(), new small_grouping<char>());
426   EXPECT_EQ("4,2,9,4,9,6,7,2,9,5",
427             fmt::format(small_grouping_loc, "{:L}", max_value<uint32_t>()));
428 }
429 
TEST(locale_test,format_default_align)430 TEST(locale_test, format_default_align) {
431   auto loc = std::locale({}, new special_grouping<char>());
432   EXPECT_EQ("  12,345", fmt::format(loc, "{:8L}", 12345));
433 }
434 
TEST(locale_test,format_plus)435 TEST(locale_test, format_plus) {
436   auto loc = std::locale({}, new special_grouping<char>());
437   EXPECT_EQ("+100", fmt::format(loc, "{:+L}", 100));
438 }
439 
TEST(locale_test,wformat)440 TEST(locale_test, wformat) {
441   auto loc = std::locale(std::locale(), new numpunct<wchar_t>());
442   EXPECT_EQ(L"1234567", fmt::format(std::locale(), L"{:L}", 1234567));
443   EXPECT_EQ(L"1~234~567", fmt::format(loc, L"{:L}", 1234567));
444   int n = 1234567;
445   EXPECT_EQ(L"1~234~567",
446             fmt::vformat(loc, L"{:L}", fmt::make_wformat_args(n)));
447   EXPECT_EQ(L"1234567", fmt::format(std::locale("C"), L"{:L}", 1234567));
448 
449   auto no_grouping_loc = std::locale(std::locale(), new no_grouping<wchar_t>());
450   EXPECT_EQ(L"1234567", fmt::format(no_grouping_loc, L"{:L}", 1234567));
451 
452   auto special_grouping_loc =
453       std::locale(std::locale(), new special_grouping<wchar_t>());
454   EXPECT_EQ(L"1,23,45,678",
455             fmt::format(special_grouping_loc, L"{:L}", 12345678));
456 
457   auto small_grouping_loc =
458       std::locale(std::locale(), new small_grouping<wchar_t>());
459   EXPECT_EQ(L"4,2,9,4,9,6,7,2,9,5",
460             fmt::format(small_grouping_loc, L"{:L}", max_value<uint32_t>()));
461 }
462 
TEST(locale_test,int_formatter)463 TEST(locale_test, int_formatter) {
464   auto loc = std::locale(std::locale(), new special_grouping<char>());
465   auto f = fmt::formatter<int>();
466   auto parse_ctx = fmt::format_parse_context("L");
467   f.parse(parse_ctx);
468   auto buf = fmt::memory_buffer();
469   fmt::basic_format_context<fmt::appender, char> format_ctx(
470       fmt::appender(buf), {}, fmt::detail::locale_ref(loc));
471   f.format(12345, format_ctx);
472   EXPECT_EQ(fmt::to_string(buf), "12,345");
473 }
474 
TEST(locale_test,chrono_weekday)475 TEST(locale_test, chrono_weekday) {
476   auto loc = get_locale("es_ES.UTF-8", "Spanish_Spain.1252");
477   auto loc_old = std::locale::global(loc);
478   auto sat = fmt::weekday(6);
479   EXPECT_EQ(fmt::format(L"{}", sat), L"Sat");
480   if (loc != std::locale::classic()) {
481     // L'\341' is 'á'.
482     auto saturdays =
483         std::vector<std::wstring>{L"s\341b", L"s\341.", L"s\341b."};
484     EXPECT_THAT(saturdays, Contains(fmt::format(loc, L"{:L}", sat)));
485   }
486   std::locale::global(loc_old);
487 }
488 
TEST(locale_test,sign)489 TEST(locale_test, sign) {
490   EXPECT_EQ(fmt::format(std::locale(), L"{:L}", -50), L"-50");
491 }
492 
TEST(std_test_xchar,complex)493 TEST(std_test_xchar, complex) {
494   auto s = fmt::format(L"{}", std::complex<double>(1, 2));
495   EXPECT_EQ(s, L"(1+2i)");
496   EXPECT_EQ(fmt::format(L"{:.2f}", std::complex<double>(1, 2)),
497             L"(1.00+2.00i)");
498   EXPECT_EQ(fmt::format(L"{:8}", std::complex<double>(1, 2)), L"(1+2i)  ");
499 }
500 
TEST(std_test_xchar,optional)501 TEST(std_test_xchar, optional) {
502 #  ifdef __cpp_lib_optional
503   EXPECT_EQ(fmt::format(L"{}", std::optional{L'C'}), L"optional(\'C\')");
504   EXPECT_EQ(fmt::format(L"{}", std::optional{std::wstring{L"wide string"}}),
505             L"optional(\"wide string\")");
506 #  endif
507 }
508 
509 #endif  // FMT_STATIC_THOUSANDS_SEPARATOR
510