• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 Google Inc. All rights reserved
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // +build ignore
16 
17 #include "strutil.h"
18 
19 #include <assert.h>
20 
21 #include <string>
22 #include <vector>
23 
24 #include "string_piece.h"
25 #include "testutil.h"
26 
27 using namespace std;
28 
29 namespace {
30 
TestWordScanner()31 void TestWordScanner() {
32   vector<StringPiece> ss;
33   for (StringPiece tok : WordScanner("foo bar baz hogeeeeeeeeeeeeeeee")) {
34     ss.push_back(tok);
35   }
36   assert(ss.size() == 4LU);
37   ASSERT_EQ(ss[0], "foo");
38   ASSERT_EQ(ss[1], "bar");
39   ASSERT_EQ(ss[2], "baz");
40   ASSERT_EQ(ss[3], "hogeeeeeeeeeeeeeeee");
41 }
42 
TestHasPrefix()43 void TestHasPrefix() {
44   assert(HasPrefix("foo", "foo"));
45   assert(HasPrefix("foo", "fo"));
46   assert(HasPrefix("foo", ""));
47   assert(!HasPrefix("foo", "fooo"));
48 }
49 
TestHasSuffix()50 void TestHasSuffix() {
51   assert(HasSuffix("bar", "bar"));
52   assert(HasSuffix("bar", "ar"));
53   assert(HasSuffix("bar", ""));
54   assert(!HasSuffix("bar", "bbar"));
55 }
56 
SubstPattern(StringPiece str,StringPiece pat,StringPiece subst)57 string SubstPattern(StringPiece str, StringPiece pat, StringPiece subst) {
58   string r;
59   Pattern(pat).AppendSubst(str, subst, &r);
60   return r;
61 }
62 
TestSubstPattern()63 void TestSubstPattern() {
64   ASSERT_EQ(SubstPattern("x.c", "%.c", "%.o"), "x.o");
65   ASSERT_EQ(SubstPattern("c.x", "c.%", "o.%"), "o.x");
66   ASSERT_EQ(SubstPattern("x.c.c", "%.c", "%.o"), "x.c.o");
67   ASSERT_EQ(SubstPattern("x.x y.c", "%.c", "%.o"), "x.x y.o");
68   ASSERT_EQ(SubstPattern("x.%.c", "%.%.c", "OK"), "OK");
69   ASSERT_EQ(SubstPattern("x.c", "x.c", "OK"), "OK");
70   ASSERT_EQ(SubstPattern("x.c.c", "x.c", "XX"), "x.c.c");
71   ASSERT_EQ(SubstPattern("x.x.c", "x.c", "XX"), "x.x.c");
72 }
73 
TestNoLineBreak()74 void TestNoLineBreak() {
75   assert(NoLineBreak("a\nb") == "a\\nb");
76   assert(NoLineBreak("a\nb\nc") == "a\\nb\\nc");
77 }
78 
TestHasWord()79 void TestHasWord() {
80   assert(HasWord("foo bar baz", "bar"));
81   assert(HasWord("foo bar baz", "foo"));
82   assert(HasWord("foo bar baz", "baz"));
83   assert(!HasWord("foo bar baz", "oo"));
84   assert(!HasWord("foo bar baz", "ar"));
85   assert(!HasWord("foo bar baz", "ba"));
86   assert(!HasWord("foo bar baz", "az"));
87   assert(!HasWord("foo bar baz", "ba"));
88   assert(!HasWord("foo bar baz", "fo"));
89 }
90 
NormalizePath(string s)91 static string NormalizePath(string s) {
92   ::NormalizePath(&s);
93   return s;
94 }
95 
TestNormalizePath()96 void TestNormalizePath() {
97   ASSERT_EQ(NormalizePath(""), "");
98   ASSERT_EQ(NormalizePath("."), "");
99   ASSERT_EQ(NormalizePath("/"), "/");
100   ASSERT_EQ(NormalizePath("/tmp"), "/tmp");
101   ASSERT_EQ(NormalizePath("////tmp////"), "/tmp");
102   ASSERT_EQ(NormalizePath("a////b"), "a/b");
103   ASSERT_EQ(NormalizePath("a//.//b"), "a/b");
104   ASSERT_EQ(NormalizePath("a////b//../c/////"), "a/c");
105   ASSERT_EQ(NormalizePath("../foo"), "../foo");
106   ASSERT_EQ(NormalizePath("./foo"), "foo");
107   ASSERT_EQ(NormalizePath("x/y/..//../foo"), "foo");
108   ASSERT_EQ(NormalizePath("x/../../foo"), "../foo");
109   ASSERT_EQ(NormalizePath("/../foo"), "/foo");
110   ASSERT_EQ(NormalizePath("/../../foo"), "/foo");
111   ASSERT_EQ(NormalizePath("/a/../../foo"), "/foo");
112   ASSERT_EQ(NormalizePath("/a/b/.."), "/a");
113   ASSERT_EQ(NormalizePath("../../a/b"), "../../a/b");
114   ASSERT_EQ(NormalizePath("../../../a/b"), "../../../a/b");
115   ASSERT_EQ(NormalizePath(".././../a/b"), "../../a/b");
116   ASSERT_EQ(NormalizePath("./../../a/b"), "../../a/b");
117 }
118 
EscapeShell(string s)119 string EscapeShell(string s) {
120   ::EscapeShell(&s);
121   return s;
122 }
123 
TestEscapeShell()124 void TestEscapeShell() {
125   ASSERT_EQ(EscapeShell(""), "");
126   ASSERT_EQ(EscapeShell("foo"), "foo");
127   ASSERT_EQ(EscapeShell("foo$`\\baz\"bar"), "foo\\$\\`\\\\baz\\\"bar");
128   ASSERT_EQ(EscapeShell("$$"), "\\$$");
129   ASSERT_EQ(EscapeShell("$$$"), "\\$$\\$");
130   ASSERT_EQ(EscapeShell("\\\n"), "\\\\\n");
131 }
132 
TestFindEndOfLine()133 void TestFindEndOfLine() {
134   size_t lf_cnt = 0;
135   ASSERT_EQ(FindEndOfLine("foo", 0, &lf_cnt), 3);
136   char buf[10] = {'f', 'o', '\\', '\0', 'x', 'y'};
137   ASSERT_EQ(FindEndOfLine(StringPiece(buf, 6), 0, &lf_cnt), 3);
138   ASSERT_EQ(FindEndOfLine(StringPiece(buf, 2), 0, &lf_cnt), 2);
139 }
140 
141 }  // namespace
142 
main()143 int main() {
144   TestWordScanner();
145   TestHasPrefix();
146   TestHasSuffix();
147   TestSubstPattern();
148   TestNoLineBreak();
149   TestHasWord();
150   TestNormalizePath();
151   TestEscapeShell();
152   TestFindEndOfLine();
153   assert(!g_failed);
154 }
155