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 "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/err.h"
7 #include "tools/gn/label.h"
8 #include "tools/gn/value.h"
9 #include "tools/gn/visibility.h"
13 struct VisPatternCase
{
17 Visibility::VisPattern::Type type
;
24 TEST(Visibility
, PatternParse
) {
25 SourceDir
current_dir("//foo/");
26 VisPatternCase cases
[] = {
28 { "", false, Visibility::VisPattern::MATCH
, "", "" },
29 { ":", false, Visibility::VisPattern::MATCH
, "", "" },
31 { ":bar", true, Visibility::VisPattern::MATCH
, "//foo/", "bar" },
32 { "//la:bar", true, Visibility::VisPattern::MATCH
, "//la/", "bar" },
33 { "*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY
, "", "" },
34 { ":*", true, Visibility::VisPattern::DIRECTORY
, "//foo/", "" },
35 { "la:*", true, Visibility::VisPattern::DIRECTORY
, "//foo/la/", "" },
36 { "la/*:*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY
,
38 { "//la:*", true, Visibility::VisPattern::DIRECTORY
, "//la/", "" },
39 { "./*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY
, "//foo/", "" },
40 { "foo/*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY
,
42 { "//l/*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY
, "//l/", "" },
43 // No toolchains allowed.
44 { "//foo(//bar)", false, Visibility::VisPattern::MATCH
, "", "" },
45 // Wildcards in invalid places.
46 { "*foo*:bar", false, Visibility::VisPattern::MATCH
, "", "" },
47 { "foo*:*bar", false, Visibility::VisPattern::MATCH
, "", "" },
48 { "*foo:bar", false, Visibility::VisPattern::MATCH
, "", "" },
49 { "foo:bar*", false, Visibility::VisPattern::MATCH
, "", "" },
50 { "*:*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY
, "", "" },
53 for (size_t i
= 0; i
< arraysize(cases
); i
++) {
54 const VisPatternCase
& cur
= cases
[i
];
56 Visibility::VisPattern result
=
57 Visibility::GetPattern(current_dir
, Value(NULL
, cur
.input
), &err
);
59 EXPECT_EQ(cur
.success
, !err
.has_error()) << i
<< " " << cur
.input
;
60 EXPECT_EQ(cur
.type
, result
.type()) << i
<< " " << cur
.input
;
61 EXPECT_EQ(cur
.dir
, result
.dir().value()) << i
<< " " << cur
.input
;
62 EXPECT_EQ(cur
.name
, result
.name()) << i
<< " " << cur
.input
;
66 TEST(Visibility
, CanSeeMe
) {
67 Value
list(NULL
, Value::LIST
);
68 list
.list_value().push_back(Value(NULL
, "//rec/*")); // Recursive.
69 list
.list_value().push_back(Value(NULL
, "//dir:*")); // One dir.
70 list
.list_value().push_back(Value(NULL
, "//my:name")); // Exact match.
74 ASSERT_TRUE(vis
.Set(SourceDir("//"), list
, &err
));
76 EXPECT_FALSE(vis
.CanSeeMe(Label(SourceDir("//random/"), "thing")));
77 EXPECT_FALSE(vis
.CanSeeMe(Label(SourceDir("//my/"), "notname")));
79 EXPECT_TRUE(vis
.CanSeeMe(Label(SourceDir("//my/"), "name")));
80 EXPECT_TRUE(vis
.CanSeeMe(Label(SourceDir("//rec/"), "anything")));
81 EXPECT_TRUE(vis
.CanSeeMe(Label(SourceDir("//rec/a/"), "anything")));
82 EXPECT_TRUE(vis
.CanSeeMe(Label(SourceDir("//rec/b/"), "anything")));
83 EXPECT_TRUE(vis
.CanSeeMe(Label(SourceDir("//dir/"), "anything")));
84 EXPECT_FALSE(vis
.CanSeeMe(Label(SourceDir("//dir/a/"), "anything")));
85 EXPECT_FALSE(vis
.CanSeeMe(Label(SourceDir("//directory/"), "anything")));
88 TEST(Visibility
, Public
) {
91 ASSERT_TRUE(vis
.Set(SourceDir("//"), Value(NULL
, "*"), &err
));
93 EXPECT_TRUE(vis
.CanSeeMe(Label(SourceDir("//random/"), "thing")));
94 EXPECT_TRUE(vis
.CanSeeMe(Label(SourceDir("//"), "")));
97 TEST(Visibility
, Private
) {
100 ASSERT_TRUE(vis
.Set(SourceDir("//"), Value(NULL
, Value::LIST
), &err
));
102 EXPECT_FALSE(vis
.CanSeeMe(Label(SourceDir("//random/"), "thing")));
103 EXPECT_FALSE(vis
.CanSeeMe(Label(SourceDir("//"), "")));