| OLD | NEW |
| 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 | |
| 21 | 7 |
| 22 TEST(CIncludeIterator, Basic) { | 8 TEST(CIncludeIterator, Basic) { |
| 23 std::string buffer; | 9 std::string buffer; |
| 24 buffer.append("// Some comment\n"); | 10 buffer.append("// Some comment\n"); |
| 25 buffer.append("\n"); | 11 buffer.append("\n"); |
| 26 buffer.append("#include \"foo/bar.h\"\n"); | 12 buffer.append("#include \"foo/bar.h\"\n"); |
| 27 buffer.append("\n"); | 13 buffer.append("\n"); |
| 28 buffer.append("#include <stdio.h>\n"); | 14 buffer.append("#include <stdio.h>\n"); |
| 29 buffer.append("\n"); | 15 buffer.append("\n"); |
| 30 buffer.append(" #include \"foo/baz.h\"\n"); // Leading whitespace | 16 buffer.append(" #include \"foo/baz.h\"\n"); // Leading whitespace |
| 31 buffer.append("#include \"la/deda.h\"\n"); | 17 buffer.append("#include \"la/deda.h\"\n"); |
| 32 buffer.append("#import \"weird_mac_import.h\"\n"); | 18 buffer.append("#import \"weird_mac_import.h\"\n"); |
| 33 buffer.append("\n"); | 19 buffer.append("\n"); |
| 34 buffer.append("void SomeCode() {\n"); | 20 buffer.append("void SomeCode() {\n"); |
| 35 | 21 |
| 36 InputFile file(SourceFile("//foo.cc")); | 22 CIncludeIterator iter(buffer); |
| 37 file.SetContents(buffer); | |
| 38 | |
| 39 CIncludeIterator iter(&file); | |
| 40 | 23 |
| 41 base::StringPiece contents; | 24 base::StringPiece contents; |
| 42 LocationRange range; | 25 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 43 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range)); | |
| 44 EXPECT_EQ("foo/bar.h", contents); | 26 EXPECT_EQ("foo/bar.h", contents); |
| 45 EXPECT_TRUE(RangeIs(range, 3, 11, 20)) << range.begin().Describe(true); | 27 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 46 | |
| 47 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range)); | |
| 48 EXPECT_EQ("foo/baz.h", contents); | 28 EXPECT_EQ("foo/baz.h", contents); |
| 49 EXPECT_TRUE(RangeIs(range, 7, 12, 21)) << range.begin().Describe(true); | 29 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 50 | |
| 51 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range)); | |
| 52 EXPECT_EQ("la/deda.h", contents); | 30 EXPECT_EQ("la/deda.h", contents); |
| 53 EXPECT_TRUE(RangeIs(range, 8, 11, 20)) << range.begin().Describe(true); | 31 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 54 | |
| 55 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range)); | |
| 56 EXPECT_EQ("weird_mac_import.h", contents); | 32 EXPECT_EQ("weird_mac_import.h", contents); |
| 57 EXPECT_TRUE(RangeIs(range, 9, 10, 28)) << range.begin().Describe(true); | 33 EXPECT_FALSE(iter.GetNextIncludeString(&contents)); |
| 58 | |
| 59 EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range)); | |
| 60 } | 34 } |
| 61 | 35 |
| 62 // Tests that we don't search for includes indefinitely. | 36 // Tests that we don't search for includes indefinitely. |
| 63 TEST(CIncludeIterator, GiveUp) { | 37 TEST(CIncludeIterator, GiveUp) { |
| 64 std::string buffer; | 38 std::string buffer; |
| 65 for (size_t i = 0; i < 1000; i++) | 39 for (size_t i = 0; i < 1000; i++) |
| 66 buffer.append("x\n"); | 40 buffer.append("x\n"); |
| 67 buffer.append("#include \"foo/bar.h\"\n"); | 41 buffer.append("#include \"foo/bar.h\"\n"); |
| 68 | 42 |
| 69 InputFile file(SourceFile("//foo.cc")); | 43 base::StringPiece contents; |
| 70 file.SetContents(buffer); | |
| 71 | 44 |
| 72 base::StringPiece contents; | 45 CIncludeIterator iter(buffer); |
| 73 LocationRange range; | 46 EXPECT_FALSE(iter.GetNextIncludeString(&contents)); |
| 74 | |
| 75 CIncludeIterator iter(&file); | |
| 76 EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range)); | |
| 77 EXPECT_TRUE(contents.empty()); | 47 EXPECT_TRUE(contents.empty()); |
| 78 } | 48 } |
| 79 | 49 |
| 80 // Don't count blank lines, comments, and preprocessor when giving up. | 50 // Don't count blank lines, comments, and preprocessor when giving up. |
| 81 TEST(CIncludeIterator, DontGiveUp) { | 51 TEST(CIncludeIterator, DontGiveUp) { |
| 82 std::string buffer; | 52 std::string buffer; |
| 83 for (size_t i = 0; i < 1000; i++) | 53 for (size_t i = 0; i < 1000; i++) |
| 84 buffer.push_back('\n'); | 54 buffer.push_back('\n'); |
| 85 for (size_t i = 0; i < 1000; i++) | 55 for (size_t i = 0; i < 1000; i++) |
| 86 buffer.append("// comment\n"); | 56 buffer.append("// comment\n"); |
| 87 for (size_t i = 0; i < 1000; i++) | 57 for (size_t i = 0; i < 1000; i++) |
| 88 buffer.append("#preproc\n"); | 58 buffer.append("#preproc\n"); |
| 89 buffer.append("#include \"foo/bar.h\"\n"); | 59 buffer.append("#include \"foo/bar.h\"\n"); |
| 90 | 60 |
| 91 InputFile file(SourceFile("//foo.cc")); | 61 base::StringPiece contents; |
| 92 file.SetContents(buffer); | |
| 93 | 62 |
| 94 base::StringPiece contents; | 63 CIncludeIterator iter(buffer); |
| 95 LocationRange range; | 64 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 96 | |
| 97 CIncludeIterator iter(&file); | |
| 98 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range)); | |
| 99 EXPECT_EQ("foo/bar.h", contents); | 65 EXPECT_EQ("foo/bar.h", contents); |
| 100 } | 66 } |
| 101 | 67 |
| 102 // Tests that we'll tolerate some small numbers of non-includes interspersed | 68 // Tests that we'll tolerate some small numbers of non-includes interspersed |
| 103 // with real includes. | 69 // with real includes. |
| 104 TEST(CIncludeIterator, TolerateNonIncludes) { | 70 TEST(CIncludeIterator, TolerateNonIncludes) { |
| 105 const size_t kSkip = CIncludeIterator::kMaxNonIncludeLines - 2; | 71 const size_t kSkip = CIncludeIterator::kMaxNonIncludeLines - 2; |
| 106 const size_t kGroupCount = 100; | 72 const size_t kGroupCount = 100; |
| 107 | 73 |
| 108 std::string include("foo/bar.h"); | 74 std::string include("foo/bar.h"); |
| 109 | 75 |
| 110 // Allow a series of includes with blanks in between. | 76 // Allow a series of includes with blanks in between. |
| 111 std::string buffer; | 77 std::string buffer; |
| 112 for (size_t group = 0; group < kGroupCount; group++) { | 78 for (size_t group = 0; group < kGroupCount; group++) { |
| 113 for (size_t i = 0; i < kSkip; i++) | 79 for (size_t i = 0; i < kSkip; i++) |
| 114 buffer.append("foo\n"); | 80 buffer.append("foo\n"); |
| 115 buffer.append("#include \"" + include + "\"\n"); | 81 buffer.append("#include \"" + include + "\"\n"); |
| 116 } | 82 } |
| 117 | 83 |
| 118 InputFile file(SourceFile("//foo.cc")); | 84 base::StringPiece contents; |
| 119 file.SetContents(buffer); | |
| 120 | 85 |
| 121 base::StringPiece contents; | 86 CIncludeIterator iter(buffer); |
| 122 LocationRange range; | |
| 123 | |
| 124 CIncludeIterator iter(&file); | |
| 125 for (size_t group = 0; group < kGroupCount; group++) { | 87 for (size_t group = 0; group < kGroupCount; group++) { |
| 126 EXPECT_TRUE(iter.GetNextIncludeString(&contents, &range)); | 88 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 127 EXPECT_EQ(include, contents.as_string()); | 89 EXPECT_EQ(include, contents.as_string()); |
| 128 } | 90 } |
| 129 EXPECT_FALSE(iter.GetNextIncludeString(&contents, &range)); | 91 EXPECT_FALSE(iter.GetNextIncludeString(&contents)); |
| 130 } | 92 } |
| OLD | NEW |