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 <vector> |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "tools/gn/header_checker.h" |
| 9 #include "tools/gn/scheduler.h" |
| 10 #include "tools/gn/target.h" |
| 11 #include "tools/gn/test_with_scope.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 class HeaderCheckerTest : public testing::Test { |
| 16 public: |
| 17 HeaderCheckerTest() |
| 18 : a_(setup_.settings(), Label(SourceDir("//"), "a")), |
| 19 b_(setup_.settings(), Label(SourceDir("//"), "a")), |
| 20 c_(setup_.settings(), Label(SourceDir("//"), "c")) { |
| 21 a_.deps().push_back(LabelTargetPair(&b_)); |
| 22 b_.deps().push_back(LabelTargetPair(&c_)); |
| 23 |
| 24 targets_.push_back(&a_); |
| 25 targets_.push_back(&b_); |
| 26 targets_.push_back(&c_); |
| 27 } |
| 28 |
| 29 protected: |
| 30 Scheduler scheduler_; |
| 31 |
| 32 TestWithScope setup_; |
| 33 |
| 34 // Some headers that are automatically set up with a dependency chain. |
| 35 // a -> b -> c |
| 36 Target a_; |
| 37 Target b_; |
| 38 Target c_; |
| 39 |
| 40 std::vector<const Target*> targets_; |
| 41 }; |
| 42 |
| 43 } // namespace |
| 44 |
| 45 TEST_F(HeaderCheckerTest, IsDependencyOf) { |
| 46 scoped_refptr<HeaderChecker> checker( |
| 47 new HeaderChecker(setup_.build_settings(), targets_)); |
| 48 |
| 49 EXPECT_FALSE(checker->IsDependencyOf(&a_, &a_)); |
| 50 EXPECT_TRUE(checker->IsDependencyOf(&b_, &a_)); |
| 51 EXPECT_TRUE(checker->IsDependencyOf(&c_, &a_)); |
| 52 EXPECT_FALSE(checker->IsDependencyOf(&a_, &c_)); |
| 53 } |
| 54 |
| 55 TEST_F(HeaderCheckerTest, CheckInclude) { |
| 56 // Add a disconnected target d with a header to check that you have to have |
| 57 // to depend on a target listing a header. |
| 58 Target d(setup_.settings(), Label(SourceDir("//"), "d")); |
| 59 SourceFile d_header("//d_header.h"); |
| 60 d.sources().push_back(SourceFile(d_header)); |
| 61 |
| 62 // Add a header on B and say everything in B is public. |
| 63 SourceFile b_public("//b_public.h"); |
| 64 b_.sources().push_back(b_public); |
| 65 c_.set_all_headers_public(true); |
| 66 |
| 67 // Add a public and private header on C. |
| 68 SourceFile c_public("//c_public.h"); |
| 69 SourceFile c_private("//c_private.h"); |
| 70 c_.sources().push_back(c_private); |
| 71 c_.public_headers().push_back(c_public); |
| 72 c_.set_all_headers_public(false); |
| 73 |
| 74 targets_.push_back(&d); |
| 75 scoped_refptr<HeaderChecker> checker( |
| 76 new HeaderChecker(setup_.build_settings(), targets_)); |
| 77 |
| 78 // A file in target A can't include a header from D because A has no |
| 79 // dependency on D. |
| 80 Err err; |
| 81 SourceFile source_file("//some_file.cc"); |
| 82 EXPECT_FALSE(checker->CheckInclude(&a_, source_file, d_header, &err)); |
| 83 EXPECT_TRUE(err.has_error()); |
| 84 |
| 85 // A can include the public header in B. |
| 86 err = Err(); |
| 87 EXPECT_TRUE(checker->CheckInclude(&a_, source_file, b_public, &err)); |
| 88 EXPECT_FALSE(err.has_error()); |
| 89 |
| 90 // Check A depending on the public and private headers in C. |
| 91 err = Err(); |
| 92 EXPECT_TRUE(checker->CheckInclude(&a_, source_file, c_public, &err)); |
| 93 EXPECT_FALSE(err.has_error()); |
| 94 EXPECT_FALSE(checker->CheckInclude(&a_, source_file, c_private, &err)); |
| 95 EXPECT_TRUE(err.has_error()); |
| 96 |
| 97 // A can depend on a random file unknown to the build. |
| 98 err = Err(); |
| 99 EXPECT_TRUE(checker->CheckInclude(&a_, source_file, SourceFile("//random.h"), |
| 100 &err)); |
| 101 EXPECT_FALSE(err.has_error()); |
| 102 } |
OLD | NEW |