Commit 6e1ec5f6 authored by Jie Luo's avatar Jie Luo

fix commends from Jon Skeet

parent 24ae5105
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\xunit.runner.visualstudio.2.1.0-beta1-build1051\build\net20\xunit.runner.visualstudio.props" Condition="Exists('..\packages\xunit.runner.visualstudio.2.1.0-beta1-build1051\build\net20\xunit.runner.visualstudio.props')" />
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
...@@ -14,6 +15,8 @@ ...@@ -14,6 +15,8 @@
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<StartupObject>Google.ProtocolBuffers.Examples.AddressBook.Program</StartupObject> <StartupObject>Google.ProtocolBuffers.Examples.AddressBook.Program</StartupObject>
<TargetFrameworkProfile>Client</TargetFrameworkProfile> <TargetFrameworkProfile>Client</TargetFrameworkProfile>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
...@@ -45,6 +48,14 @@ ...@@ -45,6 +48,14 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="xunit, Version=1.9.2.1705, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.1.9.2\lib\net20\xunit.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.should, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.should.1.1\lib\net35\xunit.should.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="AddPerson.cs" /> <Compile Include="AddPerson.cs" />
...@@ -62,8 +73,17 @@ ...@@ -62,8 +73,17 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="app.config" /> <None Include="app.config" />
<None Include="packages.config" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\xunit.runner.visualstudio.2.1.0-beta1-build1051\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.runner.visualstudio.2.1.0-beta1-build1051\build\net20\xunit.runner.visualstudio.props'))" />
<Error Condition="!Exists('..\packages\xunit.MSBuild.1.9.2.3\build\xunit.MSBuild.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\xunit.MSBuild.1.9.2.3\build\xunit.MSBuild.targets'))" />
</Target>
<Import Project="..\packages\xunit.MSBuild.1.9.2.3\build\xunit.MSBuild.targets" Condition="Exists('..\packages\xunit.MSBuild.1.9.2.3\build\xunit.MSBuild.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild"> <Target Name="BeforeBuild">
......
<?xml version="1.0"?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration> <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" /></startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="xunit" publicKeyToken="8d05b1bb7a6fdb6c" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.9.2.1705" newVersion="1.9.2.1705" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
...@@ -32,16 +32,13 @@ ...@@ -32,16 +32,13 @@
#endregion #endregion
using System; using System;
using System.Reflection;
using System.Collections.Generic;
using Google.ProtocolBuffers.Descriptors; using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos.FieldPresence; using Google.ProtocolBuffers.TestProtos.FieldPresence;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Xunit;
namespace Google.ProtocolBuffers namespace Google.ProtocolBuffers
{ {
[TestClass] public class FieldPresenceTest
class FieldPresenceTest
{ {
private void CheckHasMethodRemoved(Type proto2Type, Type proto3Type, string name) private void CheckHasMethodRemoved(Type proto2Type, Type proto3Type, string name)
{ {
...@@ -51,7 +48,7 @@ namespace Google.ProtocolBuffers ...@@ -51,7 +48,7 @@ namespace Google.ProtocolBuffers
Assert.Null(proto3Type.GetProperty("Has" + name)); Assert.Null(proto3Type.GetProperty("Has" + name));
} }
[TestMethod] [Fact]
public void TestHasMethod() public void TestHasMethod()
{ {
// Optional non-message fields don't have HasFoo method generated // Optional non-message fields don't have HasFoo method generated
...@@ -74,7 +71,7 @@ namespace Google.ProtocolBuffers ...@@ -74,7 +71,7 @@ namespace Google.ProtocolBuffers
Assert.False(TestAllTypes.CreateBuilder().HasOptionalNestedMessage); Assert.False(TestAllTypes.CreateBuilder().HasOptionalNestedMessage);
} }
[TestMethod] [Fact]
public void TestFieldPresence() public void TestFieldPresence()
{ {
// Optional non-message fields set to their default value are treated the same // Optional non-message fields set to their default value are treated the same
...@@ -87,7 +84,7 @@ namespace Google.ProtocolBuffers ...@@ -87,7 +84,7 @@ namespace Google.ProtocolBuffers
builder.SetOptionalBytes(ByteString.Empty); builder.SetOptionalBytes(ByteString.Empty);
builder.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO); builder.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO);
TestAllTypes message = builder.Build(); TestAllTypes message = builder.Build();
Assert.AreEqual(0, message.SerializedSize); Assert.Equal(0, message.SerializedSize);
// Test merge // Test merge
TestAllTypes.Builder a = TestAllTypes.CreateBuilder(); TestAllTypes.Builder a = TestAllTypes.CreateBuilder();
...@@ -97,19 +94,19 @@ namespace Google.ProtocolBuffers ...@@ -97,19 +94,19 @@ namespace Google.ProtocolBuffers
a.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.BAR); a.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.BAR);
a.MergeFrom(message); a.MergeFrom(message);
TestAllTypes messageA = a.Build(); TestAllTypes messageA = a.Build();
Assert.AreEqual(1, messageA.OptionalInt32); Assert.Equal(1, messageA.OptionalInt32);
Assert.AreEqual("x", messageA.OptionalString); Assert.Equal("x", messageA.OptionalString);
Assert.AreEqual(ByteString.CopyFromUtf8("y"), messageA.OptionalBytes); Assert.Equal(ByteString.CopyFromUtf8("y"), messageA.OptionalBytes);
Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, messageA.OptionalNestedEnum); Assert.Equal(TestAllTypes.Types.NestedEnum.BAR, messageA.OptionalNestedEnum);
// equals/hashCode should produce the same results // equals/hashCode should produce the same results
TestAllTypes empty = TestAllTypes.CreateBuilder().Build(); TestAllTypes empty = TestAllTypes.CreateBuilder().Build();
Assert.True(empty.Equals(message)); Assert.True(empty.Equals(message));
Assert.True(message.Equals(empty)); Assert.True(message.Equals(empty));
Assert.AreEqual(empty.GetHashCode(), message.GetHashCode()); Assert.Equal(empty.GetHashCode(), message.GetHashCode());
} }
[TestMethod] [Fact]
public void TestFieldPresenceReflection() public void TestFieldPresenceReflection()
{ {
MessageDescriptor descriptor = TestAllTypes.Descriptor; MessageDescriptor descriptor = TestAllTypes.Descriptor;
...@@ -135,7 +132,7 @@ namespace Google.ProtocolBuffers ...@@ -135,7 +132,7 @@ namespace Google.ProtocolBuffers
Assert.False(message.HasField(optionalStringField)); Assert.False(message.HasField(optionalStringField));
Assert.False(message.HasField(optionalBytesField)); Assert.False(message.HasField(optionalBytesField));
Assert.False(message.HasField(optionalNestedEnumField)); Assert.False(message.HasField(optionalNestedEnumField));
Assert.AreEqual(0, message.AllFields.Count); Assert.Equal(0, message.AllFields.Count);
// Set t0 non-defalut value is seen as present // Set t0 non-defalut value is seen as present
message = TestAllTypes.CreateBuilder() message = TestAllTypes.CreateBuilder()
...@@ -148,10 +145,10 @@ namespace Google.ProtocolBuffers ...@@ -148,10 +145,10 @@ namespace Google.ProtocolBuffers
Assert.True(message.HasField(optionalStringField)); Assert.True(message.HasField(optionalStringField));
Assert.True(message.HasField(optionalBytesField)); Assert.True(message.HasField(optionalBytesField));
Assert.True(message.HasField(optionalNestedEnumField)); Assert.True(message.HasField(optionalNestedEnumField));
Assert.AreEqual(4, message.AllFields.Count); Assert.Equal(4, message.AllFields.Count);
} }
[TestMethod] [Fact]
public void TestMessageField() public void TestMessageField()
{ {
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
...@@ -163,11 +160,10 @@ namespace Google.ProtocolBuffers ...@@ -163,11 +160,10 @@ namespace Google.ProtocolBuffers
builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance); builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance);
Assert.True(builder.HasOptionalNestedMessage); Assert.True(builder.HasOptionalNestedMessage);
Assert.True(builder.Build().HasOptionalNestedMessage); Assert.True(builder.Build().HasOptionalNestedMessage);
} }
[TestMethod] [Fact]
public void TestSeralizeAndParese() public void TestSeralizeAndParse()
{ {
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
builder.SetOptionalInt32(1234); builder.SetOptionalInt32(1234);
...@@ -176,12 +172,12 @@ namespace Google.ProtocolBuffers ...@@ -176,12 +172,12 @@ namespace Google.ProtocolBuffers
ByteString data = builder.Build().ToByteString(); ByteString data = builder.Build().ToByteString();
TestAllTypes message = TestAllTypes.ParseFrom(data); TestAllTypes message = TestAllTypes.ParseFrom(data);
Assert.AreEqual(1234, message.OptionalInt32); Assert.Equal(1234, message.OptionalInt32);
Assert.AreEqual("hello", message.OptionalString); Assert.Equal("hello", message.OptionalString);
Assert.AreEqual(ByteString.Empty, message.OptionalBytes); Assert.Equal(ByteString.Empty, message.OptionalBytes);
Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum); Assert.Equal(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum);
Assert.True(message.HasOptionalNestedMessage); Assert.True(message.HasOptionalNestedMessage);
Assert.AreEqual(0, message.OptionalNestedMessage.Value); Assert.Equal(0, message.OptionalNestedMessage.Value);
} }
} }
} }
...@@ -54,20 +54,15 @@ namespace Google.ProtocolBuffers.Descriptors ...@@ -54,20 +54,15 @@ namespace Google.ProtocolBuffers.Descriptors
private readonly IList<FileDescriptor> publicDependencies; private readonly IList<FileDescriptor> publicDependencies;
private readonly DescriptorPool pool; private readonly DescriptorPool pool;
public enum Syntax public enum ProtoSyntax
{ {
UNKNOWN,
PROTO2, PROTO2,
PROTO3 PROTO3
} }
public Syntax GetSyntax() public ProtoSyntax Syntax
{ {
if (proto.Syntax == "proto3") get { return proto.Syntax == "proto3" ? ProtoSyntax.PROTO3 : ProtoSyntax.PROTO2; }
{
return Syntax.PROTO3;
}
return Syntax.PROTO2;
} }
private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies) private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies)
......
...@@ -68,11 +68,7 @@ namespace Google.ProtocolBuffers.FieldAccess ...@@ -68,11 +68,7 @@ namespace Google.ProtocolBuffers.FieldAccess
{ {
this.descriptor = descriptor; this.descriptor = descriptor;
accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count]; accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count];
bool supportFieldPresence = false; bool supportFieldPresence = descriptor.File.Syntax == FileDescriptor.ProtoSyntax.PROTO2;
if (descriptor.File.GetSyntax() == FileDescriptor.Syntax.PROTO2)
{
supportFieldPresence = true;
}
for (int i = 0; i < accessors.Length; i++) for (int i = 0; i < accessors.Length; i++)
{ {
accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i], supportFieldPresence); accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i], supportFieldPresence);
......
...@@ -58,9 +58,8 @@ namespace Google.ProtocolBuffers.FieldAccess ...@@ -58,9 +58,8 @@ namespace Google.ProtocolBuffers.FieldAccess
get { return clrType; } get { return clrType; }
} }
internal SinglePrimitiveAccessor(FieldDescriptor fieldDesriptor, string name, bool supportFieldPresence) internal SinglePrimitiveAccessor(FieldDescriptor fieldDescriptor, string name, bool supportFieldPresence)
{ {
field = fieldDesriptor;
PropertyInfo messageProperty = typeof(TMessage).GetProperty(name, null, ReflectionUtil.EmptyTypes); PropertyInfo messageProperty = typeof(TMessage).GetProperty(name, null, ReflectionUtil.EmptyTypes);
PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name, null, ReflectionUtil.EmptyTypes); PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name, null, ReflectionUtil.EmptyTypes);
MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name); MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name);
...@@ -77,6 +76,9 @@ namespace Google.ProtocolBuffers.FieldAccess ...@@ -77,6 +76,9 @@ namespace Google.ProtocolBuffers.FieldAccess
throw new ArgumentException("Has properties not available"); throw new ArgumentException("Has properties not available");
} }
hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod()); hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod());
} else
{
hasDelegate = message => !GetValue(message).Equals(fieldDescriptor.DefaultValue);
} }
clrType = messageProperty.PropertyType; clrType = messageProperty.PropertyType;
...@@ -87,10 +89,6 @@ namespace Google.ProtocolBuffers.FieldAccess ...@@ -87,10 +89,6 @@ namespace Google.ProtocolBuffers.FieldAccess
public bool Has(TMessage message) public bool Has(TMessage message)
{ {
if (hasDelegate == null)
{
return !GetValue(message).Equals(field.DefaultValue);
}
return hasDelegate(message); return hasDelegate(message);
} }
......
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