@ -25,6 +25,11 @@ import com.google.protobuf.TextFormat.Parser.SingularOverwritePolicy;
import com.google.protobuf.testing.proto.TestProto3Optional ;
import com.google.protobuf.testing.proto.TestProto3Optional.NestedEnum ;
import any_test.AnyTestProto.TestAny ;
import editions_unittest.GroupLikeFileScope ;
import editions_unittest.MessageImport ;
import editions_unittest.NotGroupLikeScope ;
import editions_unittest.TestDelimited ;
import editions_unittest.UnittestDelimited ;
import map_test.MapTestProto.TestMap ;
import protobuf_unittest.UnittestMset.TestMessageSetExtension1 ;
import protobuf_unittest.UnittestMset.TestMessageSetExtension2 ;
@ -1590,6 +1595,202 @@ public class TextFormatTest {
"1:17: Couldn't parse integer: For input string: \"[\"" , "optional_int32: []\n" ) ;
}
// =======================================================================
// test delimited
@Test
public void testPrintGroupLikeDelimited ( ) throws Exception {
TestDelimited message =
TestDelimited . newBuilder ( )
. setGroupLike ( TestDelimited . GroupLike . newBuilder ( ) . setA ( 1 ) . build ( ) )
. build ( ) ;
assertThat ( TextFormat . printer ( ) . printToString ( message ) ) . isEqualTo ( "GroupLike {\n a: 1\n}\n" ) ;
}
@Test
public void testPrintGroupLikeDelimitedExtension ( ) throws Exception {
TestDelimited message =
TestDelimited . newBuilder ( )
. setExtension (
UnittestDelimited . groupLikeFileScope ,
GroupLikeFileScope . newBuilder ( ) . setA ( 1 ) . build ( ) )
. build ( ) ;
assertThat ( TextFormat . printer ( ) . printToString ( message ) )
. isEqualTo ( "[editions_unittest.grouplikefilescope] {\n a: 1\n}\n" ) ;
}
@Test
public void testPrintGroupLikeNotDelimited ( ) throws Exception {
TestDelimited message =
TestDelimited . newBuilder ( )
. setLengthprefixed ( TestDelimited . LengthPrefixed . newBuilder ( ) . setA ( 1 ) . build ( ) )
. build ( ) ;
assertThat ( TextFormat . printer ( ) . printToString ( message ) )
. isEqualTo ( "lengthprefixed {\n a: 1\n}\n" ) ;
}
@Test
public void testPrintGroupLikeMismatchedName ( ) throws Exception {
TestDelimited message =
TestDelimited . newBuilder ( )
. setNotgrouplike ( TestDelimited . GroupLike . newBuilder ( ) . setA ( 1 ) . build ( ) )
. build ( ) ;
assertThat ( TextFormat . printer ( ) . printToString ( message ) )
. isEqualTo ( "notgrouplike {\n a: 1\n}\n" ) ;
}
@Test
public void testPrintGroupLikeExtensionMismatchedName ( ) throws Exception {
TestDelimited message =
TestDelimited . newBuilder ( )
. setExtension (
UnittestDelimited . notGroupLikeScope , NotGroupLikeScope . newBuilder ( ) . setA ( 1 ) . build ( ) )
. build ( ) ;
assertThat ( TextFormat . printer ( ) . printToString ( message ) )
. isEqualTo ( "[editions_unittest.not_group_like_scope] {\n a: 1\n}\n" ) ;
}
@Test
public void testPrintGroupLikeMismatchedScope ( ) throws Exception {
TestDelimited message =
TestDelimited . newBuilder ( )
. setNotgrouplikescope ( NotGroupLikeScope . newBuilder ( ) . setA ( 1 ) . build ( ) )
. build ( ) ;
assertThat ( TextFormat . printer ( ) . printToString ( message ) )
. isEqualTo ( "notgrouplikescope {\n a: 1\n}\n" ) ;
}
@Test
public void testPrintGroupLikeExtensionMismatchedScope ( ) throws Exception {
TestDelimited message =
TestDelimited . newBuilder ( )
. setExtension (
UnittestDelimited . grouplike , TestDelimited . GroupLike . newBuilder ( ) . setA ( 1 ) . build ( ) )
. build ( ) ;
assertThat ( TextFormat . printer ( ) . printToString ( message ) )
. isEqualTo ( "[editions_unittest.grouplike] {\n a: 1\n}\n" ) ;
}
@Test
public void testPrintGroupLikeMismatchedFile ( ) throws Exception {
TestDelimited message =
TestDelimited . newBuilder ( )
. setMessageimport ( MessageImport . newBuilder ( ) . setA ( 1 ) . build ( ) )
. build ( ) ;
assertThat ( TextFormat . printer ( ) . printToString ( message ) )
. isEqualTo ( "messageimport {\n a: 1\n}\n" ) ;
}
@Test
public void testParseDelimitedGroupLikeType ( ) throws Exception {
TestDelimited . Builder message = TestDelimited . newBuilder ( ) ;
TextFormat . merge ( "GroupLike { a: 1 }" , message ) ;
assertThat ( message . build ( ) )
. isEqualTo (
TestDelimited . newBuilder ( )
. setGroupLike ( TestDelimited . GroupLike . newBuilder ( ) . setA ( 1 ) . build ( ) )
. build ( ) ) ;
}
@Test
public void testParseDelimitedGroupLikeField ( ) throws Exception {
TestDelimited . Builder message = TestDelimited . newBuilder ( ) ;
TextFormat . merge ( "grouplike { a: 2 }" , message ) ;
assertThat ( message . build ( ) )
. isEqualTo (
TestDelimited . newBuilder ( )
. setGroupLike ( TestDelimited . GroupLike . newBuilder ( ) . setA ( 2 ) . build ( ) )
. build ( ) ) ;
}
@Test
public void testParseDelimitedGroupLikeExtension ( ) throws Exception {
TestDelimited . Builder message = TestDelimited . newBuilder ( ) ;
ExtensionRegistry registry = ExtensionRegistry . newInstance ( ) ;
registry . add ( UnittestDelimited . grouplike ) ;
TextFormat . merge ( "[editions_unittest.grouplike] { a: 2 }" , registry , message ) ;
assertThat ( message . build ( ) )
. isEqualTo (
TestDelimited . newBuilder ( )
. setExtension (
UnittestDelimited . grouplike ,
TestDelimited . GroupLike . newBuilder ( ) . setA ( 2 ) . build ( ) )
. build ( ) ) ;
}
@Test
public void testParseDelimitedGroupLikeInvalid ( ) throws Exception {
TestDelimited . Builder message = TestDelimited . newBuilder ( ) ;
try {
TextFormat . merge ( "GROUPlike { a: 3 }" , message ) ;
assertWithMessage ( "Expected parse exception." ) . fail ( ) ;
} catch ( TextFormat . ParseException e ) {
assertThat ( e )
. hasMessageThat ( )
. isEqualTo (
"1:1: Input contains unknown fields and/or extensions:\n"
+ "1:1:\teditions_unittest.TestDelimited.GROUPlike" ) ;
}
}
@Test
public void testParseDelimitedGroupLikeInvalidExtension ( ) throws Exception {
TestDelimited . Builder message = TestDelimited . newBuilder ( ) ;
ExtensionRegistry registry = ExtensionRegistry . newInstance ( ) ;
registry . add ( UnittestDelimited . grouplike ) ;
try {
TextFormat . merge ( "[editions_unittest.GroupLike] { a: 2 }" , registry , message ) ;
assertWithMessage ( "Expected parse exception." ) . fail ( ) ;
} catch ( TextFormat . ParseException e ) {
assertThat ( e )
. hasMessageThat ( )
. isEqualTo (
"1:20: Input contains unknown fields and/or extensions:\n"
+ "1:20:\teditions_unittest.TestDelimited.[editions_unittest.GroupLike]" ) ;
}
}
@Test
public void testParseDelimited ( ) throws Exception {
TestDelimited . Builder message = TestDelimited . newBuilder ( ) ;
TextFormat . merge ( "notgrouplike { b: 3 }" , message ) ;
assertThat ( message . build ( ) )
. isEqualTo (
TestDelimited . newBuilder ( )
. setNotgrouplike ( TestDelimited . GroupLike . newBuilder ( ) . setB ( 3 ) . build ( ) )
. build ( ) ) ;
}
@Test
public void testParseDelimitedInvalid ( ) throws Exception {
TestDelimited . Builder message = TestDelimited . newBuilder ( ) ;
try {
TextFormat . merge ( "NotGroupLike { a: 3 }" , message ) ;
assertWithMessage ( "Expected parse exception." ) . fail ( ) ;
} catch ( TextFormat . ParseException e ) {
assertThat ( e )
. hasMessageThat ( )
. isEqualTo (
"1:1: Input contains unknown fields and/or extensions:\n"
+ "1:1:\teditions_unittest.TestDelimited.NotGroupLike" ) ;
}
}
@Test
public void testParseDelimitedInvalidScope ( ) throws Exception {
TestDelimited . Builder message = TestDelimited . newBuilder ( ) ;
try {
TextFormat . merge ( "NotGroupLikeScope { a: 3 }" , message ) ;
assertWithMessage ( "Expected parse exception." ) . fail ( ) ;
} catch ( TextFormat . ParseException e ) {
assertThat ( e )
. hasMessageThat ( )
. isEqualTo (
"1:1: Input contains unknown fields and/or extensions:\n"
+ "1:1:\teditions_unittest.TestDelimited.NotGroupLikeScope" ) ;
}
}
// =======================================================================
// test oneof