module-test.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // Formatting library for C++ - module 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. // Copyright (c) 2021 - present, Daniela Engert
  9. // All Rights Reserved
  10. // {fmt} module.
  11. #ifdef _MSC_FULL_VER
  12. // hide some implementation bugs in msvc
  13. // that are not essential to users of the module.
  14. # define FMT_HIDE_MODULE_BUGS
  15. #endif
  16. #define FMT_MODULE_TEST
  17. #include <bit>
  18. #include <chrono>
  19. #include <exception>
  20. #include <iterator>
  21. #include <locale>
  22. #include <memory>
  23. #include <ostream>
  24. #include <string>
  25. #include <string_view>
  26. #include <system_error>
  27. #if (__has_include(<fcntl.h>) || defined(__APPLE__) || \
  28. defined(__linux__)) && \
  29. (!defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP))
  30. # include <fcntl.h>
  31. # define FMT_USE_FCNTL 1
  32. #else
  33. # define FMT_USE_FCNTL 0
  34. #endif
  35. #if defined(_WIN32) && !defined(__MINGW32__)
  36. # define FMT_POSIX(call) _##call
  37. #else
  38. # define FMT_POSIX(call) call
  39. #endif
  40. #define FMT_OS_H_ // don't pull in os.h directly or indirectly
  41. import fmt;
  42. // check for macros leaking from BMI
  43. static bool macro_leaked =
  44. #if defined(FMT_CORE_H_) || defined(FMT_FORMAT_H_)
  45. true;
  46. #else
  47. false;
  48. #endif
  49. // Include sources to pick up functions and classes from the module rather than
  50. // from the non-modular library which is baked into the 'test-main' library.
  51. // This averts linker problems:
  52. // - strong ownership model: missing linker symbols
  53. // - weak ownership model: duplicate linker symbols
  54. #include "gtest-extra.cc"
  55. #include "util.cc"
  56. // an implicitly exported namespace must be visible [module.interface]/2.2
  57. TEST(module_test, namespace) {
  58. using namespace fmt;
  59. using namespace fmt::literals;
  60. ASSERT_TRUE(true);
  61. }
  62. namespace detail {
  63. bool oops_detail_namespace_is_visible;
  64. }
  65. namespace fmt {
  66. bool namespace_detail_invisible() {
  67. #if defined(FMT_HIDE_MODULE_BUGS) && defined(_MSC_FULL_VER) && \
  68. ((_MSC_VER == 1929 && _MSC_FULL_VER <= 192930136) || \
  69. (_MSC_VER == 1930 && _MSC_FULL_VER <= 193030704))
  70. // bug in msvc up to 16.11.5 / 17.0-pre5:
  71. // the namespace is visible even when it is neither
  72. // implicitly nor explicitly exported
  73. return true;
  74. #else
  75. using namespace detail;
  76. // this fails to compile if fmt::detail is visible
  77. return !oops_detail_namespace_is_visible;
  78. #endif
  79. }
  80. } // namespace fmt
  81. // the non-exported namespace 'detail' must be invisible [module.interface]/2
  82. TEST(module_test, detail_namespace) {
  83. EXPECT_TRUE(fmt::namespace_detail_invisible());
  84. }
  85. // macros must not be imported from a *named* module [cpp.import]/5.1
  86. TEST(module_test, macros) {
  87. #if defined(FMT_HIDE_MODULE_BUGS) && defined(_MSC_FULL_VER) && \
  88. _MSC_FULL_VER <= 192930130
  89. // bug in msvc up to 16.11-pre2:
  90. // include-guard macros leak from BMI
  91. // and even worse: they cannot be #undef-ined
  92. macro_leaked = false;
  93. #endif
  94. EXPECT_FALSE(macro_leaked);
  95. }
  96. // The following is less about functional testing (that's done elsewhere)
  97. // but rather visibility of all client-facing overloads, reachability of
  98. // non-exported entities, name lookup and overload resolution within
  99. // template instantitions.
  100. // Excercise all exported entities of the API at least once.
  101. // Instantiate as many code paths as possible.
  102. TEST(module_test, to_string) {
  103. EXPECT_EQ("42", fmt::to_string(42));
  104. EXPECT_EQ("42", fmt::to_string(42.0));
  105. EXPECT_EQ(L"42", fmt::to_wstring(42));
  106. EXPECT_EQ(L"42", fmt::to_wstring(42.0));
  107. }
  108. TEST(module_test, format) {
  109. EXPECT_EQ("42", fmt::format("{:}", 42));
  110. EXPECT_EQ("-42", fmt::format("{0}", -42.0));
  111. EXPECT_EQ(L"42", fmt::format(L"{:}", 42));
  112. EXPECT_EQ(L"-42", fmt::format(L"{0}", -42.0));
  113. }
  114. TEST(module_test, format_to) {
  115. std::string s;
  116. fmt::format_to(std::back_inserter(s), "{}", 42);
  117. EXPECT_EQ("42", s);
  118. char buffer[4] = {0};
  119. fmt::format_to(buffer, "{}", 42);
  120. EXPECT_EQ("42", std::string_view(buffer));
  121. fmt::memory_buffer mb;
  122. fmt::format_to(mb, "{}", 42);
  123. EXPECT_EQ("42", std::string_view(buffer));
  124. std::wstring w;
  125. fmt::format_to(std::back_inserter(w), L"{}", 42);
  126. EXPECT_EQ(L"42", w);
  127. wchar_t wbuffer[4] = {0};
  128. fmt::format_to(wbuffer, L"{}", 42);
  129. EXPECT_EQ(L"42", std::wstring_view(wbuffer));
  130. fmt::wmemory_buffer wb;
  131. fmt::format_to(wb, L"{}", 42);
  132. EXPECT_EQ(L"42", std::wstring_view(wbuffer));
  133. }
  134. TEST(module_test, formatted_size) {
  135. EXPECT_EQ(2u, fmt::formatted_size("{}", 42));
  136. EXPECT_EQ(2u, fmt::formatted_size(L"{}", 42));
  137. }
  138. TEST(module_test, format_to_n) {
  139. std::string s;
  140. auto result = fmt::format_to_n(std::back_inserter(s), 1, "{}", 42);
  141. EXPECT_EQ(2u, result.size);
  142. char buffer[4] = {0};
  143. fmt::format_to_n(buffer, 3, "{}", 12345);
  144. std::wstring w;
  145. auto wresult = fmt::format_to_n(std::back_inserter(w), 1, L"{}", 42);
  146. EXPECT_EQ(2u, wresult.size);
  147. wchar_t wbuffer[4] = {0};
  148. fmt::format_to_n(wbuffer, 3, L"{}", 12345);
  149. }
  150. TEST(module_test, format_args) {
  151. auto no_args = fmt::format_args();
  152. EXPECT_FALSE(no_args.get(1));
  153. fmt::basic_format_args args = fmt::make_format_args(42);
  154. EXPECT_TRUE(args.max_size() > 0);
  155. auto arg0 = args.get(0);
  156. EXPECT_TRUE(arg0);
  157. decltype(arg0) arg_none;
  158. EXPECT_FALSE(arg_none);
  159. EXPECT_TRUE(arg0.type() != arg_none.type());
  160. }
  161. TEST(module_test, wformat_args) {
  162. auto no_args = fmt::wformat_args();
  163. EXPECT_FALSE(no_args.get(1));
  164. fmt::basic_format_args args = fmt::make_wformat_args(42);
  165. EXPECT_TRUE(args.get(0));
  166. }
  167. TEST(module_test, dynamic_format_args) {
  168. fmt::dynamic_format_arg_store<fmt::format_context> dyn_store;
  169. dyn_store.push_back(fmt::arg("a42", 42));
  170. fmt::basic_format_args args = dyn_store;
  171. EXPECT_FALSE(args.get(3));
  172. EXPECT_TRUE(args.get(fmt::string_view("a42")));
  173. fmt::dynamic_format_arg_store<fmt::wformat_context> wdyn_store;
  174. wdyn_store.push_back(fmt::arg(L"a42", 42));
  175. fmt::basic_format_args wargs = wdyn_store;
  176. EXPECT_FALSE(wargs.get(3));
  177. EXPECT_TRUE(wargs.get(fmt::wstring_view(L"a42")));
  178. }
  179. TEST(module_test, vformat) {
  180. EXPECT_EQ("42", fmt::vformat("{}", fmt::make_format_args(42)));
  181. EXPECT_EQ(L"42", fmt::vformat(fmt::to_string_view(L"{}"),
  182. fmt::make_wformat_args(42)));
  183. }
  184. TEST(module_test, vformat_to) {
  185. auto store = fmt::make_format_args(42);
  186. std::string s;
  187. fmt::vformat_to(std::back_inserter(s), "{}", store);
  188. EXPECT_EQ("42", s);
  189. char buffer[4] = {0};
  190. fmt::vformat_to(buffer, "{:}", store);
  191. EXPECT_EQ("42", std::string_view(buffer));
  192. auto wstore = fmt::make_wformat_args(42);
  193. std::wstring w;
  194. fmt::vformat_to(std::back_inserter(w), L"{}", wstore);
  195. EXPECT_EQ(L"42", w);
  196. wchar_t wbuffer[4] = {0};
  197. fmt::vformat_to(wbuffer, L"{:}", wstore);
  198. EXPECT_EQ(L"42", std::wstring_view(wbuffer));
  199. }
  200. TEST(module_test, vformat_to_n) {
  201. auto store = fmt::make_format_args(12345);
  202. std::string s;
  203. auto result = fmt::vformat_to_n(std::back_inserter(s), 1, "{}", store);
  204. char buffer[4] = {0};
  205. fmt::vformat_to_n(buffer, 3, "{:}", store);
  206. auto wstore = fmt::make_wformat_args(12345);
  207. std::wstring w;
  208. auto wresult = fmt::vformat_to_n(std::back_inserter(w), 1,
  209. fmt::to_string_view(L"{}"), wstore);
  210. wchar_t wbuffer[4] = {0};
  211. fmt::vformat_to_n(wbuffer, 3, fmt::to_string_view(L"{:}"), wstore);
  212. }
  213. std::string as_string(std::wstring_view text) {
  214. return {reinterpret_cast<const char*>(text.data()),
  215. text.size() * sizeof(text[0])};
  216. }
  217. TEST(module_test, print) {
  218. EXPECT_WRITE(stdout, fmt::print("{}µ", 42), "42µ");
  219. EXPECT_WRITE(stderr, fmt::print(stderr, "{}µ", 4.2), "4.2µ");
  220. if (false) {
  221. EXPECT_WRITE(stdout, fmt::print(L"{}µ", 42), as_string(L"42µ"));
  222. EXPECT_WRITE(stderr, fmt::print(stderr, L"{}µ", 4.2), as_string(L"4.2µ"));
  223. }
  224. }
  225. TEST(module_test, vprint) {
  226. EXPECT_WRITE(stdout, fmt::vprint("{:}µ", fmt::make_format_args(42)), "42µ");
  227. EXPECT_WRITE(stderr, fmt::vprint(stderr, "{}", fmt::make_format_args(4.2)),
  228. "4.2");
  229. if (false) {
  230. EXPECT_WRITE(stdout, fmt::vprint(L"{:}µ", fmt::make_wformat_args(42)),
  231. as_string(L"42µ"));
  232. EXPECT_WRITE(stderr, fmt::vprint(stderr, L"{}", fmt::make_wformat_args(42)),
  233. as_string(L"42"));
  234. }
  235. }
  236. TEST(module_test, named_args) {
  237. EXPECT_EQ("42", fmt::format("{answer}", fmt::arg("answer", 42)));
  238. EXPECT_EQ(L"42", fmt::format(L"{answer}", fmt::arg(L"answer", 42)));
  239. }
  240. TEST(module_test, literals) {
  241. using namespace fmt::literals;
  242. EXPECT_EQ("42", fmt::format("{answer}", "answer"_a = 42));
  243. EXPECT_EQ("42", "{}"_format(42));
  244. EXPECT_EQ(L"42", fmt::format(L"{answer}", L"answer"_a = 42));
  245. EXPECT_EQ(L"42", L"{}"_format(42));
  246. }
  247. TEST(module_test, locale) {
  248. auto store = fmt::make_format_args(4.2);
  249. const auto classic = std::locale::classic();
  250. EXPECT_EQ("4.2", fmt::format(classic, "{:L}", 4.2));
  251. EXPECT_EQ("4.2", fmt::vformat(classic, "{:L}", store));
  252. std::string s;
  253. fmt::vformat_to(std::back_inserter(s), classic, "{:L}", store);
  254. EXPECT_EQ("4.2", s);
  255. EXPECT_EQ("4.2", fmt::format("{:L}", 4.2));
  256. auto wstore = fmt::make_wformat_args(4.2);
  257. EXPECT_EQ(L"4.2", fmt::format(classic, L"{:L}", 4.2));
  258. EXPECT_EQ(L"4.2", fmt::vformat(classic, L"{:L}", wstore));
  259. std::wstring w;
  260. fmt::vformat_to(std::back_inserter(w), classic, L"{:L}", wstore);
  261. EXPECT_EQ(L"4.2", w);
  262. EXPECT_EQ(L"4.2", fmt::format(L"{:L}", 4.2));
  263. }
  264. TEST(module_test, string_view) {
  265. fmt::string_view nsv("fmt");
  266. EXPECT_EQ("fmt", nsv);
  267. EXPECT_TRUE(fmt::string_view("fmt") == nsv);
  268. fmt::wstring_view wsv(L"fmt");
  269. EXPECT_EQ(L"fmt", wsv);
  270. EXPECT_TRUE(fmt::wstring_view(L"fmt") == wsv);
  271. }
  272. TEST(module_test, memory_buffer) {
  273. fmt::basic_memory_buffer<char, fmt::inline_buffer_size> buffer;
  274. fmt::format_to(buffer, "{}", "42");
  275. EXPECT_EQ("42", to_string(buffer));
  276. fmt::memory_buffer nbuffer(std::move(buffer));
  277. EXPECT_EQ("42", to_string(nbuffer));
  278. buffer = std::move(nbuffer);
  279. EXPECT_EQ("42", to_string(buffer));
  280. nbuffer.clear();
  281. EXPECT_EQ(0u, to_string(nbuffer).size());
  282. fmt::wmemory_buffer wbuffer;
  283. EXPECT_EQ(0u, to_string(wbuffer).size());
  284. }
  285. TEST(module_test, is_char) {
  286. EXPECT_TRUE(fmt::is_char<char>());
  287. EXPECT_TRUE(fmt::is_char<wchar_t>());
  288. EXPECT_TRUE(fmt::is_char<char8_t>());
  289. EXPECT_TRUE(fmt::is_char<char16_t>());
  290. EXPECT_TRUE(fmt::is_char<char32_t>());
  291. EXPECT_FALSE(fmt::is_char<signed char>());
  292. }
  293. TEST(module_test, ptr) {
  294. uintptr_t answer = 42;
  295. auto p = std::bit_cast<int*>(answer);
  296. EXPECT_EQ("0x2a", fmt::to_string(fmt::ptr(p)));
  297. std::unique_ptr<int> up(p);
  298. EXPECT_EQ("0x2a", fmt::to_string(fmt::ptr(up)));
  299. up.release();
  300. auto sp = std::make_shared<int>(0);
  301. p = sp.get();
  302. EXPECT_EQ(fmt::to_string(fmt::ptr(p)), fmt::to_string(fmt::ptr(sp)));
  303. }
  304. TEST(module_test, errors) {
  305. auto store = fmt::make_format_args(42);
  306. EXPECT_THROW(throw fmt::format_error("oops"), std::exception);
  307. EXPECT_THROW(throw fmt::vsystem_error(0, "{}", store), std::system_error);
  308. EXPECT_THROW(throw fmt::system_error(0, "{}", 42), std::system_error);
  309. fmt::memory_buffer buffer;
  310. fmt::format_system_error(buffer, 0, "oops");
  311. auto oops = to_string(buffer);
  312. EXPECT_TRUE(oops.size() > 0);
  313. EXPECT_WRITE(stderr, fmt::report_system_error(0, "oops"), oops + '\n');
  314. #ifdef _WIN32
  315. EXPECT_THROW(throw fmt::vwindows_error(0, "{}", store), std::system_error);
  316. EXPECT_THROW(throw fmt::windows_error(0, "{}", 42), std::system_error);
  317. output_redirect redirect(stderr);
  318. fmt::report_windows_error(0, "oops");
  319. EXPECT_TRUE(redirect.restore_and_read().size() > 0);
  320. #endif
  321. }
  322. TEST(module_test, error_code) {
  323. EXPECT_EQ("generic:42",
  324. fmt::format("{0}", std::error_code(42, std::generic_category())));
  325. EXPECT_EQ("system:42",
  326. fmt::format("{0}", std::error_code(42, fmt::system_category())));
  327. EXPECT_EQ(L"generic:42",
  328. fmt::format(L"{0}", std::error_code(42, std::generic_category())));
  329. }
  330. TEST(module_test, format_int) {
  331. fmt::format_int sanswer(42);
  332. EXPECT_EQ("42", fmt::string_view(sanswer.data(), sanswer.size()));
  333. fmt::format_int uanswer(42u);
  334. EXPECT_EQ("42", fmt::string_view(uanswer.data(), uanswer.size()));
  335. }
  336. struct test_formatter : fmt::formatter<char> {
  337. bool check() { return true; }
  338. };
  339. struct test_dynamic_formatter : fmt::dynamic_formatter<> {
  340. bool check() { return true; }
  341. };
  342. TEST(module_test, formatter) {
  343. EXPECT_TRUE(test_formatter{}.check());
  344. EXPECT_TRUE(test_dynamic_formatter{}.check());
  345. }
  346. TEST(module_test, join) {
  347. int arr[3] = {1, 2, 3};
  348. std::vector<double> vec{1.0, 2.0, 3.0};
  349. std::initializer_list<int> il{1, 2, 3};
  350. auto sep = fmt::to_string_view(", ");
  351. EXPECT_EQ("1, 2, 3", to_string(fmt::join(arr + 0, arr + 3, sep)));
  352. EXPECT_EQ("1, 2, 3", to_string(fmt::join(arr, sep)));
  353. EXPECT_EQ("1, 2, 3", to_string(fmt::join(vec.begin(), vec.end(), sep)));
  354. EXPECT_EQ("1, 2, 3", to_string(fmt::join(vec, sep)));
  355. EXPECT_EQ("1, 2, 3", to_string(fmt::join(il, sep)));
  356. auto wsep = fmt::to_string_view(L", ");
  357. EXPECT_EQ(L"1, 2, 3", fmt::format(L"{}", fmt::join(arr + 0, arr + 3, wsep)));
  358. EXPECT_EQ(L"1, 2, 3", fmt::format(L"{}", fmt::join(arr, wsep)));
  359. EXPECT_EQ(L"1, 2, 3", fmt::format(L"{}", fmt::join(il, wsep)));
  360. }
  361. TEST(module_test, time) {
  362. auto time_now = std::time(nullptr);
  363. EXPECT_TRUE(fmt::localtime(time_now).tm_year > 120);
  364. EXPECT_TRUE(fmt::gmtime(time_now).tm_year > 120);
  365. auto chrono_now = std::chrono::system_clock::now();
  366. EXPECT_TRUE(fmt::localtime(chrono_now).tm_year > 120);
  367. EXPECT_TRUE(fmt::gmtime(chrono_now).tm_year > 120);
  368. }
  369. TEST(module_test, time_point) {
  370. auto now = std::chrono::system_clock::now();
  371. std::string_view past("2021-05-20 10:30:15");
  372. EXPECT_TRUE(past < fmt::format("{:%Y-%m-%d %H:%M:%S}", now));
  373. std::wstring_view wpast(L"2021-05-20 10:30:15");
  374. EXPECT_TRUE(wpast < fmt::format(L"{:%Y-%m-%d %H:%M:%S}", now));
  375. }
  376. TEST(module_test, time_duration) {
  377. using us = std::chrono::duration<double, std::micro>;
  378. EXPECT_EQ("42s", fmt::format("{}", std::chrono::seconds{42}));
  379. EXPECT_EQ("4.2µs", fmt::format("{:3.1}", us{4.234}));
  380. EXPECT_EQ("4.2µs", fmt::format(std::locale::classic(), "{:L}", us{4.2}));
  381. EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds{42}));
  382. EXPECT_EQ(L"4.2µs", fmt::format(L"{:3.1}", us{4.234}));
  383. EXPECT_EQ(L"4.2µs", fmt::format(std::locale::classic(), L"{:L}", us{4.2}));
  384. }
  385. TEST(module_test, weekday) {
  386. EXPECT_EQ("Mon", fmt::format(std::locale::classic(), "{}", fmt::weekday(1)));
  387. }
  388. TEST(module_test, to_string_view) {
  389. using fmt::to_string_view;
  390. fmt::string_view nsv{to_string_view("42")};
  391. EXPECT_EQ("42", nsv);
  392. fmt::wstring_view wsv{to_string_view(L"42")};
  393. EXPECT_EQ(L"42", wsv);
  394. }
  395. TEST(module_test, printf) {
  396. EXPECT_WRITE(stdout, fmt::printf("%f", 42.123456), "42.123456");
  397. EXPECT_WRITE(stdout, fmt::printf("%d", 42), "42");
  398. if (false) {
  399. EXPECT_WRITE(stdout, fmt::printf(L"%f", 42.123456),
  400. as_string(L"42.123456"));
  401. EXPECT_WRITE(stdout, fmt::printf(L"%d", 42), as_string(L"42"));
  402. }
  403. }
  404. TEST(module_test, fprintf) {
  405. EXPECT_WRITE(stderr, fmt::fprintf(stderr, "%d", 42), "42");
  406. std::ostringstream os;
  407. fmt::fprintf(os, "%s", "bla");
  408. EXPECT_EQ("bla", os.str());
  409. EXPECT_WRITE(stderr, fmt::fprintf(stderr, L"%d", 42), as_string(L"42"));
  410. std::wostringstream ws;
  411. fmt::fprintf(ws, L"%s", L"bla");
  412. EXPECT_EQ(L"bla", ws.str());
  413. }
  414. TEST(module_test, sprintf) {
  415. EXPECT_EQ("42", fmt::sprintf("%d", 42));
  416. EXPECT_EQ(L"42", fmt::sprintf(L"%d", 42));
  417. }
  418. TEST(module_test, vprintf) {
  419. EXPECT_WRITE(stdout, fmt::vprintf("%d", fmt::make_printf_args(42)), "42");
  420. if (false) {
  421. EXPECT_WRITE(stdout, fmt::vprintf(L"%d", fmt::make_wprintf_args(42)),
  422. as_string(L"42"));
  423. }
  424. }
  425. TEST(module_test, vfprintf) {
  426. auto args = fmt::make_printf_args(42);
  427. EXPECT_WRITE(stderr, fmt::vfprintf(stderr, "%d", args), "42");
  428. std::ostringstream os;
  429. fmt::vfprintf(os, "%d", args);
  430. EXPECT_EQ("42", os.str());
  431. auto wargs = fmt::make_wprintf_args(42);
  432. if (false) {
  433. EXPECT_WRITE(stderr, fmt::vfprintf(stderr, L"%d", wargs), as_string(L"42"));
  434. }
  435. std::wostringstream ws;
  436. fmt::vfprintf(ws, L"%d", wargs);
  437. EXPECT_EQ(L"42", ws.str());
  438. }
  439. TEST(module_test, vsprintf) {
  440. EXPECT_EQ("42", fmt::vsprintf("%d", fmt::make_printf_args(42)));
  441. EXPECT_EQ(L"42", fmt::vsprintf(L"%d", fmt::make_wprintf_args(42)));
  442. }
  443. TEST(module_test, color) {
  444. auto fg_check = fg(fmt::rgb(255, 200, 30));
  445. auto bg_check = bg(fmt::color::dark_slate_gray) | fmt::emphasis::italic;
  446. auto emphasis_check = fmt::emphasis::underline | fmt::emphasis::bold;
  447. EXPECT_EQ("\x1B[30m42\x1B[0m",
  448. fmt::format(fg(fmt::terminal_color::black), "{}", 42));
  449. EXPECT_EQ(L"\x1B[30m42\x1B[0m",
  450. fmt::format(fg(fmt::terminal_color::black), L"{}", 42));
  451. }
  452. TEST(module_test, cstring_view) {
  453. auto s = "fmt";
  454. EXPECT_EQ(s, fmt::cstring_view(s).c_str());
  455. auto w = L"fmt";
  456. EXPECT_EQ(w, fmt::wcstring_view(w).c_str());
  457. }
  458. TEST(module_test, buffered_file) {
  459. EXPECT_TRUE(fmt::buffered_file{}.get() == nullptr);
  460. }
  461. TEST(module_test, output_file) {
  462. fmt::ostream out = fmt::output_file("module-test", fmt::buffer_size = 1);
  463. out.close();
  464. }
  465. struct custom_context {
  466. using char_type = char;
  467. using parse_context_type = fmt::format_parse_context;
  468. };
  469. TEST(module_test, custom_context) {
  470. fmt::basic_format_arg<custom_context> custom_arg;
  471. EXPECT_TRUE(!custom_arg);
  472. }
  473. struct disabled_formatter {};
  474. TEST(module_test, has_formatter) {
  475. EXPECT_FALSE(
  476. (fmt::has_formatter<disabled_formatter, fmt::format_context>::value));
  477. }
  478. TEST(module_test, is_formattable) {
  479. EXPECT_FALSE(fmt::is_formattable<disabled_formatter>::value);
  480. }
  481. TEST(module_test, compile_format_string) {
  482. using namespace fmt::literals;
  483. EXPECT_EQ("42", fmt::format("{0:x}"_cf, 0x42));
  484. EXPECT_EQ(L"42", fmt::format(L"{:}"_cf, 42));
  485. EXPECT_EQ("4.2", fmt::format("{arg:3.1f}"_cf, "arg"_a = 4.2));
  486. EXPECT_EQ(L" 42", fmt::format(L"{arg:>3}"_cf, L"arg"_a = L"42"));
  487. }