Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Mar 20, 2024
1 parent e60a9e1 commit e89e637
Show file tree
Hide file tree
Showing 7 changed files with 530 additions and 332 deletions.
299 changes: 90 additions & 209 deletions test/test_format/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,287 +7,168 @@
#include <iostream>
#include "test_format.hpp"

TEST(format, plain_text)
TYPED_TEST(format_suite, plain_text)
{
using namespace papilio;

EXPECT_EQ(PAPILIO_NS format(""), "");
EXPECT_EQ(PAPILIO_NS format("plain text"), "plain text");
EXPECT_EQ(PAPILIO_NS format("{{plain text}}"), "{plain text}");

EXPECT_EQ(PAPILIO_NS format(L""), L"");
EXPECT_EQ(PAPILIO_NS format(L"plain text"), L"plain text");
EXPECT_EQ(PAPILIO_NS format(L"{{plain text}}"), L"{plain text}");
}

TEST(format, format_to)
{
using namespace papilio;
using string_view_type = typename TestFixture::string_view_type;

{
std::vector<char> result;
auto it = PAPILIO_NS format_to(std::back_inserter(result), "vec");
*it = '\0';
EXPECT_EQ(result.size(), 4);
EXPECT_STREQ(result.data(), "vec");
}

{
auto loc = test_format::attach_yes_no();

std::vector<char> result;
auto it = PAPILIO_NS format_to(std::back_inserter(result), loc, "{:L}", true);
*it = '\0';
EXPECT_EQ(result.size(), 4);
EXPECT_STREQ(result.data(), "yes");
}
}
string_view_type empty_fmt{};

TEST(format, formatted_size)
{
using namespace papilio;

EXPECT_EQ(PAPILIO_NS formatted_size(""), 0);
EXPECT_EQ(PAPILIO_NS formatted_size("hello"), 5);
EXPECT_EQ(PAPILIO_NS formatted_size("{{hello}}"), 7);

{
auto loc = test_format::attach_yes_no();
EXPECT_EQ(PAPILIO_NS formatted_size(loc, "{:L}", true), 3);
EXPECT_EQ(
PAPILIO_NS format(empty_fmt),
string_view_type()
);
}
}

TEST(format, format_to_n)
{
using namespace papilio;

{
std::string str;
str.resize(5);
auto result = PAPILIO_NS format_to_n(str.begin(), str.size(), "hello world");
string_view_type plain_text = PAPILIO_TSTRING_VIEW(TypeParam, "plain text");

EXPECT_EQ(result.out, str.begin() + str.size());
EXPECT_EQ(result.size, str.size());
EXPECT_EQ(result.size, 5);
EXPECT_EQ(str, "hello");
EXPECT_EQ(
PAPILIO_NS format(plain_text),
plain_text
);
}

{
auto loc = test_format::attach_yes_no();
string_view_type esc_seq = PAPILIO_TSTRING_VIEW(TypeParam, "{{plain text}}");
string_view_type expected_str = PAPILIO_TSTRING_VIEW(TypeParam, "{plain text}");

std::string str;
str.resize(4);
auto result = PAPILIO_NS format_to_n(str.begin(), str.size(), loc, "{:L}!!", true);

EXPECT_EQ(result.out, str.begin() + str.size());
EXPECT_EQ(result.size, str.size());
EXPECT_EQ(result.size, 4);
EXPECT_EQ(str, "yes!");
EXPECT_EQ(
PAPILIO_NS format(esc_seq),
expected_str
);
}
}

TEST(format, exception)
TYPED_TEST(format_suite, format_to)
{
using namespace papilio;

EXPECT_THROW((void)PAPILIO_NS format("{"), papilio::format_error);
EXPECT_THROW((void)PAPILIO_NS format("}"), papilio::format_error);
}

TEST(format, script)
{
using namespace papilio;

EXPECT_EQ(PAPILIO_NS format("{$ {}: 'true'}", 1), "true");
EXPECT_EQ(PAPILIO_NS format("{$ !{}: 'false'}", 0), "false");
EXPECT_EQ(PAPILIO_NS format("{$ {val}: 'true'}", "val"_a = 1), "true");

EXPECT_EQ(PAPILIO_NS format("{$ {} == {}: 'eq'}", 1, 1), "eq");
EXPECT_EQ(PAPILIO_NS format("{$ {} != {}: 'ne'}", 1, 2), "ne");

EXPECT_EQ(PAPILIO_NS format("{$ {} > {}: 'gt'}", 2, 1), "gt");
EXPECT_EQ(PAPILIO_NS format("{$ {} < {}: 'lt'}", 1, 2), "lt");

EXPECT_EQ(PAPILIO_NS format("{$ {} >= {}: 'ge'}", 2, 1), "ge");
EXPECT_EQ(PAPILIO_NS format("{$ {} <= {}: 'le'}", 1, 2), "le");
EXPECT_EQ(PAPILIO_NS format("{$ {} >= {}: 'ge'}", 1, 1), "ge");
EXPECT_EQ(PAPILIO_NS format("{$ {} <= {}: 'le'}", 1, 1), "le");

{
std::string_view script = "{$ {}: 'a': ${}: 'b' : 'c'}";
const auto vec_str = PAPILIO_TSTRING(TypeParam, "vec");

EXPECT_EQ(PAPILIO_NS format(script, true, true), "a");
EXPECT_EQ(PAPILIO_NS format(script, true, false), "a");
EXPECT_THROW((void)PAPILIO_NS format(script, true), std::out_of_range);
std::vector<TypeParam> result;
auto it = PAPILIO_NS format_to(
std::back_inserter(result),
vec_str
);
*it = '\0';

EXPECT_EQ(PAPILIO_NS format(script, false, true), "b");
EXPECT_EQ(PAPILIO_NS format(script, false, false), "c");
EXPECT_EQ(result.size(), 4);
EXPECT_STREQ(result.data(), vec_str);
}

{
std::string_view script = "{$ {}: 'a': ${}: 'b' : ${} : 'c'}";
std::locale loc = test_format::attach_yes_no<TypeParam>();

EXPECT_EQ(PAPILIO_NS format(script, true, true, false), "a");
EXPECT_EQ(PAPILIO_NS format(script, true, false, false), "a");
std::vector<TypeParam> result;
auto it = PAPILIO_NS format_to(
std::back_inserter(result),
loc,
PAPILIO_TSTRING_VIEW(TypeParam, "{:L}"),
true
);
*it = '\0';

EXPECT_EQ(PAPILIO_NS format(script, false, true, true), "b");
EXPECT_EQ(PAPILIO_NS format(script, false, false, true), "c");
EXPECT_EQ(PAPILIO_NS format(script, false, false, false), "");
EXPECT_EQ(result.size(), 4);
EXPECT_STREQ(
result.data(),
test_format::yes_no_numpunct<TypeParam>::yes_string
);
}
}

TEST(format, composite)
TYPED_TEST(format_suite, formatted_size)
{
using namespace papilio;

EXPECT_EQ(PAPILIO_NS format("{} {}", 182375, 182376), "182375 182376");

EXPECT_EQ(PAPILIO_NS format("{.length:*>4}", "hello"), "***5");
EXPECT_EQ(PAPILIO_NS format("length is {.length}", "hello"), "length is 5");

{
std::string_view fmt = "{0} warning{${0}>1:'s'}";

EXPECT_EQ(PAPILIO_NS format(fmt, 1), "1 warning");
EXPECT_EQ(PAPILIO_NS format(fmt, 2), "2 warnings");
}

{
std::string_view fmt =
"There"
" {${0} != 1: 'are' : 'is'} "
"{0}"
" apple{${0} != 1: 's'}";

EXPECT_EQ(PAPILIO_NS format(fmt, 1), "There is 1 apple");
EXPECT_EQ(PAPILIO_NS format(fmt, 2), "There are 2 apples");
}

{
std::string_view fmt = "{${0}==0: 'zero' : {0}}";
using string_view_type = typename TestFixture::string_view_type;

EXPECT_EQ(PAPILIO_NS format(fmt, 0), "zero");
EXPECT_EQ(PAPILIO_NS format(fmt, 1), "1");
EXPECT_EQ(PAPILIO_NS format(fmt, 2), "2");
}
}
EXPECT_EQ(PAPILIO_NS formatted_size(string_view_type()), 0);

namespace test_format
{
class stream_only
{
public:
friend std::ostream& operator<<(std::ostream& os, const stream_only&)
{
os << "stream only";
return os;
string_view_type fmt = PAPILIO_TSTRING_VIEW(TypeParam, "hello");
EXPECT_EQ(PAPILIO_NS formatted_size(fmt), 5);
}

friend std::wostream& operator<<(std::wostream& os, const stream_only&)
{
os << L"stream only";
return os;
string_view_type fmt = PAPILIO_TSTRING_VIEW(TypeParam, "{{hello}}");
EXPECT_EQ(PAPILIO_NS formatted_size(fmt), 7); // Size of "{hello}"
}
};
} // namespace test_format

TEST(format, ostream_compat)
{
using namespace papilio;

{
test_format::stream_only val;
std::locale loc = test_format::attach_yes_no<TypeParam>();

EXPECT_EQ(PAPILIO_NS format("{}", val), "stream only");
string_view_type fmt = PAPILIO_TSTRING_VIEW(TypeParam, "{:L}");
EXPECT_EQ(PAPILIO_NS formatted_size(loc, fmt, true), 3); // Size of "yes"
}
}

TEST(format, wchar_t)
TYPED_TEST(format_suite, format_to_n)
{
using namespace papilio;

EXPECT_EQ(PAPILIO_NS format(L""), L"");
EXPECT_EQ(PAPILIO_NS format(L"plain text"), L"plain text");
EXPECT_EQ(PAPILIO_NS format(L"{{plain text}}"), L"{plain text}");

EXPECT_EQ(PAPILIO_NS format(L"{.length:*>4}", L"hello"), L"***5");
EXPECT_EQ(PAPILIO_NS format(L"length is {.length}", L"hello"), L"length is 5");

{
std::vector<wchar_t> result;
auto it = PAPILIO_NS format_to(std::back_inserter(result), L"vec");
*it = L'\0';
EXPECT_EQ(result.size(), 4);
EXPECT_STREQ(result.data(), L"vec");
}

{
auto loc = test_format::attach_yes_no<wchar_t>();

std::vector<wchar_t> result;
auto it = PAPILIO_NS format_to(std::back_inserter(result), loc, L"{:L}", true);
*it = L'\0';
EXPECT_EQ(result.size(), 4);
EXPECT_STREQ(result.data(), L"yes");
}
using string_type = typename TestFixture::string_type;
using string_view_type = typename TestFixture::string_view_type;

{
std::wstring str;
string_type str{};
str.resize(5);
auto result = PAPILIO_NS format_to_n(str.begin(), str.size(), L"hello world");
auto result = PAPILIO_NS format_to_n(
str.begin(),
str.size(),
PAPILIO_TSTRING_VIEW(TypeParam, "hello world")
);

EXPECT_EQ(result.out, str.begin() + str.size());
EXPECT_EQ(result.size, str.size());
EXPECT_EQ(result.size, 5);
EXPECT_EQ(str, L"hello");

const auto expected_str = PAPILIO_TSTRING_VIEW(TypeParam, "hello");
EXPECT_EQ(str, expected_str);
}

{
auto loc = test_format::attach_yes_no<wchar_t>();
std::locale loc = test_format::attach_yes_no<TypeParam>();

std::wstring str;
string_type str{};
str.resize(4);
auto result = PAPILIO_NS format_to_n(str.begin(), str.size(), loc, L"{:L}!!", true);
auto result = PAPILIO_NS format_to_n(
str.begin(),
str.size(),
loc,
PAPILIO_TSTRING_VIEW(TypeParam, "{:L}!!"),
true
);

EXPECT_EQ(result.out, str.begin() + str.size());
EXPECT_EQ(result.size, str.size());
EXPECT_EQ(result.size, 4);
EXPECT_EQ(str, L"yes!");
}

EXPECT_EQ(PAPILIO_NS formatted_size(L""), 0);
EXPECT_EQ(PAPILIO_NS formatted_size(L"hello"), 5);
EXPECT_EQ(PAPILIO_NS formatted_size(L"{{hello}}"), 7);

{
test_format::stream_only val;

EXPECT_EQ(PAPILIO_NS format(L"{}", val), L"stream only");
const auto expected_str = PAPILIO_TSTRING_VIEW(TypeParam, "yes!");
EXPECT_EQ(str, expected_str);
}
}

{
std::wstring_view fmt = L"{0} warning{${0}>1:'s'}";

EXPECT_EQ(PAPILIO_NS format(fmt, 1), L"1 warning");
EXPECT_EQ(PAPILIO_NS format(fmt, 2), L"2 warnings");
}
TYPED_TEST(format_suite, exception)
{
using namespace papilio;

{
std::wstring_view fmt = L"{${0}==0: 'zero' : {0}}";
using string_view_type = typename TestFixture::string_view_type;

EXPECT_EQ(PAPILIO_NS format(fmt, 0), L"zero");
EXPECT_EQ(PAPILIO_NS format(fmt, 1), L"1");
EXPECT_EQ(PAPILIO_NS format(fmt, 2), L"2");
}
const string_view_type bad_fmts[]{
PAPILIO_TSTRING_VIEW(TypeParam, "{"),
PAPILIO_TSTRING_VIEW(TypeParam, "}")
};

for(string_view_type f : bad_fmts)
{
std::wstring_view script = L"{$ {}: 'a': ${}: 'b' : 'c'}";

EXPECT_EQ(PAPILIO_NS format(script, true, true), L"a");
EXPECT_EQ(PAPILIO_NS format(script, true, false), L"a");
EXPECT_THROW((void)PAPILIO_NS format(script, true), std::out_of_range);

EXPECT_EQ(PAPILIO_NS format(script, false, true), L"b");
EXPECT_EQ(PAPILIO_NS format(script, false, false), L"c");
EXPECT_THROW(
(void)PAPILIO_NS format(f),
papilio::format_error
) << "f = "
<< std::quoted(utf::basic_string_ref<TypeParam>(f).to_string());
}
}
Loading

0 comments on commit e89e637

Please sign in to comment.