1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "depfile_parser.h"
19 struct DepfileParserTest : public testing::Test {
20 bool Parse(const char* input, string* err);
22 DepfileParser parser_;
26 bool DepfileParserTest::Parse(const char* input, string* err) {
28 return parser_.Parse(&input_, err);
31 TEST_F(DepfileParserTest, Basic) {
34 "build/ninja.o: ninja.cc ninja.h eval_env.h manifest_parser.h\n",
37 EXPECT_EQ("build/ninja.o", parser_.out_.AsString());
38 EXPECT_EQ(4u, parser_.ins_.size());
41 TEST_F(DepfileParserTest, EarlyNewlineAndWhitespace) {
50 TEST_F(DepfileParserTest, Continuation) {
57 EXPECT_EQ("foo.o", parser_.out_.AsString());
58 EXPECT_EQ(2u, parser_.ins_.size());
61 TEST_F(DepfileParserTest, CarriageReturnContinuation) {
68 EXPECT_EQ("foo.o", parser_.out_.AsString());
69 EXPECT_EQ(2u, parser_.ins_.size());
72 TEST_F(DepfileParserTest, BackSlashes) {
75 "Project\\Dir\\Build\\Release8\\Foo\\Foo.res : \\\n"
76 " Dir\\Library\\Foo.rc \\\n"
77 " Dir\\Library\\Version\\Bar.h \\\n"
78 " Dir\\Library\\Foo.ico \\\n"
79 " Project\\Thing\\Bar.tlb \\\n",
82 EXPECT_EQ("Project\\Dir\\Build\\Release8\\Foo\\Foo.res",
83 parser_.out_.AsString());
84 EXPECT_EQ(4u, parser_.ins_.size());
87 TEST_F(DepfileParserTest, Spaces) {
90 "a\\ bc\\ def: a\\ b c d",
94 parser_.out_.AsString());
95 ASSERT_EQ(3u, parser_.ins_.size());
97 parser_.ins_[0].AsString());
99 parser_.ins_[1].AsString());
101 parser_.ins_[2].AsString());
104 TEST_F(DepfileParserTest, Escapes) {
105 // Put backslashes before a variety of characters, see which ones make
109 "\\!\\@\\#$$\\%\\^\\&\\\\:",
112 EXPECT_EQ("\\!\\@#$\\%\\^\\&\\",
113 parser_.out_.AsString());
114 ASSERT_EQ(0u, parser_.ins_.size());
117 TEST_F(DepfileParserTest, SpecialChars) {
118 // See filenames like istreambuf.iterator_op!= in
119 // https://github.com/google/libcxx/tree/master/test/iterators/stream.iterators/istreambuf.iterator/
122 "C:/Program\\ Files\\ (x86)/Microsoft\\ crtdefs.h: \n"
123 " en@quot.header~ t+t-x!=1 \n"
124 " openldap/slapd.d/cn=config/cn=schema/cn={0}core.ldif\n"
128 EXPECT_EQ("C:/Program Files (x86)/Microsoft crtdefs.h",
129 parser_.out_.AsString());
130 ASSERT_EQ(4u, parser_.ins_.size());
131 EXPECT_EQ("en@quot.header~",
132 parser_.ins_[0].AsString());
133 EXPECT_EQ("t+t-x!=1",
134 parser_.ins_[1].AsString());
135 EXPECT_EQ("openldap/slapd.d/cn=config/cn=schema/cn={0}core.ldif",
136 parser_.ins_[2].AsString());
137 EXPECT_EQ("Fu\303\244ball",
138 parser_.ins_[3].AsString());
141 TEST_F(DepfileParserTest, UnifyMultipleOutputs) {
142 // check that multiple duplicate targets are properly unified
144 EXPECT_TRUE(Parse("foo foo: x y z", &err));
145 ASSERT_EQ("foo", parser_.out_.AsString());
146 ASSERT_EQ(3u, parser_.ins_.size());
147 EXPECT_EQ("x", parser_.ins_[0].AsString());
148 EXPECT_EQ("y", parser_.ins_[1].AsString());
149 EXPECT_EQ("z", parser_.ins_[2].AsString());
152 TEST_F(DepfileParserTest, RejectMultipleDifferentOutputs) {
153 // check that multiple different outputs are rejected by the parser
155 EXPECT_FALSE(Parse("foo bar: x y z", &err));
156 ASSERT_EQ("depfile has multiple output paths", err);