kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/file_vname_generator_test.cc (about)

     1  /*
     2   * Copyright 2014 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #include "kythe/cxx/common/file_vname_generator.h"
    18  
    19  #include <string>
    20  
    21  #include "gmock/gmock.h"
    22  #include "gtest/gtest.h"
    23  #include "kythe/proto/storage.pb.h"
    24  #include "protobuf-matchers/protocol-buffer-matchers.h"
    25  
    26  extern const char kTestFile[];
    27  extern const char kSharedTestFile[];
    28  
    29  namespace kythe {
    30  namespace {
    31  using ::protobuf_matchers::EqualsProto;
    32  
    33  TEST(FileVNameGenerator, ParsesFile) {
    34    FileVNameGenerator generator;
    35    std::string error_text;
    36    EXPECT_TRUE(generator.LoadJsonString(kTestFile, &error_text))
    37        << "Couldn't parse: " << error_text;
    38    EXPECT_TRUE(generator.LoadJsonString(kSharedTestFile, &error_text))
    39        << "Couldn't parse: " << error_text;
    40  }
    41  
    42  TEST(FileVNameGenerator, EmptyLookup) {
    43    FileVNameGenerator generator;
    44    kythe::proto::VName default_vname;
    45    ASSERT_THAT(generator.LookupBaseVName(""), EqualsProto(default_vname));
    46  }
    47  
    48  TEST(FileVNameGenerator, DefaultLookup) {
    49    FileVNameGenerator generator;
    50    kythe::proto::VName default_vname;
    51    default_vname.set_root("root");
    52    default_vname.set_corpus("corpus");
    53    generator.set_default_base_vname(default_vname);
    54    ASSERT_THAT(generator.LookupBaseVName(""), EqualsProto(default_vname));
    55  }
    56  
    57  TEST(FileVNameGenerator, LookupStatic) {
    58    FileVNameGenerator generator;
    59    std::string error_text;
    60    ASSERT_TRUE(generator.LoadJsonString(kSharedTestFile, &error_text))
    61        << "Couldn't parse: " << error_text;
    62    kythe::proto::VName test_vname;
    63    test_vname.set_root("root");
    64    test_vname.set_corpus("static");
    65    EXPECT_THAT(generator.LookupBaseVName("static/path"),
    66                EqualsProto(test_vname));
    67  }
    68  
    69  TEST(FileVNameGenerator, LookupOrdered) {
    70    FileVNameGenerator generator;
    71    std::string error_text;
    72    ASSERT_TRUE(generator.LoadJsonString(kSharedTestFile, &error_text))
    73        << "Couldn't parse: " << error_text;
    74    kythe::proto::VName first_vname;
    75    first_vname.set_corpus("first");
    76    kythe::proto::VName second_vname;
    77    second_vname.set_corpus("second");
    78    EXPECT_THAT(generator.LookupBaseVName("dup/path"), EqualsProto(first_vname));
    79    EXPECT_THAT(generator.LookupBaseVName("dup/path2"),
    80                EqualsProto(second_vname));
    81  }
    82  
    83  TEST(FileVNameGenerator, LookupGroups) {
    84    FileVNameGenerator generator;
    85    std::string error_text;
    86    ASSERT_TRUE(generator.LoadJsonString(kSharedTestFile, &error_text))
    87        << "Couldn't parse: " << error_text;
    88    kythe::proto::VName corpus_vname;
    89    corpus_vname.set_corpus("corpus");
    90    EXPECT_THAT(generator.LookupBaseVName("corpus/some/path/here"),
    91                EqualsProto(corpus_vname));
    92    kythe::proto::VName grp1_vname;
    93    grp1_vname.set_corpus("grp1/grp1/endingGroup");
    94    grp1_vname.set_root("12345");
    95    EXPECT_THAT(generator.LookupBaseVName("grp1/12345/endingGroup"),
    96                EqualsProto(grp1_vname));
    97    kythe::proto::VName kythe_java_vname;
    98    kythe_java_vname.set_corpus("kythe");
    99    kythe_java_vname.set_root("java");
   100    EXPECT_THAT(generator.LookupBaseVName(
   101                    "bazel-bin/kythe/java/some/path/A.jar!/some/path/A.class"),
   102                EqualsProto(kythe_java_vname));
   103    EXPECT_THAT(generator.LookupBaseVName(
   104                    "kythe/java/com/google/devtools/kythe/util/KytheURI.java"),
   105                EqualsProto(kythe_java_vname));
   106    kythe::proto::VName other_java_vname;
   107    other_java_vname.set_corpus("otherCorpus");
   108    other_java_vname.set_root("java");
   109    EXPECT_THAT(
   110        generator.LookupBaseVName(
   111            "otherCorpus/java/com/google/devtools/kythe/util/KytheURI.java"),
   112        EqualsProto(other_java_vname));
   113  }
   114  
   115  TEST(FileVNameGenerator, ActualConfigTests) {
   116    FileVNameGenerator generator;
   117    std::string error_text;
   118    ASSERT_TRUE(generator.LoadJsonString(kTestFile, &error_text))
   119        << "Couldn't parse: " << error_text;
   120    kythe::proto::VName test_file;
   121    test_file.set_corpus("kythe");
   122    test_file.set_path(
   123        "kythe/cxx/extractor/testdata/extract_verify_std_string_test.cc");
   124    EXPECT_THAT(
   125        generator.LookupVName(
   126            "kythe/cxx/extractor/testdata/extract_verify_std_string_test.cc"),
   127        EqualsProto(test_file));
   128  
   129    kythe::proto::VName stdlib_file;
   130    stdlib_file.set_corpus("cstdlib");
   131    stdlib_file.set_path("alloca.h");
   132    stdlib_file.set_root("/usr/include");
   133    EXPECT_THAT(generator.LookupVName("/usr/include/alloca.h"),
   134                EqualsProto(stdlib_file));
   135  
   136    kythe::proto::VName compiler_file;
   137    compiler_file.set_corpus("cstdlib");
   138    compiler_file.set_path("stdint.h");
   139    compiler_file.set_root("third_party/llvm/lib/clang/3.6.0/include");
   140    EXPECT_THAT(generator.LookupVName(
   141                    "third_party/llvm/lib/clang/3.6.0/include/stdint.h"),
   142                EqualsProto(compiler_file));
   143  }
   144  
   145  }  // namespace
   146  }  // namespace kythe
   147  
   148  int main(int argc, char** argv) {
   149    ::testing::InitGoogleTest(&argc, argv);
   150    int result = RUN_ALL_TESTS();
   151    return result;
   152  }
   153  
   154  const char kTestFile[] = R"d([
   155    {
   156      "pattern": "bazel-bin/([^/]+/javatests/.+/testdata)/.+\\.jar!/([^\\$]+)(\\$.+)?\\.class",
   157      "vname": {
   158        "corpus": "kythe",
   159        "path": "@1@/@2@.java"
   160      }
   161    },
   162    {
   163      "pattern": "bazel-bin/(.*)/(java|javatests)/.*\\.jar!/([^\\$]+)(\\$.+)?\\.class",
   164      "vname": {
   165        "corpus": "kythe",
   166        "path": "@1@/@2@/@3@.java"
   167      }
   168    },
   169    {
   170      "pattern": "(third_party/llvm/lib/clang/[^/]+/include)/(.*)",
   171      "vname": {
   172        "corpus": "cstdlib",
   173        "root": "@1@",
   174        "path": "@2@"
   175      }
   176    },
   177    {
   178      "pattern": "third_party/([^/]+)/.*\\.jar!/([^\\$]+)(\\$.+)?\\.class",
   179      "vname": {
   180        "corpus": "third_party",
   181        "root": "@1@",
   182        "path": "@2@.java"
   183      }
   184    },
   185    {
   186      "pattern": "([^/]+/java|javatests/[^\\$]+)(\\$.+)?\\.(class|java)",
   187      "vname": {
   188        "corpus": "kythe",
   189        "path": "@1@.java"
   190      }
   191    },
   192    {
   193      "pattern": "bazel-bin/[^/]+/proto/.*\\.jar!/([^\\$]+)(\\$.+)?\\.class",
   194      "vname": {
   195        "corpus": "kythe",
   196        "root": "GENERATED/proto/java",
   197        "path": "@1@.java"
   198      }
   199    },
   200    {
   201      "pattern": "(/usr/include/c\\+\\+/[^/]+)/(.*)",
   202      "vname": {
   203        "corpus": "libstdcxx",
   204        "root": "@1@",
   205        "path": "@2@"
   206      }
   207    },
   208    {
   209      "pattern": "/usr/include/(.*)",
   210      "vname": {
   211        "corpus": "cstdlib",
   212        "root": "/usr/include",
   213        "path": "@1@"
   214      }
   215    },
   216    {
   217      "pattern": "(.*)",
   218      "vname": {
   219        "corpus": "kythe",
   220        "path": "@1@"
   221      }
   222    }
   223  ])d";
   224  
   225  // Mirror of test config in kythe/storage/go/filevnames/filevnames_test.go
   226  const char kSharedTestFile[] = R"d([
   227    {
   228      "pattern": "static/path",
   229      "vname": {
   230        "root": "root",
   231        "corpus": "static"
   232      }
   233    },
   234    {
   235      "pattern": "dup/path",
   236      "vname": {
   237        "corpus": "first"
   238      }
   239    },
   240    {
   241      "pattern": "dup/path2",
   242      "vname": {
   243        "corpus": "second"
   244      }
   245    },
   246    {
   247      "pattern": "(?P<CORPUS>grp1)/(\\d+)/(.*)",
   248      "vname": {
   249        "root": "@2@",
   250        "corpus": "@1@/@CORPUS@/@3@"
   251      }
   252    },
   253    {
   254      "pattern": "bazel-bin/([^/]+)/java/.*[.]jar!/.*",
   255      "vname": {
   256        "root": "java",
   257        "corpus": "@1@"
   258      }
   259    },
   260    {
   261      "pattern": "third_party/([^/]+)/.*[.]jar!/.*",
   262      "vname": {
   263        "root": "@1@",
   264        "corpus": "third_party"
   265      }
   266    },
   267    {
   268      "pattern": "([^/]+)/java/.*",
   269      "vname": {
   270        "root": "java",
   271        "corpus": "@1@"
   272      }
   273    },
   274    {
   275      "pattern": "([^/]+)/.*",
   276      "vname": {
   277        "corpus": "@1@"
   278      }
   279    }
   280  ])d";