Repository: fmtlib/fmt
File: test/module-test.cc (C++)

Code before:
TEST(module_test, format) {
  EXPECT_EQ("42", fmt::format("{}", 42));
  EXPECT_EQ("42", fmt::format("{:}", 42u));
  EXPECT_EQ("+42", fmt::format("{:+d}", short{42}));
  EXPECT_EQ("42", fmt::format("{:d}", unsigned short{42u}));
  EXPECT_EQ("-42", fmt::format("{0}", -42L));
  EXPECT_EQ("2a", fmt::format("{0:x}", 42uL));
  EXPECT_EQ("***42", fmt::format("{:*>5}", 42LL));
  EXPECT_EQ("0X2A", fmt::format("{:#X}", 42uLL));
  EXPECT_EQ("===42.000000===", fmt::format("{:=^15f}", 42.0));
  EXPECT_EQ("00000042.1", fmt::format("{:<010.5G}", 42.1f));
  EXPECT_EQ(" +4.21235e+11", fmt::format("{:+{}.{}e}", 42.1234567e10L, 13, 5));
  EXPECT_EQ("-0x1.5000000000000p+5", fmt::format("{: a}", -42.0));
  constexpr auto nan = std::numeric_limits<double>::quiet_NaN();
  EXPECT_EQ("-nan-", fmt::format("{:-^5}", nan));
  constexpr auto inf = std::numeric_limits<double>::infinity();
  EXPECT_EQ("-INF-", fmt::format("{:-^5F}", inf));
  EXPECT_EQ("-INF ", fmt::format("{: 5G}", -inf));
  EXPECT_EQ("42", fmt::format("{:s}", "42"));
  EXPECT_EQ("a", fmt::format("{:c}", 'a'));
  EXPECT_EQ("a", fmt::format("{:c}", unsigned char{'a'}));
  EXPECT_EQ("a", fmt::format("{:c}", signed char{'a'}));
  EXPECT_EQ("false", fmt::format("{}", false));
  EXPECT_EQ("0B1", fmt::format("{:#B}", true));
  EXPECT_EQ("0x2a", fmt::format("{:p}", (void *)42));
  EXPECT_EQ("1234", fmt::format("{:L}", 1234));
}

TEST(module_test, format_to) {
  std::string s;
  fmt::format_to(std::back_inserter(s), "{}", 42);
  EXPECT_EQ("42", s);

  char buffer[4] = {0};
  fmt::format_to(buffer, "{}", 42);
  EXPECT_EQ("42", std::string_view(buffer));
}

TEST(module_test, formatted_size) {
  EXPECT_EQ(2u, fmt::formatted_size("{}", 42));
}

TEST(module_test, format_to_n) {
  {
    std::string s;
    auto result = fmt::format_to_n(std::back_inserter(s), 1, "{}", 42);
    EXPECT_EQ(2u, result.size);
    EXPECT_EQ("4", s);
  }
  {
    char buffer[4] = {0};

Review comment (suggestion):
For example here we don't need to test all argument types. Since they all go through the same code path, it is very unlikely that one of them works and another doesn't. I suggest picking one type (e.g. int) and testing public methods with it. In the unlikely case that we find some problem in the future, we can add a regression test.

Code after:
}

TEST(module_test, format_to) {
  std::string s;
  fmt::format_to(std::back_inserter(s), "{}", 42);
  EXPECT_EQ("42", s);

  char buffer[4] = {0};
  fmt::format_to(buffer, "{}", 42);
  EXPECT_EQ("42", std::string_view(buffer));

  fmt::memory_buffer mb;
  fmt::format_to(mb, "{}", 42);
  EXPECT_EQ("42", std::string_view(buffer));
}

TEST(module_test, formatted_size) {
  EXPECT_EQ(2u, fmt::formatted_size("{}", 42));
}

TEST(module_test, format_to_n) {
  std::string s;
  auto result = fmt::format_to_n(std::back_inserter(s), 1, "{}", 42);
  EXPECT_EQ(2u, result.size);
  char buffer[4] = {0};
  fmt::format_to_n(buffer, 3, "{}", 12345);
}

TEST(module_test, format_args) {
  auto no_args = fmt::format_args();
  EXPECT_FALSE(no_args.get(1));

  fmt::basic_format_args args = fmt::make_format_args(42);
  EXPECT_TRUE(args.max_size() > 0);
  auto arg0 = args.get(0);
  EXPECT_TRUE(arg0);
  decltype(arg0) arg_none;
  EXPECT_FALSE(arg_none);
  EXPECT_TRUE(arg0.type() != arg_none.type());
}

TEST(module_test, wformat_args) {
  auto no_args = fmt::wformat_args();
  EXPECT_FALSE(no_args.get(1));
  fmt::basic_format_args args = fmt::make_wformat_args(42);
  EXPECT_TRUE(args.get(0));
}

TEST(module_test, checked_format_args) {
  fmt::basic_format_args args =
    fmt::make_args_checked<int>("{}", 42);