2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
20 #include "StringPiece.h"
25 TEST(UtilTest, TrimOnlyWhitespace) {
26 const std::u16string full = u"\n ";
28 StringPiece16 trimmed = util::trimWhitespace(full);
29 EXPECT_TRUE(trimmed.empty());
30 EXPECT_EQ(0u, trimmed.size());
33 TEST(UtilTest, StringEndsWith) {
34 EXPECT_TRUE(util::stringEndsWith<char>("hello.xml", ".xml"));
37 TEST(UtilTest, StringStartsWith) {
38 EXPECT_TRUE(util::stringStartsWith<char>("hello.xml", "he"));
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 ")
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? ")
52 EXPECT_EQ(StringPiece16(u"where is the pie?"),
53 util::StringBuilder().append(u" where \t ")
54 .append(u" \nis the "" pie?")
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 ")
64 EXPECT_EQ(StringPiece16(u"@?#\\\'"),
65 util::StringBuilder().append(u"\\@\\?\\#\\\\\\'")
69 TEST(UtilTest, StringBuilderMisplacedQuote) {
70 util::StringBuilder builder{};
71 EXPECT_FALSE(builder.append(u"they're coming!"));
74 TEST(UtilTest, StringBuilderUnicodeCodes) {
75 EXPECT_EQ(StringPiece16(u"\u00AF\u0AF0 woah"),
76 util::StringBuilder().append(u"\\u00AF\\u0AF0 woah")
79 EXPECT_FALSE(util::StringBuilder().append(u"\\u00 yo"));
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"));
87 ASSERT_EQ(*iter, StringPiece16(u" is"));
89 ASSERT_EQ(*iter, StringPiece16(u"the"));
91 ASSERT_EQ(*iter, StringPiece16(u"end"));
93 ASSERT_EQ(tokenizer.end(), iter);