ostream-test.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Formatting library for C++ - std::ostream support tests
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #include <fstream>
  8. #include "fmt/format.h"
  9. using fmt::runtime;
  10. struct test {};
  11. // Test that there is no issues with specializations when fmt/ostream.h is
  12. // included after fmt/format.h.
  13. namespace fmt {
  14. template <> struct formatter<test> : formatter<int> {
  15. auto format(const test&, format_context& ctx) -> decltype(ctx.out()) {
  16. return formatter<int>::format(42, ctx);
  17. }
  18. };
  19. } // namespace fmt
  20. #include <sstream>
  21. #include "fmt/compile.h"
  22. #include "fmt/ostream.h"
  23. #include "fmt/ranges.h"
  24. #include "gmock/gmock.h"
  25. #include "gtest-extra.h"
  26. #include "util.h"
  27. auto operator<<(std::ostream& os, const date& d) -> std::ostream& {
  28. os << d.year() << '-' << d.month() << '-' << d.day();
  29. return os;
  30. }
  31. auto operator<<(std::wostream& os, const date& d) -> std::wostream& {
  32. os << d.year() << L'-' << d.month() << L'-' << d.day();
  33. return os;
  34. }
  35. // Make sure that overloaded comma operators do no harm to is_streamable.
  36. struct type_with_comma_op {};
  37. template <typename T> void operator,(type_with_comma_op, const T&);
  38. template <typename T> type_with_comma_op operator<<(T&, const date&);
  39. enum streamable_enum {};
  40. auto operator<<(std::ostream& os, streamable_enum) -> std::ostream& {
  41. return os << "streamable_enum";
  42. }
  43. enum unstreamable_enum {};
  44. auto format_as(unstreamable_enum e) -> int { return e; }
  45. struct empty_test {};
  46. auto operator<<(std::ostream& os, empty_test) -> std::ostream& {
  47. return os << "";
  48. }
  49. namespace fmt {
  50. template <> struct formatter<test_string> : ostream_formatter {};
  51. template <> struct formatter<date> : ostream_formatter {};
  52. template <> struct formatter<streamable_enum> : ostream_formatter {};
  53. template <> struct formatter<empty_test> : ostream_formatter {};
  54. } // namespace fmt
  55. TEST(ostream_test, enum) {
  56. EXPECT_EQ("streamable_enum", fmt::format("{}", streamable_enum()));
  57. EXPECT_EQ("0", fmt::format("{}", unstreamable_enum()));
  58. }
  59. TEST(ostream_test, format) {
  60. EXPECT_EQ("a string", fmt::format("{0}", test_string("a string")));
  61. EXPECT_EQ("The date is 2012-12-9",
  62. fmt::format("The date is {0}", date(2012, 12, 9)));
  63. }
  64. TEST(ostream_test, format_specs) {
  65. using fmt::format_error;
  66. EXPECT_EQ("def ", fmt::format("{0:<5}", test_string("def")));
  67. EXPECT_EQ(" def", fmt::format("{0:>5}", test_string("def")));
  68. EXPECT_EQ(" def ", fmt::format("{0:^5}", test_string("def")));
  69. EXPECT_EQ("def**", fmt::format("{0:*<5}", test_string("def")));
  70. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:+}"), test_string()),
  71. format_error, "format specifier requires numeric argument");
  72. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:-}"), test_string()),
  73. format_error, "format specifier requires numeric argument");
  74. EXPECT_THROW_MSG((void)fmt::format(runtime("{0: }"), test_string()),
  75. format_error, "format specifier requires numeric argument");
  76. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:#}"), test_string()),
  77. format_error, "format specifier requires numeric argument");
  78. EXPECT_THROW_MSG((void)fmt::format(runtime("{0:05}"), test_string()),
  79. format_error, "format specifier requires numeric argument");
  80. EXPECT_EQ("test ", fmt::format("{0:13}", test_string("test")));
  81. EXPECT_EQ("test ", fmt::format("{0:{1}}", test_string("test"), 13));
  82. EXPECT_EQ("te", fmt::format("{0:.2}", test_string("test")));
  83. EXPECT_EQ("te", fmt::format("{0:.{1}}", test_string("test"), 2));
  84. }
  85. TEST(ostream_test, empty_custom_output) {
  86. EXPECT_EQ("", fmt::format("{}", empty_test()));
  87. }
  88. TEST(ostream_test, print) {
  89. std::ostringstream os;
  90. fmt::print(os, "Don't {}!", "panic");
  91. EXPECT_EQ("Don't panic!", os.str());
  92. }
  93. TEST(ostream_test, write_to_ostream) {
  94. std::ostringstream os;
  95. fmt::memory_buffer buffer;
  96. const char* foo = "foo";
  97. buffer.append(foo, foo + std::strlen(foo));
  98. fmt::detail::write_buffer(os, buffer);
  99. EXPECT_EQ("foo", os.str());
  100. }
  101. TEST(ostream_test, write_to_ostream_max_size) {
  102. auto max_size = fmt::detail::max_value<size_t>();
  103. auto max_streamsize = fmt::detail::max_value<std::streamsize>();
  104. if (max_size <= fmt::detail::to_unsigned(max_streamsize)) return;
  105. struct test_buffer final : fmt::detail::buffer<char> {
  106. explicit test_buffer(size_t size)
  107. : fmt::detail::buffer<char>(nullptr, size, size) {}
  108. void grow(size_t) override {}
  109. } buffer(max_size);
  110. struct mock_streambuf : std::streambuf {
  111. MOCK_METHOD2(xsputn, std::streamsize(const void* s, std::streamsize n));
  112. auto xsputn(const char* s, std::streamsize n) -> std::streamsize override {
  113. const void* v = s;
  114. return xsputn(v, n);
  115. }
  116. } streambuf;
  117. struct test_ostream : std::ostream {
  118. explicit test_ostream(mock_streambuf& output_buffer)
  119. : std::ostream(&output_buffer) {}
  120. } os(streambuf);
  121. testing::InSequence sequence;
  122. const char* data = nullptr;
  123. using ustreamsize = std::make_unsigned<std::streamsize>::type;
  124. ustreamsize size = max_size;
  125. do {
  126. auto n = std::min(size, fmt::detail::to_unsigned(max_streamsize));
  127. EXPECT_CALL(streambuf, xsputn(data, static_cast<std::streamsize>(n)))
  128. .WillOnce(testing::Return(max_streamsize));
  129. data += n;
  130. size -= n;
  131. } while (size != 0);
  132. fmt::detail::write_buffer(os, buffer);
  133. }
  134. TEST(ostream_test, join) {
  135. int v[3] = {1, 2, 3};
  136. EXPECT_EQ("1, 2, 3", fmt::format("{}", fmt::join(v, v + 3, ", ")));
  137. }
  138. TEST(ostream_test, join_fallback_formatter) {
  139. auto strs = std::vector<test_string>{test_string("foo"), test_string("bar")};
  140. EXPECT_EQ("foo, bar", fmt::format("{}", fmt::join(strs, ", ")));
  141. }
  142. #if FMT_USE_CONSTEXPR
  143. TEST(ostream_test, constexpr_string) {
  144. EXPECT_EQ("42", fmt::format(FMT_STRING("{}"), std::string("42")));
  145. EXPECT_EQ("a string",
  146. fmt::format(FMT_STRING("{0}"), test_string("a string")));
  147. }
  148. #endif
  149. namespace fmt_test {
  150. struct abc {};
  151. template <typename Output> auto operator<<(Output& out, abc) -> Output& {
  152. return out << "abc";
  153. }
  154. } // namespace fmt_test
  155. template <typename T> struct test_template {};
  156. template <typename T>
  157. auto operator<<(std::ostream& os, test_template<T>) -> std::ostream& {
  158. return os << 1;
  159. }
  160. namespace fmt {
  161. template <typename T> struct formatter<test_template<T>> : formatter<int> {
  162. auto format(test_template<T>, format_context& ctx) -> decltype(ctx.out()) {
  163. return formatter<int>::format(2, ctx);
  164. }
  165. };
  166. template <> struct formatter<fmt_test::abc> : ostream_formatter {};
  167. } // namespace fmt
  168. TEST(ostream_test, template) {
  169. EXPECT_EQ("2", fmt::format("{}", test_template<int>()));
  170. }
  171. TEST(ostream_test, format_to_n) {
  172. char buffer[4];
  173. buffer[3] = 'x';
  174. auto result = fmt::format_to_n(buffer, 3, "{}", fmt_test::abc());
  175. EXPECT_EQ(3u, result.size);
  176. EXPECT_EQ(buffer + 3, result.out);
  177. EXPECT_EQ("abcx", fmt::string_view(buffer, 4));
  178. result = fmt::format_to_n(buffer, 3, "x{}y", fmt_test::abc());
  179. EXPECT_EQ(5u, result.size);
  180. EXPECT_EQ(buffer + 3, result.out);
  181. EXPECT_EQ("xabx", fmt::string_view(buffer, 4));
  182. }
  183. template <typename T> struct convertible {
  184. T value;
  185. explicit convertible(const T& val) : value(val) {}
  186. operator T() const { return value; }
  187. };
  188. TEST(ostream_test, disable_builtin_ostream_operators) {
  189. EXPECT_EQ("42", fmt::format("{:d}", convertible<unsigned short>(42)));
  190. EXPECT_EQ("foo", fmt::format("{}", convertible<const char*>("foo")));
  191. }
  192. struct streamable_and_convertible_to_bool {
  193. operator bool() const { return true; }
  194. };
  195. std::ostream& operator<<(std::ostream& os, streamable_and_convertible_to_bool) {
  196. return os << "foo";
  197. }
  198. TEST(ostream_test, format_convertible_to_bool) {
  199. // operator<< is intentionally not used because of potential ODR violations.
  200. EXPECT_EQ(fmt::format("{}", streamable_and_convertible_to_bool()), "true");
  201. }
  202. struct streamable_and_convertible_to_string_view {
  203. operator fmt::string_view() const { return "foo"; }
  204. };
  205. std::ostream& operator<<(std::ostream& os,
  206. streamable_and_convertible_to_string_view) {
  207. return os << "bar";
  208. }
  209. TEST(ostream_test, format_convertible_to_string_vew) {
  210. // operator<< is intentionally not used because of potential ODR violations.
  211. EXPECT_EQ(fmt::format("{}", streamable_and_convertible_to_string_view()),
  212. "foo");
  213. }
  214. struct copyfmt_test {};
  215. std::ostream& operator<<(std::ostream& os, copyfmt_test) {
  216. std::ios ios(nullptr);
  217. ios.copyfmt(os);
  218. return os << "foo";
  219. }
  220. namespace fmt {
  221. template <> struct formatter<copyfmt_test> : ostream_formatter {};
  222. } // namespace fmt
  223. TEST(ostream_test, copyfmt) {
  224. EXPECT_EQ("foo", fmt::format("{}", copyfmt_test()));
  225. }
  226. TEST(ostream_test, to_string) {
  227. EXPECT_EQ("abc", fmt::to_string(fmt_test::abc()));
  228. }
  229. TEST(ostream_test, range) {
  230. auto strs = std::vector<test_string>{test_string("foo"), test_string("bar")};
  231. EXPECT_EQ("[foo, bar]", fmt::format("{}", strs));
  232. }
  233. struct abstract {
  234. virtual ~abstract() = default;
  235. virtual void f() = 0;
  236. friend auto operator<<(std::ostream& os, const abstract&) -> std::ostream& {
  237. return os;
  238. }
  239. };
  240. namespace fmt {
  241. template <> struct formatter<abstract> : ostream_formatter {};
  242. } // namespace fmt
  243. void format_abstract_compiles(const abstract& a) {
  244. fmt::format(FMT_COMPILE("{}"), a);
  245. }
  246. TEST(ostream_test, is_formattable) {
  247. EXPECT_TRUE(fmt::is_formattable<std::string>());
  248. EXPECT_TRUE(fmt::is_formattable<fmt::detail::std_string_view<char>>());
  249. }
  250. struct streamable_and_unformattable {};
  251. auto operator<<(std::ostream& os, streamable_and_unformattable)
  252. -> std::ostream& {
  253. return os << "foo";
  254. }
  255. TEST(ostream_test, streamed) {
  256. EXPECT_FALSE(fmt::is_formattable<streamable_and_unformattable>());
  257. EXPECT_EQ(fmt::format("{}", fmt::streamed(streamable_and_unformattable())),
  258. "foo");
  259. }
  260. TEST(ostream_test, closed_ofstream) {
  261. std::ofstream ofs;
  262. fmt::print(ofs, "discard");
  263. }