reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
//===-- FileDistanceTests.cpp  ------------------------*- C++ -*-----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "FileDistance.h"
#include "TestFS.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {
namespace {

TEST(FileDistanceTests, Distance) {
  FileDistanceOptions Opts;
  Opts.UpCost = 5;
  Opts.DownCost = 3;
  SourceParams CostTwo;
  CostTwo.Cost = 2;
  FileDistance D(
      {{"tools/clang/lib/Format/FormatToken.cpp", SourceParams()},
       {"tools/clang/include/clang/Format/FormatToken.h", SourceParams()},
       {"include/llvm/ADT/StringRef.h", CostTwo}},
      Opts);

  // Source
  EXPECT_EQ(D.distance("tools/clang/lib/Format/FormatToken.cpp"), 0u);
  EXPECT_EQ(D.distance("include/llvm/ADT/StringRef.h"), 2u);
  // Parent
  EXPECT_EQ(D.distance("tools/clang/lib/Format/"), 5u);
  // Child
  EXPECT_EQ(D.distance("tools/clang/lib/Format/FormatToken.cpp/Oops"), 3u);
  // Ancestor (up+up+up+up)
  EXPECT_EQ(D.distance("/"), 22u);
  // Sibling (up+down)
  EXPECT_EQ(D.distance("tools/clang/lib/Format/AnotherFile.cpp"), 8u);
  // Cousin (up+up+down+down)
  EXPECT_EQ(D.distance("include/llvm/Support/Allocator.h"), 18u);
  // First cousin, once removed (up+up+up+down+down)
  EXPECT_EQ(D.distance("include/llvm-c/Core.h"), 23u);
}

TEST(FileDistanceTests, BadSource) {
  // We mustn't assume that paths above sources are best reached via them.
  FileDistanceOptions Opts;
  Opts.UpCost = 5;
  Opts.DownCost = 3;
  SourceParams CostLots;
  CostLots.Cost = 100;
  FileDistance D({{"a", SourceParams()}, {"b/b/b", CostLots}}, Opts);
  EXPECT_EQ(D.distance("b"), 8u);        // a+up+down, not b+up+up
  EXPECT_EQ(D.distance("b/b/b"), 14u);   // a+up+down+down+down, not b
  EXPECT_EQ(D.distance("b/b/b/c"), 17u); // a+up+down+down+down+down, not b+down
}

// Force the unittest URI scheme to be linked,
static int LLVM_ATTRIBUTE_UNUSED UseUnittestScheme = UnittestSchemeAnchorSource;

TEST(FileDistanceTests, URI) {
  FileDistanceOptions Opts;
  Opts.UpCost = 5;
  Opts.DownCost = 3;
  SourceParams CostLots;
  CostLots.Cost = 1000;

  URIDistance D({{testPath("foo"), CostLots},
                 {"/not/a/testpath", SourceParams()},
                 {"C:\\not\\a\\testpath", SourceParams()}},
                Opts);
#ifdef _WIN32
  EXPECT_EQ(D.distance("file:///C%3a/not/a/testpath/either"), 3u);
#else
  EXPECT_EQ(D.distance("file:///not/a/testpath/either"), 3u);
#endif
  EXPECT_EQ(D.distance("unittest:///foo"), 1000u);
  EXPECT_EQ(D.distance("unittest:///bar"), 1008u);
}

TEST(FileDistance, LimitUpTraversals) {
  FileDistanceOptions Opts;
  Opts.UpCost = Opts.DownCost = 1;
  SourceParams CheapButLimited, CostLots;
  CheapButLimited.MaxUpTraversals = 1;
  CostLots.Cost = 100;

  FileDistance D({{"/", CostLots}, {"/a/b/c", CheapButLimited}}, Opts);
  EXPECT_EQ(D.distance("/a"), 101u);
  EXPECT_EQ(D.distance("/a/z"), 102u);
  EXPECT_EQ(D.distance("/a/b"), 1u);
  EXPECT_EQ(D.distance("/a/b/z"), 2u);
}

TEST(FileDistance, DisallowDownTraversalsFromRoot) {
  FileDistanceOptions Opts;
  Opts.UpCost = Opts.DownCost = 1;
  Opts.AllowDownTraversalFromRoot = false;
  SourceParams CostLots;
  CostLots.Cost = 100;

  FileDistance D({{"/", SourceParams()}, {"/a/b/c", CostLots}}, Opts);
  EXPECT_EQ(D.distance("/"), 0u);
  EXPECT_EQ(D.distance("/a"), 102u);
  EXPECT_EQ(D.distance("/a/b"), 101u);
  EXPECT_EQ(D.distance("/x"), FileDistance::Unreachable);
}

TEST(ScopeDistance, Smoke) {
  ScopeDistance D({"x::y::z", "x::", "", "a::"});
  EXPECT_EQ(D.distance("x::y::z::"), 0u);
  EXPECT_GT(D.distance("x::y::"), D.distance("x::y::z::"));
  EXPECT_GT(D.distance("x::"), D.distance("x::y::"));
  EXPECT_GT(D.distance("x::y::z::down::"), D.distance("x::y::"));
  EXPECT_GT(D.distance(""), D.distance("a::"));
  EXPECT_GT(D.distance("x::"), D.distance("a::"));
}

} // namespace
} // namespace clangd
} // namespace clang