2025-08-15 16:42:28 +08:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <yycc.hpp>
|
|
|
|
#include <yycc/carton/wcwidth.hpp>
|
2025-08-19 13:58:05 +08:00
|
|
|
#include <yycc/carton/termcolor.hpp>
|
2025-08-15 16:42:28 +08:00
|
|
|
|
|
|
|
#define WCWDITH ::yycc::carton::wcwidth
|
2025-08-19 13:58:05 +08:00
|
|
|
#define TERMCOLOR ::yycc::carton::termcolor
|
2025-08-15 16:42:28 +08:00
|
|
|
|
|
|
|
namespace yycctest::carton::wcwidth {
|
|
|
|
|
|
|
|
#define TEST_SUCCESS(strl, len) \
|
|
|
|
{ \
|
|
|
|
auto rv = WCWDITH::wcswidth(strl); \
|
|
|
|
ASSERT_TRUE(rv.has_value()); \
|
|
|
|
EXPECT_EQ(rv.value(), len); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TEST_FAIL(strl) \
|
|
|
|
{ \
|
|
|
|
auto rv = WCWDITH::wcswidth(strl); \
|
|
|
|
EXPECT_FALSE(rv.has_value()); \
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CartonWcwdith, BadAnsi) {
|
|
|
|
TEST_FAIL(u8"\033?");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CartonWcwdith, BadCsi) {
|
|
|
|
TEST_FAIL(u8"\033[\t");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CartonWcwdith, English) {
|
|
|
|
TEST_SUCCESS(u8"abc", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CartonWcwdith, Chinese) {
|
|
|
|
TEST_SUCCESS(u8"中文", 4);
|
|
|
|
TEST_SUCCESS(u8"中a文", 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CartonWcwdith, Japanese) {
|
|
|
|
TEST_SUCCESS(u8"ありがとう", 10);
|
|
|
|
TEST_SUCCESS(u8"アリガトウ", 10);
|
|
|
|
TEST_SUCCESS(u8"アリガトウ", 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(CartonWcwdith, Termcolor) {
|
2025-08-19 13:58:05 +08:00
|
|
|
using Color = TERMCOLOR::Color;
|
|
|
|
TEST_SUCCESS(TERMCOLOR::colored(u8"abc", Color::Red), 3);
|
|
|
|
TEST_SUCCESS(TERMCOLOR::colored(u8"中文", Color::Red), 4);
|
|
|
|
TEST_SUCCESS(TERMCOLOR::colored(u8"ありがとう", Color::Red), 10);
|
2025-08-15 16:42:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace yycctest::carton::wcwidth
|