kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/textproto/textproto_schema_test.cc (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 #include "kythe/cxx/extractor/textproto/textproto_schema.h" 18 19 #include "gmock/gmock.h" 20 #include "gtest/gtest.h" 21 22 namespace kythe { 23 namespace lang_textproto { 24 namespace { 25 26 using ::testing::ElementsAreArray; 27 using ::testing::Eq; 28 29 TEST(TextprotoSchemaTest, Empty) { 30 auto schema = ParseTextprotoSchemaComments(""); 31 EXPECT_THAT(schema.proto_file, Eq("")); 32 EXPECT_THAT(schema.proto_message, Eq("")); 33 } 34 35 TEST(TextprotoSchemaTest, ProtoFile) { 36 auto schema = ParseTextprotoSchemaComments("# proto-file: some/file.proto"); 37 EXPECT_THAT(schema.proto_file, Eq("some/file.proto")); 38 EXPECT_THAT(schema.proto_message, Eq("")); 39 } 40 41 TEST(TextprotoSchemaTest, ProtoFileNoSpaces) { 42 auto schema = ParseTextprotoSchemaComments("#proto-file:some/file.proto"); 43 EXPECT_THAT(schema.proto_file, Eq("some/file.proto")); 44 EXPECT_THAT(schema.proto_message, Eq("")); 45 } 46 47 TEST(TextprotoSchemaTest, ProtoFileExtraSpaces) { 48 auto schema = 49 ParseTextprotoSchemaComments("# \t proto-file: \tsome/file.proto"); 50 EXPECT_THAT(schema.proto_file, Eq("some/file.proto")); 51 EXPECT_THAT(schema.proto_message, Eq("")); 52 } 53 54 TEST(TextprotoSchemaTest, ProtoMessage) { 55 auto schema = 56 ParseTextprotoSchemaComments("# proto-message: my_namespace.MyMessage"); 57 EXPECT_THAT(schema.proto_file, Eq("")); 58 EXPECT_THAT(schema.proto_message, Eq("my_namespace.MyMessage")); 59 } 60 61 TEST(TextprotoSchemaTest, ProtoMessageAndFile) { 62 auto schema = ParseTextprotoSchemaComments( 63 "# proto-message: my_namespace.MyMessage\n" 64 "# proto-file: some/file.proto"); 65 EXPECT_THAT(schema.proto_file, Eq("some/file.proto")); 66 EXPECT_THAT(schema.proto_message, Eq("my_namespace.MyMessage")); 67 } 68 69 TEST(TextprotoSchemaTest, LeadingNewline) { 70 auto schema = ParseTextprotoSchemaComments( 71 "\n# proto-message: my_namespace.MyMessage\n" 72 "# proto-file: some/file.proto"); 73 EXPECT_THAT(schema.proto_file, Eq("some/file.proto")); 74 EXPECT_THAT(schema.proto_message, Eq("my_namespace.MyMessage")); 75 } 76 77 TEST(TextprotoSchemaTest, ExtraNewlines) { 78 auto schema = ParseTextprotoSchemaComments( 79 "\n# proto-message: my_namespace.MyMessage\n\n\n" 80 "# proto-file: some/file.proto"); 81 EXPECT_THAT(schema.proto_file, Eq("some/file.proto")); 82 EXPECT_THAT(schema.proto_message, Eq("my_namespace.MyMessage")); 83 } 84 85 TEST(TextprotoSchemaTest, ProtoImports) { 86 auto schema = ParseTextprotoSchemaComments( 87 "# proto-file: some/file.proto\n\n" 88 "#proto-import: my_extensions.proto\n" 89 "# proto-import: other/extensions.proto"); 90 EXPECT_THAT(schema.proto_file, Eq("some/file.proto")); 91 EXPECT_THAT(schema.proto_message, Eq("")); 92 EXPECT_THAT( 93 schema.proto_imports, 94 ElementsAreArray({"my_extensions.proto", "other/extensions.proto"})); 95 } 96 97 TEST(TextprotoSchemaTest, StopSearchingAfterContent) { 98 auto schema = ParseTextprotoSchemaComments( 99 "# proto-file: some/file.proto\n" 100 "field: \"value\"\n" 101 "# proto-message: MyMessage\n"); 102 EXPECT_THAT(schema.proto_file, Eq("some/file.proto")); 103 EXPECT_THAT(schema.proto_message, Eq("")); 104 } 105 106 } // namespace 107 } // namespace lang_textproto 108 } // namespace kythe