From c27c15052851d397faba13cbb608dc875895b2b6 Mon Sep 17 00:00:00 2001 From: Daniel Burrows Date: Mon, 7 Jun 2010 09:23:57 -0700 Subject: Wrap a layer of indirection around wcwidth(). This is necessary so that I can test the behavior of terminal output routines that care about string widths, without depending on system locale settings or data. --- tests/test_teletype_mock.cc | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/tests/test_teletype_mock.cc b/tests/test_teletype_mock.cc index 3a68b59b..e0b1dd99 100644 --- a/tests/test_teletype_mock.cc +++ b/tests/test_teletype_mock.cc @@ -42,7 +42,10 @@ using testing::Test; namespace { - const wchar_t two_column_char = L'\uff01'; + // Note that I use a character that is *never* multi-column under + // normal circumstances, to verify that the locale object is + // actually being used. + const wchar_t two_column_char = L'Z'; struct TeletypeTest : public Test { @@ -61,6 +64,7 @@ namespace std::string previous_lc_ctype; shared_ptr term; + shared_ptr term_locale; shared_ptr teletype; static std::string safe_string(const char *c) @@ -88,31 +92,25 @@ namespace TeletypeTest() : widechar(1, two_column_char), term(mocks::create_combining_terminal()), - teletype(mocks::create_teletype(term)) + term_locale(mocks::terminal_locale::create()), + teletype(mocks::create_teletype(term, term_locale)) { EXPECT_CALL(*term, get_screen_width()) .WillRepeatedly(Return(80)); + + EXPECT_CALL(*term_locale, wcwidth(two_column_char)) + .WillRepeatedly(Return(2)); } void SetUp() { - previous_lc_ctype = safe_string(setlocale(LC_CTYPE, NULL)); - setlocale(LC_CTYPE, "zh_CN.UTF-8"); - // Sanity-check the widechar string. EXPECT_EQ(1, widechar.size()); if(widechar.size() >= 0) { EXPECT_EQ(two_column_char, widechar[0]); - EXPECT_TRUE(iswprint(widechar[0])); + EXPECT_EQ(2, term_locale->wcwidth(widechar[0])); } - EXPECT_EQ(1, wcslen(widechar.c_str())); - EXPECT_EQ(2, wcswidth(widechar.c_str(), widechar.size())); - } - - void TearDown() - { - setlocale(LC_CTYPE, previous_lc_ctype.c_str()); } }; } -- cgit v1.2.3