Ninja
depfile_parser_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 "depfile_parser.h"
16 
17 #include <gtest/gtest.h>
18 
19 struct DepfileParserTest : public testing::Test {
20  bool Parse(const char* input, string* err);
21 
23  string input_;
24 };
25 
26 bool DepfileParserTest::Parse(const char* input, string* err) {
27  input_ = input;
28  return parser_.Parse(&input_, err);
29 }
30 
32  string err;
33  EXPECT_TRUE(Parse(
34 "build/ninja.o: ninja.cc ninja.h eval_env.h manifest_parser.h\n",
35  &err));
36  ASSERT_EQ("", err);
37  EXPECT_EQ("build/ninja.o", parser_.out_.AsString());
38  EXPECT_EQ(4u, parser_.ins_.size());
39 }
40 
41 TEST_F(DepfileParserTest, EarlyNewlineAndWhitespace) {
42  string err;
43  EXPECT_TRUE(Parse(
44 " \\\n"
45 " out: in\n",
46  &err));
47  ASSERT_EQ("", err);
48 }
49 
50 TEST_F(DepfileParserTest, Continuation) {
51  string err;
52  EXPECT_TRUE(Parse(
53 "foo.o: \\\n"
54 " bar.h baz.h\n",
55  &err));
56  ASSERT_EQ("", err);
57  EXPECT_EQ("foo.o", parser_.out_.AsString());
58  EXPECT_EQ(2u, parser_.ins_.size());
59 }
60 
61 TEST_F(DepfileParserTest, BackSlashes) {
62  string err;
63  EXPECT_TRUE(Parse(
64 "Project\\Dir\\Build\\Release8\\Foo\\Foo.res : \\\n"
65 " Dir\\Library\\Foo.rc \\\n"
66 " Dir\\Library\\Version\\Bar.h \\\n"
67 " Dir\\Library\\Foo.ico \\\n"
68 " Project\\Thing\\Bar.tlb \\\n",
69  &err));
70  ASSERT_EQ("", err);
71  EXPECT_EQ("Project\\Dir\\Build\\Release8\\Foo\\Foo.res",
72  parser_.out_.AsString());
73  EXPECT_EQ(4u, parser_.ins_.size());
74 }
75 
77  string err;
78  EXPECT_TRUE(Parse(
79 "a\\ bc\\ def: a\\ b c d",
80  &err));
81  ASSERT_EQ("", err);
82  EXPECT_EQ("a bc def",
83  parser_.out_.AsString());
84  ASSERT_EQ(3u, parser_.ins_.size());
85  EXPECT_EQ("a b",
86  parser_.ins_[0].AsString());
87  EXPECT_EQ("c",
88  parser_.ins_[1].AsString());
89  EXPECT_EQ("d",
90  parser_.ins_[2].AsString());
91 }
92 
94  // Put backslashes before a variety of characters, see which ones make
95  // it through.
96  string err;
97  EXPECT_TRUE(Parse(
98 "\\!\\@\\#$$\\%\\^\\&\\\\",
99  &err));
100  ASSERT_EQ("", err);
101  EXPECT_EQ("\\!\\@#$\\%\\^\\&\\",
102  parser_.out_.AsString());
103  ASSERT_EQ(0u, parser_.ins_.size());
104 }
105 
106 TEST_F(DepfileParserTest, SpecialChars) {
107  // See filenames like istreambuf.iterator_op!= in
108  // https://github.com/google/libcxx/tree/master/test/iterators/stream.iterators/istreambuf.iterator/
109  string err;
110  EXPECT_TRUE(Parse(
111 "C:/Program\\ Files\\ (x86)/Microsoft\\ crtdefs.h: \n"
112 " en@quot.header~ t+t-x!=1",
113  &err));
114  ASSERT_EQ("", err);
115  EXPECT_EQ("C:/Program Files (x86)/Microsoft crtdefs.h",
116  parser_.out_.AsString());
117  ASSERT_EQ(2u, parser_.ins_.size());
118  EXPECT_EQ("en@quot.header~",
119  parser_.ins_[0].AsString());
120  EXPECT_EQ("t+t-x!=1",
121  parser_.ins_[1].AsString());
122 }
123 
124 TEST_F(DepfileParserTest, UnifyMultipleOutputs) {
125  // check that multiple duplicate targets are properly unified
126  string err;
127  EXPECT_TRUE(Parse("foo foo: x y z", &err));
128  ASSERT_EQ(parser_.out_.AsString(), "foo");
129  ASSERT_EQ(parser_.ins_.size(), 3u);
130  EXPECT_EQ("x", parser_.ins_[0].AsString());
131  EXPECT_EQ("y", parser_.ins_[1].AsString());
132  EXPECT_EQ("z", parser_.ins_[2].AsString());
133 }
134 
135 TEST_F(DepfileParserTest, RejectMultipleDifferentOutputs) {
136  // check that multiple different outputs are rejected by the parser
137  string err;
138  EXPECT_FALSE(Parse("foo bar: x y z", &err));
139 }
Parser for the dependency information emitted by gcc's -M flags.
bool Parse(string *content, string *err)
Parse an input file.
bool Parse(const char *input, string *err)
TEST_F(DepfileParserTest, Basic)