diff options
Diffstat (limited to 'csharp/src/Google.Protobuf.Test')
8 files changed, 138 insertions, 68 deletions
diff --git a/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs b/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs index c0a9ffd1..e719d2a0 100644 --- a/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs +++ b/csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs @@ -403,7 +403,7 @@ namespace Google.Protobuf output.Flush();
ms.Position = 0;
- CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2], 0, 0);
+ CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2], 0, 0, false);
uint tag = input.ReadTag();
Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag));
@@ -594,5 +594,12 @@ namespace Google.Protobuf }
Assert.IsTrue(memoryStream.CanRead); // We left the stream open
}
+
+ [Test]
+ public void Dispose_FromByteArray()
+ {
+ var stream = new CodedInputStream(new byte[10]);
+ stream.Dispose();
+ }
}
}
\ No newline at end of file diff --git a/csharp/src/Google.Protobuf.Test/CodedOutputStreamTest.cs b/csharp/src/Google.Protobuf.Test/CodedOutputStreamTest.cs index 48bf6d60..98cabd55 100644 --- a/csharp/src/Google.Protobuf.Test/CodedOutputStreamTest.cs +++ b/csharp/src/Google.Protobuf.Test/CodedOutputStreamTest.cs @@ -334,7 +334,7 @@ namespace Google.Protobuf }
// Now test Input stream:
{
- CodedInputStream cin = new CodedInputStream(new MemoryStream(bytes), new byte[50], 0, 0);
+ CodedInputStream cin = new CodedInputStream(new MemoryStream(bytes), new byte[50], 0, 0, false);
Assert.AreEqual(0, cin.Position);
// Field 1:
uint tag = cin.ReadTag();
@@ -415,5 +415,12 @@ namespace Google.Protobuf Assert.AreEqual(1, memoryStream.Position); // Flushed data from CodedOutputStream to MemoryStream
Assert.IsTrue(memoryStream.CanWrite); // We left the stream open
}
+
+ [Test]
+ public void Dispose_FromByteArray()
+ {
+ var stream = new CodedOutputStream(new byte[10]);
+ stream.Dispose();
+ }
}
}
\ No newline at end of file diff --git a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs index 9d3d69af..68b4de45 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs @@ -540,6 +540,22 @@ namespace Google.Protobuf.Collections Assert.Throws<ArgumentException>(() => map.ToString()); } +#if !NET35 + [Test] + public void IDictionaryKeys_Equals_IReadOnlyDictionaryKeys() + { + var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } }; + CollectionAssert.AreEquivalent(((IDictionary<string, string>)map).Keys, ((IReadOnlyDictionary<string, string>)map).Keys); + } + + [Test] + public void IDictionaryValues_Equals_IReadOnlyDictionaryValues() + { + var map = new MapField<string, string> { { "foo", "bar" }, { "x", "y" } }; + CollectionAssert.AreEquivalent(((IDictionary<string, string>)map).Values, ((IReadOnlyDictionary<string, string>)map).Values); + } +#endif + private static KeyValuePair<TKey, TValue> NewKeyValuePair<TKey, TValue>(TKey key, TValue value) { return new KeyValuePair<TKey, TValue>(key, value); diff --git a/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj b/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj new file mode 100644 index 00000000..06d07b9f --- /dev/null +++ b/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.csproj @@ -0,0 +1,30 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFrameworks>net451;netcoreapp1.0</TargetFrameworks> + <AssemblyOriginatorKeyFile>../../keys/Google.Protobuf.snk</AssemblyOriginatorKeyFile> + <SignAssembly>true</SignAssembly> + <PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign> + <IsPackable>False</IsPackable> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\Google.Protobuf\Google.Protobuf.csproj" /> + </ItemGroup> + + <ItemGroup> + <PackageReference Include="NUnit" Version="3.6.1" /> + <PackageReference Include="NUnitLite" Version="3.6.1" /> + </ItemGroup> + + <!-- + - Override target frameworks on non-Windows to just .NET Core + - Doing this conditionally in the initial PropertyGroup confuses + - Visual Studio. + --> + <PropertyGroup Condition="'$(OS)' != 'Windows_NT'"> + <TargetFrameworks>netcoreapp1.0</TargetFrameworks> + </PropertyGroup> + +</Project> diff --git a/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.xproj b/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.xproj deleted file mode 100644 index 6370441e..00000000 --- a/csharp/src/Google.Protobuf.Test/Google.Protobuf.Test.xproj +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> - <PropertyGroup> - <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> - <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> - </PropertyGroup> - <Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> - <PropertyGroup Label="Globals"> - <ProjectGuid>580eb013-d3c7-4578-b845-015f4a3b0591</ProjectGuid> - <RootNamespace>Google.Protobuf.Test</RootNamespace> - <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> - <OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> - </PropertyGroup> - <PropertyGroup> - <SchemaVersion>2.0</SchemaVersion> - </PropertyGroup> - <ItemGroup> - <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> - </ItemGroup> - <Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> -</Project>
\ No newline at end of file diff --git a/csharp/src/Google.Protobuf.Test/Program.cs b/csharp/src/Google.Protobuf.Test/Program.cs new file mode 100755 index 00000000..954c02b5 --- /dev/null +++ b/csharp/src/Google.Protobuf.Test/Program.cs @@ -0,0 +1,48 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2017 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// 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 NUnitLite; +using System.Reflection; + +namespace Google.Protobuf.Test +{ + class Program + { + public static int Main(string[] args) + { + #if NET35 + return new AutoRun(typeof(Program).Assembly).Execute(args);
+ #else + return new AutoRun(typeof(Program).GetTypeInfo().Assembly).Execute(args); + #endif + } + } +}
\ No newline at end of file diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs index 4aecc998..a3edd595 100644 --- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs +++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/AnyTest.cs @@ -91,6 +91,24 @@ namespace Google.Protobuf.WellKnownTypes } [Test] + public void TryUnpack_WrongType() + { + var message = SampleMessages.CreateFullTestAllTypes(); + var any = Any.Pack(message); + Assert.False(any.TryUnpack(out TestOneof unpacked)); + Assert.Null(unpacked); + } + + [Test] + public void TryUnpack_RightType() + { + var message = SampleMessages.CreateFullTestAllTypes(); + var any = Any.Pack(message); + Assert.IsTrue(any.TryUnpack(out TestAllTypes unpacked)); + Assert.AreEqual(message, unpacked); + } + + [Test] public void ToString_WithValues() { var message = SampleMessages.CreateFullTestAllTypes(); @@ -100,6 +118,16 @@ namespace Google.Protobuf.WellKnownTypes } [Test] + [TestCase("proto://foo.bar", "foo.bar")] + [TestCase("/foo/bar/baz", "baz")] + [TestCase("foobar", "")] + public void GetTypeName(string typeUrl, string expectedTypeName) + { + var any = new Any { TypeUrl = typeUrl }; + Assert.AreEqual(expectedTypeName, Any.GetTypeName(typeUrl)); + } + + [Test] public void ToString_Empty() { var any = new Any(); diff --git a/csharp/src/Google.Protobuf.Test/project.json b/csharp/src/Google.Protobuf.Test/project.json deleted file mode 100644 index dff0ab73..00000000 --- a/csharp/src/Google.Protobuf.Test/project.json +++ /dev/null @@ -1,45 +0,0 @@ -{ - "buildOptions": { - "debugType": "portable", - "keyFile": "../../keys/Google.Protobuf.snk" - }, - - "configurations": { - "Debug": { - "buildOptions": { - "define": [ "DEBUG", "TRACE" ] - } - }, - "Release": { - "buildOptions": { - "define": [ "RELEASE", "TRACE" ], - "optimize": true - } - } - }, - - "dependencies": { - "Google.Protobuf": { "target": "project" }, - "dotnet-test-nunit": "3.4.0-beta-3", - "NUnit": "3.6.0" - }, - - "testRunner": "nunit", - - "frameworks": { - "net451": {}, - "netcoreapp1.0": { - "imports" : [ "dnxcore50", "netcoreapp1.0", "portable-net45+win8" ], - "buildOptions": { - "define": [ "PCL" ] - }, - "dependencies": { - "Microsoft.NETCore.App": { - "version": "1.0.0", - "type": "platform" - }, - "System.Console": "4.0.0" - } - } - } -} |