Cleanup tests

This commit is contained in:
Victor Zverovich 2023-01-02 09:45:51 -08:00
parent 0ec65d99aa
commit 09ed1ddb9c
2 changed files with 45 additions and 44 deletions

View File

@ -95,13 +95,14 @@ template <typename Char> class test_string {
public: public:
test_string(const Char* s) : s_(s) {} test_string(const Char* s) : s_(s) {}
const Char* data() const { return s_.data(); } auto data() const -> const Char* { return s_.data(); }
size_t length() const { return s_.size(); } auto length() const -> size_t { return s_.size(); }
operator const Char*() const { return s_.c_str(); } operator const Char*() const { return s_.c_str(); }
}; };
template <typename Char> template <typename Char>
fmt::basic_string_view<Char> to_string_view(const test_string<Char>& s) { auto to_string_view(const test_string<Char>& s)
-> fmt::basic_string_view<Char> {
return {s.data(), s.length()}; return {s.data(), s.length()};
} }
} // namespace test_ns } // namespace test_ns
@ -312,7 +313,7 @@ template <typename Char> struct formatter<test_struct, Char> {
return ctx.begin(); return ctx.begin();
} }
auto format(test_struct, format_context& ctx) -> decltype(ctx.out()) { auto format(test_struct, format_context& ctx) const -> decltype(ctx.out()) {
auto test = string_view("test"); auto test = string_view("test");
return std::copy_n(test.data(), test.size(), ctx.out()); return std::copy_n(test.data(), test.size(), ctx.out());
} }
@ -326,7 +327,7 @@ TEST(arg_test, format_args) {
TEST(arg_test, make_value_with_custom_context) { TEST(arg_test, make_value_with_custom_context) {
auto t = test_struct(); auto t = test_struct();
fmt::detail::value<custom_context> arg( auto arg = fmt::detail::value<custom_context>(
fmt::detail::arg_mapper<custom_context>().map(t)); fmt::detail::arg_mapper<custom_context>().map(t));
auto ctx = custom_context(); auto ctx = custom_context();
auto parse_ctx = fmt::format_parse_context(""); auto parse_ctx = fmt::format_parse_context("");
@ -348,9 +349,9 @@ template <typename T> struct mock_visitor {
MOCK_METHOD1_T(visit, test_result(T value)); MOCK_METHOD1_T(visit, test_result(T value));
MOCK_METHOD0_T(unexpected, void()); MOCK_METHOD0_T(unexpected, void());
test_result operator()(T value) { return visit(value); } auto operator()(T value) -> test_result { return visit(value); }
template <typename U> test_result operator()(U) { template <typename U> auto operator()(U) -> test_result {
unexpected(); unexpected();
return test_result(); return test_result();
} }
@ -402,13 +403,13 @@ using test_types =
TYPED_TEST_SUITE(numeric_arg_test, test_types); TYPED_TEST_SUITE(numeric_arg_test, test_types);
template <typename T, fmt::enable_if_t<std::is_integral<T>::value, int> = 0> template <typename T, fmt::enable_if_t<std::is_integral<T>::value, int> = 0>
T test_value() { auto test_value() -> T {
return static_cast<T>(42); return static_cast<T>(42);
} }
template <typename T, template <typename T,
fmt::enable_if_t<std::is_floating_point<T>::value, int> = 0> fmt::enable_if_t<std::is_floating_point<T>::value, int> = 0>
T test_value() { auto test_value() -> T {
return static_cast<T>(4.2); return static_cast<T>(4.2);
} }
@ -451,8 +452,8 @@ TEST(arg_test, pointer_arg) {
} }
struct check_custom { struct check_custom {
test_result operator()( auto operator()(fmt::basic_format_arg<fmt::format_context>::handle h) const
fmt::basic_format_arg<fmt::format_context>::handle h) const { -> test_result {
struct test_buffer final : fmt::detail::buffer<char> { struct test_buffer final : fmt::detail::buffer<char> {
char data[10]; char data[10];
test_buffer() : fmt::detail::buffer<char>(data, 0, 10) {} test_buffer() : fmt::detail::buffer<char>(data, 0, 10) {}
@ -471,13 +472,13 @@ TEST(arg_test, custom_arg) {
auto test = test_struct(); auto test = test_struct();
using visitor = using visitor =
mock_visitor<fmt::basic_format_arg<fmt::format_context>::handle>; mock_visitor<fmt::basic_format_arg<fmt::format_context>::handle>;
testing::StrictMock<visitor> v; auto&& v = testing::StrictMock<visitor>();
EXPECT_CALL(v, visit(_)).WillOnce(Invoke(check_custom())); EXPECT_CALL(v, visit(_)).WillOnce(Invoke(check_custom()));
fmt::visit_format_arg(v, fmt::detail::make_arg<fmt::format_context>(test)); fmt::visit_format_arg(v, fmt::detail::make_arg<fmt::format_context>(test));
} }
TEST(arg_test, visit_invalid_arg) { TEST(arg_test, visit_invalid_arg) {
testing::StrictMock<mock_visitor<fmt::monostate>> visitor; auto&& visitor = testing::StrictMock<mock_visitor<fmt::monostate>>();
EXPECT_CALL(visitor, visit(_)); EXPECT_CALL(visitor, visit(_));
auto arg = fmt::basic_format_arg<fmt::format_context>(); auto arg = fmt::basic_format_arg<fmt::format_context>();
fmt::visit_format_arg(visitor, arg); fmt::visit_format_arg(visitor, arg);
@ -549,13 +550,14 @@ TEST(core_test, constexpr_parse_format_specs) {
struct test_format_string_handler { struct test_format_string_handler {
constexpr void on_text(const char*, const char*) {} constexpr void on_text(const char*, const char*) {}
constexpr int on_arg_id() { return 0; } constexpr auto on_arg_id() -> int { return 0; }
template <typename T> constexpr int on_arg_id(T) { return 0; } template <typename T> constexpr auto on_arg_id(T) -> int { return 0; }
constexpr void on_replacement_field(int, const char*) {} constexpr void on_replacement_field(int, const char*) {}
constexpr const char* on_format_specs(int, const char* begin, const char*) { constexpr auto on_format_specs(int, const char* begin, const char*) -> const
char* {
return begin; return begin;
} }
@ -570,7 +572,7 @@ template <size_t N> constexpr bool parse_string(const char (&s)[N]) {
return !h.error; return !h.error;
} }
TEST(format_test, constexpr_parse_format_string) { TEST(core_test, constexpr_parse_format_string) {
static_assert(parse_string("foo"), ""); static_assert(parse_string("foo"), "");
static_assert(!parse_string("}"), ""); static_assert(!parse_string("}"), "");
static_assert(parse_string("{}"), ""); static_assert(parse_string("{}"), "");
@ -592,7 +594,8 @@ template <> struct formatter<enabled_formatter> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin(); return ctx.begin();
} }
auto format(enabled_formatter, format_context& ctx) -> decltype(ctx.out()) { auto format(enabled_formatter, format_context& ctx) const
-> decltype(ctx.out()) {
return ctx.out(); return ctx.out();
} }
}; };
@ -601,7 +604,7 @@ template <> struct formatter<enabled_ptr_formatter*> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin(); return ctx.begin();
} }
auto format(enabled_ptr_formatter*, format_context& ctx) auto format(enabled_ptr_formatter*, format_context& ctx) const
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
return ctx.out(); return ctx.out();
} }
@ -671,28 +674,25 @@ FMT_END_NAMESPACE
enum class unformattable_scoped_enum {}; enum class unformattable_scoped_enum {};
namespace test { namespace test {
enum class formattable_scoped_enum {}; enum class scoped_enum_as_int {};
auto format_as(formattable_scoped_enum) -> int { return 42; } auto format_as(scoped_enum_as_int) -> int { return 42; }
struct convertible_to_enum { struct convertible_to_enum {
operator formattable_scoped_enum() const { return {}; } operator scoped_enum_as_int() const { return {}; }
}; };
} // namespace test } // namespace test
TEST(core_test, is_formattable) { TEST(core_test, is_formattable) {
#if 0
// This should be enabled once corresponding map overloads are gone.
static_assert(fmt::is_formattable<signed char*>::value, "");
static_assert(fmt::is_formattable<unsigned char*>::value, "");
static_assert(fmt::is_formattable<const signed char*>::value, "");
static_assert(fmt::is_formattable<const unsigned char*>::value, "");
#endif
static_assert(!fmt::is_formattable<wchar_t>::value, ""); static_assert(!fmt::is_formattable<wchar_t>::value, "");
#ifdef __cpp_char8_t #ifdef __cpp_char8_t
static_assert(!fmt::is_formattable<char8_t>::value, ""); static_assert(!fmt::is_formattable<char8_t>::value, "");
#endif #endif
static_assert(!fmt::is_formattable<char16_t>::value, ""); static_assert(!fmt::is_formattable<char16_t>::value, "");
static_assert(!fmt::is_formattable<char32_t>::value, ""); static_assert(!fmt::is_formattable<char32_t>::value, "");
static_assert(!fmt::is_formattable<signed char*>::value, "");
static_assert(!fmt::is_formattable<unsigned char*>::value, "");
static_assert(!fmt::is_formattable<const signed char*>::value, "");
static_assert(!fmt::is_formattable<const unsigned char*>::value, "");
static_assert(!fmt::is_formattable<const wchar_t*>::value, ""); static_assert(!fmt::is_formattable<const wchar_t*>::value, "");
static_assert(!fmt::is_formattable<const wchar_t[3]>::value, ""); static_assert(!fmt::is_formattable<const wchar_t[3]>::value, "");
static_assert(!fmt::is_formattable<fmt::basic_string_view<wchar_t>>::value, static_assert(!fmt::is_formattable<fmt::basic_string_view<wchar_t>>::value,
@ -720,27 +720,33 @@ TEST(core_test, is_formattable) {
static_assert(!fmt::is_formattable<int(s::*)>::value, ""); static_assert(!fmt::is_formattable<int(s::*)>::value, "");
static_assert(!fmt::is_formattable<int (s::*)()>::value, ""); static_assert(!fmt::is_formattable<int (s::*)()>::value, "");
static_assert(!fmt::is_formattable<unformattable_scoped_enum>::value, ""); static_assert(!fmt::is_formattable<unformattable_scoped_enum>::value, "");
static_assert(fmt::is_formattable<test::formattable_scoped_enum>::value, ""); static_assert(fmt::is_formattable<test::scoped_enum_as_int>::value, "");
static_assert(!fmt::is_formattable<test::convertible_to_enum>::value, ""); static_assert(!fmt::is_formattable<test::convertible_to_enum>::value, "");
} }
TEST(core_test, format) { EXPECT_EQ(fmt::format("{}", 42), "42"); } TEST(core_test, format) { EXPECT_EQ(fmt::format("{}", 42), "42"); }
TEST(core_test, format_to) { TEST(core_test, format_to) {
std::string s; auto s = std::string();
fmt::format_to(std::back_inserter(s), "{}", 42); fmt::format_to(std::back_inserter(s), "{}", 42);
EXPECT_EQ(s, "42"); EXPECT_EQ(s, "42");
} }
TEST(core_test, format_as) { TEST(core_test, format_as) {
EXPECT_EQ(fmt::format("{}", test::formattable_scoped_enum()), "42"); EXPECT_EQ(fmt::format("{}", test::scoped_enum_as_int()), "42");
} }
#ifdef __cpp_lib_byte
TEST(core_test, format_byte) {
EXPECT_EQ(fmt::format("{}", std::byte(42)), "42");
}
#endif
struct convertible_to_int { struct convertible_to_int {
operator int() const { return 42; } operator int() const { return 42; }
}; };
struct convertible_to_c_string { struct convertible_to_cstring {
operator const char*() const { return "foo"; } operator const char*() const { return "foo"; }
}; };
@ -749,16 +755,17 @@ template <> struct formatter<convertible_to_int> {
auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin(); return ctx.begin();
} }
auto format(convertible_to_int, format_context& ctx) -> decltype(ctx.out()) { auto format(convertible_to_int, format_context& ctx) const
-> decltype(ctx.out()) {
return std::copy_n("foo", 3, ctx.out()); return std::copy_n("foo", 3, ctx.out());
} }
}; };
template <> struct formatter<convertible_to_c_string> { template <> struct formatter<convertible_to_cstring> {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
return ctx.begin(); return ctx.begin();
} }
auto format(convertible_to_c_string, format_context& ctx) auto format(convertible_to_cstring, format_context& ctx) const
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
return std::copy_n("bar", 3, ctx.out()); return std::copy_n("bar", 3, ctx.out());
} }
@ -767,7 +774,7 @@ FMT_END_NAMESPACE
TEST(core_test, formatter_overrides_implicit_conversion) { TEST(core_test, formatter_overrides_implicit_conversion) {
EXPECT_EQ(fmt::format("{}", convertible_to_int()), "foo"); EXPECT_EQ(fmt::format("{}", convertible_to_int()), "foo");
EXPECT_EQ(fmt::format("{}", convertible_to_c_string()), "bar"); EXPECT_EQ(fmt::format("{}", convertible_to_cstring()), "bar");
} }
// Test that check is not found by ADL. // Test that check is not found by ADL.
@ -829,7 +836,7 @@ struct convertible_to_long_long {
operator long long() const { return 1LL << 32; } operator long long() const { return 1LL << 32; }
}; };
TEST(format_test, format_convertible_to_long_long) { TEST(core_test, format_convertible_to_long_long) {
EXPECT_EQ("100000000", fmt::format("{:x}", convertible_to_long_long())); EXPECT_EQ("100000000", fmt::format("{:x}", convertible_to_long_long()));
} }

View File

@ -1840,12 +1840,6 @@ TEST(format_test, join) {
} }
#ifdef __cpp_lib_byte #ifdef __cpp_lib_byte
TEST(format_test, format_byte) {
using arg_mapper = fmt::detail::arg_mapper<fmt::format_context>;
EXPECT_EQ(arg_mapper().map(std::byte(42)), 42);
EXPECT_EQ(fmt::format("{}", std::byte(42)), "42");
}
TEST(format_test, join_bytes) { TEST(format_test, join_bytes) {
auto v = std::vector<std::byte>{std::byte(1), std::byte(2), std::byte(3)}; auto v = std::vector<std::byte>{std::byte(1), std::byte(2), std::byte(3)};
EXPECT_EQ(fmt::format("{}", fmt::join(v, ", ")), "1, 2, 3"); EXPECT_EQ(fmt::format("{}", fmt::join(v, ", ")), "1, 2, 3");