kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/indexer/textproto/recordio_textparser_test.cc (about) 1 /* 2 * Copyright 2023 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/indexer/textproto/recordio_textparser.h" 17 18 #include "absl/strings/str_join.h" 19 #include "absl/strings/string_view.h" 20 #include "gmock/gmock.h" 21 #include "gtest/gtest.h" 22 23 namespace kythe { 24 namespace { 25 using ::testing::ElementsAre; 26 using ::testing::Pair; 27 28 std::vector<std::pair<int, std::string>> ParsedChunks(std::string content, 29 std::string separator) { 30 std::vector<std::pair<int, std::string>> chunks; 31 lang_textproto::ParseRecordTextChunks( 32 content, separator, [&](absl::string_view chunk, int chunk_start_line) { 33 chunks.emplace_back(chunk_start_line, std::string(chunk)); 34 }); 35 return chunks; 36 } 37 38 TEST(RecordioTextparserTest, TwoRecordSeparated) { 39 std::string content = absl::StrJoin({"item: 1", "---", "item: 2"}, "\n"); 40 std::vector<std::pair<int, std::string>> chunks = 41 ParsedChunks(content, "---"); 42 43 EXPECT_THAT(chunks, ElementsAre(Pair(0, "item: 1\n"), Pair(2, "item: 2"))); 44 } 45 46 TEST(RecordioTextparserTest, CommentsAlsoSeparatedByNewline) { 47 std::string content = absl::StrJoin( 48 {"# Comment1", "", "# Comment2", "item: 1", "", "# Comment3", "item: 2"}, 49 "\n"); 50 std::vector<std::pair<int, std::string>> chunks = ParsedChunks(content, "\n"); 51 52 EXPECT_THAT(chunks, 53 ElementsAre(Pair(0, "# Comment1\n\n# Comment2\nitem: 1\n"), 54 Pair(5, "# Comment3\nitem: 2"))); 55 } 56 57 TEST(RecordioTextparserTest, SeparatorStartsWithComment) { 58 std::string content = 59 absl::StrJoin({"# Comment1", " # --- ", "# Comment2", "item: 1", "# ---", 60 "# Comment3", "item: 2"}, 61 "\n"); 62 std::vector<std::pair<int, std::string>> chunks = 63 ParsedChunks(content, "# ---"); 64 65 EXPECT_THAT(chunks, 66 ElementsAre(Pair(0, "# Comment1\n # --- \n# Comment2\nitem: 1\n"), 67 Pair(5, "# Comment3\nitem: 2"))); 68 } 69 70 TEST(RecordioTextparserTest, EndsWithComment) { 71 std::string content = absl::StrJoin( 72 {"# Comment1", "item: 1", "", "item: 2", "# Comment2"}, "\n"); 73 std::vector<std::pair<int, std::string>> chunks = ParsedChunks(content, "\n"); 74 75 EXPECT_THAT(chunks, ElementsAre(Pair(0, "# Comment1\nitem: 1\n"), 76 Pair(3, "item: 2\n# Comment2"))); 77 } 78 79 } // namespace 80 } // namespace kythe