16d528ed9Sopenharmony_ci// Copyright 2014 The Chromium Authors. All rights reserved.
26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
36d528ed9Sopenharmony_ci// found in the LICENSE file.
46d528ed9Sopenharmony_ci
56d528ed9Sopenharmony_ci#include "gn/visibility.h"
66d528ed9Sopenharmony_ci#include "gn/err.h"
76d528ed9Sopenharmony_ci#include "gn/label.h"
86d528ed9Sopenharmony_ci#include "gn/scope.h"
96d528ed9Sopenharmony_ci#include "gn/value.h"
106d528ed9Sopenharmony_ci#include "util/test/test.h"
116d528ed9Sopenharmony_ci
126d528ed9Sopenharmony_ciTEST(Visibility, CanSeeMe) {
136d528ed9Sopenharmony_ci  Value list(nullptr, Value::LIST);
146d528ed9Sopenharmony_ci  list.list_value().push_back(Value(nullptr, "//rec/*"));    // Recursive.
156d528ed9Sopenharmony_ci  list.list_value().push_back(Value(nullptr, "//dir:*"));    // One dir.
166d528ed9Sopenharmony_ci  list.list_value().push_back(Value(nullptr, "//my:name"));  // Exact match.
176d528ed9Sopenharmony_ci
186d528ed9Sopenharmony_ci  Err err;
196d528ed9Sopenharmony_ci  Visibility vis;
206d528ed9Sopenharmony_ci  ASSERT_TRUE(vis.Set(SourceDir("//"), std::string_view(), list, &err));
216d528ed9Sopenharmony_ci
226d528ed9Sopenharmony_ci  EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//random/"), "thing")));
236d528ed9Sopenharmony_ci  EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//my/"), "notname")));
246d528ed9Sopenharmony_ci
256d528ed9Sopenharmony_ci  EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//my/"), "name")));
266d528ed9Sopenharmony_ci  EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//rec/"), "anything")));
276d528ed9Sopenharmony_ci  EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//rec/a/"), "anything")));
286d528ed9Sopenharmony_ci  EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//rec/b/"), "anything")));
296d528ed9Sopenharmony_ci  EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//dir/"), "anything")));
306d528ed9Sopenharmony_ci  EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//dir/a/"), "anything")));
316d528ed9Sopenharmony_ci  EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//directory/"), "anything")));
326d528ed9Sopenharmony_ci}
336d528ed9Sopenharmony_ci
346d528ed9Sopenharmony_ciTEST(Visibility, Public) {
356d528ed9Sopenharmony_ci  Err err;
366d528ed9Sopenharmony_ci  Visibility vis;
376d528ed9Sopenharmony_ci
386d528ed9Sopenharmony_ci  Value list(nullptr, Value::LIST);
396d528ed9Sopenharmony_ci  list.list_value().push_back(Value(nullptr, "*"));
406d528ed9Sopenharmony_ci  ASSERT_TRUE(vis.Set(SourceDir("//"), std::string_view(), list, &err));
416d528ed9Sopenharmony_ci
426d528ed9Sopenharmony_ci  EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//random/"), "thing")));
436d528ed9Sopenharmony_ci  EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//"), "")));
446d528ed9Sopenharmony_ci}
456d528ed9Sopenharmony_ci
466d528ed9Sopenharmony_ciTEST(Visibility, Private) {
476d528ed9Sopenharmony_ci  Err err;
486d528ed9Sopenharmony_ci  Visibility vis;
496d528ed9Sopenharmony_ci  ASSERT_TRUE(vis.Set(SourceDir("//"), std::string_view(),
506d528ed9Sopenharmony_ci                      Value(nullptr, Value::LIST), &err));
516d528ed9Sopenharmony_ci
526d528ed9Sopenharmony_ci  EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//random/"), "thing")));
536d528ed9Sopenharmony_ci  EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//"), "")));
546d528ed9Sopenharmony_ci}
556d528ed9Sopenharmony_ci
566d528ed9Sopenharmony_ciTEST(Visibility, AboveSourceDir) {
576d528ed9Sopenharmony_ci  std::string source_root = "/foo/bar/baz/";
586d528ed9Sopenharmony_ci  SourceDir cur_dir("//");
596d528ed9Sopenharmony_ci
606d528ed9Sopenharmony_ci  Err err;
616d528ed9Sopenharmony_ci  Visibility vis;
626d528ed9Sopenharmony_ci
636d528ed9Sopenharmony_ci  Value list(nullptr, Value::LIST);
646d528ed9Sopenharmony_ci  list.list_value().push_back(Value(nullptr, "../../*"));
656d528ed9Sopenharmony_ci  ASSERT_TRUE(vis.Set(cur_dir, source_root, list, &err));
666d528ed9Sopenharmony_ci
676d528ed9Sopenharmony_ci  EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//random/"), "thing")));
686d528ed9Sopenharmony_ci  EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("/foo/"), "foo")));
696d528ed9Sopenharmony_ci  EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("/foo/bar/"), "bar")));
706d528ed9Sopenharmony_ci  EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("/nowhere/"), "foo")));
716d528ed9Sopenharmony_ci}
72