From db4430e95bf01f5c74af510ee2f945d230a59c24 Mon Sep 17 00:00:00 2001 From: Tomasz Iwanek Date: Fri, 20 Feb 2015 16:16:39 +0100 Subject: [PATCH] [Unitttests] string_utils_unittest This commit adds unit tests for 'src/utils/string_util.*' Change-Id: I237ebe67f7f761afb5f6dfa5d6d648206e8f7d13 --- src/unit_tests/CMakeLists.txt | 3 + src/unit_tests/string_util_unittest.cc | 110 +++++++++++++++++++++++++++++++++ src/utils/string_util.cc | 16 ++--- src/utils/string_util.h | 9 +++ 4 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 src/unit_tests/string_util_unittest.cc diff --git a/src/unit_tests/CMakeLists.txt b/src/unit_tests/CMakeLists.txt index 608cc5a..9b46d00 100644 --- a/src/unit_tests/CMakeLists.txt +++ b/src/unit_tests/CMakeLists.txt @@ -5,6 +5,7 @@ SET(TESTS manifest_unittest manifest_handler_unittest manifest_util_unittest + string_util_unittest widget_manifest_parser_unittest xml_parser_unittest ) @@ -20,6 +21,7 @@ ADD_EXECUTABLE(manifest_handler_unittest widget_manifest_parser_manifest_handler_unittest.cc) ADD_EXECUTABLE(manifest_util_unittest widget_manifest_parser_manifest_util_unittest.cc) +ADD_EXECUTABLE(string_util_unittest string_util_unittest.cc) ADD_EXECUTABLE(widget_manifest_parser_unittest widget_manifest_parser_unittest.cc) ADD_EXECUTABLE(xml_parser_unittest @@ -45,6 +47,7 @@ target_link_libraries(values_unittest PUBLIC ${TARGET_LIBNAME_UTILS} ${GTEST_MAI target_link_libraries(manifest_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES}) target_link_libraries(manifest_handler_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES}) target_link_libraries(manifest_util_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES}) +target_link_libraries(string_util_unittest PUBLIC ${TARGET_LIBNAME_UTILS} ${GTEST_MAIN_LIBRARIES}) target_link_libraries(widget_manifest_parser_unittest PUBLIC ${TARGET_LIBNAME_WIDGET_MANIFEST_PARSER} ${GTEST_MAIN_LIBRARIES}) target_link_libraries(xml_parser_unittest PUBLIC ${TARGET_LIBNAME_XML_PARSER} ${GTEST_MAIN_LIBRARIES}) diff --git a/src/unit_tests/string_util_unittest.cc b/src/unit_tests/string_util_unittest.cc new file mode 100644 index 0000000..7f1a6c4 --- /dev/null +++ b/src/unit_tests/string_util_unittest.cc @@ -0,0 +1,110 @@ +// Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a apache-2.0 license that can be +// found in the LICENSE file. + +#include + +#include + +#include "utils/string_util.h" + +#define TEST_FOR_COLLAPSE_CHARCTER(Name, Sequence) \ + TEST(StringUtilTest, CollapseWhitespaceUTF8_ ## Name) { \ + std::string text = std::string(Sequence) + \ + std::string(Sequence) + std::string(Sequence) + \ + u8"Sample text wra" + std::string(Sequence) + u8"pped with sp" + \ + std::string(Sequence) + std::string(Sequence) + u8" aces" + \ + std::string(Sequence) + std::string(Sequence); \ + std::string result = \ + u8"Sample text wra pped with sp aces"; \ + EXPECT_EQ(result, CollapseWhitespaceUTF8(text)); \ + } \ + +#define TEST_FOR_STRIPPING_WRAPPING_BIDI_CHARACTERS(Name, Sequence) \ + TEST(StringUtilTest, \ + StripWrappingBidiControlCharactersUTF8Strip_ ## Name) { \ + std::string text = u8"Sample text wrapped with BIDI"; \ + EXPECT_EQ(text, \ + StripWrappingBidiControlCharactersUTF8( \ + std::string(Sequence) + \ + text + \ + std::string(kPopDirectionalFormatting))); \ + } \ + +namespace common_installer { +namespace utils { + +class StringUtilTest : public testing::Test { }; + +TEST(StringUtilTest, StripWrappingBidiControlCharactersUTF8None) { + std::string text = "I'm in ASCII"; + EXPECT_EQ(text, StripWrappingBidiControlCharactersUTF8(text)); +} + +TEST_FOR_STRIPPING_WRAPPING_BIDI_CHARACTERS(RTLEmbedding, + kRightToLeftEmbeddingMark) +TEST_FOR_STRIPPING_WRAPPING_BIDI_CHARACTERS(LTREmbedding, + kLeftToRightEmbeddingMark) +TEST_FOR_STRIPPING_WRAPPING_BIDI_CHARACTERS(RTLOverride, + kRightToLeftOverride) +TEST_FOR_STRIPPING_WRAPPING_BIDI_CHARACTERS(LTROverride, + kLeftToRightOverride) + +TEST(StringUtilTest, CollapseWhitespaceUTF8_NoCollapse) { + std::string text = u8"Sample text wrapped with spaces"; + EXPECT_EQ(text, CollapseWhitespaceUTF8(text)); +} + +TEST(StringUtilTest, CollapseWhitespaceUTF8_WhitespaceAtBothEnds) { + std::string text = + u8"Sample text wrapped with spaces"; + EXPECT_EQ(text, CollapseWhitespaceUTF8( + std::string(u8"\x09\xc2\x85") + + text + + std::string(u8"\xe2\x80\x8A\x0B"))); +} + +TEST(StringUtilTest, CollapseWhitespaceUTF8_WhitespaceAtBegin) { + std::string text = + u8"Sample text wrapped with spaces"; + EXPECT_EQ(text, CollapseWhitespaceUTF8( + std::string(u8"\xc2\x85\x09\xc2\x85") + + text)); +} + +TEST(StringUtilTest, CollapseWhitespaceUTF8_WhitespaceAtEnd) { + std::string text = + u8"Sample text wrapped with spaces"; + EXPECT_EQ(text, CollapseWhitespaceUTF8( + text + + std::string(u8"\x0C\xe2\x80\x8A\x0B\x0C\x0C"))); +} + +TEST_FOR_COLLAPSE_CHARCTER(CharacterTabulation, u8"\x09") +TEST_FOR_COLLAPSE_CHARCTER(LineFeed, u8"\x0A") +TEST_FOR_COLLAPSE_CHARCTER(LineTabulation, u8"\x0B") +TEST_FOR_COLLAPSE_CHARCTER(FormFeed, u8"\x0C") +TEST_FOR_COLLAPSE_CHARCTER(CarriageReturn, u8"\x0D") +TEST_FOR_COLLAPSE_CHARCTER(Space, u8"\x20") +TEST_FOR_COLLAPSE_CHARCTER(NextLine, u8"\xc2\x85") +TEST_FOR_COLLAPSE_CHARCTER(NoBreakSpace, u8"\xc2\xa0") +TEST_FOR_COLLAPSE_CHARCTER(OghamSpaceMark, u8"\xe1\x9a\x80") +TEST_FOR_COLLAPSE_CHARCTER(EnQuad, u8"\xe2\x80\x80") +TEST_FOR_COLLAPSE_CHARCTER(EmQuad, u8"\xe2\x80\x81") +TEST_FOR_COLLAPSE_CHARCTER(EnSpace, u8"\xe2\x80\x82") +TEST_FOR_COLLAPSE_CHARCTER(EmSpace, u8"\xe2\x80\x83") +TEST_FOR_COLLAPSE_CHARCTER(ThreePerEmSpace, u8"\xe2\x80\x84") +TEST_FOR_COLLAPSE_CHARCTER(FourPerEmSpace, u8"\xe2\x80\x85") +TEST_FOR_COLLAPSE_CHARCTER(SixPerEmSpace, u8"\xe2\x80\x86") +TEST_FOR_COLLAPSE_CHARCTER(FigureSpace, u8"\xe2\x80\x87") +TEST_FOR_COLLAPSE_CHARCTER(PunctuationSpace, u8"\xe2\x80\x88") +TEST_FOR_COLLAPSE_CHARCTER(ThinSpace, u8"\xe2\x80\x89") +TEST_FOR_COLLAPSE_CHARCTER(HairSeparator, u8"\xe2\x80\x8A") +TEST_FOR_COLLAPSE_CHARCTER(LineSepator, u8"\xe2\x80\xa8") +TEST_FOR_COLLAPSE_CHARCTER(ParagraphSeperator, u8"\xe2\x80\xa9") +TEST_FOR_COLLAPSE_CHARCTER(NarrowNoBreakSpace, u8"\xe2\x80\xaf") +TEST_FOR_COLLAPSE_CHARCTER(MediumMathematicalSpace, u8"\xe2\x81\x9f") +TEST_FOR_COLLAPSE_CHARCTER(IdeographicSpace, u8"\xe3\x80\x80") + +} // namespace utils +} // namespace common_installer diff --git a/src/utils/string_util.cc b/src/utils/string_util.cc index 1bc3c96..dab7dbf 100644 --- a/src/utils/string_util.cc +++ b/src/utils/string_util.cc @@ -11,14 +11,6 @@ namespace { -const char kRightToLeftMark[] = u8"\xE2\x80\x8F"; -const char kLeftToRightMark[] = u8"\xE2\x80\x8E"; -const char kLeftToRightEmbeddingMark[] = u8"\xE2\x80\xAA"; -const char kRightToLeftEmbeddingMark[] = u8"\xE2\x80\xAB"; -const char kPopDirectionalFormatting[] = u8"\xE2\x80\xAC"; -const char kLeftToRightOverride[] = u8"\xE2\x80\xAD"; -const char kRightToLeftOverride[] = u8"\xE2\x80\xAE"; - const unsigned kBidiControlCharacterLength = 3; const char kDirLTRKey[] = "ltr"; @@ -99,6 +91,14 @@ inline bool IsWhitespaceUTF8(const char* c) { namespace common_installer { namespace utils { +const char kRightToLeftMark[] = u8"\xE2\x80\x8F"; +const char kLeftToRightMark[] = u8"\xE2\x80\x8E"; +const char kLeftToRightEmbeddingMark[] = u8"\xE2\x80\xAA"; +const char kRightToLeftEmbeddingMark[] = u8"\xE2\x80\xAB"; +const char kPopDirectionalFormatting[] = u8"\xE2\x80\xAC"; +const char kLeftToRightOverride[] = u8"\xE2\x80\xAD"; +const char kRightToLeftOverride[] = u8"\xE2\x80\xAE"; + std::string CollapseWhitespaceUTF8(const std::string& text) { std::string result; result.resize(text.size()); diff --git a/src/utils/string_util.h b/src/utils/string_util.h index 03905be..719ae25 100644 --- a/src/utils/string_util.h +++ b/src/utils/string_util.h @@ -11,6 +11,15 @@ namespace common_installer { namespace utils { +// utf8 representation of unicode BIDI characters +extern const char kRightToLeftMark[]; +extern const char kLeftToRightMark[]; +extern const char kLeftToRightEmbeddingMark[]; +extern const char kRightToLeftEmbeddingMark[]; +extern const char kPopDirectionalFormatting[]; +extern const char kLeftToRightOverride[]; +extern const char kRightToLeftOverride[]; + std::string CollapseWhitespaceUTF8(const std::string& text); std::string StripWrappingBidiControlCharactersUTF8(const std::string& text); std::string GetDirTextUTF8(const std::string& text, const std::string& dir); -- 2.7.4