forked from OData/odata.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing CopyFrom method in OdataMessageReader and OdataMessageWriter t…
…o include Arraypool (OData#1595) * Changes and test * Fixing the build error * Fixes based on Review Comments Fixes based on Review Comments * Update Microsoft.Test.OData.Tests.Client.NetCore.csproj * Revert "Merge branch 'master' into UpdateCopyFromMethod" This reverts commit 1345914, reversing changes made to 9a786ed. * Update Microsoft.Test.OData.Tests.Client.NetCore.csproj Reverting Microsoft.Test.OData.Tests.Client.NetCore * reverted stylecop buildnumber reverted stylecop buildnumber as its already committed * Fixed as per review comments Added copyright, sorted usings etc * Restructured EvaluateDifferences Restructured EvaluateDifferences * Update ValidationHelper.cs
- Loading branch information
1 parent
748aa5c
commit ce1dd6c
Showing
7 changed files
with
189 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
test/FunctionalTests/Microsoft.OData.Core.Tests/TestCharArrayPool.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//--------------------------------------------------------------------- | ||
// <copyright file="TestCharArrayPool.cs" company="Microsoft"> | ||
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. | ||
// </copyright> | ||
//--------------------------------------------------------------------- | ||
|
||
using System; | ||
using Microsoft.OData.Buffers; | ||
|
||
namespace Microsoft.OData.Tests | ||
{ | ||
public class TestCharArrayPool : ICharArrayPool | ||
{ | ||
public char[] Buffer { get { return Rent(MinSize); } } | ||
|
||
public int MinSize { set; get; } | ||
|
||
public TestCharArrayPool(int minSize) | ||
{ | ||
this.MinSize = minSize; | ||
} | ||
|
||
public char[] Rent(int minSize) | ||
{ | ||
return new char[minSize]; | ||
} | ||
|
||
public void Return(char[] array) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
test/FunctionalTests/Microsoft.OData.Core.Tests/ValidationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
//--------------------------------------------------------------------- | ||
// <copyright file="ValidationHelper.cs" company="Microsoft"> | ||
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. | ||
// </copyright> | ||
//--------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.OData.Tests | ||
{ | ||
public static class ValidationHelper | ||
{ | ||
/// <summary> | ||
/// This method takes 2 generic types and see if one is the deep copy of other | ||
/// </summary> | ||
/// <typeparam name="T">Method accepts Generic type</typeparam> | ||
/// <param name="obj">Object to be compared with</param> | ||
/// <param name="copy">Copy object to compare</param> | ||
/// <returns>Returns a list of differences</returns> | ||
public static List<string> GetDifferences<T>(T obj, T copy) | ||
{ | ||
var diff = new List<string>(); | ||
EvaluateDifferences(obj, copy, diff); | ||
|
||
return diff; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// To evaluate the differences of 2 generic types, called recursively | ||
/// </summary> | ||
/// <typeparam name="T">Method accepts Generic type</typeparam> | ||
/// <param name="obj">Object to be compared with</param> | ||
/// <param name="copy">Copy object to compare</param> | ||
/// <param name="diff">List Of Differences</param> | ||
/// <param name="objName">Optional parameter for objectname, to be used in differences string, default empty string</param> | ||
/// <param name="isCollection">Optional parameter to see if its a collection, default false</param> | ||
private static void EvaluateDifferences<T>(T obj, T copy, List<string> diff,string objName ="", bool isCollection =false) | ||
{ | ||
if (obj == null && copy == null) | ||
{ | ||
return; | ||
} | ||
|
||
string collectionStatement = isCollection?" collection":string.Empty; | ||
|
||
if (obj == null || copy == null) | ||
{ | ||
diff.Add(string.Format("Value of{0} {1} does not match", collectionStatement, objName)); | ||
return; | ||
} | ||
|
||
var objType = obj.GetType(); | ||
|
||
if (typeof(Uri) == objType) | ||
{ | ||
if (Uri.Compare(new Uri(obj.ToString()), new Uri(copy.ToString()), UriComponents.AbsoluteUri, UriFormat.Unescaped, StringComparison.CurrentCulture) != 0) | ||
{ | ||
diff.Add(string.Format("Value of Uri{0} {1} does not match.", collectionStatement,objName)); | ||
} | ||
} | ||
else if (typeof(IComparable).IsAssignableFrom(objType) || objType.IsPrimitive() || objType.IsValueType()) | ||
{ | ||
if ((!obj.Equals(copy))) | ||
{ | ||
diff.Add(string.Format("Value of{0} {1} does not match.",collectionStatement, objName)); | ||
} | ||
} | ||
else if (objType.IsArray || typeof(IEnumerable).IsAssignableFrom(objType)) | ||
{ | ||
var collection1 = ((IEnumerable)obj).Cast<object>(); | ||
var collection2 = ((IEnumerable)copy).Cast<object>(); | ||
|
||
if (collection1.Count() != collection2.Count()) | ||
{ | ||
diff.Add(string.Format("Collection count of {0} does not match.", objName)); | ||
} | ||
else | ||
{ | ||
for (int i = 0; i < collection1.Count(); i++) | ||
{ | ||
var element1 = collection1.ElementAt(i); | ||
var element2 = collection2.ElementAt(i); | ||
|
||
EvaluateDifferences(element1, element2, diff, collection1.GetType().Name, true); | ||
} | ||
} | ||
} | ||
else if ( objType.IsClass()) | ||
{ | ||
foreach (var prop in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(x => x.CanRead && x.CanWrite)) | ||
{ | ||
var val1 = prop.GetValue(obj); | ||
var val2 = prop.GetValue(copy); | ||
|
||
EvaluateDifferences(val1, val2, diff,prop.Name); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} |