kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/testlib.h (about)

     1  /*
     2   * Copyright 2019 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  #ifndef KYTHE_CXX_EXTRACTOR_TESTLIB_H_
    18  #define KYTHE_CXX_EXTRACTOR_TESTLIB_H_
    19  
    20  #include <optional>
    21  #include <string>
    22  #include <vector>
    23  
    24  #include "absl/container/flat_hash_map.h"
    25  #include "gmock/gmock.h"
    26  #include "google/protobuf/message.h"
    27  #include "kythe/proto/analysis.pb.h"
    28  
    29  namespace kythe {
    30  
    31  /// \brief Replaces relevant hashes with a unique ID based on visitation order.
    32  void CanonicalizeHashes(kythe::proto::CompilationUnit* unit);
    33  
    34  /// \brief Options used during test extraction.
    35  struct ExtractorOptions {
    36    std::vector<std::string> arguments;
    37    absl::flat_hash_map<std::string, std::string> environment;
    38    std::string working_directory;
    39  };
    40  
    41  /// \brief Runs the C++ extractor using the provided options and returns the
    42  /// resulting compilations or std::nullopt if there was an error.
    43  std::optional<std::vector<kythe::proto::CompilationUnit>> ExtractCompilations(
    44      ExtractorOptions options);
    45  
    46  /// \brief Runs the C++ extractor using the provided options and returns the
    47  /// resulting CompilationUnit or exits.
    48  kythe::proto::CompilationUnit ExtractSingleCompilationOrDie(
    49      ExtractorOptions options);
    50  
    51  /// \brief Resolves the provided workspace-relative path to find the absolute
    52  /// runfiles path of the given file.
    53  std::optional<std::string> ResolveRunfiles(absl::string_view path);
    54  
    55  /// \brief Compares the two protobuf messages with MessageDifferencer and
    56  /// reports the delta.
    57  bool EquivalentCompilations(const kythe::proto::CompilationUnit& lhs,
    58                              const kythe::proto::CompilationUnit& rhs,
    59                              std::string* delta);
    60  
    61  /// \brief Returns a gMock matcher which compares its argument aginst the
    62  /// provided compilation unit and returns the result.
    63  ::testing::Matcher<const kythe::proto::CompilationUnit&> EquivToCompilation(
    64      const kythe::proto::CompilationUnit& expected);
    65  
    66  /// \brief Returns a gMock matcher which compares its argument aginst the
    67  /// provided compilation unit and returns the result.
    68  ::testing::Matcher<const kythe::proto::CompilationUnit&> EquivToCompilation(
    69      absl::string_view expected);
    70  
    71  /// \brief Parses a TextFormat CompilationUnit protocol buffer or aborts.
    72  kythe::proto::CompilationUnit ParseTextCompilationUnitOrDie(
    73      absl::string_view text);
    74  
    75  }  // namespace kythe
    76  #endif  // KYTHE_CXX_EXTRACTOR_TESTLIB_H_