Ninja
util_test.cc
Go to the documentation of this file.
1 // Copyright 2011 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 #include "util.h"
16 
17 #include "test.h"
18 
19 TEST(CanonicalizePath, PathSamples) {
20  string path;
21  string err;
22 
23  EXPECT_FALSE(CanonicalizePath(&path, &err));
24  EXPECT_EQ("empty path", err);
25 
26  path = "foo.h"; err = "";
27  EXPECT_TRUE(CanonicalizePath(&path, &err));
28  EXPECT_EQ("foo.h", path);
29 
30  path = "./foo.h";
31  EXPECT_TRUE(CanonicalizePath(&path, &err));
32  EXPECT_EQ("foo.h", path);
33 
34  path = "./foo/./bar.h";
35  EXPECT_TRUE(CanonicalizePath(&path, &err));
36  EXPECT_EQ("foo/bar.h", path);
37 
38  path = "./x/foo/../bar.h";
39  EXPECT_TRUE(CanonicalizePath(&path, &err));
40  EXPECT_EQ("x/bar.h", path);
41 
42  path = "./x/foo/../../bar.h";
43  EXPECT_TRUE(CanonicalizePath(&path, &err));
44  EXPECT_EQ("bar.h", path);
45 
46  path = "foo//bar";
47  EXPECT_TRUE(CanonicalizePath(&path, &err));
48  EXPECT_EQ("foo/bar", path);
49 
50  path = "foo//.//..///bar";
51  EXPECT_TRUE(CanonicalizePath(&path, &err));
52  EXPECT_EQ("bar", path);
53 
54  path = "./x/../foo/../../bar.h";
55  EXPECT_TRUE(CanonicalizePath(&path, &err));
56  EXPECT_EQ("../bar.h", path);
57 
58  path = "foo/./.";
59  EXPECT_TRUE(CanonicalizePath(&path, &err));
60  EXPECT_EQ("foo", path);
61 
62  path = "foo/bar/..";
63  EXPECT_TRUE(CanonicalizePath(&path, &err));
64  EXPECT_EQ("foo", path);
65 
66  path = "foo/.hidden_bar";
67  EXPECT_TRUE(CanonicalizePath(&path, &err));
68  EXPECT_EQ("foo/.hidden_bar", path);
69 
70  path = "/foo";
71  EXPECT_TRUE(CanonicalizePath(&path, &err));
72  EXPECT_EQ("/foo", path);
73 
74  path = "//foo";
75  EXPECT_TRUE(CanonicalizePath(&path, &err));
76 #ifdef _WIN32
77  EXPECT_EQ("//foo", path);
78 #else
79  EXPECT_EQ("/foo", path);
80 #endif
81 
82  path = "/";
83  EXPECT_TRUE(CanonicalizePath(&path, &err));
84  EXPECT_EQ("", path);
85 }
86 
87 TEST(CanonicalizePath, EmptyResult) {
88  string path;
89  string err;
90 
91  EXPECT_FALSE(CanonicalizePath(&path, &err));
92  EXPECT_EQ("empty path", err);
93 
94  path = ".";
95  EXPECT_FALSE(CanonicalizePath(&path, &err));
96  EXPECT_EQ("path canonicalizes to the empty path", err);
97 
98  path = "./.";
99  EXPECT_FALSE(CanonicalizePath(&path, &err));
100  EXPECT_EQ("path canonicalizes to the empty path", err);
101 }
102 
104  string path, err;
105  path = "../../foo/bar.h";
106  EXPECT_TRUE(CanonicalizePath(&path, &err));
107  EXPECT_EQ("../../foo/bar.h", path);
108 
109  path = "test/../../foo/bar.h";
110  EXPECT_TRUE(CanonicalizePath(&path, &err));
111  EXPECT_EQ("../foo/bar.h", path);
112 }
113 
114 TEST(CanonicalizePath, AbsolutePath) {
115  string path = "/usr/include/stdio.h";
116  string err;
117  EXPECT_TRUE(CanonicalizePath(&path, &err));
118  EXPECT_EQ("/usr/include/stdio.h", path);
119 }
120 
121 TEST(CanonicalizePath, NotNullTerminated) {
122  string path;
123  string err;
124  size_t len;
125 
126  path = "foo/. bar/.";
127  len = strlen("foo/."); // Canonicalize only the part before the space.
128  EXPECT_TRUE(CanonicalizePath(&path[0], &len, &err));
129  EXPECT_EQ(strlen("foo"), len);
130  EXPECT_EQ("foo/. bar/.", string(path));
131 
132  path = "foo/../file bar/.";
133  len = strlen("foo/../file");
134  EXPECT_TRUE(CanonicalizePath(&path[0], &len, &err));
135  EXPECT_EQ(strlen("file"), len);
136  EXPECT_EQ("file ./file bar/.", string(path));
137 }
138 
139 TEST(StripAnsiEscapeCodes, EscapeAtEnd) {
140  string stripped = StripAnsiEscapeCodes("foo\33");
141  EXPECT_EQ("foo", stripped);
142 
143  stripped = StripAnsiEscapeCodes("foo\33[");
144  EXPECT_EQ("foo", stripped);
145 }
146 
147 TEST(StripAnsiEscapeCodes, StripColors) {
148  // An actual clang warning.
149  string input = "\33[1maffixmgr.cxx:286:15: \33[0m\33[0;1;35mwarning: "
150  "\33[0m\33[1musing the result... [-Wparentheses]\33[0m";
151  string stripped = StripAnsiEscapeCodes(input);
152  EXPECT_EQ("affixmgr.cxx:286:15: warning: using the result... [-Wparentheses]",
153  stripped);
154 }
155 
156 TEST(ElideMiddle, NothingToElide) {
157  string input = "Nothing to elide in this short string.";
158  EXPECT_EQ(input, ElideMiddle(input, 80));
159 }
160 
161 TEST(ElideMiddle, ElideInTheMiddle) {
162  string input = "01234567890123456789";
163  string elided = ElideMiddle(input, 10);
164  EXPECT_EQ("012...789", elided);
165 }
bool CanonicalizePath(string *path, string *err)
Canonicalize a path like "foo/../bar.h" into just "bar.h".
Definition: util.cc:87
TEST(CanonicalizePath, PathSamples)
Definition: util_test.cc:19
string StripAnsiEscapeCodes(const string &in)
Removes all Ansi escape codes (http://www.termsys.demon.co.uk/vtansi.htm).
Definition: util.cc:280
string ElideMiddle(const string &str, size_t width)
Elide the given string str with '...' in the middle if the length exceeds width.
Definition: util.cc:351