1 // Copyright (c) Microsoft. All rights reserved. 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 3 //----------------------------------------------------------------------- 4 // </copyright> 5 // <summary>Helper class for serialization tests.</summary> 6 //----------------------------------------------------------------------- 7 8 using System; 9 using System.Collections.Generic; 10 using System.Linq; 11 using System.Text; 12 using System.IO; 13 using Microsoft.Build.Framework; 14 using Microsoft.Build.BackEnd; 15 16 namespace Microsoft.Build.UnitTests.BackEnd 17 { 18 /// <summary> 19 /// Class containing methods used to assist in testing serialization methods. 20 /// </summary> 21 static internal class TranslationHelpers 22 { 23 /// <summary> 24 /// The stream backing the serialization classes. 25 /// </summary> 26 static private MemoryStream s_serializationStream; 27 28 /// <summary> 29 /// Gets a serializer used to write data. Note that only one such serializer may be used from this class at a time. 30 /// </summary> GetWriteTranslator()31 static internal INodePacketTranslator GetWriteTranslator() 32 { 33 s_serializationStream = new MemoryStream(); 34 return NodePacketTranslator.GetWriteTranslator(s_serializationStream); 35 } 36 37 /// <summary> 38 /// Gets a serializer used to read data. Note that only one such serializer may be used from this class at a time, 39 /// and this must be called after GetWriteTranslator() has been called. 40 /// </summary> GetReadTranslator()41 static internal INodePacketTranslator GetReadTranslator() 42 { 43 s_serializationStream.Seek(0, SeekOrigin.Begin); 44 return NodePacketTranslator.GetReadTranslator(s_serializationStream, null); 45 } 46 47 /// <summary> 48 /// Compares two collections. 49 /// </summary> 50 /// <typeparam name="T">The collections element type.</typeparam> 51 /// <param name="left">The left collections.</param> 52 /// <param name="right">The right collections.</param> 53 /// <param name="comparer">The comparer to use on each element.</param> 54 /// <returns>True if the collections are equivalent.</returns> CompareCollections(ICollection<T> left, ICollection<T> right, IComparer<T> comparer)55 static internal bool CompareCollections<T>(ICollection<T> left, ICollection<T> right, IComparer<T> comparer) 56 { 57 if (Object.ReferenceEquals(left, right)) 58 { 59 return true; 60 } 61 62 if ((left == null) ^ (right == null)) 63 { 64 return false; 65 } 66 67 if (left.Count != right.Count) 68 { 69 return false; 70 } 71 72 T[] leftArray = left.ToArray(); 73 T[] rightArray = right.ToArray(); 74 75 for (int i = 0; i < leftArray.Length; i++) 76 { 77 if (comparer.Compare(leftArray[i], rightArray[i]) != 0) 78 { 79 return false; 80 } 81 } 82 83 return true; 84 } 85 86 /// <summary> 87 /// Compares two exceptions. 88 /// </summary> CompareExceptions(Exception left, Exception right)89 static internal bool CompareExceptions(Exception left, Exception right) 90 { 91 if (Object.ReferenceEquals(left, right)) 92 { 93 return true; 94 } 95 96 if ((left == null) ^ (right == null)) 97 { 98 return false; 99 } 100 101 if (left.Message != right.Message) 102 { 103 return false; 104 } 105 106 if (left.StackTrace != right.StackTrace) 107 { 108 return false; 109 } 110 111 return CompareExceptions(left.InnerException, right.InnerException); 112 } 113 } 114 } 115