diff --git a/test/Microsoft.TestCommon/Assert.cs b/test/Microsoft.TestCommon/Assert.cs index a33f4c5390..0d7af9a059 100644 --- a/test/Microsoft.TestCommon/Assert.cs +++ b/test/Microsoft.TestCommon/Assert.cs @@ -9,22 +9,5 @@ namespace Microsoft.TestCommon // See files named XxxAssertions for root extensions to Assert. public partial class Assert : Xunit.Assert { - public static readonly ReflectionAssert Reflection = new ReflectionAssert(); - - public static readonly TypeAssert Type = new TypeAssert(); - - public static readonly HttpAssert Http = new HttpAssert(); - - public static readonly MediaTypeAssert MediaType = new MediaTypeAssert(); - - public static readonly GenericTypeAssert GenericType = new GenericTypeAssert(); - - public static readonly SerializerAssert Serializer = new SerializerAssert(); - - public static readonly StreamAssert Stream = new StreamAssert(); - - public static readonly TaskAssert Task = new TaskAssert(); - - public static readonly XmlAssert Xml = new XmlAssert(); } } diff --git a/test/Microsoft.TestCommon/Microsoft.TestCommon.csproj b/test/Microsoft.TestCommon/Microsoft.TestCommon.csproj index dfcafb63ab..fc7c447b43 100644 --- a/test/Microsoft.TestCommon/Microsoft.TestCommon.csproj +++ b/test/Microsoft.TestCommon/Microsoft.TestCommon.csproj @@ -37,17 +37,10 @@ - - - - - - Code - Code @@ -55,13 +48,6 @@ - - - - - - - diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/GenericTypeAssert.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/GenericTypeAssert.cs deleted file mode 100644 index 10c7232787..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/GenericTypeAssert.cs +++ /dev/null @@ -1,490 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; -using System.Linq; -using System.Reflection; - -namespace Microsoft.TestCommon -{ - /// - /// MSTest assertion class to provide convenience and assert methods for generic types - /// whose type parameters are not known at compile time. - /// - public class GenericTypeAssert - { - private static readonly GenericTypeAssert singleton = new GenericTypeAssert(); - - public static GenericTypeAssert Singleton { get { return singleton; } } - - /// - /// Asserts the given is a generic type and creates a new - /// bound generic type using . It then asserts there - /// is a constructor that will accept and returns it. - /// - /// The unbound generic base type. - /// The type of the single generic parameter to apply to create a bound generic type. - /// The list of parameter types for a constructor that must exist. - /// The of that constructor which may be invoked to create that new generic type. - public ConstructorInfo GetConstructor(Type genericBaseType, Type genericParameterType, params Type[] parameterTypes) - { - Assert.NotNull(genericBaseType); - Assert.True(genericBaseType.IsGenericTypeDefinition); - Assert.NotNull(genericParameterType); - Assert.NotNull(parameterTypes); - - Type genericType = genericBaseType.MakeGenericType(new Type[] { genericParameterType }); - ConstructorInfo ctor = genericType.GetConstructor(parameterTypes); - Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<{1}>',", genericBaseType.Name, genericParameterType.Name)); - return ctor; - } - - /// - /// Asserts the given is a generic type and creates a new - /// bound generic type using . It then asserts there - /// is a constructor that will accept and returns it. - /// - /// The unbound generic base type. - /// The types of the generic parameters to apply to create a bound generic type. - /// The list of parameter types for a constructor that must exist. - /// The of that constructor which may be invoked to create that new generic type. - public ConstructorInfo GetConstructor(Type genericBaseType, Type[] genericParameterTypes, params Type[] parameterTypes) - { - Assert.NotNull(genericBaseType); - Assert.True(genericBaseType.IsGenericTypeDefinition); - Assert.NotNull(genericParameterTypes); - Assert.NotNull(parameterTypes); - - Type genericType = genericBaseType.MakeGenericType(genericParameterTypes); - ConstructorInfo ctor = genericType.GetConstructor(parameterTypes); - Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<>',", genericBaseType.Name)); - return ctor; - } - - /// - /// Creates a new bound generic type and invokes the constructor matched from . - /// - /// The unbound generic base type. - /// The type of the single generic parameter to apply to create a bound generic type. - /// The list of parameter types for a constructor that must exist. - /// The list of values to supply to the constructor - /// The instance created by calling that constructor. - public object InvokeConstructor(Type genericBaseType, Type genericParameterType, Type[] parameterTypes, object[] parameterValues) - { - ConstructorInfo ctor = GetConstructor(genericBaseType, genericParameterType, parameterTypes); - Assert.NotNull(parameterValues); - Assert.Equal(parameterTypes.Length, parameterValues.Length); - return ctor.Invoke(parameterValues); - } - - /// - /// Creates a new bound generic type and invokes the constructor matched from . - /// - /// The unbound generic base type. - /// The types of the generic parameters to apply to create a bound generic type. - /// The list of parameter types for a constructor that must exist. - /// The list of values to supply to the constructor - /// The instance created by calling that constructor. - public object InvokeConstructor(Type genericBaseType, Type[] genericParameterTypes, Type[] parameterTypes, object[] parameterValues) - { - ConstructorInfo ctor = GetConstructor(genericBaseType, genericParameterTypes, parameterTypes); - Assert.NotNull(parameterValues); - Assert.Equal(parameterTypes.Length, parameterValues.Length); - return ctor.Invoke(parameterValues); - } - - /// - /// Creates a new bound generic type and invokes the constructor matched from the types of . - /// - /// The unbound generic base type. - /// The type of the single generic parameter to apply to create a bound generic type. - /// The list of values to supply to the constructor. It must be possible to determine the - /// The instance created by calling that constructor. - public object InvokeConstructor(Type genericBaseType, Type genericParameterType, params object[] parameterValues) - { - Assert.NotNull(genericBaseType); - Assert.True(genericBaseType.IsGenericTypeDefinition); - Assert.NotNull(genericParameterType); - - Type genericType = genericBaseType.MakeGenericType(new Type[] { genericParameterType }); - - ConstructorInfo ctor = FindConstructor(genericType, parameterValues); - Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<{1}>',", genericBaseType.Name, genericParameterType.Name)); - return ctor.Invoke(parameterValues); - } - - /// - /// Creates a new bound generic type and invokes the constructor matched from the types of . - /// - /// The unbound generic base type. - /// The types of the generic parameters to apply to create a bound generic type. - /// The list of values to supply to the constructor. It must be possible to determine the - /// The instance created by calling that constructor. - public object InvokeConstructor(Type genericBaseType, Type[] genericParameterTypes, params object[] parameterValues) - { - Assert.NotNull(genericBaseType); - Assert.True(genericBaseType.IsGenericTypeDefinition); - Assert.NotNull(genericParameterTypes); - - Type genericType = genericBaseType.MakeGenericType(genericParameterTypes); - - ConstructorInfo ctor = FindConstructor(genericType, parameterValues); - Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<>',", genericBaseType.Name)); - return ctor.Invoke(parameterValues); - } - - /// - /// Creates a new bound generic type and invokes the constructor matched from . - /// - /// The type of object the constuctor is expected to yield. - /// The unbound generic base type. - /// The type of the single generic parameter to apply to create a bound generic type. - /// The list of parameter types for a constructor that must exist. - /// The list of values to supply to the constructor - /// An instance of type . - public T InvokeConstructor(Type genericBaseType, Type genericParameterType, Type[] parameterTypes, object[] parameterValues) - { - ConstructorInfo ctor = GetConstructor(genericBaseType, genericParameterType, parameterTypes); - Assert.NotNull(parameterValues); - Assert.Equal(parameterTypes.Length, parameterValues.Length); - return (T)ctor.Invoke(parameterValues); - } - - /// - /// Creates a new bound generic type and invokes the constructor matched from . - /// - /// The type of object the constuctor is expected to yield. - /// The unbound generic base type. - /// The types of the generic parameters to apply to create a bound generic type. - /// The list of parameter types for a constructor that must exist. - /// The list of values to supply to the constructor - /// An instance of type . - public T InvokeConstructor(Type genericBaseType, Type[] genericParameterTypes, Type[] parameterTypes, object[] parameterValues) - { - ConstructorInfo ctor = GetConstructor(genericBaseType, genericParameterTypes, parameterTypes); - Assert.NotNull(parameterValues); - Assert.Equal(parameterTypes.Length, parameterValues.Length); - return (T)ctor.Invoke(parameterValues); - } - - /// - /// Creates a new bound generic type and invokes the constructor matched from . - /// - /// The type of object the constuctor is expected to yield. - /// The unbound generic base type. - /// The type of the single generic parameter to apply to create a bound generic type. - /// The list of values to supply to the constructor. It must be possible to determine the - /// The instance created by calling that constructor. - /// An instance of type . - public T InvokeConstructor(Type genericBaseType, Type genericParameterType, params object[] parameterValues) - { - Assert.NotNull(genericBaseType == null); - Assert.True(genericBaseType.IsGenericTypeDefinition); - Assert.NotNull(genericParameterType); - - Type genericType = genericBaseType.MakeGenericType(new Type[] { genericParameterType }); - - ConstructorInfo ctor = FindConstructor(genericType, parameterValues); - Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<{1}>',", genericBaseType.Name, genericParameterType.Name)); - return (T)ctor.Invoke(parameterValues); - } - - /// - /// Creates a new bound generic type and invokes the constructor matched from . - /// - /// The type of object the constuctor is expected to yield. - /// The unbound generic base type. - /// The types of the generic parameters to apply to create a bound generic type. - /// The list of values to supply to the constructor. It must be possible to determine the - /// The instance created by calling that constructor. - /// An instance of type . - public T InvokeConstructor(Type genericBaseType, Type[] genericParameterTypes, params object[] parameterValues) - { - Assert.NotNull(genericBaseType); - Assert.True(genericBaseType.IsGenericTypeDefinition); - Assert.NotNull(genericParameterTypes); - - Type genericType = genericBaseType.MakeGenericType(genericParameterTypes); - - ConstructorInfo ctor = FindConstructor(genericType, parameterValues); - Assert.True(ctor != null, String.Format("Test error: failed to locate generic ctor for type '{0}<>',", genericBaseType.Name)); - return (T)ctor.Invoke(parameterValues); - } - - /// - /// Asserts the given instance is one from a generic type of the specified parameter type. - /// - /// The type of instance. - /// The instance to test. - /// The type of the generic parameter to which the instance's generic type should have been bound. - public void IsCorrectGenericType(T instance, Type genericTypeParameter) - { - Assert.NotNull(instance); - Assert.NotNull(genericTypeParameter); - Assert.True(instance.GetType().IsGenericType); - Type[] genericArguments = instance.GetType().GetGenericArguments(); - Assert.Equal(1, genericArguments.Length); - Assert.Equal(genericTypeParameter, genericArguments[0]); - } - - /// - /// Invokes via Reflection the method on the given instance. - /// - /// The instance to use. - /// The name of the method to call. - /// The types of the parameters to the method. - /// The values to supply to the method. - /// The results of the method. - public object InvokeMethod(object instance, string methodName, Type[] parameterTypes, object[] parameterValues) - { - Assert.NotNull(instance); - Assert.NotNull(parameterTypes); - Assert.NotNull(parameterValues); - Assert.Equal(parameterTypes.Length, parameterValues.Length); - MethodInfo methodInfo = instance.GetType().GetMethod(methodName, parameterTypes); - Assert.NotNull(methodInfo); - return methodInfo.Invoke(instance, parameterValues); - } - - /// - /// Invokes via Reflection the static method on the given type. - /// - /// The type containing the method. - /// The name of the method to call. - /// The types of the parameters to the method. - /// The values to supply to the method. - /// The results of the method. - public object InvokeMethod(Type type, string methodName, Type[] parameterTypes, object[] parameterValues) - { - Assert.NotNull(type); - Assert.NotNull(parameterTypes); - Assert.NotNull(parameterValues); - Assert.Equal(parameterTypes.Length, parameterValues.Length); - MethodInfo methodInfo = type.GetMethod(methodName, parameterTypes); - Assert.NotNull(methodInfo); - return methodInfo.Invoke(null, parameterValues); - } - - /// - /// Invokes via Reflection the static method on the given type. - /// - /// The type containing the method. - /// The name of the method to call. - /// The generic parameter type of the method. - /// The types of the parameters to the method. - /// The values to supply to the method. - /// The results of the method. - public MethodInfo CreateGenericMethod(Type type, string methodName, Type genericParameterType, Type[] parameterTypes) - { - Assert.NotNull(type); - Assert.NotNull(parameterTypes); - Assert.NotNull(genericParameterType); - //MethodInfo methodInfo = type.GetMethod(methodName, parameterTypes); - MethodInfo methodInfo = type.GetMethods().Where((m) => m.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase) && m.IsGenericMethod && AreAssignableFrom(m.GetParameters(), parameterTypes)).FirstOrDefault(); - Assert.NotNull(methodInfo); - Assert.True(methodInfo.IsGenericMethod); - MethodInfo genericMethod = methodInfo.MakeGenericMethod(genericParameterType); - Assert.NotNull(genericMethod); - return genericMethod; - } - - /// - /// Invokes via Reflection the static generic method on the given type. - /// - /// The type containing the method. - /// The name of the method to call. - /// The generic parameter type of the method. - /// The types of the parameters to the method. - /// The values to supply to the method. - /// The results of the method. - public object InvokeGenericMethod(Type type, string methodName, Type genericParameterType, Type[] parameterTypes, object[] parameterValues) - { - MethodInfo methodInfo = CreateGenericMethod(type, methodName, genericParameterType, parameterTypes); - Assert.Equal(parameterTypes.Length, parameterValues.Length); - return methodInfo.Invoke(null, parameterValues); - } - - /// - /// Invokes via Reflection the generic method on the given instance. - /// - /// The instance on which to invoke the method. - /// The name of the method to call. - /// The generic parameter type of the method. - /// The types of the parameters to the method. - /// The values to supply to the method. - /// The results of the method. - public object InvokeGenericMethod(object instance, string methodName, Type genericParameterType, Type[] parameterTypes, object[] parameterValues) - { - Assert.NotNull(instance); - MethodInfo methodInfo = CreateGenericMethod(instance.GetType(), methodName, genericParameterType, parameterTypes); - Assert.Equal(parameterTypes.Length, parameterValues.Length); - return methodInfo.Invoke(instance, parameterValues); - } - - /// - /// Invokes via Reflection the generic method on the given instance. - /// - /// The type of the return value from the method. - /// The instance on which to invoke the method. - /// The name of the method to call. - /// The generic parameter type of the method. - /// The types of the parameters to the method. - /// The values to supply to the method. - /// The results of the method. - public T InvokeGenericMethod(object instance, string methodName, Type genericParameterType, Type[] parameterTypes, object[] parameterValues) - { - return (T)InvokeGenericMethod(instance, methodName, genericParameterType, parameterTypes, parameterValues); - } - - /// - /// Invokes via Reflection the method on the given instance. - /// - /// The instance to use. - /// The name of the method to call. - /// The values to supply to the method. - /// The results of the method. - public object InvokeMethod(object instance, string methodName, params object[] parameterValues) - { - Assert.NotNull(instance); - MethodInfo methodInfo = FindMethod(instance.GetType(), methodName, parameterValues); - Assert.NotNull(methodInfo); - return methodInfo.Invoke(instance, parameterValues); - } - - /// - /// Invokes via Reflection the static method on the given type. - /// - /// The instance to use. - /// The name of the method to call. - /// The values to supply to the method. - /// The results of the method. - public object InvokeMethod(Type type, string methodName, params object[] parameterValues) - { - Assert.NotNull(type); - MethodInfo methodInfo = FindMethod(type, methodName, parameterValues); - Assert.NotNull(methodInfo); - return methodInfo.Invoke(null, parameterValues); - } - - /// - /// Invokes via Reflection the method on the given instance. - /// - /// The instance to use. - /// The name of the method to call. - /// The type of the generic parameter. - /// The values to supply to the method. - /// The results of the method. - public object InvokeGenericMethod(object instance, string methodName, Type genericParameterType, params object[] parameterValues) - { - Assert.NotNull(instance); - Assert.NotNull(genericParameterType); - MethodInfo methodInfo = FindMethod(instance.GetType(), methodName, parameterValues); - Assert.NotNull(methodInfo); - Assert.True(methodInfo.IsGenericMethod); - MethodInfo genericMethod = methodInfo.MakeGenericMethod(genericParameterType); - return genericMethod.Invoke(instance, parameterValues); - } - - /// - /// Invokes via Reflection the method on the given instance. - /// - /// The instance to use. - /// The name of the method to call. - /// The type of the generic parameter. - /// The values to supply to the method. - /// The results of the method. - public object InvokeGenericMethod(Type type, string methodName, Type genericParameterType, params object[] parameterValues) - { - Assert.NotNull(type); - Assert.NotNull(genericParameterType); - MethodInfo methodInfo = FindMethod(type, methodName, parameterValues); - Assert.NotNull(methodInfo); - Assert.True(methodInfo.IsGenericMethod); - MethodInfo genericMethod = methodInfo.MakeGenericMethod(genericParameterType); - return genericMethod.Invoke(null, parameterValues); - } - - /// - /// Retrieves the value from the specified property. - /// - /// The instance containing the property value. - /// The name of the property. - /// The error message to prefix any test assertions. - /// The value returned from the property. - public object GetProperty(object instance, string propertyName, string failureMessage) - { - PropertyInfo propertyInfo = instance.GetType().GetProperty(propertyName); - Assert.NotNull(propertyInfo); - return propertyInfo.GetValue(instance, null); - } - - private static bool AreAssignableFrom(Type[] parameterTypes, params object[] parameterValues) - { - Assert.NotNull(parameterTypes); - Assert.NotNull(parameterValues); - if (parameterTypes.Length != parameterValues.Length) - { - return false; - } - - for (int i = 0; i < parameterTypes.Length; ++i) - { - if (!parameterTypes[i].IsInstanceOfType(parameterValues[i])) - { - return false; - } - } - - return true; - } - - private static bool AreAssignableFrom(ParameterInfo[] parameterInfos, params Type[] parameterTypes) - { - Assert.NotNull(parameterInfos); - Assert.NotNull(parameterTypes); - Type[] parameterInfoTypes = parameterInfos.Select((info) => info.ParameterType).ToArray(); - if (parameterInfoTypes.Length != parameterTypes.Length) - { - return false; - } - - for (int i = 0; i < parameterInfoTypes.Length; ++i) - { - // Generic parameters are assumed to be assignable - if (parameterInfoTypes[i].IsGenericParameter) - { - continue; - } - - if (!parameterInfoTypes[i].IsAssignableFrom(parameterTypes[i])) - { - return false; - } - } - - return true; - } - - private static bool AreAssignableFrom(ParameterInfo[] parameterInfos, params object[] parameterValues) - { - Assert.NotNull(parameterInfos); - Assert.NotNull(parameterValues); - Type[] parameterTypes = parameterInfos.Select((info) => info.ParameterType).ToArray(); - return AreAssignableFrom(parameterTypes, parameterValues); - } - - private static ConstructorInfo FindConstructor(Type type, params object[] parameterValues) - { - Assert.NotNull(type); - Assert.NotNull(parameterValues); - return type.GetConstructors().FirstOrDefault((c) => AreAssignableFrom(c.GetParameters(), parameterValues)); - } - - private static MethodInfo FindMethod(Type type, string methodName, params object[] parameterValues) - { - Assert.NotNull(type); - Assert.False(String.IsNullOrWhiteSpace(methodName)); - Assert.NotNull(parameterValues); - return type.GetMethods().FirstOrDefault((m) => String.Equals(m.Name, methodName, StringComparison.Ordinal) && AreAssignableFrom(m.GetParameters(), parameterValues)); - } - } -} diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/HttpAssert.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/HttpAssert.cs deleted file mode 100644 index 1054ea538f..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/HttpAssert.cs +++ /dev/null @@ -1,256 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Text.RegularExpressions; - -namespace Microsoft.TestCommon -{ - /// - /// Unit test utility for testing instances. - /// - public class HttpAssert - { - private const string CommaSeperator = ", "; - private static readonly HttpAssert singleton = new HttpAssert(); - - public static HttpAssert Singleton { get { return singleton; } } - - /// - /// Asserts that the expected is equal to the actual . - /// - /// The expected . Should not be null. - /// The actual . Should not be null. - public void Equal(HttpRequestMessage expected, HttpRequestMessage actual) - { - Assert.NotNull(expected); - Assert.NotNull(actual); - - Assert.Equal(expected.Version, actual.Version); - Equal(expected.Headers, actual.Headers); - - if (expected.Content == null) - { - Assert.Null(actual.Content); - } - else - { - string expectedContent = CleanContentString(expected.Content.ReadAsStringAsync().Result); - string actualContent = CleanContentString(actual.Content.ReadAsStringAsync().Result); - Assert.Equal(expectedContent, actualContent); - Equal(expected.Content.Headers, actual.Content.Headers); - } - } - - /// - /// Asserts that the expected is equal to the actual . - /// - /// The expected . Should not be null. - /// The actual . Should not be null. - public void Equal(HttpResponseMessage expected, HttpResponseMessage actual) - { - Equal(expected, actual, null); - } - - /// - /// Asserts that the expected is equal to the actual . - /// - /// The expected . Should not be null. - /// The actual . Should not be null. - /// The callback to verify the Content string. If it is null, Assert.Equal will be used. - public void Equal(HttpResponseMessage expected, HttpResponseMessage actual, Action verifyContentStringCallback) - { - Assert.NotNull(expected); - Assert.NotNull(actual); - - Assert.Equal(expected.StatusCode, actual.StatusCode); - Assert.Equal(expected.ReasonPhrase, actual.ReasonPhrase); - Assert.Equal(expected.Version, actual.Version); - Equal(expected.Headers, actual.Headers); - - if (expected.Content == null) - { - Assert.Null(actual.Content); - } - else - { - string expectedContent = CleanContentString(expected.Content.ReadAsStringAsync().Result); - string actualContent = CleanContentString(actual.Content.ReadAsStringAsync().Result); - if (verifyContentStringCallback != null) - { - verifyContentStringCallback(expectedContent, actualContent); - } - else - { - Assert.Equal(expectedContent, actualContent); - } - Equal(expected.Content.Headers, actual.Content.Headers); - } - } - - /// - /// Asserts that the expected instance is equal to the actual instance. - /// - /// The expected instance. Should not be null. - /// The actual instance. Should not be null. - public void Equal(HttpHeaders expectedHeaders, HttpHeaders actualHeaders) - { - Assert.NotNull(expectedHeaders); - Assert.NotNull(actualHeaders); - - Assert.Equal(expectedHeaders.Count(), actualHeaders.Count()); - - foreach (KeyValuePair> expectedHeader in expectedHeaders) - { - KeyValuePair> actualHeader = actualHeaders.FirstOrDefault(h => h.Key == expectedHeader.Key); - Assert.NotNull(actualHeader); - - if (expectedHeader.Key == "Date") - { - HandleDateHeader(expectedHeader.Value.ToArray(), actualHeader.Value.ToArray()); - } - else - { - string expectedHeaderStr = string.Join(CommaSeperator, expectedHeader.Value); - string actualHeaderStr = string.Join(CommaSeperator, actualHeader.Value); - Assert.Equal(expectedHeaderStr, actualHeaderStr); - } - } - } - - /// - /// Asserts the given contain the given - /// for the given . - /// - /// The to examine. It cannot be null. - /// The name of the header. It cannot be empty. - /// The values that must all be present. It cannot be null. - public void Contains(HttpHeaders headers, string name, params string[] values) - { - Assert.NotNull(headers); - Assert.False(String.IsNullOrWhiteSpace(name), "Test error: name cannot be empty."); - Assert.NotNull(values); - - IEnumerable headerValues = null; - bool foundIt = headers.TryGetValues(name, out headerValues); - Assert.True(foundIt); - - foreach (string value in values) - { - Assert.Contains(value, headerValues); - } - } - - public bool IsKnownUnserializableType(Type type, Func isTypeUnserializableCallback) - { - if (isTypeUnserializableCallback != null && isTypeUnserializableCallback(type)) - { - return true; - } - - if (type.IsGenericType) - { - if (typeof(IEnumerable).IsAssignableFrom(type)) - { - if (type.GetMethod("Add") == null) - { - return true; - } - } - - // Generic type -- recursively analyze generic arguments - return IsKnownUnserializableType(type.GetGenericArguments()[0], isTypeUnserializableCallback); - } - - if (type.HasElementType && IsKnownUnserializableType(type.GetElementType(), isTypeUnserializableCallback)) - { - return true; - } - - return false; - } - - public bool IsKnownUnserializable(Type type, object obj, Func isTypeUnserializableCallback) - { - if (IsKnownUnserializableType(type, isTypeUnserializableCallback)) - { - return true; - } - - return obj != null && IsKnownUnserializableType(obj.GetType(), isTypeUnserializableCallback); - } - - public bool IsKnownUnserializable(Type type, object obj) - { - return IsKnownUnserializable(type, obj, null); - } - - public bool CanRoundTrip(Type type) - { - if (typeof(DateTime).IsAssignableFrom(type)) - { - return false; - } - - if (typeof(DateTimeOffset).IsAssignableFrom(type)) - { - return false; - } - - if (type.IsGenericType) - { - foreach (Type genericParameterType in type.GetGenericArguments()) - { - if (!CanRoundTrip(genericParameterType)) - { - return false; - } - } - } - - if (type.HasElementType) - { - return CanRoundTrip(type.GetElementType()); - } - - return true; - } - - private static void HandleDateHeader(string[] expectedDateHeaderValues, string[] actualDateHeaderValues) - { - Assert.Equal(expectedDateHeaderValues.Length, actualDateHeaderValues.Length); - - for (int i = 0; i < expectedDateHeaderValues.Length; i++) - { - DateTime expectedDateTime = DateTime.Parse(expectedDateHeaderValues[i]); - DateTime actualDateTime = DateTime.Parse(actualDateHeaderValues[i]); - - Assert.Equal(expectedDateTime.Year, actualDateTime.Year); - Assert.Equal(expectedDateTime.Month, actualDateTime.Month); - Assert.Equal(expectedDateTime.Day, actualDateTime.Day); - - int hourDifference = Math.Abs(actualDateTime.Hour - expectedDateTime.Hour); - Assert.True(hourDifference <= 1); - - int minuteDifference = Math.Abs(actualDateTime.Minute - expectedDateTime.Minute); - Assert.True(minuteDifference <= 1); - } - } - - private static string CleanContentString(string content) - { - Assert.Null(content); - - string cleanedContent = null; - - // remove any port numbers from Uri's - cleanedContent = Regex.Replace(content, ":\\d+", ""); - - return cleanedContent; - } - } -} diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/MediaTypeAssert.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/MediaTypeAssert.cs deleted file mode 100644 index 0bca8e37f5..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/MediaTypeAssert.cs +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; -using System.Net.Http.Headers; - -namespace Microsoft.TestCommon -{ - public class MediaTypeAssert - { - private static readonly MediaTypeAssert singleton = new MediaTypeAssert(); - - public static MediaTypeAssert Singleton { get { return singleton; } } - - public void AreEqual(MediaTypeHeaderValue expected, MediaTypeHeaderValue actual, string errorMessage) - { - if (expected != null || actual != null) - { - Assert.NotNull(expected); - Assert.Equal(0, new MediaTypeHeaderValueComparer().Compare(expected, actual)); - } - } - - public void AreEqual(MediaTypeHeaderValue expected, string actual, string errorMessage) - { - if (expected != null || !String.IsNullOrEmpty(actual)) - { - MediaTypeHeaderValue actualMediaType = new MediaTypeHeaderValue(actual); - Assert.NotNull(expected); - Assert.Equal(0, new MediaTypeHeaderValueComparer().Compare(expected, actualMediaType)); - } - } - - public void AreEqual(string expected, string actual, string errorMessage) - { - if (!String.IsNullOrEmpty(expected) || !String.IsNullOrEmpty(actual)) - { - Assert.NotNull(expected); - MediaTypeHeaderValue expectedMediaType = new MediaTypeHeaderValue(expected); - MediaTypeHeaderValue actualMediaType = new MediaTypeHeaderValue(actual); - Assert.Equal(0, new MediaTypeHeaderValueComparer().Compare(expectedMediaType, actualMediaType)); - } - } - - public void AreEqual(string expected, MediaTypeHeaderValue actual, string errorMessage) - { - if (!String.IsNullOrEmpty(expected) || actual != null) - { - Assert.NotNull(expected); - MediaTypeHeaderValue expectedMediaType = new MediaTypeHeaderValue(expected); - Assert.Equal(0, new MediaTypeHeaderValueComparer().Compare(expectedMediaType, actual)); - } - } - } -} diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/MediaTypeHeaderValueComparer.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/MediaTypeHeaderValueComparer.cs deleted file mode 100644 index d454146a5c..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/MediaTypeHeaderValueComparer.cs +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.Net.Http.Headers; - -namespace Microsoft.TestCommon -{ - public class MediaTypeHeaderValueComparer : IComparer - { - private static readonly MediaTypeHeaderValueComparer mediaTypeComparer = new MediaTypeHeaderValueComparer(); - - public MediaTypeHeaderValueComparer() - { - } - - public static MediaTypeHeaderValueComparer Comparer - { - get - { - return mediaTypeComparer; - } - } - - public int Compare(MediaTypeHeaderValue mediaType1, MediaTypeHeaderValue mediaType2) - { - ParsedMediaTypeHeaderValue parsedMediaType1 = new ParsedMediaTypeHeaderValue(mediaType1); - ParsedMediaTypeHeaderValue parsedMediaType2 = new ParsedMediaTypeHeaderValue(mediaType2); - - int returnValue = CompareBasedOnQualityFactor(parsedMediaType1, parsedMediaType2); - - if (returnValue == 0) - { - if (!String.Equals(parsedMediaType1.Type, parsedMediaType2.Type, StringComparison.OrdinalIgnoreCase)) - { - if (parsedMediaType1.IsAllMediaRange) - { - return 1; - } - else if (parsedMediaType2.IsAllMediaRange) - { - return -1; - } - } - else if (!String.Equals(parsedMediaType1.SubType, parsedMediaType2.SubType, StringComparison.OrdinalIgnoreCase)) - { - if (parsedMediaType1.IsSubTypeMediaRange) - { - return 1; - } - else if (parsedMediaType2.IsSubTypeMediaRange) - { - return -1; - } - } - else - { - if (!parsedMediaType1.HasNonQualityFactorParameter) - { - if (parsedMediaType2.HasNonQualityFactorParameter) - { - return 1; - } - } - else if (!parsedMediaType2.HasNonQualityFactorParameter) - { - return -1; - } - } - } - - return returnValue; - } - - private static int CompareBasedOnQualityFactor(ParsedMediaTypeHeaderValue parsedMediaType1, ParsedMediaTypeHeaderValue parsedMediaType2) - { - double qualityDifference = parsedMediaType1.QualityFactor - parsedMediaType2.QualityFactor; - if (qualityDifference < 0) - { - return 1; - } - else if (qualityDifference > 0) - { - return -1; - } - - return 0; - } - } -} \ No newline at end of file diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/ParsedMediaTypeHeaderValue.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/ParsedMediaTypeHeaderValue.cs deleted file mode 100644 index a71a21825c..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/ParsedMediaTypeHeaderValue.cs +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; -using System.Net.Http.Headers; - -namespace Microsoft.TestCommon -{ - internal class ParsedMediaTypeHeaderValue - { - private const string MediaRangeAsterisk = "*"; - private const char MediaTypeSubTypeDelimiter = '/'; - private const string QualityFactorParameterName = "q"; - private const double DefaultQualityFactor = 1.0; - - private MediaTypeHeaderValue mediaType; - private string type; - private string subType; - private bool? hasNonQualityFactorParameter; - private double? qualityFactor; - - public ParsedMediaTypeHeaderValue(MediaTypeHeaderValue mediaType) - { - this.mediaType = mediaType; - string[] splitMediaType = mediaType.MediaType.Split(MediaTypeSubTypeDelimiter); - this.type = splitMediaType[0]; - this.subType = splitMediaType[1]; - } - - public string Type - { - get - { - return this.type; - } - } - - public string SubType - { - get - { - return this.subType; - } - } - - public bool IsAllMediaRange - { - get - { - return this.IsSubTypeMediaRange && String.Equals(MediaRangeAsterisk, this.Type, StringComparison.Ordinal); - } - } - - public bool IsSubTypeMediaRange - { - get - { - return String.Equals(MediaRangeAsterisk, this.SubType, StringComparison.Ordinal); - } - } - - public bool HasNonQualityFactorParameter - { - get - { - if (!this.hasNonQualityFactorParameter.HasValue) - { - this.hasNonQualityFactorParameter = false; - foreach (NameValueHeaderValue param in this.mediaType.Parameters) - { - if (!String.Equals(QualityFactorParameterName, param.Name, StringComparison.Ordinal)) - { - this.hasNonQualityFactorParameter = true; - } - } - } - - return this.hasNonQualityFactorParameter.Value; - } - } - - public string CharSet - { - get - { - return this.mediaType.CharSet; - } - } - - public double QualityFactor - { - get - { - if (!this.qualityFactor.HasValue) - { - MediaTypeWithQualityHeaderValue mediaTypeWithQuality = this.mediaType as MediaTypeWithQualityHeaderValue; - if (mediaTypeWithQuality != null) - { - this.qualityFactor = mediaTypeWithQuality.Quality; - } - - if (!this.qualityFactor.HasValue) - { - this.qualityFactor = DefaultQualityFactor; - } - } - - return this.qualityFactor.Value; - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/RegexReplacement.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/RegexReplacement.cs deleted file mode 100644 index 36cf366c02..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/RegexReplacement.cs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System.Text.RegularExpressions; - -namespace Microsoft.TestCommon -{ - public class RegexReplacement - { - Regex regex; - string replacement; - - public RegexReplacement(Regex regex, string replacement) - { - this.regex = regex; - this.replacement = replacement; - } - - public RegexReplacement(string regex, string replacement) - { - this.regex = new Regex(regex); - this.replacement = replacement; - } - - public Regex Regex - { - get - { - return this.regex; - } - } - - public string Replacement - { - get - { - return this.replacement; - } - } - } -} diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/SerializerAssert.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/SerializerAssert.cs deleted file mode 100644 index feaca48ac1..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/SerializerAssert.cs +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; -using System.IO; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Json; -using System.Xml.Serialization; - -namespace Microsoft.TestCommon -{ - /// - /// MSTest utility for testing code operating against a stream. - /// - public class SerializerAssert - { - private static SerializerAssert singleton = new SerializerAssert(); - - public static SerializerAssert Singleton { get { return singleton; } } - - /// - /// Creates a , serializes to it using - /// , rewinds the stream and calls . - /// - /// The type to serialize. It cannot be null. - /// The value to serialize. - /// Code to check the contents of the stream. - public void UsingXmlSerializer(Type type, object objectInstance, Action codeThatChecks) - { - if (type == null) - { - throw new ArgumentNullException("type"); - } - - if (codeThatChecks == null) - { - throw new ArgumentNullException("codeThatChecks"); - } - - XmlSerializer serializer = new XmlSerializer(type); - - using (MemoryStream stream = new MemoryStream()) - { - serializer.Serialize(stream, objectInstance); - - stream.Flush(); - stream.Seek(0L, SeekOrigin.Begin); - - codeThatChecks(stream); - } - } - - /// - /// Creates a , serializes to it using - /// , rewinds the stream and calls . - /// - /// The type to serialize. - /// The value to serialize. - /// Code to check the contents of the stream. - public void UsingXmlSerializer(T objectInstance, Action codeThatChecks) - { - UsingXmlSerializer(typeof(T), objectInstance, codeThatChecks); - } - - /// - /// Creates a , serializes to it using - /// , rewinds the stream and calls . - /// - /// The type to serialize. It cannot be null. - /// The value to serialize. - /// Code to check the contents of the stream. - public void UsingDataContractSerializer(Type type, object objectInstance, Action codeThatChecks) - { - if (type == null) - { - throw new ArgumentNullException("type"); - } - - if (codeThatChecks == null) - { - throw new ArgumentNullException("codeThatChecks"); - } - - DataContractSerializer serializer = new DataContractSerializer(type); - - using (MemoryStream stream = new MemoryStream()) - { - serializer.WriteObject(stream, objectInstance); - - stream.Flush(); - stream.Seek(0L, SeekOrigin.Begin); - - codeThatChecks(stream); - } - } - - /// - /// Creates a , serializes to it using - /// , rewinds the stream and calls . - /// - /// The type to serialize. - /// The value to serialize. - /// Code to check the contents of the stream. - public void UsingDataContractSerializer(T objectInstance, Action codeThatChecks) - { - UsingDataContractSerializer(typeof(T), objectInstance, codeThatChecks); - } - - /// - /// Creates a , serializes to it using - /// , rewinds the stream and calls . - /// - /// The type to serialize. It cannot be null. - /// The value to serialize. - /// Code to check the contents of the stream. - public static void UsingDataContractJsonSerializer(Type type, object objectInstance, Action codeThatChecks) - { - if (type == null) - { - throw new ArgumentNullException("type"); - } - - if (codeThatChecks == null) - { - throw new ArgumentNullException("codeThatChecks"); - } - - DataContractJsonSerializer serializer = new DataContractJsonSerializer(type); - - using (MemoryStream stream = new MemoryStream()) - { - serializer.WriteObject(stream, objectInstance); - - stream.Flush(); - stream.Seek(0L, SeekOrigin.Begin); - - codeThatChecks(stream); - } - } - - /// - /// Creates a , serializes to it using - /// , rewinds the stream and calls . - /// - /// The type to serialize. - /// The value to serialize. - /// Code to check the contents of the stream. - public void UsingDataContractJsonSerializer(T objectInstance, Action codeThatChecks) - { - UsingDataContractJsonSerializer(typeof(T), objectInstance, codeThatChecks); - } - } -} diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/StreamAssert.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/StreamAssert.cs deleted file mode 100644 index c0ab0a5aac..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/StreamAssert.cs +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; -using System.IO; - -namespace Microsoft.TestCommon -{ - //// TODO RONCAIN using System.Runtime.Serialization.Json; - - /// - /// MSTest utility for testing code operating against a stream. - /// - public class StreamAssert - { - private static StreamAssert singleton = new StreamAssert(); - - public static StreamAssert Singleton { get { return singleton; } } - - /// - /// Creates a , invokes to write to it, - /// rewinds the stream to the beginning and invokes . - /// - /// Code to write to the stream. It cannot be null. - /// Code that reads from the stream. It cannot be null. - public void WriteAndRead(Action codeThatWrites, Action codeThatReads) - { - if (codeThatWrites == null) - { - throw new ArgumentNullException("codeThatWrites"); - } - - if (codeThatReads == null) - { - throw new ArgumentNullException("codeThatReads"); - } - - using (MemoryStream stream = new MemoryStream()) - { - codeThatWrites(stream); - - stream.Flush(); - stream.Seek(0L, SeekOrigin.Begin); - - codeThatReads(stream); - } - } - - /// - /// Creates a , invokes to write to it, - /// rewinds the stream to the beginning and invokes to obtain - /// the result to return from this method. - /// - /// Code to write to the stream. It cannot be null. - /// Code that reads from the stream and returns the result. It cannot be null. - /// The value returned from . - public static object WriteAndReadResult(Action codeThatWrites, Func codeThatReads) - { - if (codeThatWrites == null) - { - throw new ArgumentNullException("codeThatWrites"); - } - - if (codeThatReads == null) - { - throw new ArgumentNullException("codeThatReads"); - } - - object result = null; - using (MemoryStream stream = new MemoryStream()) - { - codeThatWrites(stream); - - stream.Flush(); - stream.Seek(0L, SeekOrigin.Begin); - - result = codeThatReads(stream); - } - - return result; - } - - /// - /// Creates a , invokes to write to it, - /// rewinds the stream to the beginning and invokes to obtain - /// the result to return from this method. - /// - /// The type of the result expected. - /// Code to write to the stream. It cannot be null. - /// Code that reads from the stream and returns the result. It cannot be null. - /// The value returned from . - public T WriteAndReadResult(Action codeThatWrites, Func codeThatReads) - { - return (T)WriteAndReadResult(codeThatWrites, codeThatReads); - } - } -} diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/TaskAssert.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/TaskAssert.cs deleted file mode 100644 index b49ae0ddb0..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/TaskAssert.cs +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; -using System.Reflection; -using System.Threading.Tasks; - -namespace Microsoft.TestCommon -{ - /// - /// MSTest assert class to make assertions about tests using . - /// - public class TaskAssert - { - private static int timeOutMs = System.Diagnostics.Debugger.IsAttached ? TimeoutConstant.DefaultTimeout : TimeoutConstant.DefaultTimeout * 10; - private static TaskAssert singleton = new TaskAssert(); - - public static TaskAssert Singleton { get { return singleton; } } - - /// - /// Asserts the given task has been started. TAP guidelines are that all - /// objects returned from public API's have been started. - /// - /// The to test. - public void IsStarted(Task task) - { - Assert.NotNull(task); - Assert.True(task.Status != TaskStatus.Created); - } - - /// - /// Asserts the given task completes successfully. This method will block the - /// current thread waiting for the task, but will timeout if it does not complete. - /// - /// The to test. - public void Succeeds(Task task) - { - IsStarted(task); - task.Wait(timeOutMs); - AggregateException aggregateException = task.Exception; - Exception innerException = aggregateException == null ? null : aggregateException.InnerException; - Assert.Null(innerException); - } - - /// - /// Asserts the given task completes successfully and returns a result. - /// Use this overload for a generic whose generic parameter is not known at compile time. - /// This method will block the current thread waiting for the task, but will timeout if it does not complete. - /// - /// The to test. - /// The result from that task. - public object SucceedsWithResult(Task task) - { - Succeeds(task); - Assert.True(task.GetType().IsGenericType); - Type[] genericArguments = task.GetType().GetGenericArguments(); - Assert.Equal(1, genericArguments.Length); - PropertyInfo resultProperty = task.GetType().GetProperty("Result"); - Assert.NotNull(resultProperty); - return resultProperty.GetValue(task, null); - } - - /// - /// Asserts the given task completes successfully and returns a result. - /// This method will block the current thread waiting for the task, but will timeout if it does not complete. - /// - /// The result of the . - /// The to test. - /// The result from that task. - public T SucceedsWithResult(Task task) - { - Succeeds(task); - return task.Result; - } - - /// - /// Asserts the given completes successfully and yields - /// the expected result. - /// - /// The to test. - /// The expected result. - public void ResultEquals(Task task, object expectedObj) - { - object actualObj = SucceedsWithResult(task); - Assert.Equal(expectedObj, actualObj); - } - - /// - /// Asserts the given completes successfully and yields - /// the expected result. - /// - /// The type the task will return. - /// The task to test. - /// The expected result. - public void ResultEquals(Task task, T expectedObj) - { - T actualObj = SucceedsWithResult(task); - Assert.Equal(expectedObj, actualObj); - } - } -} diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/TypeAssert.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/TypeAssert.cs deleted file mode 100644 index 9eae1d3d9e..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/TypeAssert.cs +++ /dev/null @@ -1,163 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; - -namespace Microsoft.TestCommon -{ - /// - /// MSTest utility for testing that a given type has the expected properties such as being public, sealed, etc. - /// - public class TypeAssert - { - /// - /// Specifies a set of type properties to test for using the method. - /// This enumeration has a attribute that allows a bitwise combination of its member values. - /// - [Flags] - public enum TypeProperties - { - /// - /// Indicates that the type must be abstract. - /// - IsAbstract = 0x1, - - /// - /// Indicates that the type must be a class. - /// - IsClass = 0x2, - - /// - /// Indicates that the type must be a COM object. - /// - IsComObject = 0x4, - - /// - /// Indicates that the type must be disposable. - /// - IsDisposable = 0x8, - - /// - /// Indicates that the type must be an enum. - /// - IsEnum = 0x10, - - /// - /// Indicates that the type must be a generic type. - /// - IsGenericType = 0x20, - - /// - /// Indicates that the type must be a generic type definition. - /// - IsGenericTypeDefinition = 0x40, - - /// - /// Indicates that the type must be an interface. - /// - IsInterface = 0x80, - - /// - /// Indicates that the type must be nested and declared private. - /// - IsNestedPrivate = 0x100, - - /// - /// Indicates that the type must be nested and declared public. - /// - IsNestedPublic = 0x200, - - /// - /// Indicates that the type must be public. - /// - IsPublic = 0x400, - - /// - /// Indicates that the type must be sealed. - /// - IsSealed = 0x800, - - /// - /// Indicates that the type must be visible outside the assembly. - /// - IsVisible = 0x1000, - - /// - /// Indicates that the type must be static. - /// - IsStatic = TypeAssert.TypeProperties.IsAbstract | TypeAssert.TypeProperties.IsSealed, - - /// - /// Indicates that the type must be a public, visible class. - /// - IsPublicVisibleClass = TypeAssert.TypeProperties.IsClass | TypeAssert.TypeProperties.IsPublic | TypeAssert.TypeProperties.IsVisible - } - - private static void CheckProperty(Type type, bool expected, bool actual, string property) - { - Assert.NotNull(type); - Assert.True(expected == actual, String.Format("Type '{0}' should{1} be {2}.", type.FullName, expected ? "" : " NOT", property)); - } - - /// - /// Determines whether the specified type has a given set of properties such as being public, sealed, etc. - /// The method asserts if one or more of the properties are not satisfied. - /// - /// The type to test for properties. - /// The set of type properties to test for. - public void HasProperties(TypeProperties typeProperties) - { - HasProperties(typeof(T), typeProperties); - } - - /// - /// Determines whether the specified type has a given set of properties such as being public, sealed, etc. - /// The method asserts if one or more of the properties are not satisfied. - /// - /// The type to test for properties. - /// Verify that the type to test is assignable from this type. - /// The set of type properties to test for. - public void HasProperties(TypeProperties typeProperties) - { - HasProperties(typeof(T), typeProperties, typeof(TIsAssignableFrom)); - } - - /// - /// Determines whether the specified type has a given set of properties such as being public, sealed, etc. - /// The method asserts if one or more of the properties are not satisfied. - /// - /// The type to test for properties. - /// The set of type properties to test for. - public void HasProperties(Type type, TypeProperties typeProperties) - { - HasProperties(type, typeProperties, null); - } - - /// - /// Determines whether the specified type has a given set of properties such as being public, sealed, etc. - /// The method asserts if one or more of the properties are not satisfied. - /// - /// The type to test for properties. - /// The set of type properties to test for. - /// Verify that the type to test is assignable from this type. - public void HasProperties(Type type, TypeProperties typeProperties, Type isAssignableFrom) - { - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsAbstract) > 0, type.IsAbstract, "abstract"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsClass) > 0, type.IsClass, "a class"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsComObject) > 0, type.IsCOMObject, "a COM object"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsDisposable) > 0, typeof(IDisposable).IsAssignableFrom(type), "disposable"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsEnum) > 0, type.IsEnum, "an enum"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsGenericType) > 0, type.IsGenericType, "a generic type"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsGenericTypeDefinition) > 0, type.IsGenericTypeDefinition, "a generic type definition"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsInterface) > 0, type.IsInterface, "an interface"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsNestedPrivate) > 0, type.IsNestedPrivate, "nested private"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsNestedPublic) > 0, type.IsNestedPublic, "nested public"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsPublic) > 0, type.IsPublic, "public"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsSealed) > 0, type.IsSealed, "sealed"); - TypeAssert.CheckProperty(type, (typeProperties & TypeProperties.IsVisible) > 0, type.IsVisible, "visible"); - if (isAssignableFrom != null) - { - TypeAssert.CheckProperty(type, true, isAssignableFrom.IsAssignableFrom(type), String.Format("assignable from {0}", isAssignableFrom.FullName)); - } - } - } -} \ No newline at end of file diff --git a/test/Microsoft.TestCommon/Microsoft/TestCommon/XmlAssert.cs b/test/Microsoft.TestCommon/Microsoft/TestCommon/XmlAssert.cs deleted file mode 100644 index 4608150b72..0000000000 --- a/test/Microsoft.TestCommon/Microsoft/TestCommon/XmlAssert.cs +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Xml.Linq; - -namespace Microsoft.TestCommon -{ - /// - /// Assert class that compares two XML strings for equality. Namespaces are ignored during comparison - /// - public class XmlAssert - { - public void Equal(string expected, string actual, params RegexReplacement[] regexReplacements) - { - if (regexReplacements != null) - { - for (int i = 0; i < regexReplacements.Length; i++) - { - actual = regexReplacements[i].Regex.Replace(actual, regexReplacements[i].Replacement); - } - } - - Equal(XElement.Parse(expected), XElement.Parse(actual)); - } - - public void Equal(XElement expected, XElement actual) - { - Assert.Equal(Normalize(expected).ToString(), Normalize(actual).ToString()); - } - - private static XElement Normalize(XElement element) - { - if (element.HasElements) - { - return new XElement( - Encode(element.Name), - Normalize(element.Attributes()), - Normalize(element.Elements())); - } - - if (element.IsEmpty) - { - return new XElement( - Encode(element.Name), - Normalize(element.Attributes())); - } - else - { - return new XElement( - Encode(element.Name), - Normalize(element.Attributes()), - element.Value); - } - } - - private static IEnumerable Normalize(IEnumerable attributes) - { - return attributes - .Where((attrib) => !attrib.IsNamespaceDeclaration) - .Select((attrib) => new XAttribute(Encode(attrib.Name), attrib.Value)) - .OrderBy(a => a.Name.ToString()); - } - - private static IEnumerable Normalize(IEnumerable elements) - { - return elements - .Select(e => Normalize(e)) - .OrderBy(a => a.ToString()); - } - - private static string Encode(XName name) - { - return string.Format("{0}_{1}", HttpUtility.UrlEncode(name.NamespaceName).Replace('%', '_'), name.LocalName); - } - } -} \ No newline at end of file diff --git a/test/Microsoft.TestCommon/ReflectionAssert.cs b/test/Microsoft.TestCommon/ReflectionAssert.cs deleted file mode 100644 index 74d0766a44..0000000000 --- a/test/Microsoft.TestCommon/ReflectionAssert.cs +++ /dev/null @@ -1,221 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. - -using System; -using System.Linq.Expressions; -using System.Reflection; - -namespace Microsoft.TestCommon -{ - public class ReflectionAssert - { - private static PropertyInfo GetPropertyInfo(Expression> property) - { - if (property.Body is MemberExpression) - { - return (PropertyInfo)((MemberExpression)property.Body).Member; - } - else if (property.Body is UnaryExpression && property.Body.NodeType == ExpressionType.Convert) - { - return (PropertyInfo)((MemberExpression)((UnaryExpression)property.Body).Operand).Member; - } - else - { - throw new InvalidOperationException("Could not determine property from lambda expression."); - } - } - - private static void TestPropertyValue(TInstance instance, Func getFunc, Action setFunc, TValue valueToSet, TValue valueToCheck) - { - setFunc(instance, valueToSet); - TValue newValue = getFunc(instance); - Assert.Equal(valueToCheck, newValue); - } - - private static void TestPropertyValue(TInstance instance, Func getFunc, Action setFunc, TValue value) - { - TestPropertyValue(instance, getFunc, setFunc, value, value); - } - - public void Property(T instance, Expression> propertyGetter, TResult expectedDefaultValue, bool allowNull = false, TResult roundTripTestValue = null) where TResult : class - { - PropertyInfo property = GetPropertyInfo(propertyGetter); - Func getFunc = (obj) => (TResult)property.GetValue(obj, index: null); - Action setFunc = (obj, value) => property.SetValue(obj, value, index: null); - - Assert.Equal(expectedDefaultValue, getFunc(instance)); - - if (allowNull) - { - TestPropertyValue(instance, getFunc, setFunc, null); - } - else - { - Assert.ThrowsArgumentNull(() => - { - setFunc(instance, null); - }, "value"); - } - - if (roundTripTestValue != null) - { - TestPropertyValue(instance, getFunc, setFunc, roundTripTestValue); - } - } - - public void IntegerProperty(T instance, Expression> propertyGetter, TResult expectedDefaultValue, - TResult? minLegalValue, TResult? illegalLowerValue, - TResult? maxLegalValue, TResult? illegalUpperValue, - TResult roundTripTestValue) where TResult : struct - { - PropertyInfo property = GetPropertyInfo(propertyGetter); - Func getFunc = (obj) => (TResult)property.GetValue(obj, index: null); - Action setFunc = (obj, value) => property.SetValue(obj, value, index: null); - - Assert.Equal(expectedDefaultValue, getFunc(instance)); - - if (minLegalValue.HasValue) - { - TestPropertyValue(instance, getFunc, setFunc, minLegalValue.Value); - } - - if (maxLegalValue.HasValue) - { - TestPropertyValue(instance, getFunc, setFunc, maxLegalValue.Value); - } - - if (illegalLowerValue.HasValue) - { - Assert.ThrowsArgumentGreaterThanOrEqualTo(() => { setFunc(instance, illegalLowerValue.Value); }, "value", minLegalValue.Value.ToString(), illegalLowerValue.Value); - } - - if (illegalUpperValue.HasValue) - { - Assert.ThrowsArgumentLessThanOrEqualTo(() => { setFunc(instance, illegalLowerValue.Value); }, "value", maxLegalValue.Value.ToString(), illegalUpperValue.Value); - } - - TestPropertyValue(instance, getFunc, setFunc, roundTripTestValue); - } - - public void NullableIntegerProperty(T instance, Expression> propertyGetter, TResult? expectedDefaultValue, - TResult? minLegalValue, TResult? illegalLowerValue, - TResult? maxLegalValue, TResult? illegalUpperValue, - TResult roundTripTestValue) where TResult : struct - { - PropertyInfo property = GetPropertyInfo(propertyGetter); - Func getFunc = (obj) => (TResult?)property.GetValue(obj, index: null); - Action setFunc = (obj, value) => property.SetValue(obj, value, index: null); - - Assert.Equal(expectedDefaultValue, getFunc(instance)); - - TestPropertyValue(instance, getFunc, setFunc, null); - - if (minLegalValue.HasValue) - { - TestPropertyValue(instance, getFunc, setFunc, minLegalValue.Value); - } - - if (maxLegalValue.HasValue) - { - TestPropertyValue(instance, getFunc, setFunc, maxLegalValue.Value); - } - - if (illegalLowerValue.HasValue) - { - Assert.ThrowsArgumentGreaterThanOrEqualTo(() => { setFunc(instance, illegalLowerValue.Value); }, "value", minLegalValue.Value.ToString(), illegalLowerValue.Value); - } - - if (illegalUpperValue.HasValue) - { - Assert.ThrowsArgumentLessThanOrEqualTo(() => { setFunc(instance, illegalLowerValue.Value); }, "value", maxLegalValue.Value.ToString(), illegalUpperValue.Value); - } - - TestPropertyValue(instance, getFunc, setFunc, roundTripTestValue); - } - - public void BooleanProperty(T instance, Expression> propertyGetter, bool expectedDefaultValue) - { - PropertyInfo property = GetPropertyInfo(propertyGetter); - Func getFunc = (obj) => (bool)property.GetValue(obj, index: null); - Action setFunc = (obj, value) => property.SetValue(obj, value, index: null); - - Assert.Equal(expectedDefaultValue, getFunc(instance)); - - TestPropertyValue(instance, getFunc, setFunc, !expectedDefaultValue); - } - - public void EnumProperty(T instance, Expression> propertyGetter, TResult expectedDefaultValue, TResult illegalValue, TResult roundTripTestValue) where TResult : struct - { - PropertyInfo property = GetPropertyInfo(propertyGetter); - Func getFunc = (obj) => (TResult)property.GetValue(obj, index: null); - Action setFunc = (obj, value) => property.SetValue(obj, value, index: null); - - Assert.Equal(expectedDefaultValue, getFunc(instance)); - - Assert.ThrowsInvalidEnumArgument(() => { setFunc(instance, illegalValue); }, "value", Convert.ToInt32(illegalValue), typeof(TResult)); - - TestPropertyValue(instance, getFunc, setFunc, roundTripTestValue); - } - - public void EnumPropertyWithoutIllegalValueCheck(T instance, Expression> propertyGetter, TResult expectedDefaultValue, TResult roundTripTestValue) where TResult : struct - { - PropertyInfo property = GetPropertyInfo(propertyGetter); - Func getFunc = (obj) => (TResult)property.GetValue(obj, index: null); - Action setFunc = (obj, value) => property.SetValue(obj, value, index: null); - - Assert.Equal(expectedDefaultValue, getFunc(instance)); - - TestPropertyValue(instance, getFunc, setFunc, roundTripTestValue); - } - - public void StringProperty(T instance, Expression> propertyGetter, string expectedDefaultValue, - bool allowNullAndEmpty = true, bool treatNullAsEmpty = true) - { - PropertyInfo property = GetPropertyInfo(propertyGetter); - Func getFunc = (obj) => (string)property.GetValue(obj, index: null); - Action setFunc = (obj, value) => property.SetValue(obj, value, index: null); - - Assert.Equal(expectedDefaultValue, getFunc(instance)); - - if (allowNullAndEmpty) - { - // Assert get/set works for null - TestPropertyValue(instance, getFunc, setFunc, null, treatNullAsEmpty ? String.Empty : null); - - // Assert get/set works for String.Empty - TestPropertyValue(instance, getFunc, setFunc, String.Empty, String.Empty); - } - else - { - Assert.ThrowsArgumentNullOrEmpty( - delegate() - { - try - { - TestPropertyValue(instance, getFunc, setFunc, null); - } - catch (TargetInvocationException e) - { - throw e.InnerException; - } - }, - "value"); - Assert.ThrowsArgumentNullOrEmpty( - delegate() - { - try - { - TestPropertyValue(instance, getFunc, setFunc, String.Empty); - } - catch (TargetInvocationException e) - { - throw e.InnerException; - } - }, - "value"); - } - - // Assert get/set works for arbitrary value - TestPropertyValue(instance, getFunc, setFunc, "TestValue"); - } - } -}