@ -30,8 +30,10 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# endregion
using System ;
using Google.Protobuf.TestProtos ;
using NUnit.Framework ;
using System.Collections ;
namespace Google.Protobuf.WellKnownTypes
{
@ -52,6 +54,36 @@ namespace Google.Protobuf.WellKnownTypes
Assert . IsNull ( message . Uint64Field ) ;
}
[Test]
public void NonDefaultSingleValues ( )
{
var message = new TestWellKnownTypes
{
StringField = "x" ,
BytesField = ByteString . CopyFrom ( 1 , 2 , 3 ) ,
BoolField = true ,
FloatField = 1 2.5f ,
DoubleField = 1 2.25d ,
Int32Field = 1 ,
Int64Field = 2 ,
Uint32Field = 3 ,
Uint64Field = 4
} ;
var bytes = message . ToByteArray ( ) ;
var parsed = TestWellKnownTypes . Parser . ParseFrom ( bytes ) ;
Assert . AreEqual ( "x" , parsed . StringField ) ;
Assert . AreEqual ( ByteString . CopyFrom ( 1 , 2 , 3 ) , parsed . BytesField ) ;
Assert . AreEqual ( true , parsed . BoolField ) ;
Assert . AreEqual ( 1 2.5f , parsed . FloatField ) ;
Assert . AreEqual ( 1 2.25d , parsed . DoubleField ) ;
Assert . AreEqual ( 1 , parsed . Int32Field ) ;
Assert . AreEqual ( 2L , parsed . Int64Field ) ;
Assert . AreEqual ( 3 U , parsed . Uint32Field ) ;
Assert . AreEqual ( 4 UL , parsed . Uint64Field ) ;
}
[Test]
public void NonNullDefaultIsPreservedThroughSerialization ( )
{
@ -71,15 +103,152 @@ namespace Google.Protobuf.WellKnownTypes
var bytes = message . ToByteArray ( ) ;
var parsed = TestWellKnownTypes . Parser . ParseFrom ( bytes ) ;
Assert . AreEqual ( "" , message . StringField ) ;
Assert . AreEqual ( ByteString . Empty , message . BytesField ) ;
Assert . AreEqual ( false , message . BoolField ) ;
Assert . AreEqual ( 0f , message . FloatField ) ;
Assert . AreEqual ( 0d , message . DoubleField ) ;
Assert . AreEqual ( 0 , message . Int32Field ) ;
Assert . AreEqual ( 0L , message . Int64Field ) ;
Assert . AreEqual ( 0 U , message . Uint32Field ) ;
Assert . AreEqual ( 0 UL , message . Uint64Field ) ;
Assert . AreEqual ( "" , parsed . StringField ) ;
Assert . AreEqual ( ByteString . Empty , parsed . BytesField ) ;
Assert . AreEqual ( false , parsed . BoolField ) ;
Assert . AreEqual ( 0f , parsed . FloatField ) ;
Assert . AreEqual ( 0d , parsed . DoubleField ) ;
Assert . AreEqual ( 0 , parsed . Int32Field ) ;
Assert . AreEqual ( 0L , parsed . Int64Field ) ;
Assert . AreEqual ( 0 U , parsed . Uint32Field ) ;
Assert . AreEqual ( 0 UL , parsed . Uint64Field ) ;
}
[Test]
public void RepeatedWrappersProhibitNullItems ( )
{
var message = new RepeatedWellKnownTypes ( ) ;
Assert . Throws < ArgumentNullException > ( ( ) = > message . BoolField . Add ( ( bool? ) null ) ) ;
Assert . Throws < ArgumentNullException > ( ( ) = > message . Int32Field . Add ( ( int? ) null ) ) ;
Assert . Throws < ArgumentNullException > ( ( ) = > message . StringField . Add ( ( string ) null ) ) ;
Assert . Throws < ArgumentNullException > ( ( ) = > message . BytesField . Add ( ( ByteString ) null ) ) ;
}
[Test]
public void RepeatedWrappersSerializeDeserialize ( )
{
var message = new RepeatedWellKnownTypes
{
BoolField = { true , false } ,
BytesField = { ByteString . CopyFrom ( 1 , 2 , 3 ) , ByteString . CopyFrom ( 4 , 5 , 6 ) , ByteString . Empty } ,
DoubleField = { 1 2.5 , - 1.5 , 0d } ,
FloatField = { 1 2 3.25f , - 2 0f , 0f } ,
Int32Field = { int . MaxValue , int . MinValue , 0 } ,
Int64Field = { long . MaxValue , long . MinValue , 0L } ,
StringField = { "First" , "Second" , "" } ,
Uint32Field = { uint . MaxValue , uint . MinValue , 0 U } ,
Uint64Field = { ulong . MaxValue , ulong . MinValue , 0 UL } ,
} ;
var bytes = message . ToByteArray ( ) ;
var parsed = RepeatedWellKnownTypes . Parser . ParseFrom ( bytes ) ;
Assert . AreEqual ( message , parsed ) ;
// Just to test a single value for sanity...
Assert . AreEqual ( "Second" , message . StringField [ 1 ] ) ;
}
[Test]
public void MapWrappersSerializeDeserialize ( )
{
var message = new MapWellKnownTypes
{
BoolField = { { 1 0 , false } , { 2 0 , true } } ,
BytesField = {
{ - 1 , ByteString . CopyFrom ( 1 , 2 , 3 ) } ,
{ 1 0 , ByteString . CopyFrom ( 4 , 5 , 6 ) } ,
{ 1 0 0 0 , ByteString . Empty } ,
{ 1 0 0 0 0 , null }
} ,
DoubleField = { { 1 , 1 2.5 } , { 1 0 , - 1.5 } , { 2 0 , 0d } } ,
FloatField = { { 2 , 1 2 3.25f } , { 3 , - 2 0f } , { 4 , 0f } } ,
Int32Field = { { 5 , int . MaxValue } , { 6 , int . MinValue } , { 7 , 0 } } ,
Int64Field = { { 8 , long . MaxValue } , { 9 , long . MinValue } , { 1 0 , 0L } } ,
StringField = { { 1 1 , "First" } , { 1 2 , "Second" } , { 1 3 , "" } , { 1 4 , null } } ,
Uint32Field = { { 1 5 , uint . MaxValue } , { 1 6 , uint . MinValue } , { 1 7 , 0 U } } ,
Uint64Field = { { 1 8 , ulong . MaxValue } , { 1 9 , ulong . MinValue } , { 2 0 , 0 UL } } ,
} ;
}
[Test]
public void Reflection_SingleValues ( )
{
var message = new TestWellKnownTypes
{
StringField = "x" ,
BytesField = ByteString . CopyFrom ( 1 , 2 , 3 ) ,
BoolField = true ,
FloatField = 1 2.5f ,
DoubleField = 1 2.25d ,
Int32Field = 1 ,
Int64Field = 2 ,
Uint32Field = 3 ,
Uint64Field = 4
} ;
var fields = ( ( IReflectedMessage ) message ) . Fields ;
Assert . AreEqual ( "x" , fields [ TestWellKnownTypes . StringFieldFieldNumber ] . GetValue ( message ) ) ;
Assert . AreEqual ( ByteString . CopyFrom ( 1 , 2 , 3 ) , fields [ TestWellKnownTypes . BytesFieldFieldNumber ] . GetValue ( message ) ) ;
Assert . AreEqual ( true , fields [ TestWellKnownTypes . BoolFieldFieldNumber ] . GetValue ( message ) ) ;
Assert . AreEqual ( 1 2.5f , fields [ TestWellKnownTypes . FloatFieldFieldNumber ] . GetValue ( message ) ) ;
Assert . AreEqual ( 1 2.25d , fields [ TestWellKnownTypes . DoubleFieldFieldNumber ] . GetValue ( message ) ) ;
Assert . AreEqual ( 1 , fields [ TestWellKnownTypes . Int32FieldFieldNumber ] . GetValue ( message ) ) ;
Assert . AreEqual ( 2L , fields [ TestWellKnownTypes . Int64FieldFieldNumber ] . GetValue ( message ) ) ;
Assert . AreEqual ( 3 U , fields [ TestWellKnownTypes . Uint32FieldFieldNumber ] . GetValue ( message ) ) ;
Assert . AreEqual ( 4 UL , fields [ TestWellKnownTypes . Uint64FieldFieldNumber ] . GetValue ( message ) ) ;
// And a couple of null fields...
message . StringField = null ;
message . FloatField = null ;
Assert . IsNull ( fields [ TestWellKnownTypes . StringFieldFieldNumber ] . GetValue ( message ) ) ;
Assert . IsNull ( fields [ TestWellKnownTypes . FloatFieldFieldNumber ] . GetValue ( message ) ) ;
}
[Test]
public void Reflection_RepeatedFields ( )
{
// Just a single example... note that we can't have a null value here
var message = new RepeatedWellKnownTypes { Int32Field = { 1 , 2 } } ;
var fields = ( ( IReflectedMessage ) message ) . Fields ;
var list = ( IList ) fields [ RepeatedWellKnownTypes . Int32FieldFieldNumber ] . GetValue ( message ) ;
CollectionAssert . AreEqual ( new [ ] { 1 , 2 } , list ) ;
}
[Test]
public void Reflection_MapFields ( )
{
// Just a single example... note that we can't have a null value here
var message = new MapWellKnownTypes { Int32Field = { { 1 , 2 } , { 3 , null } } } ;
var fields = ( ( IReflectedMessage ) message ) . Fields ;
var dictionary = ( IDictionary ) fields [ MapWellKnownTypes . Int32FieldFieldNumber ] . GetValue ( message ) ;
Assert . AreEqual ( 2 , dictionary [ 1 ] ) ;
Assert . IsNull ( dictionary [ 3 ] ) ;
Assert . IsTrue ( dictionary . Contains ( 3 ) ) ;
}
// Merging is odd with wrapper types, due to the way that default values aren't emitted in
// the binary stream. In fact we cheat a little bit - a message with an explicitly present default
// value will have that default value ignored.
[Test]
[TestCase("x", "y", "y")]
[TestCase("x", "", "x")]
[TestCase("x", null, "x")]
[TestCase("", "y", "y")]
[TestCase("", "", "")]
[TestCase("", null, "")]
[TestCase(null, "y", "y")]
[TestCase(null, "", "")]
[TestCase(null, null, null)]
public void Merging ( string original , string merged , string expected )
{
var originalMessage = new TestWellKnownTypes { StringField = original } ;
var mergingMessage = new TestWellKnownTypes { StringField = merged } ;
originalMessage . MergeFrom ( mergingMessage ) ;
Assert . AreEqual ( expected , originalMessage . StringField ) ;
// Try it using MergeFrom(CodedInputStream) too...
originalMessage = new TestWellKnownTypes { StringField = original } ;
originalMessage . MergeFrom ( mergingMessage . ToByteArray ( ) ) ;
Assert . AreEqual ( expected , originalMessage . StringField ) ;
}
}
}