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

     1  /*
     2   * Copyright 2020 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  #include "kythe/cxx/extractor/bazel_event_reader.h"
    17  
    18  #include <sstream>
    19  #include <string>
    20  #include <vector>
    21  
    22  #include "absl/strings/str_cat.h"
    23  #include "gmock/gmock.h"
    24  #include "google/protobuf/io/zero_copy_stream_impl.h"
    25  #include "google/protobuf/util/delimited_message_util.h"
    26  #include "gtest/gtest.h"
    27  #include "third_party/bazel/src/main/java/com/google/devtools/build/lib/buildeventstream/proto/build_event_stream.pb.h"
    28  
    29  namespace kythe {
    30  namespace {
    31  using ::google::protobuf::io::IstreamInputStream;
    32  using ::google::protobuf::util::SerializeDelimitedToOstream;
    33  using ::testing::ElementsAre;
    34  
    35  TEST(BazelEventReaderTest, ReadsExpectedEvents) {
    36    std::stringstream stream;
    37    for (int i = 0; i < 5; i++) {
    38      build_event_stream::BuildEvent event;
    39      event.mutable_id()->mutable_unknown()->set_details(absl::StrCat(i));
    40      ASSERT_TRUE(SerializeDelimitedToOstream(event, &stream));
    41    }
    42    IstreamInputStream input_stream(&stream);
    43  
    44    std::vector<std::string> events;
    45    BazelEventReader reader(&input_stream);
    46    for (; !reader.Done(); reader.Next()) {
    47      events.push_back(reader.Ref().id().unknown().details());
    48    }
    49    ASSERT_TRUE(reader.status().ok()) << reader.status();
    50    EXPECT_THAT(events, ElementsAre("0", "1", "2", "3", "4"));
    51  }
    52  
    53  }  // namespace
    54  }  // namespace kythe