Commit 7d396f9d authored by csharptest's avatar csharptest Committed by unknown

Testing and related fixes

parent 487da48a
......@@ -150,7 +150,8 @@
<arg file="${protos-dir}/google/protobuf/unittest_lite.proto" />
<arg file="${protos-dir}/google/protobuf/unittest_lite_imports_nonlite.proto" />
<arg file="${protos-dir}/google/protobuf/unittest_no_generic_services.proto" />
<arg file="${protos-dir}/extest/unittest_extras_lite.proto" />
<arg file="${protos-dir}/tutorial/addressbook.proto" />
</exec>
......@@ -178,7 +179,6 @@
<include name="UnitTestEmptyProtoFile.cs" />
<include name="UnitTestEnormousDescriptorProtoFile.cs" />
<include name="UnitTestImportProtoFile.cs" />
<include name="UnitTestLiteImportNonLiteProtoFile.cs" />
<include name="UnitTestMessageSetProtoFile.cs" />
<include name="UnitTestNoGenericServicesProtoFile.cs" />
<include name="UnitTestOptimizeForProtoFile.cs" />
......@@ -188,8 +188,12 @@
<copy todir="${src}/ProtocolBuffersLite.Test/TestProtos">
<fileset basedir="${tmp-dir}">
<include name="UnitTestExtrasLiteProtoFile.cs" />
<include name="UnitTestImportLiteProtoFile.cs" />
<include name="UnitTestImportProtoFile.cs" />
<include name="UnitTestLiteImportNonLiteProtoFile.cs" />
<include name="UnitTestLiteProtoFile.cs" />
<include name="UnitTestProtoFile.cs" />
</fileset>
</copy>
......@@ -273,6 +277,8 @@
<nunit2>
<formatter type="Plain" />
<test assemblyname="${src}/ProtocolBuffers.Test/bin/${build-configuration}/Google.ProtocolBuffers.Test.dll" />
<test assemblyname="${src}/ProtocolBuffersLite.Test/bin/${build-configuration}/Google.ProtocolBuffersLite.Test.dll" />
<test assemblyname="${src}/ProtocolBuffersLite.Test/bin/${build-configuration}/Google.ProtocolBuffersMixedLite.Test.dll" />
<test assemblyname="${src}/Protogen.Test/bin/${build-configuration}/Google.ProtocolBuffers.ProtoGen.Test.dll" />
</nunit2>
</target>
......
// Additional options required for C# generation. File from copyright
// line onwards is as per original distribution.
import "google/protobuf/csharp_options.proto";
option (google.protobuf.csharp_file_options).namespace = "Google.ProtocolBuffers.TestProtos";
option (google.protobuf.csharp_file_options).umbrella_classname = "UnitTestExtrasLiteProtoFile";
package protobuf_unittest_extra;
option optimize_for = LITE_RUNTIME;
option java_package = "com.google.protobuf";
message TestRequiredLite {
required int32 d = 1;
required ExtraEnum en = 2 [default = DEFAULT];
}
enum ExtraEnum {
DEFAULT = 1;
EXLITE_FOO = 7;
EXLITE_BAR = 8;
EXLITE_BAZ = 9;
}
......@@ -99,7 +99,7 @@ namespace Google.ProtocolBuffers.ProtoGen {
writer.WriteLine("{0},", Descriptor.HasDefaultValue ? DefaultValue : IsNullableType ? "null" : "default(" + type + ")");
writer.WriteLine("{0},", (Descriptor.MappedType == MappedType.Message) ? type + ".DefaultInstance" : "null");
writer.WriteLine("{0},", (Descriptor.MappedType == MappedType.Enum) ? "new EnumLiteMap<" + type + ">()" : "null");
writer.WriteLine("{0},", Descriptor.Index);
writer.WriteLine("{0}.{1}FieldNumber,", scope, name);
writer.Write("pbd::FieldType.{0}", Descriptor.FieldType);
if (Descriptor.IsRepeated) {
writer.WriteLine(",");
......
This diff is collapsed.
......@@ -63,6 +63,7 @@
<Compile Include="DescriptorsTest.cs" />
<Compile Include="Descriptors\MessageDescriptorTest.cs" />
<Compile Include="DynamicMessageTest.cs" />
<Compile Include="ExtendableMessageTest.cs" />
<Compile Include="GeneratedMessageTest.cs" />
<Compile Include="MessageStreamIteratorTest.cs" />
<Compile Include="MessageStreamWriterTest.cs" />
......@@ -80,7 +81,6 @@
<Compile Include="TestProtos\UnitTestEnormousDescriptorProtoFile.cs" />
<Compile Include="TestProtos\UnitTestImportLiteProtoFile.cs" />
<Compile Include="TestProtos\UnitTestImportProtoFile.cs" />
<Compile Include="TestProtos\UnitTestLiteImportNonLiteProtoFile.cs" />
<Compile Include="TestProtos\UnitTestMessageSetProtoFile.cs" />
<Compile Include="TestProtos\UnitTestNoGenericServicesProtoFile.cs" />
<Compile Include="TestProtos\UnitTestOptimizeForProtoFile.cs" />
......
......@@ -5,7 +5,7 @@ using System.Text;
namespace Google.ProtocolBuffers.TestProtos {
public sealed partial class TestExtremeDefaultValues {
//These values are not currently handled by the generator...
#warning ToDo - These values are not currently handled by the generator...
const double InfinityD = double.PositiveInfinity;
const double NaND = double.NaN;
const float InfinityF = float.PositiveInfinity;
......
......@@ -38,6 +38,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "unittest", "unittest", "{C8
..\protos\google\protobuf\unittest_embed_optimize_for.proto = ..\protos\google\protobuf\unittest_embed_optimize_for.proto
..\protos\google\protobuf\unittest_empty.proto = ..\protos\google\protobuf\unittest_empty.proto
..\protos\google\protobuf\unittest_enormous_descriptor.proto = ..\protos\google\protobuf\unittest_enormous_descriptor.proto
..\protos\extest\unittest_extras_lite.proto = ..\protos\extest\unittest_extras_lite.proto
..\protos\google\protobuf\unittest_import.proto = ..\protos\google\protobuf\unittest_import.proto
..\protos\google\protobuf\unittest_import_lite.proto = ..\protos\google\protobuf\unittest_import_lite.proto
..\protos\google\protobuf\unittest_lite.proto = ..\protos\google\protobuf\unittest_lite.proto
......@@ -47,6 +48,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "unittest", "unittest", "{C8
..\protos\google\protobuf\unittest_optimize_for.proto = ..\protos\google\protobuf\unittest_optimize_for.proto
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProtocolBuffersLiteMixed.Test", "ProtocolBuffersLite.Test\ProtocolBuffersLiteMixed.Test.csproj", "{EEFFED24-3750-4567-9A23-1DB676A15610}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -114,6 +117,12 @@ Global
{EE01ED24-3750-4567-9A23-1DB676A15610}.Release|Any CPU.Build.0 = Release|Any CPU
{EE01ED24-3750-4567-9A23-1DB676A15610}.Silverlight2|Any CPU.ActiveCfg = Silverlight2|Any CPU
{EE01ED24-3750-4567-9A23-1DB676A15610}.Silverlight2|Any CPU.Build.0 = Silverlight2|Any CPU
{EEFFED24-3750-4567-9A23-1DB676A15610}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEFFED24-3750-4567-9A23-1DB676A15610}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEFFED24-3750-4567-9A23-1DB676A15610}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEFFED24-3750-4567-9A23-1DB676A15610}.Release|Any CPU.Build.0 = Release|Any CPU
{EEFFED24-3750-4567-9A23-1DB676A15610}.Silverlight2|Any CPU.ActiveCfg = Silverlight2|Any CPU
{EEFFED24-3750-4567-9A23-1DB676A15610}.Silverlight2|Any CPU.Build.0 = Silverlight2|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
......@@ -72,7 +72,10 @@ namespace Google.ProtocolBuffers {
}
public sealed override TBuilder MergeFrom(IMessageLite other) {
return MergeFrom((IMessage)other);
if (other is IMessage) {
return MergeFrom((IMessage) other);
}
throw new ArgumentException("MergeFrom(Message) can only merge messages of the same type.");
}
/// <summary>
......@@ -112,13 +115,13 @@ namespace Google.ProtocolBuffers {
}
} else if (field.MappedType == MappedType.Message) {
// Merge singular messages
IMessage existingValue = (IMessage) this[field];
IMessageLite existingValue = (IMessageLite)this[field];
if (existingValue == existingValue.WeakDefaultInstanceForType) {
this[field] = entry.Value;
} else {
this[field] = existingValue.WeakCreateBuilderForType()
.WeakMergeFrom(existingValue)
.WeakMergeFrom((IMessage) entry.Value)
.WeakMergeFrom((IMessageLite)entry.Value)
.WeakBuild();
}
} else {
......
......@@ -81,13 +81,13 @@ namespace Google.ProtocolBuffers {
if (field.IsRepeated) {
// We know it's an IList<T>, but not the exact type - so
// IEnumerable is the best we can do. (C# generics aren't covariant yet.)
foreach (IMessage element in (IEnumerable) entry.Value) {
foreach (IMessageLite element in (IEnumerable)entry.Value) {
if (!element.IsInitialized) {
return false;
}
}
} else {
if (!((IMessage)entry.Value).IsInitialized) {
if (!((IMessageLite)entry.Value).IsInitialized) {
return false;
}
}
......
......@@ -30,6 +30,8 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Collections.Generic;
using Google.ProtocolBuffers.Collections;
namespace Google.ProtocolBuffers.Descriptors {
......@@ -46,5 +48,29 @@ namespace Google.ProtocolBuffers.Descriptors {
internal MappedType MappedType { get; private set; }
internal WireFormat.WireType WireType { get; private set; }
/// <summary>
/// Immutable mapping from field type to mapped type. Built using the attributes on
/// FieldType values.
/// </summary>
static readonly IDictionary<FieldType, FieldMappingAttribute> FieldTypeToMappedTypeMap = MapFieldTypes();
private static IDictionary<FieldType, FieldMappingAttribute> MapFieldTypes() {
var map = new Dictionary<FieldType, FieldMappingAttribute>();
foreach (System.Reflection.FieldInfo field in typeof(FieldType).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)) {
FieldType fieldType = (FieldType)field.GetValue(null);
FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0];
map[fieldType] = mapping;
}
return Dictionaries.AsReadOnly(map);
}
internal static MappedType MappedTypeFromFieldType(FieldType type) {
return FieldTypeToMappedTypeMap[type].MappedType;
}
internal static WireFormat.WireType WireTypeFromFieldType(FieldType type, bool packed) {
return packed ? WireFormat.WireType.LengthDelimited : FieldTypeToMappedTypeMap[type].WireType;
}
}
}
......@@ -295,7 +295,8 @@ namespace Google.ProtocolBuffers {
}
public override Builder MergeFrom(DynamicMessage other) {
return MergeFrom((IMessage)other);
IMessage downcast = other;
return MergeFrom(downcast);
}
public override DynamicMessage Build() {
......
......@@ -68,16 +68,13 @@ namespace Google.ProtocolBuffers {
public class EnumLiteMap<TEnum> : IEnumLiteMap<IEnumLite>
where TEnum : struct, IComparable, IFormattable {
struct EnumValue : IEnumLite, IComparable<IEnumLite> {
int _value;
struct EnumValue : IEnumLite {
readonly int value;
public EnumValue(int value) {
_value = value;
this.value = value;
}
int IEnumLite.Number {
get { return _value; }
}
int IComparable<IEnumLite>.CompareTo(IEnumLite other) {
return _value.CompareTo(other.Number);
get { return value; }
}
}
......
......@@ -94,21 +94,21 @@ namespace Google.ProtocolBuffers {
/// <summary>
/// Appends a value to a repeated extension.
/// </summary>
public ExtendableBuilder<TMessage, TBuilder> AddExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, TExtension value) {
public TBuilder AddExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, TExtension value) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyExtensionContainingType(extension);
message.Extensions.AddRepeatedField(extension.Descriptor, extension.SingularToReflectionType(value));
return this;
return ThisBuilder;
}
/// <summary>
/// Clears an extension.
/// </summary>
public ExtendableBuilder<TMessage, TBuilder> ClearExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
public TBuilder ClearExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyExtensionContainingType(extension);
message.Extensions.ClearField(extension.Descriptor);
return this;
return ThisBuilder;
}
/// <summary>
......
......@@ -34,6 +34,7 @@
using System;
using System.Collections.Generic;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers {
public abstract class ExtendableBuilderLite<TMessage, TBuilder> : GeneratedBuilderLite<TMessage, TBuilder>
......@@ -93,21 +94,21 @@ namespace Google.ProtocolBuffers {
/// <summary>
/// Appends a value to a repeated extension.
/// </summary>
public ExtendableBuilderLite<TMessage, TBuilder> AddExtension<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension, TExtension value) {
public TBuilder AddExtension<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension, TExtension value) {
ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyExtensionContainingType(extension);
message.Extensions.AddRepeatedField(extension.Descriptor, extension.SingularToReflectionType(value));
return this;
return ThisBuilder;
}
/// <summary>
/// Clears an extension.
/// </summary>
public ExtendableBuilderLite<TMessage, TBuilder> ClearExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension) {
public TBuilder ClearExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension) {
ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
message.VerifyExtensionContainingType(extension);
message.Extensions.ClearField(extension.Descriptor);
return this;
return ThisBuilder;
}
/// <summary>
......@@ -117,11 +118,99 @@ namespace Google.ProtocolBuffers {
[CLSCompliant(false)]
protected override bool ParseUnknownField(CodedInputStream input,
ExtensionRegistry extensionRegistry, uint tag) {
return input.SkipField(tag);
FieldSet extensions = MessageBeingBuilt.Extensions;
WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
int fieldNumber = WireFormat.GetTagFieldNumber(tag);
IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber];
bool unknown = false;
bool packed = false;
if (extension == null) {
unknown = true; // Unknown field.
} else if (wireType == FieldMappingAttribute.WireTypeFromFieldType(extension.Descriptor.FieldType, false /* isPacked */)) {
packed = false; // Normal, unpacked value.
} else if (extension.Descriptor.IsRepeated &&
//?? just returns true ?? extension.Descriptor.type.isPackable() &&
wireType == FieldMappingAttribute.WireTypeFromFieldType(extension.Descriptor.FieldType, true /* isPacked */)) {
packed = true; // Packed value.
} else {
unknown = true; // Wrong wire type.
}
if (unknown) { // Unknown field or wrong wire type. Skip.
return input.SkipField(tag);
}
if (packed) {
int length = (int)Math.Min(int.MaxValue, input.ReadRawVarint32());
int limit = input.PushLimit(length);
if (extension.Descriptor.FieldType == FieldType.Enum) {
while (!input.ReachedLimit) {
int rawValue = input.ReadEnum();
Object value =
extension.Descriptor.EnumType.FindValueByNumber(rawValue);
if (value == null) {
// If the number isn't recognized as a valid value for this
// enum, drop it (don't even add it to unknownFields).
return true;
}
extensions.AddRepeatedField(extension.Descriptor, value);
}
} else {
while (!input.ReachedLimit) {
Object value = input.ReadPrimitiveField(extension.Descriptor.FieldType);
extensions.AddRepeatedField(extension.Descriptor, value);
}
}
input.PopLimit(limit);
} else {
Object value;
switch (extension.Descriptor.MappedType) {
case MappedType.Message: {
IBuilderLite subBuilder = null;
if (!extension.Descriptor.IsRepeated) {
IMessageLite existingValue = extensions[extension.Descriptor] as IMessageLite;
if (existingValue != null) {
subBuilder = existingValue.WeakToBuilder();
}
}
if (subBuilder == null) {
subBuilder = extension.MessageDefaultInstance.WeakCreateBuilderForType();
}
if (extension.Descriptor.FieldType == FieldType.Group) {
input.ReadGroup(extension.Number, subBuilder, extensionRegistry);
} else {
input.ReadMessage(subBuilder, extensionRegistry);
}
value = subBuilder.WeakBuild();
break;
}
case MappedType.Enum:
int rawValue = input.ReadEnum();
value = extension.Descriptor.EnumType.FindValueByNumber(rawValue);
// If the number isn't recognized as a valid value for this enum,
// drop it.
if (value == null) {
return true;
}
break;
default:
value = input.ReadPrimitiveField(extension.Descriptor.FieldType);
break;
}
if (extension.Descriptor.IsRepeated) {
extensions.AddRepeatedField(extension.Descriptor, value);
} else {
extensions[extension.Descriptor] = value;
}
}
return true;
}
// ---------------------------------------------------------------
// Reflection
#region Reflection
public object this[IFieldDescriptorLite field, int index] {
set {
......@@ -168,5 +257,6 @@ namespace Google.ProtocolBuffers {
protected void MergeExtensionFields(ExtendableMessageLite<TMessage, TBuilder> other) {
MessageBeingBuilt.Extensions.MergeFrom(other.Extensions);
}
#endregion
}
}
......@@ -42,7 +42,9 @@ namespace Google.ProtocolBuffers
/// </summary>
public FieldDescriptor Descriptor { get; private set; }
/// <summary>
IFieldDescriptorLite IGeneratedExtensionLite.Descriptor { get { return Descriptor; } }
/// <summary>
/// A default instance of the extensions's type, if it has a message type,
/// or null otherwise.
/// </summary>
......
......@@ -71,7 +71,7 @@ namespace Google.ProtocolBuffers.FieldAccess {
}
// No... so let's create a builder of the right type, and merge the value in.
IMessage message = (IMessage) value;
IMessageLite message = (IMessageLite) value;
return CreateBuilder().WeakMergeFrom(message).WeakBuild();
}
......
......@@ -67,7 +67,7 @@ namespace Google.ProtocolBuffers.FieldAccess {
}
// No... so let's create a builder of the right type, and merge the value in.
IMessage message = (IMessage) value;
IMessageLite message = (IMessageLite) value;
return CreateBuilder().WeakMergeFrom(message).WeakBuild();
}
......
......@@ -486,7 +486,9 @@ namespace Google.ProtocolBuffers {
IMessageLite messageValue = value as IMessageLite;
isValid = messageValue != null;
#if !LITE
isValid = isValid && ((IMessage)messageValue).DescriptorForType == ((Google.ProtocolBuffers.Descriptors.FieldDescriptor)field).MessageType;
if (isValid && messageValue is IMessage && field is FieldDescriptor) {
isValid = ((IMessage) messageValue).DescriptorForType == ((FieldDescriptor) field).MessageType;
}
#endif
break;
}
......
......@@ -148,10 +148,10 @@ namespace Google.ProtocolBuffers {
}
} else if (field.MappedType == MappedType.Message && HasField(field)) {
// Merge singular embedded messages
IMessage oldValue = (IMessage)this[field];
IMessageLite oldValue = (IMessageLite)this[field];
this[field] = oldValue.WeakCreateBuilderForType()
.WeakMergeFrom(oldValue)
.WeakMergeFrom((IMessage)entry.Value)
.WeakMergeFrom((IMessageLite)entry.Value)
.WeakBuildPartial();
} else {
// Just overwrite
......
......@@ -80,6 +80,7 @@ namespace Google.ProtocolBuffers {
if (defaultInstanceProperty == null) {
throw new ArgumentException("No public static DefaultInstance property for type " + typeof(TExtension).Name);
}
#warning ToDo - Invalid cast, could be IMessageLite
messageDefaultInstance = (IMessage)defaultInstanceProperty.GetValue(null, null);
}
}
......@@ -112,7 +113,7 @@ namespace Google.ProtocolBuffers {
// This should not happen in normal use. But, to be nice, we'll
// copy the message to whatever type the caller was expecting.
return MessageDefaultInstance.WeakCreateBuilderForType()
.WeakMergeFrom((IMessage)value).WeakBuild();
.WeakMergeFrom((IMessageLite)value).WeakBuild();
}
case MappedType.Enum:
// Just return a boxed int - that can be unboxed to the enum
......
......@@ -44,25 +44,10 @@ namespace Google.ProtocolBuffers {
int Number { get; }
object ContainingType { get; }
IMessageLite MessageDefaultInstance { get; }
IFieldDescriptorLite Descriptor { get; }
}
public class ExtensionDescriptorLite : IFieldDescriptorLite {
/// <summary>
/// Immutable mapping from field type to mapped type. Built using the attributes on
/// FieldType values.
/// </summary>
public static readonly IDictionary<FieldType, MappedType> FieldTypeToMappedTypeMap = MapFieldTypes();
private static IDictionary<FieldType, MappedType> MapFieldTypes() {
var map = new Dictionary<FieldType, MappedType>();
foreach (System.Reflection.FieldInfo field in typeof(FieldType).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)) {
FieldType fieldType = (FieldType)field.GetValue(null);
FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0];
map[fieldType] = mapping.MappedType;
}
return Dictionaries.AsReadOnly(map);
}
private readonly IEnumLiteMap enumTypeMap;
private readonly int number;
private readonly FieldType type;
......@@ -75,7 +60,7 @@ namespace Google.ProtocolBuffers {
this.enumTypeMap = enumTypeMap;
this.number = number;
this.type = type;
this.mapType = FieldTypeToMappedTypeMap[type];
this.mapType = FieldMappingAttribute.MappedTypeFromFieldType(type);
this.isRepeated = isRepeated;
this.isPacked = isPacked;
this.defaultValue = defaultValue;
......@@ -99,7 +84,7 @@ namespace Google.ProtocolBuffers {
#warning ToDo - Discover the meaning and purpose of this durring serialization and return the correct value
public bool MessageSetWireFormat {
get { return true; }
get { return false; }
}
public int FieldNumber {
......@@ -135,11 +120,11 @@ namespace Google.ProtocolBuffers {
}
public override object ToReflectionType(object value) {
IList<TExtensionType> result = new List<TExtensionType>();
foreach (object element in (IEnumerable)value) {
result.Add((TExtensionType)SingularToReflectionType(element));
}
return result;
IList<object> result = new List<object>();
foreach (object element in (IEnumerable) value) {
result.Add(SingularToReflectionType(element));
}
return result;
}
public override object FromReflectionType(object value) {
......@@ -191,6 +176,7 @@ namespace Google.ProtocolBuffers {
false /* isRepeated */, false /* isPacked */)) {
}
private static readonly IList<object> Empty = new object[0];
/** Repeating fields: For use by generated code only. */
protected GeneratedExtensionLite(
TContainingType containingTypeDefaultInstance,
......@@ -201,7 +187,7 @@ namespace Google.ProtocolBuffers {
FieldType type,
bool isPacked)
: this(containingTypeDefaultInstance, defaultValue, messageDefaultInstance,
new ExtensionDescriptorLite(enumTypeMap, number, type, defaultValue,
new ExtensionDescriptorLite(enumTypeMap, number, type, Empty,
true /* isRepeated */, isPacked)) {
}
......
......@@ -99,13 +99,13 @@ namespace Google.ProtocolBuffers {
if (field.IsRepeated) {
// We know it's an IList<T>, but not the exact type - so
// IEnumerable is the best we can do. (C# generics aren't covariant yet.)
foreach (IMessage element in (IEnumerable) this[field]) {
foreach (IMessageLite element in (IEnumerable) this[field]) {
if (!element.IsInitialized) {
return false;
}
}
} else {
if (HasField(field) && !((IMessage) this[field]).IsInitialized) {
if (HasField(field) && !((IMessageLite) this[field]).IsInitialized) {
return false;
}
}
......
......@@ -176,7 +176,11 @@ namespace Google.ProtocolBuffers {
case FieldType.Message:
case FieldType.Group:
Print((IMessage) value, generator);
if (value is IMessage) {
Print((IMessage)value, generator);
} else {
#warning ToDo - What do we print for IMessageLite?
}
break;
}
}
......
......@@ -124,7 +124,7 @@ namespace Google.ProtocolBuffers {
foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields) {
FieldDescriptor field = entry.Key;
object value = entry.Value;
#warning ToDo - bad assumption, could be IMessageLite
if (field.MappedType == MappedType.Message) {
if (field.IsRepeated) {
int i = 0;
......
......@@ -578,14 +578,14 @@ namespace Google.ProtocolBuffers {
switch (field.FieldType) {
case FieldType.Group:
case FieldType.Message: {
IBuilder subBuilder;
IBuilderLite subBuilder;
if (defaultFieldInstance != null) {
subBuilder = defaultFieldInstance.WeakCreateBuilderForType();
} else {
subBuilder = builder.CreateBuilderForField(field);
}
if (!field.IsRepeated) {
subBuilder.WeakMergeFrom((IMessage)builder[field]);
subBuilder.WeakMergeFrom((IMessageLite)builder[field]);
}
if (field.FieldType == FieldType.Group) {
input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
......@@ -661,7 +661,7 @@ namespace Google.ProtocolBuffers {
if (extension != null) {
field = extension.Descriptor;
subBuilder = extension.DefaultInstance.WeakCreateBuilderForType();
IMessage originalMessage = (IMessage)builder[field];
IMessageLite originalMessage = (IMessageLite)builder[field];
if (originalMessage != null) {
subBuilder.WeakMergeFrom(originalMessage);
}
......
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using Google.ProtocolBuffers;
using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework;
namespace Google.ProtocolBuffers {
[TestFixture]
public class AbstractBuilderLiteTest {
[Test]
public void TestMergeFromCodedInputStream() {
TestAllTypesLite copy, msg = TestAllTypesLite.CreateBuilder()
.SetOptionalUint32(uint.MaxValue).Build();
copy = TestAllTypesLite.DefaultInstance;
Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
using (MemoryStream ms = new MemoryStream(msg.ToByteArray())) {
CodedInputStream ci = CodedInputStream.CreateInstance(ms);
copy = copy.ToBuilder().MergeFrom(ci).Build();
}
Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
}
[Test]
public void TestIBuilderLiteWeakClear() {
TestAllTypesLite copy, msg = TestAllTypesLite.DefaultInstance;
copy = msg.ToBuilder().SetOptionalString("Should be removed.").Build();
Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakClear().WeakBuild();
Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
}
[Test]
public void TestIBuilderLiteWeakMergeFromByteString() {
TestAllTypesLite copy, msg = TestAllTypesLite.CreateBuilder()
.SetOptionalString("Should be merged.").Build();
copy = TestAllTypesLite.DefaultInstance;
Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString()).WeakBuild();
Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
//again with extension registry
copy = TestAllTypesLite.DefaultInstance;
Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString(), ExtensionRegistry.Empty).WeakBuild();
Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
}
[Test]
public void TestIBuilderLiteWeakMergeFromCodedInputStream() {
TestAllTypesLite copy, msg = TestAllTypesLite.CreateBuilder()
.SetOptionalUint32(uint.MaxValue).Build();
copy = TestAllTypesLite.DefaultInstance;
Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
using (MemoryStream ms = new MemoryStream(msg.ToByteArray())) {
CodedInputStream ci = CodedInputStream.CreateInstance(ms);
copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(ci).WeakBuild();
}
Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
}
[Test]
public void TestIBuilderLiteWeakBuildPartial() {
IBuilderLite builder = TestRequiredLite.CreateBuilder();
Assert.IsFalse(builder.IsInitialized);
IMessageLite msg = builder.WeakBuildPartial();
Assert.IsFalse(msg.IsInitialized);
Assert.AreEqual(msg.ToByteArray(), TestRequiredLite.DefaultInstance.ToByteArray());
}
[Test, ExpectedException(typeof(UninitializedMessageException ))]
public void TestIBuilderLiteWeakBuildUninitialized() {
IBuilderLite builder = TestRequiredLite.CreateBuilder();
Assert.IsFalse(builder.IsInitialized);
builder.WeakBuild();
}
[Test]
public void TestIBuilderLiteWeakBuild() {
IBuilderLite builder = TestRequiredLite.CreateBuilder()
.SetD(0)
.SetEn(ExtraEnum.EXLITE_BAZ);
Assert.IsTrue(builder.IsInitialized);
builder.WeakBuild();
}
[Test]
public void TestIBuilderLiteWeakClone() {
TestRequiredLite msg = TestRequiredLite.CreateBuilder()
.SetD(1).SetEn(ExtraEnum.EXLITE_BAR).Build();
Assert.IsTrue(msg.IsInitialized);
IMessageLite copy = ((IBuilderLite)msg.ToBuilder()).WeakClone().WeakBuild();
Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
}
[Test]
public void TestIBuilderLiteWeakDefaultInstance() {
Assert.IsTrue(ReferenceEquals(TestRequiredLite.DefaultInstance,
((IBuilderLite)TestRequiredLite.CreateBuilder()).WeakDefaultInstanceForType));
}
}
}
This diff is collapsed.
This diff is collapsed.
......@@ -57,7 +57,12 @@
<Compile Include="..\ProtocolBuffers.Test\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
<Compile Include="AbstractBuilderLiteTest.cs" />
<Compile Include="ExtendableBuilderLiteTest.cs" />
<Compile Include="ExtendableMessageLiteTest.cs" />
<Compile Include="LiteTest.cs" />
<Compile Include="TestLiteByApi.cs" />
<Compile Include="TestProtos\UnitTestExtrasLiteProtoFile.cs" />
<Compile Include="TestProtos\UnitTestImportLiteProtoFile.cs" />
<Compile Include="TestProtos\UnitTestLiteProtoFile.cs" />
</ItemGroup>
......@@ -70,6 +75,7 @@
<ProjectReference Include="..\ProtocolBuffers\ProtocolBuffersLite.csproj">
<Project>{6969BDCE-D925-43F3-94AC-A531E6DF2591}</Project>
<Name>ProtocolBuffersLite</Name>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
......
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.Collections.Generic;
using Google.ProtocolBuffers;
using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework;
namespace Google.ProtocolBuffers {
[TestFixture]
public class TestLiteByApi {
[Test, Ignore("Currently broken as equality/hash is not implemented")]
public void TestAllTypesEquality() {
TestAllTypesLite msg = TestAllTypesLite.DefaultInstance;
TestAllTypesLite copy = msg.ToBuilder().Build();
Assert.AreEqual(msg.GetHashCode(), copy.GetHashCode());
Assert.IsTrue(msg.Equals(copy));
}
[Test]
public void TestAllTypesDefaultedRoundTrip() {
TestAllTypesLite msg = TestAllTypesLite.DefaultInstance;
Assert.IsTrue(msg.IsInitialized);
TestAllTypesLite copy = TestAllTypesLite.CreateBuilder().MergeFrom(msg.ToByteArray()).Build();
Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
}
[Test]
public void TestAllTypesModifiedRoundTrip() {
TestAllTypesLite msg = TestAllTypesLite.DefaultInstance;
msg.ToBuilder()
.SetOptionalBool(true)
.SetOptionalCord("Hi")
.SetOptionalDouble(1.123)
.SetOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_FOO)
.SetOptionalForeignMessage(ForeignMessageLite.CreateBuilder().SetC('c').Build())
.SetOptionalGroup(TestAllTypesLite.Types.OptionalGroup.CreateBuilder().SetA('a').Build())
.SetOptionalImportEnum(ImportEnumLite.IMPORT_LITE_BAR)
.SetOptionalInt32(32)
.SetOptionalInt64(64)
.SetOptionalNestedEnum(TestAllTypesLite.Types.NestedEnum.FOO)
.SetOptionalString("SetOptionalString")
.AddRepeatedGroup(TestAllTypesLite.Types.RepeatedGroup.CreateBuilder().SetA('a').Build())
.AddRepeatedGroup(TestAllTypesLite.Types.RepeatedGroup.CreateBuilder().SetA('A').Build())
;
TestAllTypesLite copy = TestAllTypesLite.CreateBuilder().MergeFrom(msg.ToByteArray()).Build();
Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
}
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment