OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "tools/gn/c_include_iterator.h" |
| 7 |
| 8 TEST(CIncludeIterator, Basic) { |
| 9 std::string buffer; |
| 10 buffer.append("// Some comment\n"); |
| 11 buffer.append("\n"); |
| 12 buffer.append("#include \"foo/bar.h\"\n"); |
| 13 buffer.append("\n"); |
| 14 buffer.append("#include <stdio.h>\n"); |
| 15 buffer.append("\n"); |
| 16 buffer.append(" #include \"foo/baz.h\"\n"); // Leading whitespace |
| 17 buffer.append("#include \"la/deda.h\"\n"); |
| 18 buffer.append("#import \"weird_mac_import.h\"\n"); |
| 19 buffer.append("\n"); |
| 20 buffer.append("void SomeCode() {\n"); |
| 21 |
| 22 CIncludeIterator iter(buffer); |
| 23 |
| 24 base::StringPiece contents; |
| 25 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 26 EXPECT_EQ("foo/bar.h", contents); |
| 27 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 28 EXPECT_EQ("foo/baz.h", contents); |
| 29 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 30 EXPECT_EQ("la/deda.h", contents); |
| 31 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 32 EXPECT_EQ("weird_mac_import.h", contents); |
| 33 EXPECT_FALSE(iter.GetNextIncludeString(&contents)); |
| 34 } |
| 35 |
| 36 // Tests that we don't search for includes indefinitely. |
| 37 TEST(CIncludeIterator, GiveUp) { |
| 38 std::string buffer; |
| 39 for (size_t i = 0; i < 1000; i++) |
| 40 buffer.append("x\n"); |
| 41 buffer.append("#include \"foo/bar.h\"\n"); |
| 42 |
| 43 base::StringPiece contents; |
| 44 |
| 45 CIncludeIterator iter(buffer); |
| 46 EXPECT_FALSE(iter.GetNextIncludeString(&contents)); |
| 47 EXPECT_TRUE(contents.empty()); |
| 48 } |
| 49 |
| 50 // Don't count blank lines, comments, and preprocessor when giving up. |
| 51 TEST(CIncludeIterator, DontGiveUp) { |
| 52 std::string buffer; |
| 53 for (size_t i = 0; i < 1000; i++) |
| 54 buffer.push_back('\n'); |
| 55 for (size_t i = 0; i < 1000; i++) |
| 56 buffer.append("// comment\n"); |
| 57 for (size_t i = 0; i < 1000; i++) |
| 58 buffer.append("#preproc\n"); |
| 59 buffer.append("#include \"foo/bar.h\"\n"); |
| 60 |
| 61 base::StringPiece contents; |
| 62 |
| 63 CIncludeIterator iter(buffer); |
| 64 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 65 EXPECT_EQ("foo/bar.h", contents); |
| 66 } |
| 67 |
| 68 // Tests that we'll tolerate some small numbers of non-includes interspersed |
| 69 // with real includes. |
| 70 TEST(CIncludeIterator, TolerateNonIncludes) { |
| 71 const size_t kSkip = CIncludeIterator::kMaxNonIncludeLines - 2; |
| 72 const size_t kGroupCount = 100; |
| 73 |
| 74 std::string include("foo/bar.h"); |
| 75 |
| 76 // Allow a series of includes with blanks in between. |
| 77 std::string buffer; |
| 78 for (size_t group = 0; group < kGroupCount; group++) { |
| 79 for (size_t i = 0; i < kSkip; i++) |
| 80 buffer.append("foo\n"); |
| 81 buffer.append("#include \"" + include + "\"\n"); |
| 82 } |
| 83 |
| 84 base::StringPiece contents; |
| 85 |
| 86 CIncludeIterator iter(buffer); |
| 87 for (size_t group = 0; group < kGroupCount; group++) { |
| 88 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 89 EXPECT_EQ(include, contents.as_string()); |
| 90 } |
| 91 EXPECT_FALSE(iter.GetNextIncludeString(&contents)); |
| 92 } |
OLD | NEW |