Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / tools / gn / header_checker_unittest.cc
blob05c5f647c5cd4b717a381ef01a405d5f0c04737e
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.
5 #include <vector>
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"
13 namespace {
15 class HeaderCheckerTest : public testing::Test {
16 public:
17 HeaderCheckerTest()
18 : a_(setup_.settings(), Label(SourceDir("//a/"), "a")),
19 b_(setup_.settings(), Label(SourceDir("//b/"), "a")),
20 c_(setup_.settings(), Label(SourceDir("//c/"), "c")) {
21 a_.deps().push_back(LabelTargetPair(&b_));
22 b_.deps().push_back(LabelTargetPair(&c_));
24 // Start with all public visibility.
25 a_.visibility().SetPublic();
26 b_.visibility().SetPublic();
27 c_.visibility().SetPublic();
29 targets_.push_back(&a_);
30 targets_.push_back(&b_);
31 targets_.push_back(&c_);
34 protected:
35 Scheduler scheduler_;
37 TestWithScope setup_;
39 // Some headers that are automatically set up with a dependency chain.
40 // a -> b -> c
41 Target a_;
42 Target b_;
43 Target c_;
45 std::vector<const Target*> targets_;
48 } // namespace
50 TEST_F(HeaderCheckerTest, IsDependencyOf) {
51 scoped_refptr<HeaderChecker> checker(
52 new HeaderChecker(setup_.build_settings(), targets_));
54 EXPECT_FALSE(checker->IsDependencyOf(&a_, &a_));
55 EXPECT_TRUE(checker->IsDependencyOf(&b_, &a_));
56 EXPECT_TRUE(checker->IsDependencyOf(&c_, &a_));
57 EXPECT_FALSE(checker->IsDependencyOf(&a_, &c_));
60 TEST_F(HeaderCheckerTest, CheckInclude) {
61 InputFile input_file(SourceFile("//some_file.cc"));
62 input_file.SetContents(std::string());
63 LocationRange range; // Dummy value.
65 // Add a disconnected target d with a header to check that you have to have
66 // to depend on a target listing a header.
67 Target d(setup_.settings(), Label(SourceDir("//"), "d"));
68 SourceFile d_header("//d_header.h");
69 d.sources().push_back(SourceFile(d_header));
71 // Add a header on B and say everything in B is public.
72 SourceFile b_public("//b_public.h");
73 b_.sources().push_back(b_public);
74 c_.set_all_headers_public(true);
76 // Add a public and private header on C.
77 SourceFile c_public("//c_public.h");
78 SourceFile c_private("//c_private.h");
79 c_.sources().push_back(c_private);
80 c_.public_headers().push_back(c_public);
81 c_.set_all_headers_public(false);
83 targets_.push_back(&d);
84 scoped_refptr<HeaderChecker> checker(
85 new HeaderChecker(setup_.build_settings(), targets_));
87 // A file in target A can't include a header from D because A has no
88 // dependency on D.
89 Err err;
90 EXPECT_FALSE(checker->CheckInclude(&a_, input_file, d_header, range, &err));
91 EXPECT_TRUE(err.has_error());
93 // A can include the public header in B.
94 err = Err();
95 EXPECT_TRUE(checker->CheckInclude(&a_, input_file, b_public, range, &err));
96 EXPECT_FALSE(err.has_error());
98 // Check A depending on the public and private headers in C.
99 err = Err();
100 EXPECT_TRUE(checker->CheckInclude(&a_, input_file, c_public, range, &err));
101 EXPECT_FALSE(err.has_error());
102 EXPECT_FALSE(checker->CheckInclude(&a_, input_file, c_private, range, &err));
103 EXPECT_TRUE(err.has_error());
105 // A can depend on a random file unknown to the build.
106 err = Err();
107 EXPECT_TRUE(checker->CheckInclude(&a_, input_file, SourceFile("//random.h"),
108 range, &err));
109 EXPECT_FALSE(err.has_error());
111 // If C is not visible from A, A can't include public headers even if there
112 // is a dependency path.
113 c_.visibility().SetPrivate(c_.label().dir());
114 err = Err();
115 EXPECT_FALSE(checker->CheckInclude(&a_, input_file, c_public, range, &err));
116 EXPECT_TRUE(err.has_error());