blob: c16f6bb95d96e34dbf0c7e94ac0aeb64573eca32 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
23namespace aapt {
24
25TEST(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
33TEST(UtilTest, StringEndsWith) {
Adam Lesinski4d3a9872015-04-09 19:53:22 -070034 EXPECT_TRUE(util::stringEndsWith<char>("hello.xml", ".xml"));
35}
36
37TEST(UtilTest, StringStartsWith) {
38 EXPECT_TRUE(util::stringStartsWith<char>("hello.xml", "he"));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039}
40
41TEST(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
58TEST(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
69TEST(UtilTest, StringBuilderMisplacedQuote) {
70 util::StringBuilder builder{};
71 EXPECT_FALSE(builder.append(u"they're coming!"));
72}
73
74TEST(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
82TEST(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