strip_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 // This file contains functions that remove a defined part from the string,
00016 // i.e., strip the string.
00017 
00018 #include "absl/strings/strip.h"
00019 
00020 #include <cassert>
00021 #include <cstdio>
00022 #include <cstring>
00023 
00024 #include "gmock/gmock.h"
00025 #include "gtest/gtest.h"
00026 #include "absl/strings/string_view.h"
00027 
00028 namespace {
00029 
00030 TEST(Strip, ConsumePrefixOneChar) {
00031   absl::string_view input("abc");
00032   EXPECT_TRUE(absl::ConsumePrefix(&input, "a"));
00033   EXPECT_EQ(input, "bc");
00034 
00035   EXPECT_FALSE(absl::ConsumePrefix(&input, "x"));
00036   EXPECT_EQ(input, "bc");
00037 
00038   EXPECT_TRUE(absl::ConsumePrefix(&input, "b"));
00039   EXPECT_EQ(input, "c");
00040 
00041   EXPECT_TRUE(absl::ConsumePrefix(&input, "c"));
00042   EXPECT_EQ(input, "");
00043 
00044   EXPECT_FALSE(absl::ConsumePrefix(&input, "a"));
00045   EXPECT_EQ(input, "");
00046 }
00047 
00048 TEST(Strip, ConsumePrefix) {
00049   absl::string_view input("abcdef");
00050   EXPECT_FALSE(absl::ConsumePrefix(&input, "abcdefg"));
00051   EXPECT_EQ(input, "abcdef");
00052 
00053   EXPECT_FALSE(absl::ConsumePrefix(&input, "abce"));
00054   EXPECT_EQ(input, "abcdef");
00055 
00056   EXPECT_TRUE(absl::ConsumePrefix(&input, ""));
00057   EXPECT_EQ(input, "abcdef");
00058 
00059   EXPECT_FALSE(absl::ConsumePrefix(&input, "abcdeg"));
00060   EXPECT_EQ(input, "abcdef");
00061 
00062   EXPECT_TRUE(absl::ConsumePrefix(&input, "abcdef"));
00063   EXPECT_EQ(input, "");
00064 
00065   input = "abcdef";
00066   EXPECT_TRUE(absl::ConsumePrefix(&input, "abcde"));
00067   EXPECT_EQ(input, "f");
00068 }
00069 
00070 TEST(Strip, ConsumeSuffix) {
00071   absl::string_view input("abcdef");
00072   EXPECT_FALSE(absl::ConsumeSuffix(&input, "abcdefg"));
00073   EXPECT_EQ(input, "abcdef");
00074 
00075   EXPECT_TRUE(absl::ConsumeSuffix(&input, ""));
00076   EXPECT_EQ(input, "abcdef");
00077 
00078   EXPECT_TRUE(absl::ConsumeSuffix(&input, "def"));
00079   EXPECT_EQ(input, "abc");
00080 
00081   input = "abcdef";
00082   EXPECT_FALSE(absl::ConsumeSuffix(&input, "abcdeg"));
00083   EXPECT_EQ(input, "abcdef");
00084 
00085   EXPECT_TRUE(absl::ConsumeSuffix(&input, "f"));
00086   EXPECT_EQ(input, "abcde");
00087 
00088   EXPECT_TRUE(absl::ConsumeSuffix(&input, "abcde"));
00089   EXPECT_EQ(input, "");
00090 }
00091 
00092 TEST(Strip, StripPrefix) {
00093   const absl::string_view null_str;
00094 
00095   EXPECT_EQ(absl::StripPrefix("foobar", "foo"), "bar");
00096   EXPECT_EQ(absl::StripPrefix("foobar", ""), "foobar");
00097   EXPECT_EQ(absl::StripPrefix("foobar", null_str), "foobar");
00098   EXPECT_EQ(absl::StripPrefix("foobar", "foobar"), "");
00099   EXPECT_EQ(absl::StripPrefix("foobar", "bar"), "foobar");
00100   EXPECT_EQ(absl::StripPrefix("foobar", "foobarr"), "foobar");
00101   EXPECT_EQ(absl::StripPrefix("", ""), "");
00102 }
00103 
00104 TEST(Strip, StripSuffix) {
00105   const absl::string_view null_str;
00106 
00107   EXPECT_EQ(absl::StripSuffix("foobar", "bar"), "foo");
00108   EXPECT_EQ(absl::StripSuffix("foobar", ""), "foobar");
00109   EXPECT_EQ(absl::StripSuffix("foobar", null_str), "foobar");
00110   EXPECT_EQ(absl::StripSuffix("foobar", "foobar"), "");
00111   EXPECT_EQ(absl::StripSuffix("foobar", "foo"), "foobar");
00112   EXPECT_EQ(absl::StripSuffix("foobar", "ffoobar"), "foobar");
00113   EXPECT_EQ(absl::StripSuffix("", ""), "");
00114 }
00115 
00116 TEST(Strip, RemoveExtraAsciiWhitespace) {
00117   const char* inputs[] = {
00118       "No extra space",
00119       "  Leading whitespace",
00120       "Trailing whitespace  ",
00121       "  Leading and trailing  ",
00122       " Whitespace \t  in\v   middle  ",
00123       "'Eeeeep!  \n Newlines!\n",
00124       "nospaces",
00125   };
00126   const char* outputs[] = {
00127       "No extra space",
00128       "Leading whitespace",
00129       "Trailing whitespace",
00130       "Leading and trailing",
00131       "Whitespace in middle",
00132       "'Eeeeep! Newlines!",
00133       "nospaces",
00134   };
00135   int NUM_TESTS = 7;
00136 
00137   for (int i = 0; i < NUM_TESTS; i++) {
00138     std::string s(inputs[i]);
00139     absl::RemoveExtraAsciiWhitespace(&s);
00140     EXPECT_STREQ(outputs[i], s.c_str());
00141   }
00142 
00143   // Test that absl::RemoveExtraAsciiWhitespace returns immediately for empty
00144   // strings (It was adding the \0 character to the C++ std::string, which broke
00145   // tests involving empty())
00146   std::string zero_string = "";
00147   assert(zero_string.empty());
00148   absl::RemoveExtraAsciiWhitespace(&zero_string);
00149   EXPECT_EQ(zero_string.size(), 0);
00150   EXPECT_TRUE(zero_string.empty());
00151 }
00152 
00153 TEST(Strip, StripTrailingAsciiWhitespace) {
00154   std::string test = "foo  ";
00155   absl::StripTrailingAsciiWhitespace(&test);
00156   EXPECT_EQ(test, "foo");
00157 
00158   test = "   ";
00159   absl::StripTrailingAsciiWhitespace(&test);
00160   EXPECT_EQ(test, "");
00161 
00162   test = "";
00163   absl::StripTrailingAsciiWhitespace(&test);
00164   EXPECT_EQ(test, "");
00165 
00166   test = " abc\t";
00167   absl::StripTrailingAsciiWhitespace(&test);
00168   EXPECT_EQ(test, " abc");
00169 }
00170 
00171 TEST(String, StripLeadingAsciiWhitespace) {
00172   absl::string_view orig = "\t  \n\f\r\n\vfoo";
00173   EXPECT_EQ("foo", absl::StripLeadingAsciiWhitespace(orig));
00174   orig = "\t  \n\f\r\v\n\t  \n\f\r\v\n";
00175   EXPECT_EQ(absl::string_view(), absl::StripLeadingAsciiWhitespace(orig));
00176 }
00177 
00178 TEST(Strip, StripAsciiWhitespace) {
00179   std::string test2 = "\t  \f\r\n\vfoo \t\f\r\v\n";
00180   absl::StripAsciiWhitespace(&test2);
00181   EXPECT_EQ(test2, "foo");
00182   std::string test3 = "bar";
00183   absl::StripAsciiWhitespace(&test3);
00184   EXPECT_EQ(test3, "bar");
00185   std::string test4 = "\t  \f\r\n\vfoo";
00186   absl::StripAsciiWhitespace(&test4);
00187   EXPECT_EQ(test4, "foo");
00188   std::string test5 = "foo \t\f\r\v\n";
00189   absl::StripAsciiWhitespace(&test5);
00190   EXPECT_EQ(test5, "foo");
00191   absl::string_view test6("\t  \f\r\n\vfoo \t\f\r\v\n");
00192   test6 = absl::StripAsciiWhitespace(test6);
00193   EXPECT_EQ(test6, "foo");
00194   test6 = absl::StripAsciiWhitespace(test6);
00195   EXPECT_EQ(test6, "foo");  // already stripped
00196 }
00197 
00198 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15