forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_include_iterator_unittest.cc
159 lines (127 loc) · 4.75 KB
/
c_include_iterator_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/c_include_iterator.h"
#include "tools/gn/input_file.h"
#include "tools/gn/location.h"
namespace {
bool RangeIs(const LocationRange& range,
int line, int begin_char, int end_char) {
return range.begin().line_number() == line &&
range.end().line_number() == line &&
range.begin().column_number() == begin_char &&
range.end().column_number() == end_char;
}
} // namespace
TEST(CIncludeIterator, Basic) {
std::string buffer;
buffer.append("// Some comment\n");
buffer.append("\n");
buffer.append("#include \"foo/bar.h\"\n");
buffer.append("\n");
buffer.append("#include <stdio.h>\n");
buffer.append("\n");
buffer.append(" #include \"foo/baz.h\"\n"); // Leading whitespace
buffer.append("#include \"la/deda.h\"\n");
// Line annotated with "// nogncheck"
buffer.append("#include \"should_be_skipped.h\" // nogncheck\n");
buffer.append("#import \"weird_mac_import.h\"\n");
buffer.append("\n");
buffer.append("void SomeCode() {\n");
InputFile file(SourceFile("//foo.cc"));
file.SetContents(buffer);
CIncludeIterator iter(&file);
base::StringPiece contents;
LocationRange range;
EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
EXPECT_EQ("foo/bar.h", contents);
EXPECT_TRUE(RangeIs(range, 3, 11, 20)) << range.begin().Describe(true);
EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
EXPECT_EQ("foo/baz.h", contents);
EXPECT_TRUE(RangeIs(range, 7, 12, 21)) << range.begin().Describe(true);
EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
EXPECT_EQ("la/deda.h", contents);
EXPECT_TRUE(RangeIs(range, 8, 11, 20)) << range.begin().Describe(true);
// The line annotated with "nogncheck" should be skipped.
EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
EXPECT_EQ("weird_mac_import.h", contents);
EXPECT_TRUE(RangeIs(range, 10, 10, 28)) << range.begin().Describe(true);
EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range));
}
// Tests that we don't search for includes indefinitely.
TEST(CIncludeIterator, GiveUp) {
std::string buffer;
for (size_t i = 0; i < 1000; i++)
buffer.append("x\n");
buffer.append("#include \"foo/bar.h\"\n");
InputFile file(SourceFile("//foo.cc"));
file.SetContents(buffer);
base::StringPiece contents;
LocationRange range;
CIncludeIterator iter(&file);
EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range));
EXPECT_TRUE(contents.empty());
}
// Don't count blank lines, comments, and preprocessor when giving up.
TEST(CIncludeIterator, DontGiveUp) {
std::string buffer;
for (size_t i = 0; i < 1000; i++)
buffer.push_back('\n');
for (size_t i = 0; i < 1000; i++)
buffer.append("// comment\n");
for (size_t i = 0; i < 1000; i++)
buffer.append("#preproc\n");
buffer.append("#include \"foo/bar.h\"\n");
InputFile file(SourceFile("//foo.cc"));
file.SetContents(buffer);
base::StringPiece contents;
LocationRange range;
CIncludeIterator iter(&file);
EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
EXPECT_EQ("foo/bar.h", contents);
}
// Tests that we'll tolerate some small numbers of non-includes interspersed
// with real includes.
TEST(CIncludeIterator, TolerateNonIncludes) {
const size_t kSkip = CIncludeIterator::kMaxNonIncludeLines - 2;
const size_t kGroupCount = 100;
std::string include("foo/bar.h");
// Allow a series of includes with blanks in between.
std::string buffer;
for (size_t group = 0; group < kGroupCount; group++) {
for (size_t i = 0; i < kSkip; i++)
buffer.append("foo\n");
buffer.append("#include \"" + include + "\"\n");
}
InputFile file(SourceFile("//foo.cc"));
file.SetContents(buffer);
base::StringPiece contents;
LocationRange range;
CIncludeIterator iter(&file);
for (size_t group = 0; group < kGroupCount; group++) {
EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
EXPECT_EQ(include, contents.as_string());
}
EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range));
}
// Tests that comments of the form
// /*
// *
// */
// are not counted toward the non-include line count.
TEST(CIncludeIterator, CStyleComments) {
std::string buffer("/*");
for (size_t i = 0; i < 1000; i++)
buffer.append(" *\n");
buffer.append(" */\n\n");
buffer.append("#include \"foo/bar.h\"\n");
InputFile file(SourceFile("//foo.cc"));
file.SetContents(buffer);
base::StringPiece contents;
LocationRange range;
CIncludeIterator iter(&file);
EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
EXPECT_EQ("foo/bar.h", contents);
}