-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to print strings of arbitrary character type
Previously, this could cause compiler errors for funky character types
- Loading branch information
Showing
6 changed files
with
115 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,92 @@ | ||
#include <mettle.hpp> | ||
using namespace mettle; | ||
|
||
#include <cstring> | ||
|
||
template<typename Char> | ||
std::basic_string<Char> make_string(const char *s) { | ||
std::size_t n = std::strlen(s); | ||
std::basic_string<Char> result(n, '\0'); | ||
for(std::size_t i = 0; i != n; i++) | ||
result[i] = s[i]; | ||
return result; | ||
} | ||
|
||
suite<> test_string_output("string output", [](auto &_) { | ||
subsuite<>(_, "escape_string()", [](auto &_) { | ||
_.test("char", []() { | ||
_.test("std::string", []() { | ||
using namespace std::literals::string_literals; | ||
expect(escape_string("text"s), equal_to("\"text\"")); | ||
}); | ||
|
||
_.test("std::string_view", []() { | ||
using namespace std::literals::string_view_literals; | ||
expect(escape_string("text"sv), equal_to("\"text\"")); | ||
}); | ||
|
||
_.test("char *", []() { | ||
expect(escape_string("text"), equal_to("\"text\"")); | ||
}); | ||
|
||
_.test("escaping", []() { | ||
expect(escape_string("\"text\""), equal_to("\"\\\"text\\\"\"")); | ||
expect(escape_string("te\\xt"), equal_to("\"te\\\\xt\"")); | ||
expect(escape_string("text\nmore"), equal_to("\"text\\nmore\"")); | ||
expect(escape_string(std::string{'a', '\0', 'b'}), equal_to("\"a\\0b\"")); | ||
expect(escape_string("\a\b\f\n\r\t\v\x1f"), | ||
equal_to("\"\\a\\b\\f\\n\\r\\t\\v\\x1f\"")); | ||
expect(escape_string(std::string("text")), equal_to("\"text\"")); | ||
expect(escape_string(std::string{'a', '\0', 'b'}), equal_to("\"a\\0b\"")); | ||
}); | ||
|
||
_.test("delimiter", []() { | ||
expect(escape_string("'text'", '\''), equal_to("'\\'text\\''")); | ||
expect(escape_string("te\\xt", '\''), equal_to("'te\\\\xt'")); | ||
}); | ||
}); | ||
|
||
_.test("wchar_t", []() { | ||
expect(escape_string(L"text"), equal_to(L"\"text\"")); | ||
expect(escape_string(L"\"text\""), equal_to(L"\"\\\"text\\\"\"")); | ||
expect(escape_string(L"text\nmore"), equal_to(L"\"text\\nmore\"")); | ||
expect(escape_string(L"\a\b\f\n\r\t\v\x1f"), | ||
equal_to(L"\"\\a\\b\\f\\n\\r\\t\\v\\x1f\"")); | ||
expect(escape_string(std::wstring(L"text")), equal_to(L"\"text\"")); | ||
expect(escape_string(std::wstring{'a', '\0', 'b'}), | ||
equal_to(L"\"a\\0b\"")); | ||
|
||
expect(escape_string(L"'text'", L'\''), equal_to(L"'\\'text\\''")); | ||
subsuite< | ||
char, unsigned char, signed char, wchar_t, char16_t, char32_t | ||
>(_, "convert_string()", type_only, [](auto &_) { | ||
using Char = fixture_type_t<decltype(_)>; | ||
auto S = &make_string<Char>; | ||
|
||
_.test("std::basic_string", [S]() { | ||
expect(convert_string(S("text")), equal_to("text")); | ||
expect(convert_string(std::basic_string<Char>{'a', '\0', 'b'}), | ||
equal_to(std::string{'a', '\0', 'b'})); | ||
}); | ||
|
||
_.test("std::basic_string_view", [S]() { | ||
using sv = std::basic_string_view<Char>; | ||
expect(convert_string(sv(S("text"))), equal_to("text")); | ||
expect(convert_string(sv(std::basic_string<Char>{'a', '\0', 'b'})), | ||
equal_to(std::string{'a', '\0', 'b'})); | ||
}); | ||
|
||
_.test("C string", [S]() { | ||
expect(convert_string(S("text").c_str()), equal_to("text")); | ||
}); | ||
}); | ||
|
||
subsuite<>(_, "string_convert()", [](auto &_) { | ||
subsuite<>(_, "represent_string()", [](auto &_) { | ||
_.test("char", []() { | ||
std::string with_nul{'a', '\0', 'b'}; | ||
expect(string_convert("text"), equal_to("text")); | ||
expect(string_convert(std::string("text")), equal_to("text")); | ||
expect(string_convert(std::string{'a', '\0', 'b'}), | ||
equal_to(std::string{'a', '\0', 'b'})); | ||
expect(represent_string("text"), equal_to("\"text\"")); | ||
}); | ||
|
||
_.test("wchar_t", []() { | ||
std::wstring with_nul{'a', '\0', 'b'}; | ||
expect(string_convert(L"text"), equal_to("text")); | ||
expect(string_convert(std::wstring(L"text")), equal_to("text")); | ||
expect(string_convert(std::wstring{'a', '\0', 'b'}), | ||
equal_to(std::string{'a', '\0', 'b'})); | ||
expect(represent_string(L"text"), equal_to("\"text\"")); | ||
}); | ||
|
||
_.test("char16_t", []() { | ||
std::u16string with_nul{'a', '\0', 'b'}; | ||
expect(string_convert(u"text"), equal_to("text")); | ||
expect(string_convert(std::u16string(u"text")), equal_to("text")); | ||
expect(string_convert(std::u16string{'a', '\0', 'b'}), | ||
equal_to(std::string{'a', '\0', 'b'})); | ||
expect(represent_string(u"text"), equal_to("\"text\"")); | ||
}); | ||
|
||
_.test("char32_t", []() { | ||
std::u32string with_nul{'a', '\0', 'b'}; | ||
expect(string_convert(U"text"), equal_to("text")); | ||
expect(string_convert(std::u32string(U"text")), equal_to("text")); | ||
expect(string_convert(std::u32string{'a', '\0', 'b'}), | ||
equal_to(std::string{'a', '\0', 'b'})); | ||
expect(represent_string(U"text"), equal_to("\"text\"")); | ||
}); | ||
|
||
_.test("int", []() { | ||
expect(represent_string(std::basic_string<int>{'t', 'e', 'x', 't'}), | ||
equal_to("(unrepresentable string)")); | ||
}); | ||
}); | ||
}); |