Cleanup tests and format string compilation

This commit is contained in:
Victor Zverovich
2021-04-29 15:25:02 -07:00
parent fd43e4dcbc
commit e96a92f869
4 changed files with 90 additions and 295 deletions

View File

@ -814,8 +814,7 @@ constexpr auto compile_format_string(S format_str) {
} }
template <typename... Args, typename S, template <typename... Args, typename S,
FMT_ENABLE_IF(is_compile_string<S>::value || FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
detail::is_compiled_string<S>::value)>
constexpr auto compile(S format_str) { constexpr auto compile(S format_str) {
constexpr basic_string_view<typename S::char_type> str = format_str; constexpr basic_string_view<typename S::char_type> str = format_str;
if constexpr (str.size() == 0) { if constexpr (str.size() == 0) {
@ -834,27 +833,11 @@ constexpr auto compile(S format_str) -> detail::compiled_format<S, Args...> {
return detail::compiled_format<S, Args...>(to_string_view(format_str)); return detail::compiled_format<S, Args...>(to_string_view(format_str));
} }
#endif // __cpp_if_constexpr #endif // __cpp_if_constexpr
// Compiles the format string which must be a string literal.
template <typename... Args, typename Char, size_t N>
auto compile(const Char (&format_str)[N])
-> detail::compiled_format<const Char*, Args...> {
return detail::compiled_format<const Char*, Args...>(
basic_string_view<Char>(format_str, N - 1));
}
} // namespace detail } // namespace detail
// DEPRECATED! use FMT_COMPILE instead.
template <typename... Args>
FMT_DEPRECATED auto compile(const Args&... args)
-> decltype(detail::compile(args...)) {
return detail::compile(args...);
}
FMT_MODULE_EXPORT_BEGIN FMT_MODULE_EXPORT_BEGIN
#if FMT_USE_CONSTEXPR #ifdef __cpp_if_constexpr
# ifdef __cpp_if_constexpr
template <typename CompiledFormat, typename... Args, template <typename CompiledFormat, typename... Args,
typename Char = typename CompiledFormat::char_type, typename Char = typename CompiledFormat::char_type,
@ -872,20 +855,7 @@ constexpr OutputIt format_to(OutputIt out, const CompiledFormat& cf,
const Args&... args) { const Args&... args) {
return cf.format(out, args...); return cf.format(out, args...);
} }
# endif // __cpp_if_constexpr #endif // __cpp_if_constexpr
#endif // FMT_USE_CONSTEXPR
template <typename CompiledFormat, typename... Args,
typename Char = typename CompiledFormat::char_type,
FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
CompiledFormat>::value)>
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
basic_memory_buffer<Char> buffer;
using context = buffer_context<Char>;
detail::cf::vformat_to<context>(detail::buffer_appender<Char>(buffer), cf,
make_format_args<context>(args...));
return to_string(buffer);
}
template <typename S, typename... Args, template <typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)> FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
@ -919,17 +889,6 @@ FMT_INLINE std::basic_string<typename S::char_type> format(const S&,
#endif #endif
} }
template <typename OutputIt, typename CompiledFormat, typename... Args,
FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format,
CompiledFormat>::value)>
constexpr OutputIt format_to(OutputIt out, const CompiledFormat& cf,
const Args&... args) {
using char_type = typename CompiledFormat::char_type;
using context = format_context_t<OutputIt, char_type>;
return detail::cf::vformat_to<context>(out, cf,
make_format_args<context>(args...));
}
template <typename OutputIt, typename S, typename... Args, template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)> FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) { FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
@ -948,35 +907,19 @@ FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) {
#endif #endif
} }
template <typename OutputIt, typename CompiledFormat, typename... Args>
auto format_to_n(OutputIt out, size_t n, const CompiledFormat& cf,
const Args&... args) ->
typename std::enable_if<
detail::is_output_iterator<OutputIt,
typename CompiledFormat::char_type>::value &&
std::is_base_of<detail::basic_compiled_format,
CompiledFormat>::value,
format_to_n_result<OutputIt>>::type {
auto it =
format_to(detail::truncating_iterator<OutputIt>(out, n), cf, args...);
return {it.base(), it.count()};
}
template <typename OutputIt, typename S, typename... Args, template <typename OutputIt, typename S, typename... Args,
FMT_ENABLE_IF(detail::is_compiled_string<S>::value)> FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n, const S&, format_to_n_result<OutputIt> format_to_n(OutputIt out, size_t n,
Args&&... args) { const S& format_str, Args&&... args) {
auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), S(), auto it = format_to(detail::truncating_iterator<OutputIt>(out, n), format_str,
std::forward<Args>(args)...); std::forward<Args>(args)...);
return {it.base(), it.count()}; return {it.base(), it.count()};
} }
template <typename CompiledFormat, typename... Args, template <typename S, typename... Args,
FMT_ENABLE_IF(std::is_base_of<detail::basic_compiled_format, FMT_ENABLE_IF(detail::is_compiled_string<S>::value)>
CompiledFormat>::value || size_t formatted_size(const S& format_str, const Args&... args) {
detail::is_compiled_string<CompiledFormat>::value)> return format_to(detail::counting_iterator(), format_str, args...).count();
size_t formatted_size(const CompiledFormat& cf, const Args&... args) {
return format_to(detail::counting_iterator(), cf, args...).count();
} }
#if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS #if FMT_USE_NONTYPE_TEMPLATE_PARAMETERS

View File

@ -5,77 +5,67 @@
// //
// For the license information refer to format.h. // For the license information refer to format.h.
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "fmt/chrono.h" #include "fmt/chrono.h"
#include <iomanip>
#include "gtest-extra.h" #include "gtest-extra.h"
std::tm make_tm() { auto make_tm() -> std::tm {
auto time = std::tm(); auto time = std::tm();
time.tm_mday = 1; time.tm_mday = 1;
return time; return time;
} }
std::tm make_hour(int h) { auto make_hour(int h) -> std::tm {
auto time = make_tm(); auto time = make_tm();
time.tm_hour = h; time.tm_hour = h;
return time; return time;
} }
std::tm make_minute(int m) { auto make_minute(int m) -> std::tm {
auto time = make_tm(); auto time = make_tm();
time.tm_min = m; time.tm_min = m;
return time; return time;
} }
std::tm make_second(int s) { auto make_second(int s) -> std::tm {
auto time = make_tm(); auto time = make_tm();
time.tm_sec = s; time.tm_sec = s;
return time; return time;
} }
std::string format_tm(const std::tm& time, const char* spec, TEST(chrono_test, format_tm) {
const std::locale& loc) { auto tm = std::tm();
auto& facet = std::use_facet<std::time_put<char>>(loc);
std::ostringstream os;
os.imbue(loc);
facet.put(os, os, ' ', &time, spec, spec + std::strlen(spec));
return os.str();
}
TEST(time_test, Format) {
std::tm tm = std::tm();
tm.tm_year = 116; tm.tm_year = 116;
tm.tm_mon = 3; tm.tm_mon = 3;
tm.tm_mday = 25; tm.tm_mday = 25;
EXPECT_EQ("The date is 2016-04-25.", tm.tm_hour = 11;
fmt::format("The date is {:%Y-%m-%d}.", tm)); tm.tm_min = 22;
tm.tm_sec = 33;
EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
"The date is 2016-04-25 11:22:33.");
EXPECT_EQ(fmt::format(L"The date is {:%Y-%m-%d %H:%M:%S}.", tm),
L"The date is 2016-04-25 11:22:33.");
} }
TEST(time_test, GrowBuffer) { TEST(chrono_test, grow_buffer) {
std::string s = "{:"; auto s = std::string("{:");
for (int i = 0; i < 30; ++i) s += "%c"; for (int i = 0; i < 30; ++i) s += "%c";
s += "}\n"; s += "}\n";
std::time_t t = std::time(nullptr); auto t = std::time(nullptr);
fmt::format(s, *std::localtime(&t)); fmt::format(s, *std::localtime(&t));
} }
TEST(time_test, FormatToEmptyContainer) { TEST(chrono_test, format_to_empty_container) {
std::string s;
auto time = std::tm(); auto time = std::tm();
time.tm_sec = 42; time.tm_sec = 42;
auto s = std::string();
fmt::format_to(std::back_inserter(s), "{:%S}", time); fmt::format_to(std::back_inserter(s), "{:%S}", time);
EXPECT_EQ(s, "42"); EXPECT_EQ(s, "42");
} }
TEST(time_test, EmptyResult) { EXPECT_EQ("", fmt::format("{}", std::tm())); } TEST(chrono_test, empty_result) { EXPECT_EQ(fmt::format("{}", std::tm()), ""); }
static bool EqualTime(const std::tm& lhs, const std::tm& rhs) { auto equal(const std::tm& lhs, const std::tm& rhs) -> bool {
return lhs.tm_sec == rhs.tm_sec && lhs.tm_min == rhs.tm_min && return lhs.tm_sec == rhs.tm_sec && lhs.tm_min == rhs.tm_min &&
lhs.tm_hour == rhs.tm_hour && lhs.tm_mday == rhs.tm_mday && lhs.tm_hour == rhs.tm_hour && lhs.tm_mday == rhs.tm_mday &&
lhs.tm_mon == rhs.tm_mon && lhs.tm_year == rhs.tm_year && lhs.tm_mon == rhs.tm_mon && lhs.tm_year == rhs.tm_year &&
@ -83,41 +73,27 @@ static bool EqualTime(const std::tm& lhs, const std::tm& rhs) {
lhs.tm_isdst == rhs.tm_isdst; lhs.tm_isdst == rhs.tm_isdst;
} }
TEST(time_test, LocalTime) { TEST(chrono_test, localtime) {
std::time_t t = std::time(nullptr); auto t = std::time(nullptr);
std::tm tm = *std::localtime(&t); auto tm = *std::localtime(&t);
EXPECT_TRUE(EqualTime(tm, fmt::localtime(t))); EXPECT_TRUE(equal(tm, fmt::localtime(t)));
} }
TEST(time_test, GMTime) { TEST(chrono_test, gmtime) {
std::time_t t = std::time(nullptr); auto t = std::time(nullptr);
std::tm tm = *std::gmtime(&t); auto tm = *std::gmtime(&t);
EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t))); EXPECT_TRUE(equal(tm, fmt::gmtime(t)));
} }
TEST(time_test, FormatTM) { template <typename TimePoint> auto strftime(TimePoint tp) -> std::string {
auto point = std::chrono::system_clock::now(); auto t = std::chrono::system_clock::to_time_t(tp);
std::time_t t = std::chrono::system_clock::to_time_t(point); auto tm = *std::localtime(&t);
std::tm tm = *std::localtime(&t); char output[256] = {};
char strftime_output[256];
std::strftime(strftime_output, sizeof(strftime_output), "%Y-%m-%d %H:%M:%S",
&tm);
EXPECT_EQ(strftime_output, fmt::format("{:%Y-%m-%d %H:%M:%S}", tm));
auto wstrftime_output = std::wstring();
std::copy(strftime_output, strftime_output + strlen(strftime_output),
std::back_inserter(wstrftime_output));
EXPECT_EQ(wstrftime_output, fmt::format(L"{:%Y-%m-%d %H:%M:%S}", tm));
}
template <typename TimePoint> std::string strftime(TimePoint tp) {
std::time_t t = std::chrono::system_clock::to_time_t(tp);
std::tm tm = *std::localtime(&t);
char output[256];
std::strftime(output, sizeof(output), "%Y-%m-%d %H:%M:%S", &tm); std::strftime(output, sizeof(output), "%Y-%m-%d %H:%M:%S", &tm);
return output; return output;
} }
TEST(time_test, TimePoint) { TEST(chrono_test, time_point) {
auto t1 = std::chrono::system_clock::now(); auto t1 = std::chrono::system_clock::now();
EXPECT_EQ(strftime(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1)); EXPECT_EQ(strftime(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
using time_point = using time_point =
@ -126,16 +102,9 @@ TEST(time_test, TimePoint) {
EXPECT_EQ(strftime(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2)); EXPECT_EQ(strftime(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
} }
#define EXPECT_TIME(spec, time, duration) \
{ \
std::locale jp_loc("ja_JP.utf8"); \
EXPECT_EQ(format_tm(time, spec, jp_loc), \
fmt::format(loc, "{:" spec "}", duration)); \
}
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR #ifndef FMT_STATIC_THOUSANDS_SEPARATOR
TEST(ChronoTest, FormatDefault) { TEST(chrono_test, format_default) {
EXPECT_EQ("42s", fmt::format("{}", std::chrono::seconds(42))); EXPECT_EQ("42s", fmt::format("{}", std::chrono::seconds(42)));
EXPECT_EQ("42as", EXPECT_EQ("42as",
fmt::format("{}", std::chrono::duration<int, std::atto>(42))); fmt::format("{}", std::chrono::duration<int, std::atto>(42)));
@ -177,49 +146,11 @@ TEST(ChronoTest, FormatDefault) {
fmt::format("{}", std::chrono::duration<int, std::ratio<15, 4>>(42))); fmt::format("{}", std::chrono::duration<int, std::ratio<15, 4>>(42)));
} }
TEST(ChronoTest, FormatWide) { TEST(chrono_test, format_wide) {
EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds(42))); EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds(42)));
EXPECT_EQ(L"42as",
fmt::format(L"{}", std::chrono::duration<int, std::atto>(42)));
EXPECT_EQ(L"42fs",
fmt::format(L"{}", std::chrono::duration<int, std::femto>(42)));
EXPECT_EQ(L"42ps",
fmt::format(L"{}", std::chrono::duration<int, std::pico>(42)));
EXPECT_EQ(L"42ns", fmt::format(L"{}", std::chrono::nanoseconds(42)));
EXPECT_EQ(L"42\u00B5s", fmt::format(L"{}", std::chrono::microseconds(42)));
EXPECT_EQ(L"42ms", fmt::format(L"{}", std::chrono::milliseconds(42)));
EXPECT_EQ(L"42cs",
fmt::format(L"{}", std::chrono::duration<int, std::centi>(42)));
EXPECT_EQ(L"42ds",
fmt::format(L"{}", std::chrono::duration<int, std::deci>(42)));
EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds(42)));
EXPECT_EQ(L"42das",
fmt::format(L"{}", std::chrono::duration<int, std::deca>(42)));
EXPECT_EQ(L"42hs",
fmt::format(L"{}", std::chrono::duration<int, std::hecto>(42)));
EXPECT_EQ(L"42ks",
fmt::format(L"{}", std::chrono::duration<int, std::kilo>(42)));
EXPECT_EQ(L"42Ms",
fmt::format(L"{}", std::chrono::duration<int, std::mega>(42)));
EXPECT_EQ(L"42Gs",
fmt::format(L"{}", std::chrono::duration<int, std::giga>(42)));
EXPECT_EQ(L"42Ts",
fmt::format(L"{}", std::chrono::duration<int, std::tera>(42)));
EXPECT_EQ(L"42Ps",
fmt::format(L"{}", std::chrono::duration<int, std::peta>(42)));
EXPECT_EQ(L"42Es",
fmt::format(L"{}", std::chrono::duration<int, std::exa>(42)));
EXPECT_EQ(L"42m", fmt::format(L"{}", std::chrono::minutes(42)));
EXPECT_EQ(L"42h", fmt::format(L"{}", std::chrono::hours(42)));
EXPECT_EQ(
L"42[15]s",
fmt::format(L"{}", std::chrono::duration<int, std::ratio<15, 1>>(42)));
EXPECT_EQ(
L"42[15/4]s",
fmt::format(L"{}", std::chrono::duration<int, std::ratio<15, 4>>(42)));
} }
TEST(ChronoTest, Align) { TEST(chrono_test, align) {
auto s = std::chrono::seconds(42); auto s = std::chrono::seconds(42);
EXPECT_EQ("42s ", fmt::format("{:5}", s)); EXPECT_EQ("42s ", fmt::format("{:5}", s));
EXPECT_EQ("42s ", fmt::format("{:{}}", s, 5)); EXPECT_EQ("42s ", fmt::format("{:{}}", s, 5));
@ -235,7 +166,7 @@ TEST(ChronoTest, Align) {
fmt::format("{:{}%H:%M:%S}", std::chrono::seconds(12345), 12)); fmt::format("{:{}%H:%M:%S}", std::chrono::seconds(12345), 12));
} }
TEST(ChronoTest, FormatSpecs) { TEST(chrono_test, format_specs) {
EXPECT_EQ("%", fmt::format("{:%%}", std::chrono::seconds(0))); EXPECT_EQ("%", fmt::format("{:%%}", std::chrono::seconds(0)));
EXPECT_EQ("\n", fmt::format("{:%n}", std::chrono::seconds(0))); EXPECT_EQ("\n", fmt::format("{:%n}", std::chrono::seconds(0)));
EXPECT_EQ("\t", fmt::format("{:%t}", std::chrono::seconds(0))); EXPECT_EQ("\t", fmt::format("{:%t}", std::chrono::seconds(0)));
@ -264,7 +195,7 @@ TEST(ChronoTest, FormatSpecs) {
EXPECT_EQ("s", fmt::format("{:%q}", std::chrono::seconds(12345))); EXPECT_EQ("s", fmt::format("{:%q}", std::chrono::seconds(12345)));
} }
TEST(ChronoTest, InvalidSpecs) { TEST(chrono_test, invalid_specs) {
auto sec = std::chrono::seconds(0); auto sec = std::chrono::seconds(0);
EXPECT_THROW_MSG(fmt::format(+"{:%a}", sec), fmt::format_error, "no date"); EXPECT_THROW_MSG(fmt::format(+"{:%a}", sec), fmt::format_error, "no date");
EXPECT_THROW_MSG(fmt::format(+"{:%A}", sec), fmt::format_error, "no date"); EXPECT_THROW_MSG(fmt::format(+"{:%A}", sec), fmt::format_error, "no date");
@ -288,10 +219,26 @@ TEST(ChronoTest, InvalidSpecs) {
"invalid format"); "invalid format");
} }
TEST(ChronoTest, Locale) { auto format_tm(const std::tm& time, fmt::string_view spec,
const char* loc_name = "ja_JP.utf8"; const std::locale& loc) -> std::string {
auto& facet = std::use_facet<std::time_put<char>>(loc);
std::ostringstream os;
os.imbue(loc);
facet.put(os, os, ' ', &time, spec.begin(), spec.end());
return os.str();
}
# define EXPECT_TIME(spec, time, duration) \
{ \
auto jp_loc = std::locale("ja_JP.utf8"); \
EXPECT_EQ(format_tm(time, spec, jp_loc), \
fmt::format(loc, "{:" spec "}", duration)); \
}
TEST(chrono_test, locale) {
auto loc_name = "ja_JP.utf8";
bool has_locale = false; bool has_locale = false;
std::locale loc; auto loc = std::locale();
try { try {
loc = std::locale(loc_name); loc = std::locale(loc_name);
has_locale = true; has_locale = true;
@ -314,9 +261,9 @@ TEST(ChronoTest, Locale) {
EXPECT_TIME("%p", time, sec); EXPECT_TIME("%p", time, sec);
} }
typedef std::chrono::duration<double, std::milli> dms; using dms = std::chrono::duration<double, std::milli>;
TEST(ChronoTest, FormatDefaultFP) { TEST(chrono_test, format_default_fp) {
typedef std::chrono::duration<float> fs; typedef std::chrono::duration<float> fs;
EXPECT_EQ("1.234s", fmt::format("{}", fs(1.234))); EXPECT_EQ("1.234s", fmt::format("{}", fs(1.234)));
typedef std::chrono::duration<float, std::milli> fms; typedef std::chrono::duration<float, std::milli> fms;
@ -326,7 +273,7 @@ TEST(ChronoTest, FormatDefaultFP) {
EXPECT_EQ("1.234ms", fmt::format("{}", dms(1.234))); EXPECT_EQ("1.234ms", fmt::format("{}", dms(1.234)));
} }
TEST(ChronoTest, FormatPrecision) { TEST(chrono_test, format_precision) {
EXPECT_THROW_MSG(fmt::format(+"{:.2}", std::chrono::seconds(42)), EXPECT_THROW_MSG(fmt::format(+"{:.2}", std::chrono::seconds(42)),
fmt::format_error, fmt::format_error,
"precision not allowed for this argument type"); "precision not allowed for this argument type");
@ -334,7 +281,7 @@ TEST(ChronoTest, FormatPrecision) {
EXPECT_EQ("1.23ms", fmt::format("{:.{}}", dms(1.234), 2)); EXPECT_EQ("1.23ms", fmt::format("{:.{}}", dms(1.234), 2));
} }
TEST(ChronoTest, FormatFullSpecs) { TEST(chrono_test, format_full_specs) {
EXPECT_EQ("1.2ms ", fmt::format("{:6.1}", dms(1.234))); EXPECT_EQ("1.2ms ", fmt::format("{:6.1}", dms(1.234)));
EXPECT_EQ(" 1.23ms", fmt::format("{:>8.{}}", dms(1.234), 2)); EXPECT_EQ(" 1.23ms", fmt::format("{:>8.{}}", dms(1.234), 2));
EXPECT_EQ(" 1.2ms ", fmt::format("{:^{}.{}}", dms(1.234), 7, 1)); EXPECT_EQ(" 1.2ms ", fmt::format("{:^{}.{}}", dms(1.234), 7, 1));
@ -343,7 +290,7 @@ TEST(ChronoTest, FormatFullSpecs) {
EXPECT_EQ("*1.2340ms*", fmt::format("{:*^10.4}", dms(1.234))); EXPECT_EQ("*1.2340ms*", fmt::format("{:*^10.4}", dms(1.234)));
} }
TEST(ChronoTest, FormatSimpleQq) { TEST(chrono_test, format_simple_q) {
typedef std::chrono::duration<float> fs; typedef std::chrono::duration<float> fs;
EXPECT_EQ("1.234 s", fmt::format("{:%Q %q}", fs(1.234))); EXPECT_EQ("1.234 s", fmt::format("{:%Q %q}", fs(1.234)));
typedef std::chrono::duration<float, std::milli> fms; typedef std::chrono::duration<float, std::milli> fms;
@ -353,7 +300,7 @@ TEST(ChronoTest, FormatSimpleQq) {
EXPECT_EQ("1.234 ms", fmt::format("{:%Q %q}", dms(1.234))); EXPECT_EQ("1.234 ms", fmt::format("{:%Q %q}", dms(1.234)));
} }
TEST(ChronoTest, FormatPrecisionQq) { TEST(chrono_test, format_precision_q) {
EXPECT_THROW_MSG(fmt::format(+"{:.2%Q %q}", std::chrono::seconds(42)), EXPECT_THROW_MSG(fmt::format(+"{:.2%Q %q}", std::chrono::seconds(42)),
fmt::format_error, fmt::format_error,
"precision not allowed for this argument type"); "precision not allowed for this argument type");
@ -361,7 +308,7 @@ TEST(ChronoTest, FormatPrecisionQq) {
EXPECT_EQ("1.23 ms", fmt::format("{:.{}%Q %q}", dms(1.234), 2)); EXPECT_EQ("1.23 ms", fmt::format("{:.{}%Q %q}", dms(1.234), 2));
} }
TEST(ChronoTest, FormatFullSpecsQq) { TEST(chrono_test, format_full_specs_q) {
EXPECT_EQ("1.2 ms ", fmt::format("{:7.1%Q %q}", dms(1.234))); EXPECT_EQ("1.2 ms ", fmt::format("{:7.1%Q %q}", dms(1.234)));
EXPECT_EQ(" 1.23 ms", fmt::format("{:>8.{}%Q %q}", dms(1.234), 2)); EXPECT_EQ(" 1.23 ms", fmt::format("{:>8.{}%Q %q}", dms(1.234), 2));
EXPECT_EQ(" 1.2 ms ", fmt::format("{:^{}.{}%Q %q}", dms(1.234), 8, 1)); EXPECT_EQ(" 1.2 ms ", fmt::format("{:^{}.{}%Q %q}", dms(1.234), 8, 1));
@ -370,17 +317,17 @@ TEST(ChronoTest, FormatFullSpecsQq) {
EXPECT_EQ("*1.2340 ms*", fmt::format("{:*^11.4%Q %q}", dms(1.234))); EXPECT_EQ("*1.2340 ms*", fmt::format("{:*^11.4%Q %q}", dms(1.234)));
} }
TEST(ChronoTest, InvalidWidthId) { TEST(chrono_test, invalid_width_id) {
EXPECT_THROW(fmt::format(+"{:{o}", std::chrono::seconds(0)), EXPECT_THROW(fmt::format(+"{:{o}", std::chrono::seconds(0)),
fmt::format_error); fmt::format_error);
} }
TEST(ChronoTest, InvalidColons) { TEST(chrono_test, invalid_colons) {
EXPECT_THROW(fmt::format(+"{0}=:{0::", std::chrono::seconds(0)), EXPECT_THROW(fmt::format(+"{0}=:{0::", std::chrono::seconds(0)),
fmt::format_error); fmt::format_error);
} }
TEST(ChronoTest, NegativeDurations) { TEST(chrono_test, negative_durations) {
EXPECT_EQ("-12345", fmt::format("{:%Q}", std::chrono::seconds(-12345))); EXPECT_EQ("-12345", fmt::format("{:%Q}", std::chrono::seconds(-12345)));
EXPECT_EQ("-03:25:45", EXPECT_EQ("-03:25:45",
fmt::format("{:%H:%M:%S}", std::chrono::seconds(-12345))); fmt::format("{:%H:%M:%S}", std::chrono::seconds(-12345)));
@ -395,7 +342,7 @@ TEST(ChronoTest, NegativeDurations) {
fmt::format("{:%Q}", std::chrono::duration<int>(min))); fmt::format("{:%Q}", std::chrono::duration<int>(min)));
} }
TEST(ChronoTest, SpecialDurations) { TEST(chrono_test, special_durations) {
EXPECT_EQ( EXPECT_EQ(
"40.", "40.",
fmt::format("{:%S}", std::chrono::duration<double>(1e20)).substr(0, 3)); fmt::format("{:%S}", std::chrono::duration<double>(1e20)).substr(0, 3));
@ -415,7 +362,7 @@ TEST(ChronoTest, SpecialDurations) {
"03:33:20"); "03:33:20");
} }
TEST(ChronoTest, UnsignedDuration) { TEST(chrono_test, unsigned_duration) {
EXPECT_EQ("42s", fmt::format("{}", std::chrono::duration<unsigned>(42))); EXPECT_EQ("42s", fmt::format("{}", std::chrono::duration<unsigned>(42)));
} }

View File

@ -7,52 +7,11 @@
#include "fmt/color.h" #include "fmt/color.h"
#include <iterator> #include <iterator> // std::back_inserter
#include <string>
#include <utility>
#include "gtest-extra.h" #include "gtest-extra.h"
TEST(ColorsTest, ColorsPrint) { TEST(color_test, format) {
EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
EXPECT_WRITE(stdout, fmt::print(fg(fmt::color::blue), "blue"),
"\x1b[38;2;000;000;255mblue\x1b[0m");
EXPECT_WRITE(
stdout,
fmt::print(fg(fmt::color::blue) | bg(fmt::color::red), "two color"),
"\x1b[38;2;000;000;255m\x1b[48;2;255;000;000mtwo color\x1b[0m");
EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::bold, "bold"),
"\x1b[1mbold\x1b[0m");
EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::italic, "italic"),
"\x1b[3mitalic\x1b[0m");
EXPECT_WRITE(stdout, fmt::print(fmt::emphasis::underline, "underline"),
"\x1b[4munderline\x1b[0m");
EXPECT_WRITE(stdout,
fmt::print(fmt::emphasis::strikethrough, "strikethrough"),
"\x1b[9mstrikethrough\x1b[0m");
EXPECT_WRITE(
stdout,
fmt::print(fg(fmt::color::blue) | fmt::emphasis::bold, "blue/bold"),
"\x1b[1m\x1b[38;2;000;000;255mblue/bold\x1b[0m");
EXPECT_WRITE(stderr, fmt::print(stderr, fmt::emphasis::bold, "bold error"),
"\x1b[1mbold error\x1b[0m");
EXPECT_WRITE(stderr, fmt::print(stderr, fg(fmt::color::blue), "blue log"),
"\x1b[38;2;000;000;255mblue log\x1b[0m");
EXPECT_WRITE(stdout, fmt::print(fmt::text_style(), "hi"), "hi");
EXPECT_WRITE(stdout, fmt::print(fg(fmt::terminal_color::red), "tred"),
"\x1b[31mtred\x1b[0m");
EXPECT_WRITE(stdout, fmt::print(bg(fmt::terminal_color::cyan), "tcyan"),
"\x1b[46mtcyan\x1b[0m");
EXPECT_WRITE(stdout,
fmt::print(fg(fmt::terminal_color::bright_green), "tbgreen"),
"\x1b[92mtbgreen\x1b[0m");
EXPECT_WRITE(stdout,
fmt::print(bg(fmt::terminal_color::bright_magenta), "tbmagenta"),
"\x1b[105mtbmagenta\x1b[0m");
}
TEST(ColorsTest, Format) {
EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"), EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m"); "\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), L"rgb(255,20,30) wide"), EXPECT_EQ(fmt::format(fg(fmt::rgb(255, 20, 30)), L"rgb(255,20,30) wide"),
@ -89,11 +48,15 @@ TEST(ColorsTest, Format) {
"\x1b[31mfoo\x1b[0m"); "\x1b[31mfoo\x1b[0m");
} }
TEST(ColorsTest, FormatToOutAcceptsTextStyle) { TEST(color_test, format_to) {
fmt::text_style ts = fg(fmt::rgb(255, 20, 30)); auto out = std::string();
std::string out; fmt::format_to(std::back_inserter(out), fg(fmt::rgb(255, 20, 30)),
fmt::format_to(std::back_inserter(out), ts, "rgb(255,20,30){}{}{}", 1, 2, 3); "rgb(255,20,30){}{}{}", 1, 2, 3);
EXPECT_EQ(fmt::to_string(out), EXPECT_EQ(fmt::to_string(out),
"\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m"); "\x1b[38;2;255;020;030mrgb(255,20,30)123\x1b[0m");
} }
TEST(color_test, print) {
EXPECT_WRITE(stdout, fmt::print(fg(fmt::rgb(255, 20, 30)), "rgb(255,20,30)"),
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
}

View File

@ -86,54 +86,6 @@ TEST(CompileTest, CompileTimePreparedPartsTypeProvider) {
} }
#endif #endif
TEST(CompileTest, PassStringLiteralFormat) {
const auto prepared = fmt::detail::compile<int>("test {}");
EXPECT_EQ("test 42", fmt::format(prepared, 42));
const auto wprepared = fmt::detail::compile<int>(L"test {}");
EXPECT_EQ(L"test 42", fmt::format(wprepared, 42));
}
TEST(CompileTest, FormatToArrayOfChars) {
char buffer[32] = {0};
const auto prepared = fmt::detail::compile<int>("4{}");
fmt::format_to(fmt::detail::make_checked(buffer, 32), prepared, 2);
EXPECT_EQ(std::string("42"), buffer);
wchar_t wbuffer[32] = {0};
const auto wprepared = fmt::detail::compile<int>(L"4{}");
fmt::format_to(fmt::detail::make_checked(wbuffer, 32), wprepared, 2);
EXPECT_EQ(std::wstring(L"42"), wbuffer);
}
TEST(CompileTest, FormatToIterator) {
std::string s(2, ' ');
const auto prepared = fmt::detail::compile<int>("4{}");
fmt::format_to(s.begin(), prepared, 2);
EXPECT_EQ("42", s);
std::wstring ws(2, L' ');
const auto wprepared = fmt::detail::compile<int>(L"4{}");
fmt::format_to(ws.begin(), wprepared, 2);
EXPECT_EQ(L"42", ws);
}
TEST(CompileTest, FormatToN) {
char buf[5];
auto f = fmt::detail::compile<int>("{:10}");
auto result = fmt::format_to_n(buf, 5, f, 42);
EXPECT_EQ(result.size, 10);
EXPECT_EQ(result.out, buf + 5);
EXPECT_EQ(fmt::string_view(buf, 5), " ");
}
TEST(CompileTest, FormattedSize) {
auto f = fmt::detail::compile<int>("{:10}");
EXPECT_EQ(fmt::formatted_size(f, 42), 10);
}
TEST(CompileTest, MultipleTypes) {
auto f = fmt::detail::compile<int, int>("{} {}");
EXPECT_EQ(fmt::format(f, 42, 42), "42 42");
}
struct test_formattable {}; struct test_formattable {};
FMT_BEGIN_NAMESPACE FMT_BEGIN_NAMESPACE
@ -145,16 +97,6 @@ template <> struct formatter<test_formattable> : formatter<const char*> {
}; };
FMT_END_NAMESPACE FMT_END_NAMESPACE
TEST(CompileTest, FormatUserDefinedType) {
auto f = fmt::detail::compile<test_formattable>("{}");
EXPECT_EQ(fmt::format(f, test_formattable()), "foo");
}
TEST(CompileTest, EmptyFormatString) {
auto f = fmt::detail::compile<>("");
EXPECT_EQ(fmt::format(f), "");
}
TEST(CompileTest, CompileFallback) { TEST(CompileTest, CompileFallback) {
// FMT_COMPILE should fallback on runtime formatting when `if constexpr` is // FMT_COMPILE should fallback on runtime formatting when `if constexpr` is
// not available. // not available.