Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: tools/gn/c_include_iterator_unittest.cc

Issue 231813002: Improve GN public header file checking (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/gn/c_include_iterator.cc ('k') | tools/gn/header_checker.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/c_include_iterator.h" 6 #include "tools/gn/c_include_iterator.h"
7 #include "tools/gn/input_file.h"
8 #include "tools/gn/location.h"
9
10 namespace {
11
12 bool RangeIs(const LocationRange& range,
13 int line, int begin_char, int end_char) {
14 return range.begin().line_number() == line &&
15 range.end().line_number() == line &&
16 range.begin().char_offset() == begin_char &&
17 range.end().char_offset() == end_char;
18 }
19
20 } // namespace
7 21
8 TEST(CIncludeIterator, Basic) { 22 TEST(CIncludeIterator, Basic) {
9 std::string buffer; 23 std::string buffer;
10 buffer.append("// Some comment\n"); 24 buffer.append("// Some comment\n");
11 buffer.append("\n"); 25 buffer.append("\n");
12 buffer.append("#include \"foo/bar.h\"\n"); 26 buffer.append("#include \"foo/bar.h\"\n");
13 buffer.append("\n"); 27 buffer.append("\n");
14 buffer.append("#include <stdio.h>\n"); 28 buffer.append("#include <stdio.h>\n");
15 buffer.append("\n"); 29 buffer.append("\n");
16 buffer.append(" #include \"foo/baz.h\"\n"); // Leading whitespace 30 buffer.append(" #include \"foo/baz.h\"\n"); // Leading whitespace
17 buffer.append("#include \"la/deda.h\"\n"); 31 buffer.append("#include \"la/deda.h\"\n");
18 buffer.append("#import \"weird_mac_import.h\"\n"); 32 buffer.append("#import \"weird_mac_import.h\"\n");
19 buffer.append("\n"); 33 buffer.append("\n");
20 buffer.append("void SomeCode() {\n"); 34 buffer.append("void SomeCode() {\n");
21 35
22 CIncludeIterator iter(buffer); 36 InputFile file(SourceFile("//foo.cc"));
37 file.SetContents(buffer);
38
39 CIncludeIterator iter(&file);
23 40
24 base::StringPiece contents; 41 base::StringPiece contents;
25 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); 42 LocationRange range;
43 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
26 EXPECT_EQ("foo/bar.h", contents); 44 EXPECT_EQ("foo/bar.h", contents);
27 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); 45 EXPECT_TRUE(RangeIs(range, 3, 11, 20)) << range.begin().Describe(true);
46
47 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
28 EXPECT_EQ("foo/baz.h", contents); 48 EXPECT_EQ("foo/baz.h", contents);
29 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); 49 EXPECT_TRUE(RangeIs(range, 7, 12, 21)) << range.begin().Describe(true);
50
51 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
30 EXPECT_EQ("la/deda.h", contents); 52 EXPECT_EQ("la/deda.h", contents);
31 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); 53 EXPECT_TRUE(RangeIs(range, 8, 11, 20)) << range.begin().Describe(true);
54
55 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
32 EXPECT_EQ("weird_mac_import.h", contents); 56 EXPECT_EQ("weird_mac_import.h", contents);
33 EXPECT_FALSE(iter.GetNextIncludeString(&contents)); 57 EXPECT_TRUE(RangeIs(range, 9, 10, 28)) << range.begin().Describe(true);
58
59 EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range));
34 } 60 }
35 61
36 // Tests that we don't search for includes indefinitely. 62 // Tests that we don't search for includes indefinitely.
37 TEST(CIncludeIterator, GiveUp) { 63 TEST(CIncludeIterator, GiveUp) {
38 std::string buffer; 64 std::string buffer;
39 for (size_t i = 0; i < 1000; i++) 65 for (size_t i = 0; i < 1000; i++)
40 buffer.append("x\n"); 66 buffer.append("x\n");
41 buffer.append("#include \"foo/bar.h\"\n"); 67 buffer.append("#include \"foo/bar.h\"\n");
42 68
69 InputFile file(SourceFile("//foo.cc"));
70 file.SetContents(buffer);
71
43 base::StringPiece contents; 72 base::StringPiece contents;
73 LocationRange range;
44 74
45 CIncludeIterator iter(buffer); 75 CIncludeIterator iter(&file);
46 EXPECT_FALSE(iter.GetNextIncludeString(&contents)); 76 EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range));
47 EXPECT_TRUE(contents.empty()); 77 EXPECT_TRUE(contents.empty());
48 } 78 }
49 79
50 // Don't count blank lines, comments, and preprocessor when giving up. 80 // Don't count blank lines, comments, and preprocessor when giving up.
51 TEST(CIncludeIterator, DontGiveUp) { 81 TEST(CIncludeIterator, DontGiveUp) {
52 std::string buffer; 82 std::string buffer;
53 for (size_t i = 0; i < 1000; i++) 83 for (size_t i = 0; i < 1000; i++)
54 buffer.push_back('\n'); 84 buffer.push_back('\n');
55 for (size_t i = 0; i < 1000; i++) 85 for (size_t i = 0; i < 1000; i++)
56 buffer.append("// comment\n"); 86 buffer.append("// comment\n");
57 for (size_t i = 0; i < 1000; i++) 87 for (size_t i = 0; i < 1000; i++)
58 buffer.append("#preproc\n"); 88 buffer.append("#preproc\n");
59 buffer.append("#include \"foo/bar.h\"\n"); 89 buffer.append("#include \"foo/bar.h\"\n");
60 90
91 InputFile file(SourceFile("//foo.cc"));
92 file.SetContents(buffer);
93
61 base::StringPiece contents; 94 base::StringPiece contents;
95 LocationRange range;
62 96
63 CIncludeIterator iter(buffer); 97 CIncludeIterator iter(&file);
64 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); 98 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
65 EXPECT_EQ("foo/bar.h", contents); 99 EXPECT_EQ("foo/bar.h", contents);
66 } 100 }
67 101
68 // Tests that we'll tolerate some small numbers of non-includes interspersed 102 // Tests that we'll tolerate some small numbers of non-includes interspersed
69 // with real includes. 103 // with real includes.
70 TEST(CIncludeIterator, TolerateNonIncludes) { 104 TEST(CIncludeIterator, TolerateNonIncludes) {
71 const size_t kSkip = CIncludeIterator::kMaxNonIncludeLines - 2; 105 const size_t kSkip = CIncludeIterator::kMaxNonIncludeLines - 2;
72 const size_t kGroupCount = 100; 106 const size_t kGroupCount = 100;
73 107
74 std::string include("foo/bar.h"); 108 std::string include("foo/bar.h");
75 109
76 // Allow a series of includes with blanks in between. 110 // Allow a series of includes with blanks in between.
77 std::string buffer; 111 std::string buffer;
78 for (size_t group = 0; group < kGroupCount; group++) { 112 for (size_t group = 0; group < kGroupCount; group++) {
79 for (size_t i = 0; i < kSkip; i++) 113 for (size_t i = 0; i < kSkip; i++)
80 buffer.append("foo\n"); 114 buffer.append("foo\n");
81 buffer.append("#include \"" + include + "\"\n"); 115 buffer.append("#include \"" + include + "\"\n");
82 } 116 }
83 117
118 InputFile file(SourceFile("//foo.cc"));
119 file.SetContents(buffer);
120
84 base::StringPiece contents; 121 base::StringPiece contents;
122 LocationRange range;
85 123
86 CIncludeIterator iter(buffer); 124 CIncludeIterator iter(&file);
87 for (size_t group = 0; group < kGroupCount; group++) { 125 for (size_t group = 0; group < kGroupCount; group++) {
88 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); 126 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range));
89 EXPECT_EQ(include, contents.as_string()); 127 EXPECT_EQ(include, contents.as_string());
90 } 128 }
91 EXPECT_FALSE(iter.GetNextIncludeString(&contents)); 129 EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range));
92 } 130 }
OLDNEW
« no previous file with comments | « tools/gn/c_include_iterator.cc ('k') | tools/gn/header_checker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698