@@ -162,6 +162,99 @@ TEST(GeneratedMessageReflectionTest, GetStringReferenceCopy) {
162162 &cord_scratch));
163163}
164164
165+ TEST (GeneratedMessageReflectionTest, GetStringView) {
166+ unittest::TestAllTypes message;
167+ TestUtil::SetAllFields (&message);
168+
169+ const Reflection* reflection = message.GetReflection ();
170+ Reflection::ScratchSpace scratch;
171+
172+ EXPECT_EQ (" 115" ,
173+ reflection->GetStringView (message, F (" optional_string" ), scratch));
174+ EXPECT_EQ (" 124" , reflection->GetStringView (
175+ message, F (" optional_string_piece" ), scratch));
176+ EXPECT_EQ (" 125" ,
177+ reflection->GetStringView (message, F (" optional_cord" ), scratch));
178+ }
179+
180+ TEST (GeneratedMessageReflectionTest, GetStringViewWithExtensions) {
181+ unittest::TestAllExtensions message;
182+ google::protobuf::FileDescriptor const * descriptor_file =
183+ message.GetDescriptor ()->file ();
184+ google::protobuf::FieldDescriptor const * string_ext =
185+ descriptor_file->FindExtensionByName (" optional_string_extension" );
186+ google::protobuf::FieldDescriptor const * string_piece_ext =
187+ descriptor_file->FindExtensionByName (" optional_string_piece_extension" );
188+ google::protobuf::FieldDescriptor const * cord_ext =
189+ descriptor_file->FindExtensionByName (" optional_cord_extension" );
190+ message.SetExtension (protobuf_unittest::optional_string_extension, " foo" );
191+ message.SetExtension (protobuf_unittest::optional_string_piece_extension, " bar" );
192+ message.SetExtension (protobuf_unittest::optional_cord_extension, " baz" );
193+ const Reflection* reflection = message.GetReflection ();
194+ Reflection::ScratchSpace scratch;
195+
196+ EXPECT_EQ (" foo" , reflection->GetStringView (message, string_ext, scratch));
197+ EXPECT_EQ (" bar" ,
198+ reflection->GetStringView (message, string_piece_ext, scratch));
199+ EXPECT_EQ (" baz" , reflection->GetStringView (message, cord_ext, scratch));
200+ }
201+
202+ TEST (GeneratedMessageReflectionTest, GetStringViewWithOneof) {
203+ unittest::TestOneof2 message;
204+ const Reflection* reflection = message.GetReflection ();
205+ const FieldDescriptor* string_field =
206+ message.GetDescriptor ()->FindFieldByName (" foo_string" );
207+ const FieldDescriptor* string_piece_field =
208+ message.GetDescriptor ()->FindFieldByName (" foo_string_piece" );
209+ Reflection::ScratchSpace scratch;
210+
211+ message.set_foo_string (" foo" );
212+ EXPECT_EQ (" foo" , reflection->GetStringView (message, string_field, scratch));
213+ EXPECT_EQ (" " ,
214+ reflection->GetStringView (message, string_piece_field, scratch));
215+
216+ }
217+
218+ TEST (GeneratedMessageReflectionTest, GetRepeatedStringView) {
219+ unittest::TestAllTypes message;
220+ TestUtil::AddRepeatedFields1 (&message);
221+ TestUtil::AddRepeatedFields2 (&message);
222+
223+ const Reflection* reflection = message.GetReflection ();
224+ Reflection::ScratchSpace scratch;
225+
226+ EXPECT_EQ (" 215" , reflection->GetRepeatedStringView (
227+ message, F (" repeated_string" ), 0 , scratch));
228+ EXPECT_EQ (" 224" , reflection->GetRepeatedStringView (
229+ message, F (" repeated_string_piece" ), 0 , scratch));
230+ EXPECT_EQ (" 225" , reflection->GetRepeatedStringView (
231+ message, F (" repeated_cord" ), 0 , scratch));
232+ }
233+
234+ TEST (GeneratedMessageReflectionTest, GetRepeatedStringViewWithExtensions) {
235+ unittest::TestAllExtensions message;
236+ google::protobuf::FileDescriptor const * descriptor_file =
237+ message.GetDescriptor ()->file ();
238+ google::protobuf::FieldDescriptor const * string_ext =
239+ descriptor_file->FindExtensionByName (" repeated_string_extension" );
240+ google::protobuf::FieldDescriptor const * string_piece_ext =
241+ descriptor_file->FindExtensionByName (" repeated_string_piece_extension" );
242+ google::protobuf::FieldDescriptor const * cord_ext =
243+ descriptor_file->FindExtensionByName (" repeated_cord_extension" );
244+ message.AddExtension (protobuf_unittest::repeated_string_extension, " foo" );
245+ message.AddExtension (protobuf_unittest::repeated_string_piece_extension, " bar" );
246+ message.AddExtension (protobuf_unittest::repeated_cord_extension, " baz" );
247+ const Reflection* reflection = message.GetReflection ();
248+ Reflection::ScratchSpace scratch;
249+
250+ EXPECT_EQ (" foo" ,
251+ reflection->GetRepeatedStringView (message, string_ext, 0 , scratch));
252+ EXPECT_EQ (" bar" , reflection->GetRepeatedStringView (message, string_piece_ext,
253+ 0 , scratch));
254+ EXPECT_EQ (" baz" ,
255+ reflection->GetRepeatedStringView (message, cord_ext, 0 , scratch));
256+ }
257+
165258
166259class GeneratedMessageReflectionSwapTest : public testing ::TestWithParam<bool > {
167260 protected:
0 commit comments