Ninja
|
00001 // Copyright 2011 Google Inc. All Rights Reserved. 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 // http://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 #include "util.h" 00016 00017 #include "test.h" 00018 00019 TEST(CanonicalizePath, PathSamples) { 00020 string path; 00021 string err; 00022 00023 EXPECT_FALSE(CanonicalizePath(&path, &err)); 00024 EXPECT_EQ("empty path", err); 00025 00026 path = "foo.h"; err = ""; 00027 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00028 EXPECT_EQ("foo.h", path); 00029 00030 path = "./foo.h"; 00031 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00032 EXPECT_EQ("foo.h", path); 00033 00034 path = "./foo/./bar.h"; 00035 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00036 EXPECT_EQ("foo/bar.h", path); 00037 00038 path = "./x/foo/../bar.h"; 00039 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00040 EXPECT_EQ("x/bar.h", path); 00041 00042 path = "./x/foo/../../bar.h"; 00043 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00044 EXPECT_EQ("bar.h", path); 00045 00046 path = "foo//bar"; 00047 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00048 EXPECT_EQ("foo/bar", path); 00049 00050 path = "foo//.//..///bar"; 00051 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00052 EXPECT_EQ("bar", path); 00053 00054 path = "./x/../foo/../../bar.h"; 00055 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00056 EXPECT_EQ("../bar.h", path); 00057 00058 path = "foo/./."; 00059 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00060 EXPECT_EQ("foo", path); 00061 00062 path = "foo/bar/.."; 00063 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00064 EXPECT_EQ("foo", path); 00065 00066 path = "foo/.hidden_bar"; 00067 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00068 EXPECT_EQ("foo/.hidden_bar", path); 00069 00070 path = "/foo"; 00071 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00072 EXPECT_EQ("/foo", path); 00073 00074 path = "//foo"; 00075 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00076 #ifdef _WIN32 00077 EXPECT_EQ("//foo", path); 00078 #else 00079 EXPECT_EQ("/foo", path); 00080 #endif 00081 00082 path = "/"; 00083 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00084 EXPECT_EQ("", path); 00085 } 00086 00087 TEST(CanonicalizePath, EmptyResult) { 00088 string path; 00089 string err; 00090 00091 EXPECT_FALSE(CanonicalizePath(&path, &err)); 00092 EXPECT_EQ("empty path", err); 00093 00094 path = "."; 00095 EXPECT_FALSE(CanonicalizePath(&path, &err)); 00096 EXPECT_EQ("path canonicalizes to the empty path", err); 00097 00098 path = "./."; 00099 EXPECT_FALSE(CanonicalizePath(&path, &err)); 00100 EXPECT_EQ("path canonicalizes to the empty path", err); 00101 } 00102 00103 TEST(CanonicalizePath, UpDir) { 00104 string path, err; 00105 path = "../../foo/bar.h"; 00106 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00107 EXPECT_EQ("../../foo/bar.h", path); 00108 00109 path = "test/../../foo/bar.h"; 00110 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00111 EXPECT_EQ("../foo/bar.h", path); 00112 } 00113 00114 TEST(CanonicalizePath, AbsolutePath) { 00115 string path = "/usr/include/stdio.h"; 00116 string err; 00117 EXPECT_TRUE(CanonicalizePath(&path, &err)); 00118 EXPECT_EQ("/usr/include/stdio.h", path); 00119 } 00120 00121 TEST(CanonicalizePath, NotNullTerminated) { 00122 string path; 00123 string err; 00124 size_t len; 00125 00126 path = "foo/. bar/."; 00127 len = strlen("foo/."); // Canonicalize only the part before the space. 00128 EXPECT_TRUE(CanonicalizePath(&path[0], &len, &err)); 00129 EXPECT_EQ(strlen("foo"), len); 00130 EXPECT_EQ("foo/. bar/.", string(path)); 00131 00132 path = "foo/../file bar/."; 00133 len = strlen("foo/../file"); 00134 EXPECT_TRUE(CanonicalizePath(&path[0], &len, &err)); 00135 EXPECT_EQ(strlen("file"), len); 00136 EXPECT_EQ("file ./file bar/.", string(path)); 00137 } 00138 00139 TEST(StripAnsiEscapeCodes, EscapeAtEnd) { 00140 string stripped = StripAnsiEscapeCodes("foo\33"); 00141 EXPECT_EQ("foo", stripped); 00142 00143 stripped = StripAnsiEscapeCodes("foo\33["); 00144 EXPECT_EQ("foo", stripped); 00145 } 00146 00147 TEST(StripAnsiEscapeCodes, StripColors) { 00148 // An actual clang warning. 00149 string input = "\33[1maffixmgr.cxx:286:15: \33[0m\33[0;1;35mwarning: " 00150 "\33[0m\33[1musing the result... [-Wparentheses]\33[0m"; 00151 string stripped = StripAnsiEscapeCodes(input); 00152 EXPECT_EQ("affixmgr.cxx:286:15: warning: using the result... [-Wparentheses]", 00153 stripped); 00154 } 00155 00156 TEST(ElideMiddle, NothingToElide) { 00157 string input = "Nothing to elide in this short string."; 00158 EXPECT_EQ(input, ElideMiddle(input, 80)); 00159 } 00160 00161 TEST(ElideMiddle, ElideInTheMiddle) { 00162 string input = "01234567890123456789"; 00163 string elided = ElideMiddle(input, 10); 00164 EXPECT_EQ("012...789", elided); 00165 }