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 "depfile_parser.h" 00016 00017 #include <gtest/gtest.h> 00018 00019 struct DepfileParserTest : public testing::Test { 00020 bool Parse(const char* input, string* err); 00021 00022 DepfileParser parser_; 00023 string input_; 00024 }; 00025 00026 bool DepfileParserTest::Parse(const char* input, string* err) { 00027 input_ = input; 00028 return parser_.Parse(&input_, err); 00029 } 00030 00031 TEST_F(DepfileParserTest, Basic) { 00032 string err; 00033 EXPECT_TRUE(Parse( 00034 "build/ninja.o: ninja.cc ninja.h eval_env.h manifest_parser.h\n", 00035 &err)); 00036 ASSERT_EQ("", err); 00037 EXPECT_EQ("build/ninja.o", parser_.out_.AsString()); 00038 EXPECT_EQ(4u, parser_.ins_.size()); 00039 } 00040 00041 TEST_F(DepfileParserTest, EarlyNewlineAndWhitespace) { 00042 string err; 00043 EXPECT_TRUE(Parse( 00044 " \\\n" 00045 " out: in\n", 00046 &err)); 00047 ASSERT_EQ("", err); 00048 } 00049 00050 TEST_F(DepfileParserTest, Continuation) { 00051 string err; 00052 EXPECT_TRUE(Parse( 00053 "foo.o: \\\n" 00054 " bar.h baz.h\n", 00055 &err)); 00056 ASSERT_EQ("", err); 00057 EXPECT_EQ("foo.o", parser_.out_.AsString()); 00058 EXPECT_EQ(2u, parser_.ins_.size()); 00059 } 00060 00061 TEST_F(DepfileParserTest, BackSlashes) { 00062 string err; 00063 EXPECT_TRUE(Parse( 00064 "Project\\Dir\\Build\\Release8\\Foo\\Foo.res : \\\n" 00065 " Dir\\Library\\Foo.rc \\\n" 00066 " Dir\\Library\\Version\\Bar.h \\\n" 00067 " Dir\\Library\\Foo.ico \\\n" 00068 " Project\\Thing\\Bar.tlb \\\n", 00069 &err)); 00070 ASSERT_EQ("", err); 00071 EXPECT_EQ("Project\\Dir\\Build\\Release8\\Foo\\Foo.res", 00072 parser_.out_.AsString()); 00073 EXPECT_EQ(4u, parser_.ins_.size()); 00074 } 00075 00076 TEST_F(DepfileParserTest, Spaces) { 00077 string err; 00078 EXPECT_TRUE(Parse( 00079 "a\\ bc\\ def: a\\ b c d", 00080 &err)); 00081 ASSERT_EQ("", err); 00082 EXPECT_EQ("a bc def", 00083 parser_.out_.AsString()); 00084 ASSERT_EQ(3u, parser_.ins_.size()); 00085 EXPECT_EQ("a b", 00086 parser_.ins_[0].AsString()); 00087 EXPECT_EQ("c", 00088 parser_.ins_[1].AsString()); 00089 EXPECT_EQ("d", 00090 parser_.ins_[2].AsString()); 00091 } 00092 00093 TEST_F(DepfileParserTest, Escapes) { 00094 // Put backslashes before a variety of characters, see which ones make 00095 // it through. 00096 string err; 00097 EXPECT_TRUE(Parse( 00098 "\\!\\@\\#$$\\%\\^\\&\\\\", 00099 &err)); 00100 ASSERT_EQ("", err); 00101 EXPECT_EQ("\\!\\@#$\\%\\^\\&\\", 00102 parser_.out_.AsString()); 00103 ASSERT_EQ(0u, parser_.ins_.size()); 00104 } 00105 00106 TEST_F(DepfileParserTest, SpecialChars) { 00107 // See filenames like istreambuf.iterator_op!= in 00108 // https://github.com/google/libcxx/tree/master/test/iterators/stream.iterators/istreambuf.iterator/ 00109 string err; 00110 EXPECT_TRUE(Parse( 00111 "C:/Program\\ Files\\ (x86)/Microsoft\\ crtdefs.h: \n" 00112 " en@quot.header~ t+t-x!=1", 00113 &err)); 00114 ASSERT_EQ("", err); 00115 EXPECT_EQ("C:/Program Files (x86)/Microsoft crtdefs.h", 00116 parser_.out_.AsString()); 00117 ASSERT_EQ(2u, parser_.ins_.size()); 00118 EXPECT_EQ("en@quot.header~", 00119 parser_.ins_[0].AsString()); 00120 EXPECT_EQ("t+t-x!=1", 00121 parser_.ins_[1].AsString()); 00122 } 00123 00124 TEST_F(DepfileParserTest, UnifyMultipleOutputs) { 00125 // check that multiple duplicate targets are properly unified 00126 string err; 00127 EXPECT_TRUE(Parse("foo foo: x y z", &err)); 00128 ASSERT_EQ(parser_.out_.AsString(), "foo"); 00129 ASSERT_EQ(parser_.ins_.size(), 3u); 00130 EXPECT_EQ("x", parser_.ins_[0].AsString()); 00131 EXPECT_EQ("y", parser_.ins_[1].AsString()); 00132 EXPECT_EQ("z", parser_.ins_[2].AsString()); 00133 } 00134 00135 TEST_F(DepfileParserTest, RejectMultipleDifferentOutputs) { 00136 // check that multiple different outputs are rejected by the parser 00137 string err; 00138 EXPECT_FALSE(Parse("foo bar: x y z", &err)); 00139 }