Răsfoiți Sursa

fix commends from Jon Skeet

Jie Luo 10 ani în urmă
părinte
comite
6e1ec5f6ea

+ 20 - 0
csharp/src/AddressBook/AddressBook.csproj

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <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>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -14,6 +15,8 @@
     <FileAlignment>512</FileAlignment>
     <StartupObject>Google.ProtocolBuffers.Examples.AddressBook.Program</StartupObject>
     <TargetFrameworkProfile>Client</TargetFrameworkProfile>
+    <NuGetPackageImportStamp>
+    </NuGetPackageImportStamp>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -45,6 +48,14 @@
     <Reference Include="System" />
     <Reference Include="System.Data" />
     <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>
     <Compile Include="AddPerson.cs" />
@@ -62,8 +73,17 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />
+    <None Include="packages.config" />
   </ItemGroup>
   <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. 
        Other similar extension points exist, see Microsoft.Common.targets.
   <Target Name="BeforeBuild">

+ 12 - 3
csharp/src/AddressBook/app.config

@@ -1,3 +1,12 @@
-<?xml version="1.0"?>
-<configuration>
-	<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration>
+<?xml version="1.0" encoding="utf-8"?>
+<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>

+ 21 - 25
csharp/src/ProtocolBuffers.Test/FieldPresenceTest.cs

@@ -32,16 +32,13 @@
 #endregion
 
 using System;
-using System.Reflection;
-using System.Collections.Generic;
 using Google.ProtocolBuffers.Descriptors;
 using Google.ProtocolBuffers.TestProtos.FieldPresence;
-using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Xunit;
 
 namespace Google.ProtocolBuffers
 {
-    [TestClass]
-    class FieldPresenceTest
+    public class FieldPresenceTest
     {
         private void CheckHasMethodRemoved(Type proto2Type, Type proto3Type, string name)
         {
@@ -51,7 +48,7 @@ namespace Google.ProtocolBuffers
             Assert.Null(proto3Type.GetProperty("Has" + name));
         }
 
-        [TestMethod]
+        [Fact]
         public void TestHasMethod()
         {
             // Optional non-message fields don't have HasFoo method generated
@@ -74,7 +71,7 @@ namespace Google.ProtocolBuffers
             Assert.False(TestAllTypes.CreateBuilder().HasOptionalNestedMessage);
         }
 
-        [TestMethod]
+        [Fact]
         public void TestFieldPresence()
         {
             // Optional non-message fields set to their default value are treated the same
@@ -87,7 +84,7 @@ namespace Google.ProtocolBuffers
             builder.SetOptionalBytes(ByteString.Empty);
             builder.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.FOO);
             TestAllTypes message = builder.Build();
-            Assert.AreEqual(0, message.SerializedSize);
+            Assert.Equal(0, message.SerializedSize);
 
             // Test merge
             TestAllTypes.Builder a = TestAllTypes.CreateBuilder();
@@ -97,19 +94,19 @@ namespace Google.ProtocolBuffers
             a.SetOptionalNestedEnum(TestAllTypes.Types.NestedEnum.BAR);
             a.MergeFrom(message);
             TestAllTypes messageA = a.Build();
-            Assert.AreEqual(1, messageA.OptionalInt32);
-            Assert.AreEqual("x", messageA.OptionalString);
-            Assert.AreEqual(ByteString.CopyFromUtf8("y"), messageA.OptionalBytes);
-            Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, messageA.OptionalNestedEnum);
+            Assert.Equal(1, messageA.OptionalInt32);
+            Assert.Equal("x", messageA.OptionalString);
+            Assert.Equal(ByteString.CopyFromUtf8("y"), messageA.OptionalBytes);
+            Assert.Equal(TestAllTypes.Types.NestedEnum.BAR, messageA.OptionalNestedEnum);
 
             // equals/hashCode should produce the same results
             TestAllTypes empty = TestAllTypes.CreateBuilder().Build();
             Assert.True(empty.Equals(message));
             Assert.True(message.Equals(empty));
-            Assert.AreEqual(empty.GetHashCode(), message.GetHashCode());
+            Assert.Equal(empty.GetHashCode(), message.GetHashCode());
         }
 
-        [TestMethod]
+        [Fact]
         public void TestFieldPresenceReflection()
         {
             MessageDescriptor descriptor = TestAllTypes.Descriptor;
@@ -135,7 +132,7 @@ namespace Google.ProtocolBuffers
             Assert.False(message.HasField(optionalStringField));
             Assert.False(message.HasField(optionalBytesField));
             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
             message = TestAllTypes.CreateBuilder()
@@ -148,10 +145,10 @@ namespace Google.ProtocolBuffers
             Assert.True(message.HasField(optionalStringField));
             Assert.True(message.HasField(optionalBytesField));
             Assert.True(message.HasField(optionalNestedEnumField));
-            Assert.AreEqual(4, message.AllFields.Count);
+            Assert.Equal(4, message.AllFields.Count);
         }
 
-        [TestMethod]
+        [Fact]
         public void TestMessageField()
         {
             TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
@@ -163,11 +160,10 @@ namespace Google.ProtocolBuffers
             builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance);
             Assert.True(builder.HasOptionalNestedMessage);
             Assert.True(builder.Build().HasOptionalNestedMessage);
-
         }
 
-        [TestMethod]
-        public void TestSeralizeAndParese()
+        [Fact]
+        public void TestSeralizeAndParse()
         {
             TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
             builder.SetOptionalInt32(1234);
@@ -176,12 +172,12 @@ namespace Google.ProtocolBuffers
             ByteString data = builder.Build().ToByteString();
 
             TestAllTypes message = TestAllTypes.ParseFrom(data);
-            Assert.AreEqual(1234, message.OptionalInt32);
-            Assert.AreEqual("hello", message.OptionalString);
-            Assert.AreEqual(ByteString.Empty, message.OptionalBytes);
-            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum);
+            Assert.Equal(1234, message.OptionalInt32);
+            Assert.Equal("hello", message.OptionalString);
+            Assert.Equal(ByteString.Empty, message.OptionalBytes);
+            Assert.Equal(TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum);
             Assert.True(message.HasOptionalNestedMessage);
-            Assert.AreEqual(0, message.OptionalNestedMessage.Value);
+            Assert.Equal(0, message.OptionalNestedMessage.Value);
         }
     }
 }

+ 0 - 0
csharp/src/ProtocolBuffers.Test/TestProtos/FieldPresense.cs → csharp/src/ProtocolBuffers.Test/TestProtos/FieldPresence.cs


+ 3 - 8
csharp/src/ProtocolBuffers/Descriptors/FileDescriptor.cs

@@ -54,20 +54,15 @@ namespace Google.ProtocolBuffers.Descriptors
         private readonly IList<FileDescriptor> publicDependencies;
         private readonly DescriptorPool pool;
 
-        public enum Syntax
+        public enum ProtoSyntax
         {
-            UNKNOWN,
             PROTO2,
             PROTO3
         }
 
-        public Syntax GetSyntax()
+        public ProtoSyntax Syntax
         {
-            if (proto.Syntax == "proto3")
-            {
-                return Syntax.PROTO3;
-            }
-            return Syntax.PROTO2;
+            get { return proto.Syntax == "proto3" ? ProtoSyntax.PROTO3 : ProtoSyntax.PROTO2; }
         }
 
         private FileDescriptor(FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies)

+ 1 - 5
csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs

@@ -68,11 +68,7 @@ namespace Google.ProtocolBuffers.FieldAccess
         {
             this.descriptor = descriptor;
             accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count];
-            bool supportFieldPresence = false;
-            if (descriptor.File.GetSyntax() == FileDescriptor.Syntax.PROTO2)
-            {
-                supportFieldPresence = true;
-            }
+            bool supportFieldPresence = descriptor.File.Syntax == FileDescriptor.ProtoSyntax.PROTO2;
             for (int i = 0; i < accessors.Length; i++)
             {
                 accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i], supportFieldPresence);

+ 5 - 7
csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs

@@ -58,9 +58,8 @@ namespace Google.ProtocolBuffers.FieldAccess
             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 builderProperty = typeof(TBuilder).GetProperty(name, null, ReflectionUtil.EmptyTypes);
             MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name);
@@ -77,8 +76,11 @@ namespace Google.ProtocolBuffers.FieldAccess
                     throw new ArgumentException("Has properties not available");
                 }
                 hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod());
+            } else
+            {
+                hasDelegate = message => !GetValue(message).Equals(fieldDescriptor.DefaultValue);
             }
-            
+
             clrType = messageProperty.PropertyType;
             clearDelegate = ReflectionUtil.CreateDelegateFunc<TBuilder, IBuilder>(clearMethod);
             getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod());
@@ -87,10 +89,6 @@ namespace Google.ProtocolBuffers.FieldAccess
 
         public bool Has(TMessage message)
         {
-            if (hasDelegate == null)
-            {
-                return !GetValue(message).Equals(field.DefaultValue);
-            }
             return hasDelegate(message);
         }