diff options
Diffstat (limited to 'src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests')
5 files changed, 429 insertions, 0 deletions
diff --git a/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Collections/TCollectionsTests.cs b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Collections/TCollectionsTests.cs new file mode 100644 index 000000000..1be99b48f --- /dev/null +++ b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Collections/TCollectionsTests.cs @@ -0,0 +1,83 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Collections; + +namespace Thrift.Tests.Collections +{ + // ReSharper disable once InconsistentNaming + [TestClass] + public class TCollectionsTests + { + //TODO: Add tests for IEnumerable with objects and primitive values inside + + [TestMethod] + public void TCollection_Equals_Primitive_Test() + { + var collection1 = new List<int> {1,2,3}; + var collection2 = new List<int> {1,2,3}; + + var result = TCollections.Equals(collection1, collection2); + + Assert.IsTrue(result); + } + + [TestMethod] + public void TCollection_Equals_Primitive_Different_Test() + { + var collection1 = new List<int> { 1, 2, 3 }; + var collection2 = new List<int> { 1, 2 }; + + var result = TCollections.Equals(collection1, collection2); + + Assert.IsFalse(result); + } + + [TestMethod] + public void TCollection_Equals_Objects_Test() + { + var collection1 = new List<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } }; + var collection2 = new List<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } }; + + var result = TCollections.Equals(collection1, collection2); + + // references to different collections + Assert.IsFalse(result); + } + + [TestMethod] + public void TCollection_Equals_OneAndTheSameObject_Test() + { + var collection1 = new List<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } }; + var collection2 = collection1; + + var result = TCollections.Equals(collection1, collection2); + + // references to one and the same collection + Assert.IsTrue(result); + } + + private class ExampleClass + { + public int X { get; set; } + } + } +} diff --git a/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Collections/THashSetTests.cs b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Collections/THashSetTests.cs new file mode 100644 index 000000000..8de573eee --- /dev/null +++ b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Collections/THashSetTests.cs @@ -0,0 +1,71 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Collections; + +namespace Thrift.Tests.Collections +{ + // ReSharper disable once InconsistentNaming + [TestClass] + public class THashSetTests + { + [TestMethod] + public void THashSet_Equals_Primitive_Test() + { + const int value = 1; + + var hashSet = new THashSet<int> {value}; + + Assert.IsTrue(hashSet.Contains(value)); + + hashSet.Remove(value); + + Assert.IsTrue(hashSet.Count == 0); + + hashSet.Add(value); + + Assert.IsTrue(hashSet.Contains(value)); + + hashSet.Clear(); + + Assert.IsTrue(hashSet.Count == 0); + + var newArr = new int[1]; + hashSet.Add(value); + hashSet.CopyTo(newArr, 0); + + Assert.IsTrue(newArr.Contains(value)); + + var en = hashSet.GetEnumerator(); + en.MoveNext(); + + Assert.IsTrue((int)en.Current == value); + + using (var ien = ((IEnumerable<int>)hashSet).GetEnumerator()) + { + ien.MoveNext(); + + Assert.IsTrue(ien.Current == value); + } + } + } +} diff --git a/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolHelperTests.cs b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolHelperTests.cs new file mode 100644 index 000000000..6d391516e --- /dev/null +++ b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolHelperTests.cs @@ -0,0 +1,172 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Protocol; +using Thrift.Protocol.Entities; +using Thrift.Protocol.Utilities; + +namespace Thrift.Tests.Protocols +{ + [TestClass] + public class TJSONProtocolHelperTests + { + [TestMethod] + public void GetTypeNameForTypeId_Test() + { + // input/output + var sets = new List<Tuple<TType, byte[]>> + { + new Tuple<TType, byte[]>(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool), + new Tuple<TType, byte[]>(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte), + new Tuple<TType, byte[]>(TType.I16, TJSONProtocolConstants.TypeNames.NameI16), + new Tuple<TType, byte[]>(TType.I32, TJSONProtocolConstants.TypeNames.NameI32), + new Tuple<TType, byte[]>(TType.I64, TJSONProtocolConstants.TypeNames.NameI64), + new Tuple<TType, byte[]>(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble), + new Tuple<TType, byte[]>(TType.String, TJSONProtocolConstants.TypeNames.NameString), + new Tuple<TType, byte[]>(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct), + new Tuple<TType, byte[]>(TType.Map, TJSONProtocolConstants.TypeNames.NameMap), + new Tuple<TType, byte[]>(TType.Set, TJSONProtocolConstants.TypeNames.NameSet), + new Tuple<TType, byte[]>(TType.List, TJSONProtocolConstants.TypeNames.NameList), + }; + + foreach (var t in sets) + { + Assert.IsTrue(TJSONProtocolHelper.GetTypeNameForTypeId(t.Item1) == t.Item2, $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}"); + } + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeNameForTypeId_TStop_Test() + { + TJSONProtocolHelper.GetTypeNameForTypeId(TType.Stop); + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeNameForTypeId_NonExistingTType_Test() + { + TJSONProtocolHelper.GetTypeNameForTypeId((TType)100); + } + + [TestMethod] + public void GetTypeIdForTypeName_Test() + { + // input/output + var sets = new List<Tuple<TType, byte[]>> + { + new Tuple<TType, byte[]>(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool), + new Tuple<TType, byte[]>(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte), + new Tuple<TType, byte[]>(TType.I16, TJSONProtocolConstants.TypeNames.NameI16), + new Tuple<TType, byte[]>(TType.I32, TJSONProtocolConstants.TypeNames.NameI32), + new Tuple<TType, byte[]>(TType.I64, TJSONProtocolConstants.TypeNames.NameI64), + new Tuple<TType, byte[]>(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble), + new Tuple<TType, byte[]>(TType.String, TJSONProtocolConstants.TypeNames.NameString), + new Tuple<TType, byte[]>(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct), + new Tuple<TType, byte[]>(TType.Map, TJSONProtocolConstants.TypeNames.NameMap), + new Tuple<TType, byte[]>(TType.Set, TJSONProtocolConstants.TypeNames.NameSet), + new Tuple<TType, byte[]>(TType.List, TJSONProtocolConstants.TypeNames.NameList), + }; + + foreach (var t in sets) + { + Assert.IsTrue(TJSONProtocolHelper.GetTypeIdForTypeName(t.Item2) == t.Item1, $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}"); + } + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeIdForTypeName_TStopTypeName_Test() + { + TJSONProtocolHelper.GetTypeIdForTypeName(new []{(byte)TType.Stop, (byte)TType.Stop}); + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeIdForTypeName_NonExistingTypeName_Test() + { + TJSONProtocolHelper.GetTypeIdForTypeName(new byte[]{100}); + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void GetTypeIdForTypeName_EmptyName_Test() + { + TJSONProtocolHelper.GetTypeIdForTypeName(new byte[] {}); + } + + [TestMethod] + public void IsJsonNumeric_Test() + { + // input/output + var correctJsonNumeric = "+-.0123456789Ee"; + var incorrectJsonNumeric = "AaBcDd/*\\"; + + var sets = correctJsonNumeric.Select(ch => new Tuple<byte, bool>((byte) ch, true)).ToList(); + sets.AddRange(incorrectJsonNumeric.Select(ch => new Tuple<byte, bool>((byte) ch, false))); + + foreach (var t in sets) + { + Assert.IsTrue(TJSONProtocolHelper.IsJsonNumeric(t.Item1) == t.Item2, $"Wrong mapping of Char {t.Item1} to bool: {t.Item2}"); + } + } + + [TestMethod] + public void ToHexVal_Test() + { + // input/output + var chars = "0123456789abcdef"; + var expectedHexValues = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + + var sets = chars.Select((ch, i) => new Tuple<char, byte>(ch, expectedHexValues[i])).ToList(); + + foreach (var t in sets) + { + var actualResult = TJSONProtocolHelper.ToHexVal((byte)t.Item1); + Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of char byte {t.Item1} to it expected hex value: {t.Item2}. Actual hex value: {actualResult}"); + } + } + + [TestMethod] + [ExpectedException(typeof(TProtocolException))] + public void ToHexVal_WrongInputChar_Test() + { + TJSONProtocolHelper.ToHexVal((byte)'s'); + } + + [TestMethod] + public void ToHexChar_Test() + { + // input/output + var hexValues = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + var expectedChars = "0123456789abcdef"; + + + var sets = hexValues.Select((hv, i) => new Tuple<byte, char>(hv, expectedChars[i])).ToList(); + + foreach (var t in sets) + { + var actualResult = TJSONProtocolHelper.ToHexChar(t.Item1); + Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of hex value {t.Item1} to it expected char: {t.Item2}. Actual hex value: {actualResult}"); + } + } + } +}
\ No newline at end of file diff --git a/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolTests.cs b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolTests.cs new file mode 100644 index 000000000..970ce7ece --- /dev/null +++ b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Protocols/TJsonProtocolTests.cs @@ -0,0 +1,67 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using NSubstitute; +using Thrift.Protocol; +using Thrift.Protocol.Entities; +using Thrift.Transport; +using Thrift.Transport.Client; + +namespace Thrift.Tests.Protocols +{ + // ReSharper disable once InconsistentNaming + [TestClass] + public class TJSONProtocolTests + { + [TestMethod] + public void TJSONProtocol_Can_Create_Instance_Test() + { + var httpClientTransport = Substitute.For<THttpTransport>(new Uri("http://localhost"), null, null); + + var result = new TJSONProtocolWrapper(httpClientTransport); + + Assert.IsNotNull(result); + Assert.IsNotNull(result.WrappedContext); + Assert.IsNotNull(result.WrappedReader); + Assert.IsNotNull(result.Transport); + Assert.IsTrue(result.WrappedRecursionDepth == 0); + Assert.IsTrue(result.WrappedRecursionLimit == TProtocol.DefaultRecursionDepth); + + Assert.IsTrue(result.Transport.Equals(httpClientTransport)); + Assert.IsTrue(result.WrappedContext.GetType().Name.Equals("JSONBaseContext", StringComparison.OrdinalIgnoreCase)); + Assert.IsTrue(result.WrappedReader.GetType().Name.Equals("LookaheadReader", StringComparison.OrdinalIgnoreCase)); + } + + private class TJSONProtocolWrapper : TJsonProtocol + { + public TJSONProtocolWrapper(TTransport trans) : base(trans) + { + } + + public object WrappedContext => Context; + public object WrappedReader => Reader; + public int WrappedRecursionDepth => RecursionDepth; + public int WrappedRecursionLimit => RecursionLimit; + } + } +} diff --git a/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Thrift.Tests.csproj b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Thrift.Tests.csproj new file mode 100644 index 000000000..434424dba --- /dev/null +++ b/src/jaegertracing/thrift/lib/netstd/Tests/Thrift.Tests/Thrift.Tests.csproj @@ -0,0 +1,36 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <!-- + Licensed to the Apache Software Foundation(ASF) under one + or more contributor license agreements.See the NOTICE file + distributed with this work for additional information + regarding copyright ownership.The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + --> + <PropertyGroup> + <TargetFramework>netcoreapp2.0</TargetFramework> + </PropertyGroup> + <ItemGroup> + <PackageReference Include="CompareNETObjects" Version="4.58.0" /> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> + <PackageReference Include="MSTest.TestAdapter" Version="1.4.0" /> + <PackageReference Include="MSTest.TestFramework" Version="1.4.0" /> + <PackageReference Include="NSubstitute" Version="4.0.0" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\Thrift\Thrift.csproj" /> + </ItemGroup> + <ItemGroup> + <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> + </ItemGroup> +</Project>
\ No newline at end of file |