From 39818e79798cd1c809a4ca435cde16b3c5c20841 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 25 Apr 2021 16:19:01 -0700 Subject: [PATCH] Cleanup core-test --- test/core-test.cc | 149 +++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 75 deletions(-) diff --git a/test/core-test.cc b/test/core-test.cc index 3c2ab68c..3df04618 100644 --- a/test/core-test.cc +++ b/test/core-test.cc @@ -11,15 +11,14 @@ #include "fmt/core.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include // std::copy_n +#include // INT_MAX +#include // std::strlen +#include // std::equal_to +#include // std::back_insert_iterator +#include // std::numeric_limits +#include // std::string +#include // std::is_same #include "gmock.h" @@ -71,6 +70,50 @@ TEST(string_view_test, compare) { check_op(); } +namespace test_ns { +template class test_string { + private: + std::basic_string s_; + + public: + test_string(const Char* s) : s_(s) {} + const Char* data() const { return s_.data(); } + size_t length() const { return s_.size(); } + operator const Char*() const { return s_.c_str(); } +}; + +template +fmt::basic_string_view to_string_view(const test_string& s) { + return {s.data(), s.length()}; +} + +struct non_string {}; +} // namespace test_ns + +template class is_string_test : public testing::Test {}; + +using string_char_types = testing::Types; +TYPED_TEST_SUITE(is_string_test, string_char_types); + +template +struct derived_from_string_view : fmt::basic_string_view {}; + +TYPED_TEST(is_string_test, is_string) { + EXPECT_TRUE(fmt::detail::is_string::value); + EXPECT_TRUE(fmt::detail::is_string::value); + EXPECT_TRUE(fmt::detail::is_string::value); + EXPECT_TRUE(fmt::detail::is_string::value); + EXPECT_TRUE(fmt::detail::is_string>::value); + EXPECT_TRUE(fmt::detail::is_string>::value); + EXPECT_TRUE( + fmt::detail::is_string>::value); + using fmt_string_view = fmt::detail::std_string_view; + EXPECT_TRUE(std::is_empty::value != + fmt::detail::is_string::value); + EXPECT_TRUE(fmt::detail::is_string>::value); + EXPECT_FALSE(fmt::detail::is_string::value); +} + #if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 470 TEST(buffer_test, noncopyable) { EXPECT_FALSE(std::is_copy_constructible>::value); @@ -453,6 +496,14 @@ TEST(core_test, is_formattable) { static_assert(fmt::is_formattable::value, ""); } +TEST(core_test, format) { EXPECT_EQ(fmt::format("{}", 42), "42"); } + +TEST(core_test, format_to) { + std::string s; + fmt::format_to(std::back_inserter(s), "{}", 42); + EXPECT_EQ(s, "42"); +} + struct convertible_to_int { operator int() const { return 42; } }; @@ -487,76 +538,24 @@ TEST(core_test, formatter_overrides_implicit_conversion) { EXPECT_EQ(fmt::format("{}", convertible_to_c_string()), "bar"); } -namespace my_ns { -template class my_string { - private: - std::basic_string s_; - - public: - my_string(const Char* s) : s_(s) {} - const Char* data() const FMT_NOEXCEPT { return s_.data(); } - size_t length() const FMT_NOEXCEPT { return s_.size(); } - operator const Char*() const { return s_.c_str(); } -}; - -template -fmt::basic_string_view to_string_view(const my_string& s) - FMT_NOEXCEPT { - return {s.data(), s.length()}; -} - -struct non_string {}; -} // namespace my_ns - -template class IsStringTest : public testing::Test {}; - -typedef testing::Types StringCharTypes; -TYPED_TEST_SUITE(IsStringTest, StringCharTypes); - -namespace { -template -struct derived_from_string_view : fmt::basic_string_view {}; -} // namespace - -TYPED_TEST(IsStringTest, IsString) { - EXPECT_TRUE(fmt::detail::is_string::value); - EXPECT_TRUE(fmt::detail::is_string::value); - EXPECT_TRUE(fmt::detail::is_string::value); - EXPECT_TRUE(fmt::detail::is_string::value); - EXPECT_TRUE(fmt::detail::is_string>::value); - EXPECT_TRUE(fmt::detail::is_string>::value); - EXPECT_TRUE( - fmt::detail::is_string>::value); - using fmt_string_view = fmt::detail::std_string_view; - EXPECT_TRUE(std::is_empty::value != - fmt::detail::is_string::value); - EXPECT_TRUE(fmt::detail::is_string>::value); - EXPECT_FALSE(fmt::detail::is_string::value); -} - -TEST(core_test, Format) { EXPECT_EQ(fmt::format("{}", 42), "42"); } - +// Test that check is not found by ADL. template void check(T); -TEST(core_test, ADL) { EXPECT_EQ(fmt::format("{}", test_struct()), "test"); } - -TEST(core_test, FormatTo) { - std::string s; - fmt::format_to(std::back_inserter(s), "{}", 42); - EXPECT_EQ(s, "42"); +TEST(core_test, adl_check) { + EXPECT_EQ(fmt::format("{}", test_struct()), "test"); } -TEST(core_test, ToStringViewForeignStrings) { - using namespace my_ns; - EXPECT_EQ(to_string_view(my_string("42")), "42"); +TEST(core_test, to_string_view_foreign_strings) { + using namespace test_ns; + EXPECT_EQ(to_string_view(test_string("42")), "42"); fmt::detail::type type = - fmt::detail::mapped_type_constant, + fmt::detail::mapped_type_constant, fmt::format_context>::value; EXPECT_EQ(type, fmt::detail::type::string_type); } -TEST(core_test, FormatForeignStrings) { - using namespace my_ns; - EXPECT_EQ(fmt::format(my_string("{}"), 42), "42"); +TEST(core_test, format_foreign_strings) { + using namespace test_ns; + EXPECT_EQ(fmt::format(test_string("{}"), 42), "42"); } struct implicitly_convertible_to_string { @@ -567,7 +566,7 @@ struct implicitly_convertible_to_string_view { operator fmt::string_view() const { return "foo"; } }; -TEST(core_test, FormatImplicitlyConvertibleToStringView) { +TEST(core_test, format_implicitly_convertible_to_string_view) { EXPECT_EQ("foo", fmt::format("{}", implicitly_convertible_to_string_view())); } @@ -577,7 +576,7 @@ struct explicitly_convertible_to_string_view { explicit operator fmt::string_view() const { return "foo"; } }; -TEST(core_test, FormatExplicitlyConvertibleToStringView) { +TEST(core_test, format_explicitly_convertible_to_string_view) { EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_string_view())); } @@ -586,7 +585,7 @@ struct explicitly_convertible_to_std_string_view { explicit operator std::string_view() const { return "foo"; } }; -TEST(core_test, FormatExplicitlyConvertibleToStdStringView) { +TEST(core_test, format_explicitly_convertible_to_std_string_view) { EXPECT_EQ("foo", fmt::format("{}", explicitly_convertible_to_std_string_view())); } @@ -600,6 +599,6 @@ struct disabled_rvalue_conversion { operator const char*() && = delete; }; -TEST(core_test, DisabledRValueConversion) { +TEST(core_test, disabled_rvalue_conversion) { EXPECT_EQ("foo", fmt::format("{}", disabled_rvalue_conversion())); }