1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <gtest/gtest.h> 18 #include <string> 19 20 #include "StringPiece.h" 21 #include "Util.h" 22 23 namespace aapt { 24 25 TEST(UtilTest, TrimOnlyWhitespace) { 26 const std::u16string full = u"\n "; 27 28 StringPiece16 trimmed = util::trimWhitespace(full); 29 EXPECT_TRUE(trimmed.empty()); 30 EXPECT_EQ(0u, trimmed.size()); 31 } 32 33 TEST(UtilTest, StringEndsWith) { 34 EXPECT_TRUE(util::stringEndsWith<char>("hello.xml", ".xml")); 35 } 36 37 TEST(UtilTest, StringStartsWith) { 38 EXPECT_TRUE(util::stringStartsWith<char>("hello.xml", "he")); 39 } 40 41 TEST(UtilTest, StringBuilderWhitespaceRemoval) { 42 EXPECT_EQ(StringPiece16(u"hey guys this is so cool"), 43 util::StringBuilder().append(u" hey guys ") 44 .append(u" this is so cool ") 45 .str()); 46 47 EXPECT_EQ(StringPiece16(u" wow, so many \t spaces. what?"), 48 util::StringBuilder().append(u" \" wow, so many \t ") 49 .append(u"spaces. \"what? ") 50 .str()); 51 52 EXPECT_EQ(StringPiece16(u"where is the pie?"), 53 util::StringBuilder().append(u" where \t ") 54 .append(u" \nis the "" pie?") 55 .str()); 56 } 57 58 TEST(UtilTest, StringBuilderEscaping) { 59 EXPECT_EQ(StringPiece16(u"hey guys\n this \t is so\\ cool"), 60 util::StringBuilder().append(u" hey guys\\n ") 61 .append(u" this \\t is so\\\\ cool ") 62 .str()); 63 64 EXPECT_EQ(StringPiece16(u"@?#\\\'"), 65 util::StringBuilder().append(u"\\@\\?\\#\\\\\\'") 66 .str()); 67 } 68 69 TEST(UtilTest, StringBuilderMisplacedQuote) { 70 util::StringBuilder builder{}; 71 EXPECT_FALSE(builder.append(u"they're coming!")); 72 } 73 74 TEST(UtilTest, StringBuilderUnicodeCodes) { 75 EXPECT_EQ(StringPiece16(u"\u00AF\u0AF0 woah"), 76 util::StringBuilder().append(u"\\u00AF\\u0AF0 woah") 77 .str()); 78 79 EXPECT_FALSE(util::StringBuilder().append(u"\\u00 yo")); 80 } 81 82 TEST(UtilTest, TokenizeInput) { 83 auto tokenizer = util::tokenize(StringPiece16(u"this| is|the|end"), u'|'); 84 auto iter = tokenizer.begin(); 85 ASSERT_EQ(*iter, StringPiece16(u"this")); 86 ++iter; 87 ASSERT_EQ(*iter, StringPiece16(u" is")); 88 ++iter; 89 ASSERT_EQ(*iter, StringPiece16(u"the")); 90 ++iter; 91 ASSERT_EQ(*iter, StringPiece16(u"end")); 92 ++iter; 93 ASSERT_EQ(tokenizer.end(), iter); 94 } 95 96 TEST(UtilTest, IsJavaClassName) { 97 EXPECT_TRUE(util::isJavaClassName(u"android.test.Class")); 98 EXPECT_TRUE(util::isJavaClassName(u"android.test.Class$Inner")); 99 EXPECT_TRUE(util::isJavaClassName(u"android_test.test.Class")); 100 EXPECT_TRUE(util::isJavaClassName(u"_android_.test._Class_")); 101 EXPECT_FALSE(util::isJavaClassName(u"android.test.$Inner")); 102 EXPECT_FALSE(util::isJavaClassName(u"android.test.Inner$")); 103 EXPECT_FALSE(util::isJavaClassName(u".test.Class")); 104 EXPECT_FALSE(util::isJavaClassName(u"android")); 105 } 106 107 TEST(UtilTest, FullyQualifiedClassName) { 108 Maybe<std::u16string> res = util::getFullyQualifiedClassName(u"android", u"asdf"); 109 ASSERT_TRUE(res); 110 EXPECT_EQ(res.value(), u"android.asdf"); 111 112 res = util::getFullyQualifiedClassName(u"android", u".asdf"); 113 ASSERT_TRUE(res); 114 EXPECT_EQ(res.value(), u"android.asdf"); 115 116 res = util::getFullyQualifiedClassName(u"android", u".a.b"); 117 ASSERT_TRUE(res); 118 EXPECT_EQ(res.value(), u"android.a.b"); 119 120 res = util::getFullyQualifiedClassName(u"android", u"a.b"); 121 ASSERT_TRUE(res); 122 EXPECT_EQ(res.value(), u"a.b"); 123 124 res = util::getFullyQualifiedClassName(u"", u"a.b"); 125 ASSERT_TRUE(res); 126 EXPECT_EQ(res.value(), u"a.b"); 127 128 res = util::getFullyQualifiedClassName(u"", u""); 129 ASSERT_FALSE(res); 130 131 res = util::getFullyQualifiedClassName(u"android", u"./Apple"); 132 ASSERT_FALSE(res); 133 } 134 135 136 } // namespace aapt 137