Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 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 "test/Common.h" |
| 18 | #include "util/StringPiece.h" |
| 19 | #include "util/Util.h" |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace aapt { |
| 25 | |
| 26 | TEST(UtilTest, TrimOnlyWhitespace) { |
| 27 | const std::u16string full = u"\n "; |
| 28 | |
| 29 | StringPiece16 trimmed = util::trimWhitespace(full); |
| 30 | EXPECT_TRUE(trimmed.empty()); |
| 31 | EXPECT_EQ(0u, trimmed.size()); |
| 32 | } |
| 33 | |
| 34 | TEST(UtilTest, StringEndsWith) { |
| 35 | EXPECT_TRUE(util::stringEndsWith<char>("hello.xml", ".xml")); |
| 36 | } |
| 37 | |
| 38 | TEST(UtilTest, StringStartsWith) { |
| 39 | EXPECT_TRUE(util::stringStartsWith<char>("hello.xml", "he")); |
| 40 | } |
| 41 | |
| 42 | TEST(UtilTest, StringBuilderSplitEscapeSequence) { |
| 43 | EXPECT_EQ(StringPiece16(u"this is a new\nline."), |
| 44 | util::StringBuilder().append(u"this is a new\\") |
| 45 | .append(u"nline.") |
| 46 | .str()); |
| 47 | } |
| 48 | |
| 49 | TEST(UtilTest, StringBuilderWhitespaceRemoval) { |
| 50 | EXPECT_EQ(StringPiece16(u"hey guys this is so cool"), |
| 51 | util::StringBuilder().append(u" hey guys ") |
| 52 | .append(u" this is so cool ") |
| 53 | .str()); |
| 54 | |
| 55 | EXPECT_EQ(StringPiece16(u" wow, so many \t spaces. what?"), |
| 56 | util::StringBuilder().append(u" \" wow, so many \t ") |
| 57 | .append(u"spaces. \"what? ") |
| 58 | .str()); |
| 59 | |
| 60 | EXPECT_EQ(StringPiece16(u"where is the pie?"), |
| 61 | util::StringBuilder().append(u" where \t ") |
| 62 | .append(u" \nis the "" pie?") |
| 63 | .str()); |
| 64 | } |
| 65 | |
| 66 | TEST(UtilTest, StringBuilderEscaping) { |
| 67 | EXPECT_EQ(StringPiece16(u"hey guys\n this \t is so\\ cool"), |
| 68 | util::StringBuilder().append(u" hey guys\\n ") |
| 69 | .append(u" this \\t is so\\\\ cool ") |
| 70 | .str()); |
| 71 | |
| 72 | EXPECT_EQ(StringPiece16(u"@?#\\\'"), |
| 73 | util::StringBuilder().append(u"\\@\\?\\#\\\\\\'") |
| 74 | .str()); |
| 75 | } |
| 76 | |
| 77 | TEST(UtilTest, StringBuilderMisplacedQuote) { |
| 78 | util::StringBuilder builder{}; |
| 79 | EXPECT_FALSE(builder.append(u"they're coming!")); |
| 80 | } |
| 81 | |
| 82 | TEST(UtilTest, StringBuilderUnicodeCodes) { |
| 83 | EXPECT_EQ(StringPiece16(u"\u00AF\u0AF0 woah"), |
| 84 | util::StringBuilder().append(u"\\u00AF\\u0AF0 woah") |
| 85 | .str()); |
| 86 | |
| 87 | EXPECT_FALSE(util::StringBuilder().append(u"\\u00 yo")); |
| 88 | } |
| 89 | |
| 90 | TEST(UtilTest, TokenizeInput) { |
| 91 | auto tokenizer = util::tokenize(StringPiece16(u"this| is|the|end"), u'|'); |
| 92 | auto iter = tokenizer.begin(); |
| 93 | ASSERT_EQ(*iter, StringPiece16(u"this")); |
| 94 | ++iter; |
| 95 | ASSERT_EQ(*iter, StringPiece16(u" is")); |
| 96 | ++iter; |
| 97 | ASSERT_EQ(*iter, StringPiece16(u"the")); |
| 98 | ++iter; |
| 99 | ASSERT_EQ(*iter, StringPiece16(u"end")); |
| 100 | ++iter; |
| 101 | ASSERT_EQ(tokenizer.end(), iter); |
| 102 | } |
| 103 | |
Adam Lesinski | cf95a58 | 2015-11-16 15:37:30 -0800 | [diff] [blame] | 104 | TEST(UtilTest, TokenizeEmptyString) { |
| 105 | auto tokenizer = util::tokenize(StringPiece16(u""), u'|'); |
| 106 | auto iter = tokenizer.begin(); |
| 107 | ASSERT_NE(tokenizer.end(), iter); |
| 108 | ASSERT_EQ(StringPiece16(), *iter); |
| 109 | ++iter; |
| 110 | ASSERT_EQ(tokenizer.end(), iter); |
| 111 | } |
| 112 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 113 | TEST(UtilTest, TokenizeAtEnd) { |
| 114 | auto tokenizer = util::tokenize(StringPiece16(u"one."), u'.'); |
| 115 | auto iter = tokenizer.begin(); |
| 116 | ASSERT_EQ(*iter, StringPiece16(u"one")); |
| 117 | ++iter; |
| 118 | ASSERT_NE(iter, tokenizer.end()); |
| 119 | ASSERT_EQ(*iter, StringPiece16()); |
| 120 | } |
| 121 | |
| 122 | TEST(UtilTest, IsJavaClassName) { |
| 123 | EXPECT_TRUE(util::isJavaClassName(u"android.test.Class")); |
| 124 | EXPECT_TRUE(util::isJavaClassName(u"android.test.Class$Inner")); |
| 125 | EXPECT_TRUE(util::isJavaClassName(u"android_test.test.Class")); |
| 126 | EXPECT_TRUE(util::isJavaClassName(u"_android_.test._Class_")); |
| 127 | EXPECT_FALSE(util::isJavaClassName(u"android.test.$Inner")); |
| 128 | EXPECT_FALSE(util::isJavaClassName(u"android.test.Inner$")); |
| 129 | EXPECT_FALSE(util::isJavaClassName(u".test.Class")); |
| 130 | EXPECT_FALSE(util::isJavaClassName(u"android")); |
| 131 | } |
| 132 | |
| 133 | TEST(UtilTest, IsJavaPackageName) { |
| 134 | EXPECT_TRUE(util::isJavaPackageName(u"android")); |
| 135 | EXPECT_TRUE(util::isJavaPackageName(u"android.test")); |
| 136 | EXPECT_TRUE(util::isJavaPackageName(u"android.test_thing")); |
| 137 | EXPECT_FALSE(util::isJavaPackageName(u"_android")); |
| 138 | EXPECT_FALSE(util::isJavaPackageName(u"android_")); |
| 139 | EXPECT_FALSE(util::isJavaPackageName(u"android.")); |
| 140 | EXPECT_FALSE(util::isJavaPackageName(u".android")); |
| 141 | EXPECT_FALSE(util::isJavaPackageName(u"android._test")); |
| 142 | EXPECT_FALSE(util::isJavaPackageName(u"..")); |
| 143 | } |
| 144 | |
| 145 | TEST(UtilTest, FullyQualifiedClassName) { |
| 146 | Maybe<std::u16string> res = util::getFullyQualifiedClassName(u"android", u"asdf"); |
| 147 | AAPT_ASSERT_TRUE(res); |
| 148 | EXPECT_EQ(res.value(), u"android.asdf"); |
| 149 | |
| 150 | res = util::getFullyQualifiedClassName(u"android", u".asdf"); |
| 151 | AAPT_ASSERT_TRUE(res); |
| 152 | EXPECT_EQ(res.value(), u"android.asdf"); |
| 153 | |
| 154 | res = util::getFullyQualifiedClassName(u"android", u".a.b"); |
| 155 | AAPT_ASSERT_TRUE(res); |
| 156 | EXPECT_EQ(res.value(), u"android.a.b"); |
| 157 | |
| 158 | res = util::getFullyQualifiedClassName(u"android", u"a.b"); |
| 159 | AAPT_ASSERT_TRUE(res); |
| 160 | EXPECT_EQ(res.value(), u"a.b"); |
| 161 | |
| 162 | res = util::getFullyQualifiedClassName(u"", u"a.b"); |
| 163 | AAPT_ASSERT_TRUE(res); |
| 164 | EXPECT_EQ(res.value(), u"a.b"); |
| 165 | |
| 166 | res = util::getFullyQualifiedClassName(u"", u""); |
| 167 | AAPT_ASSERT_FALSE(res); |
| 168 | |
| 169 | res = util::getFullyQualifiedClassName(u"android", u"./Apple"); |
| 170 | AAPT_ASSERT_FALSE(res); |
| 171 | } |
| 172 | |
| 173 | TEST(UtilTest, ExtractResourcePathComponents) { |
| 174 | StringPiece16 prefix, entry, suffix; |
| 175 | ASSERT_TRUE(util::extractResFilePathParts(u"res/xml-sw600dp/entry.xml", &prefix, &entry, |
| 176 | &suffix)); |
| 177 | EXPECT_EQ(prefix, u"res/xml-sw600dp/"); |
| 178 | EXPECT_EQ(entry, u"entry"); |
| 179 | EXPECT_EQ(suffix, u".xml"); |
| 180 | |
| 181 | ASSERT_TRUE(util::extractResFilePathParts(u"res/xml-sw600dp/entry.9.png", &prefix, &entry, |
| 182 | &suffix)); |
| 183 | |
| 184 | EXPECT_EQ(prefix, u"res/xml-sw600dp/"); |
| 185 | EXPECT_EQ(entry, u"entry"); |
| 186 | EXPECT_EQ(suffix, u".9.png"); |
| 187 | |
| 188 | EXPECT_FALSE(util::extractResFilePathParts(u"AndroidManifest.xml", &prefix, &entry, &suffix)); |
| 189 | EXPECT_FALSE(util::extractResFilePathParts(u"res/.xml", &prefix, &entry, &suffix)); |
| 190 | |
| 191 | ASSERT_TRUE(util::extractResFilePathParts(u"res//.", &prefix, &entry, &suffix)); |
| 192 | EXPECT_EQ(prefix, u"res//"); |
| 193 | EXPECT_EQ(entry, u""); |
| 194 | EXPECT_EQ(suffix, u"."); |
| 195 | } |
| 196 | |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 197 | TEST(UtilTest, VerifyJavaStringFormat) { |
| 198 | ASSERT_TRUE(util::verifyJavaStringFormat(u"%09.34f")); |
| 199 | ASSERT_TRUE(util::verifyJavaStringFormat(u"%9$.34f %8$")); |
| 200 | ASSERT_TRUE(util::verifyJavaStringFormat(u"%% %%")); |
| 201 | ASSERT_FALSE(util::verifyJavaStringFormat(u"%09$f %f")); |
| 202 | ASSERT_FALSE(util::verifyJavaStringFormat(u"%09f %08s")); |
| 203 | } |
| 204 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 205 | } // namespace aapt |