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 |
| 53 std::string buffer; |
| 54 for (size_t i = 0; i < 1000; i++) |
| 55 buffer.push_back('\n'); |
| 56 for (size_t i = 0; i < 1000; i++) |
| 57 buffer.append("// comment\n"); |
| 58 for (size_t i = 0; i < 1000; i++) |
| 59 buffer.append("#preproc\n"); |
| 60 buffer.append("#include \"foo/bar.h\"\n"); |
| 61 |
| 62 base::StringPiece contents; |
| 63 |
| 64 CIncludeIterator iter(buffer); |
| 65 EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
| 66 EXPECT_EQ("foo/bar.h", contents); |
| 67 } |
OLD | NEW |