OSDN Git Service

am 1f440cb0: am 895f3c27: am 6a47a861: Merge "Add getPrefixAids()."
[android-x86/frameworks-base.git] / tools / aapt2 / Util_test.cpp
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 } // namespace aapt