blob: 7dbe7e02f16aa05cd02a1c96c2cde9a67666a7f0 [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) {
34 EXPECT_TRUE(util::stringEndsWith("hello.xml", ".xml"));
35}
36
37TEST(UtilTest, StringBuilderWhitespaceRemoval) {
38 EXPECT_EQ(StringPiece16(u"hey guys this is so cool"),
39 util::StringBuilder().append(u" hey guys ")
40 .append(u" this is so cool ")
41 .str());
42
43 EXPECT_EQ(StringPiece16(u" wow, so many \t spaces. what?"),
44 util::StringBuilder().append(u" \" wow, so many \t ")
45 .append(u"spaces. \"what? ")
46 .str());
47
48 EXPECT_EQ(StringPiece16(u"where is the pie?"),
49 util::StringBuilder().append(u" where \t ")
50 .append(u" \nis the "" pie?")
51 .str());
52}
53
54TEST(UtilTest, StringBuilderEscaping) {
55 EXPECT_EQ(StringPiece16(u"hey guys\n this \t is so\\ cool"),
56 util::StringBuilder().append(u" hey guys\\n ")
57 .append(u" this \\t is so\\\\ cool ")
58 .str());
59
60 EXPECT_EQ(StringPiece16(u"@?#\\\'"),
61 util::StringBuilder().append(u"\\@\\?\\#\\\\\\'")
62 .str());
63}
64
65TEST(UtilTest, StringBuilderMisplacedQuote) {
66 util::StringBuilder builder{};
67 EXPECT_FALSE(builder.append(u"they're coming!"));
68}
69
70TEST(UtilTest, StringBuilderUnicodeCodes) {
71 EXPECT_EQ(StringPiece16(u"\u00AF\u0AF0 woah"),
72 util::StringBuilder().append(u"\\u00AF\\u0AF0 woah")
73 .str());
74
75 EXPECT_FALSE(util::StringBuilder().append(u"\\u00 yo"));
76}
77
78TEST(UtilTest, TokenizeInput) {
79 auto tokenizer = util::tokenize(StringPiece16(u"this| is|the|end"), u'|');
80 auto iter = tokenizer.begin();
81 ASSERT_EQ(*iter, StringPiece16(u"this"));
82 ++iter;
83 ASSERT_EQ(*iter, StringPiece16(u" is"));
84 ++iter;
85 ASSERT_EQ(*iter, StringPiece16(u"the"));
86 ++iter;
87 ASSERT_EQ(*iter, StringPiece16(u"end"));
88 ++iter;
89 ASSERT_EQ(tokenizer.end(), iter);
90}
91
92} // namespace aapt