Index: tools/gn/c_include_iterator_unittest.cc |
diff --git a/tools/gn/c_include_iterator_unittest.cc b/tools/gn/c_include_iterator_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0b09a952a834ae5a66f9bdf7d269bdbdc8ced5b1 |
--- /dev/null |
+++ b/tools/gn/c_include_iterator_unittest.cc |
@@ -0,0 +1,67 @@ |
+// 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 "testing/gtest/include/gtest/gtest.h" |
+#include "tools/gn/c_include_iterator.h" |
+ |
+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"); |
+ buffer.append("#import \"weird_mac_import.h\"\n"); |
+ buffer.append("\n"); |
+ buffer.append("void SomeCode() {\n"); |
+ |
+ CIncludeIterator iter(buffer); |
+ |
+ base::StringPiece contents; |
+ EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
+ EXPECT_EQ("foo/bar.h", contents); |
+ EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
+ EXPECT_EQ("foo/baz.h", contents); |
+ EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
+ EXPECT_EQ("la/deda.h", contents); |
+ EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
+ EXPECT_EQ("weird_mac_import.h", contents); |
+ EXPECT_FALSE(iter.GetNextIncludeString(&contents)); |
+} |
+ |
+// 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"); |
+ |
+ base::StringPiece contents; |
+ |
+ CIncludeIterator iter(buffer); |
+ EXPECT_FALSE(iter.GetNextIncludeString(&contents)); |
+ 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"); |
+ |
+ base::StringPiece contents; |
+ |
+ CIncludeIterator iter(buffer); |
+ EXPECT_TRUE(iter.GetNextIncludeString(&contents)); |
+ EXPECT_EQ("foo/bar.h", contents); |
+} |