Skip to content

Commit

Permalink
Add support for char8_t
Browse files Browse the repository at this point in the history
  • Loading branch information
jimporter committed Jul 23, 2024
1 parent e79ec88 commit 5d20c93
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
matchers which need access to non-const objects (e.g. to call a non-const
member function)
- New matchers `in_interval` and `exception_what`
- Add support for `char8_t`

### Bug fixes
- Test failures across multiple runs are now correctly grouped in the summary
Expand Down
1 change: 1 addition & 0 deletions doc/about/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ in progress
matchers which need access to non-const objects (e.g. to call a non-const
member function)
- New matchers `in_interval` and `exception_what`
- Add support for `char8_t`

### Bug fixes
- Test failures across multiple runs are now correctly grouped in the summary
Expand Down
8 changes: 8 additions & 0 deletions include/mettle/output/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ namespace mettle {
return s;
}

#if __cpp_char8_t
inline std::string_view
convert_string(const std::u8string_view &s) {
return std::string_view(reinterpret_cast<const char *>(s.begin()),
reinterpret_cast<const char *>(s.end()));
}
#endif

// Ignore warnings about deprecated <codecvt>.
#if defined(_MSC_VER) && !defined(__clang__)
# pragma warning(push)
Expand Down
6 changes: 6 additions & 0 deletions include/mettle/output/to_printable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ namespace mettle {
return represent_string(std::wstring(1, c), '\'');
}

#if __cpp_char8_t
inline std::string to_printable(char8_t c) {
return represent_string(std::u8string(1, c), '\'');
}
#endif

inline std::string to_printable(char16_t c) {
return represent_string(std::u16string(1, c), '\'');
}
Expand Down
3 changes: 3 additions & 0 deletions include/mettle/output/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace mettle {

template<> struct is_any_char<char> : std::true_type {};
template<> struct is_any_char<wchar_t> : std::true_type {};
#if __cpp_char8_t
template<> struct is_any_char<char8_t> : std::true_type {};
#endif
template<> struct is_any_char<char16_t> : std::true_type {};
template<> struct is_any_char<char32_t> : std::true_type {};

Expand Down
12 changes: 10 additions & 2 deletions test/output/test_string_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ suite<> test_string_output("string output", [](auto &_) {
});

subsuite<
char, wchar_t, char16_t, char32_t
char,
#if __cpp_char8_t
char8_t,
#endif
wchar_t, char16_t, char32_t
>(_, "convert_string()", type_only, [](auto &_) {
using C = fixture_type_t<decltype(_)>;
auto T = &make_string<C>;
Expand All @@ -69,7 +73,11 @@ suite<> test_string_output("string output", [](auto &_) {
});

subsuite<
char, wchar_t, char16_t, char32_t
char,
#if __cpp_char8_t
char8_t,
#endif
wchar_t, char16_t, char32_t
>(_, "represent_string()", type_only, [](auto &_) {
using C = fixture_type_t<decltype(_)>;
auto T = &make_string<C>;
Expand Down
26 changes: 26 additions & 0 deletions test/output/test_to_printable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,32 @@ suite<> test_to_printable("to_printable()", [](auto &_) {
expect(ns, stringified("nullptr"));
});

#if __cpp_char8_t
_.test("char8_t", []() {
expect(u8'x', stringified("'x'"));
expect(u8'\n', stringified("'\\n'"));
expect(u8'\0', stringified("'\\0'"));
expect(u8'\x1f', stringified("'\\x1f'"));

expect(u8"text", stringified("\"text\""));
expect(u8"text\nmore", stringified("\"text\\nmore\""));
expect(std::u8string(u8"text"), stringified("\"text\""));
expect(std::u8string_view(u8"text"), stringified("\"text\""));
expect(std::u8string{u8'a', u8'\0', u8'b'}, stringified("\"a\\0b\""));

char8_t s[] = u8"text";
expect(s, stringified("\"text\""));
expect(static_cast<char8_t*>(s), stringified("\"text\""));

const char8_t cs[] = u8"text";
expect(cs, stringified("\"text\""));
expect(static_cast<const char8_t*>(cs), stringified("\"text\""));

char8_t *ns = nullptr;
expect(ns, stringified("nullptr"));
});
#endif

_.test("char16_t", []() {
expect(u'x', stringified("'x'"));
expect(u'\n', stringified("'\\n'"));
Expand Down

0 comments on commit 5d20c93

Please sign in to comment.