Removed a lot of the Assert extensibility types
This commit is contained in:
parent
ce7f79ce50
commit
771da62400
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,17 +37,10 @@
|
|||
<Compile Include="ExceptionAssertions.cs" />
|
||||
<Compile Include="FactAttribute.cs" />
|
||||
<Compile Include="InlineDataAttribute.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\GenericTypeAssert.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\MediaTypeHeaderValueComparer.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\ParsedMediaTypeHeaderValue.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\RegexReplacement.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\TimeoutConstant.cs" />
|
||||
<Compile Include="Platform.cs" />
|
||||
<Compile Include="PlatformInfo.cs" />
|
||||
<Compile Include="PropertyDataAttribute.cs" />
|
||||
<Compile Include="ReflectionAssert.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="ReplaceCultureAttribute.cs" />
|
||||
<Compile Include="Assert.cs">
|
||||
<SubType>Code</SubType>
|
||||
|
|
@ -55,13 +48,6 @@
|
|||
<Compile Include="CultureReplacer.cs" />
|
||||
<Compile Include="TestFile.cs" />
|
||||
<Compile Include="TheoryAttribute.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\HttpAssert.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\MediaTypeAssert.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\SerializerAssert.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\StreamAssert.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\TaskAssert.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\TypeAssert.cs" />
|
||||
<Compile Include="Microsoft\TestCommon\XmlAssert.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// MSTest assertion class to provide convenience and assert methods for generic types
|
||||
/// whose type parameters are not known at compile time.
|
||||
/// </summary>
|
||||
public class GenericTypeAssert
|
||||
{
|
||||
private static readonly GenericTypeAssert singleton = new GenericTypeAssert();
|
||||
|
||||
public static GenericTypeAssert Singleton { get { return singleton; } }
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the given <paramref name="genericBaseType"/> is a generic type and creates a new
|
||||
/// bound generic type using <paramref name="genericParameterType"/>. It then asserts there
|
||||
/// is a constructor that will accept <paramref name="parameterTypes"/> and returns it.
|
||||
/// </summary>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
|
||||
/// <returns>The <see cref="ConstructorInfo"/> of that constructor which may be invoked to create that new generic type.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the given <paramref name="genericBaseType"/> is a generic type and creates a new
|
||||
/// bound generic type using <paramref name="genericParameterType"/>. It then asserts there
|
||||
/// is a constructor that will accept <paramref name="parameterTypes"/> and returns it.
|
||||
/// </summary>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterTypes">The types of the generic parameters to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
|
||||
/// <returns>The <see cref="ConstructorInfo"/> of that constructor which may be invoked to create that new generic type.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
|
||||
/// </summary>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
|
||||
/// <param name="parameterValues">The list of values to supply to the constructor</param>
|
||||
/// <returns>The instance created by calling that constructor.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
|
||||
/// </summary>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterTypse">The types of the generic parameters to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
|
||||
/// <param name="parameterValues">The list of values to supply to the constructor</param>
|
||||
/// <returns>The instance created by calling that constructor.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new bound generic type and invokes the constructor matched from the types of <paramref name="parameterValues"/>.
|
||||
/// </summary>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterValues">The list of values to supply to the constructor. It must be possible to determine the</param>
|
||||
/// <returns>The instance created by calling that constructor.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new bound generic type and invokes the constructor matched from the types of <paramref name="parameterValues"/>.
|
||||
/// </summary>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterTypes">The types of the generic parameters to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterValues">The list of values to supply to the constructor. It must be possible to determine the</param>
|
||||
/// <returns>The instance created by calling that constructor.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object the constuctor is expected to yield.</typeparam>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
|
||||
/// <param name="parameterValues">The list of values to supply to the constructor</param>
|
||||
/// <returns>An instance of type <typeparamref name="T"/>.</returns>
|
||||
public T InvokeConstructor<T>(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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object the constuctor is expected to yield.</typeparam>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterTypes">The types of the generic parameters to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterTypes">The list of parameter types for a constructor that must exist.</param>
|
||||
/// <param name="parameterValues">The list of values to supply to the constructor</param>
|
||||
/// <returns>An instance of type <typeparamref name="T"/>.</returns>
|
||||
public T InvokeConstructor<T>(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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object the constuctor is expected to yield.</typeparam>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterType">The type of the single generic parameter to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterValues">The list of values to supply to the constructor. It must be possible to determine the</param>
|
||||
/// <returns>The instance created by calling that constructor.</returns>
|
||||
/// <returns>An instance of type <typeparamref name="T"/>.</returns>
|
||||
public T InvokeConstructor<T>(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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new bound generic type and invokes the constructor matched from <see cref="parameterTypes"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object the constuctor is expected to yield.</typeparam>
|
||||
/// <param name="genericBaseType">The unbound generic base type.</param>
|
||||
/// <param name="genericParameterTypes">The types of the generic parameters to apply to create a bound generic type.</param>
|
||||
/// <param name="parameterValues">The list of values to supply to the constructor. It must be possible to determine the</param>
|
||||
/// <returns>The instance created by calling that constructor.</returns>
|
||||
/// <returns>An instance of type <typeparamref name="T"/>.</returns>
|
||||
public T InvokeConstructor<T>(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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the given instance is one from a generic type of the specified parameter type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of instance.</typeparam>
|
||||
/// <param name="instance">The instance to test.</param>
|
||||
/// <param name="genericTypeParameter">The type of the generic parameter to which the instance's generic type should have been bound.</param>
|
||||
public void IsCorrectGenericType<T>(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]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the method on the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to use.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="parameterTypes">The types of the parameters to the method.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the static method on the given type.
|
||||
/// </summary>
|
||||
/// <param name="type">The type containing the method.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="parameterTypes">The types of the parameters to the method.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the static method on the given type.
|
||||
/// </summary>
|
||||
/// <param name="type">The type containing the method.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="genericParameterType">The generic parameter type of the method.</param>
|
||||
/// <param name="parameterTypes">The types of the parameters to the method.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the static generic method on the given type.
|
||||
/// </summary>
|
||||
/// <param name="type">The type containing the method.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="genericParameterType">The generic parameter type of the method.</param>
|
||||
/// <param name="parameterTypes">The types of the parameters to the method.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the generic method on the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance on which to invoke the method.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="genericParameterType">The generic parameter type of the method.</param>
|
||||
/// <param name="parameterTypes">The types of the parameters to the method.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the generic method on the given instance.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the return value from the method.</typeparam>
|
||||
/// <param name="instance">The instance on which to invoke the method.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="genericParameterType">The generic parameter type of the method.</param>
|
||||
/// <param name="parameterTypes">The types of the parameters to the method.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
public T InvokeGenericMethod<T>(object instance, string methodName, Type genericParameterType, Type[] parameterTypes, object[] parameterValues)
|
||||
{
|
||||
return (T)InvokeGenericMethod(instance, methodName, genericParameterType, parameterTypes, parameterValues);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the method on the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to use.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the static method on the given type.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to use.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the method on the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to use.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="genericParameterType">The type of the generic parameter.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes via Reflection the method on the given instance.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance to use.</param>
|
||||
/// <param name="methodName">The name of the method to call.</param>
|
||||
/// <param name="genericParameterType">The type of the generic parameter.</param>
|
||||
/// <param name="parameterValues">The values to supply to the method.</param>
|
||||
/// <returns>The results of the method.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the value from the specified property.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance containing the property value.</param>
|
||||
/// <param name="propertyName">The name of the property.</param>
|
||||
/// <param name="failureMessage">The error message to prefix any test assertions.</param>
|
||||
/// <returns>The value returned from the property.</returns>
|
||||
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<ParameterInfo, Type>((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<ParameterInfo, Type>((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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Unit test utility for testing <see cref="HttpResponseMessage"/> instances.
|
||||
/// </summary>
|
||||
public class HttpAssert
|
||||
{
|
||||
private const string CommaSeperator = ", ";
|
||||
private static readonly HttpAssert singleton = new HttpAssert();
|
||||
|
||||
public static HttpAssert Singleton { get { return singleton; } }
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the expected <see cref="HttpRequestMessage"/> is equal to the actual <see cref="HttpRequestMessage"/>.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected <see cref="HttpRequestMessage"/>. Should not be <c>null</c>.</param>
|
||||
/// <param name="actual">The actual <see cref="HttpRequestMessage"/>. Should not be <c>null</c>.</param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the expected <see cref="HttpResponseMessage"/> is equal to the actual <see cref="HttpResponseMessage"/>.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected <see cref="HttpResponseMessage"/>. Should not be <c>null</c>.</param>
|
||||
/// <param name="actual">The actual <see cref="HttpResponseMessage"/>. Should not be <c>null</c>.</param>
|
||||
public void Equal(HttpResponseMessage expected, HttpResponseMessage actual)
|
||||
{
|
||||
Equal(expected, actual, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the expected <see cref="HttpResponseMessage"/> is equal to the actual <see cref="HttpResponseMessage"/>.
|
||||
/// </summary>
|
||||
/// <param name="expected">The expected <see cref="HttpResponseMessage"/>. Should not be <c>null</c>.</param>
|
||||
/// <param name="actual">The actual <see cref="HttpResponseMessage"/>. Should not be <c>null</c>.</param>
|
||||
/// <param name="verifyContentCallback">The callback to verify the Content string. If it is null, Assert.Equal will be used. </param>
|
||||
public void Equal(HttpResponseMessage expected, HttpResponseMessage actual, Action<string, string> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts that the expected <see cref="HttpHeaders"/> instance is equal to the actual <see cref="actualHeaders"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="expectedHeaders">The expected <see cref="HttpHeaders"/> instance. Should not be <c>null</c>.</param>
|
||||
/// <param name="actualHeaders">The actual <see cref="HttpHeaders"/> instance. Should not be <c>null</c>.</param>
|
||||
public void Equal(HttpHeaders expectedHeaders, HttpHeaders actualHeaders)
|
||||
{
|
||||
Assert.NotNull(expectedHeaders);
|
||||
Assert.NotNull(actualHeaders);
|
||||
|
||||
Assert.Equal(expectedHeaders.Count(), actualHeaders.Count());
|
||||
|
||||
foreach (KeyValuePair<string, IEnumerable<string>> expectedHeader in expectedHeaders)
|
||||
{
|
||||
KeyValuePair<string, IEnumerable<string>> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the given <see cref="HttpHeaders"/> contain the given <paramref name="values"/>
|
||||
/// for the given <paramref name="name"/>.
|
||||
/// </summary>
|
||||
/// <param name="headers">The <see cref="HttpHeaders"/> to examine. It cannot be <c>null</c>.</param>
|
||||
/// <param name="name">The name of the header. It cannot be empty.</param>
|
||||
/// <param name="values">The values that must all be present. It cannot be null.</param>
|
||||
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<string> 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<Type, bool> 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<Type, bool> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<MediaTypeHeaderValue>
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// MSTest utility for testing code operating against a stream.
|
||||
/// </summary>
|
||||
public class SerializerAssert
|
||||
{
|
||||
private static SerializerAssert singleton = new SerializerAssert();
|
||||
|
||||
public static SerializerAssert Singleton { get { return singleton; } }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
|
||||
/// <see cref="XmlSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to serialize. It cannot be <c>null</c>.</param>
|
||||
/// <param name="objectInstance">The value to serialize.</param>
|
||||
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
|
||||
public void UsingXmlSerializer(Type type, object objectInstance, Action<Stream> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
|
||||
/// <see cref="XmlSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to serialize.</typeparam>
|
||||
/// <param name="objectInstance">The value to serialize.</param>
|
||||
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
|
||||
public void UsingXmlSerializer<T>(T objectInstance, Action<Stream> codeThatChecks)
|
||||
{
|
||||
UsingXmlSerializer(typeof(T), objectInstance, codeThatChecks);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
|
||||
/// <see cref="DataContractSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to serialize. It cannot be <c>null</c>.</param>
|
||||
/// <param name="objectInstance">The value to serialize.</param>
|
||||
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
|
||||
public void UsingDataContractSerializer(Type type, object objectInstance, Action<Stream> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
|
||||
/// <see cref="DataContractSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to serialize.</typeparam>
|
||||
/// <param name="objectInstance">The value to serialize.</param>
|
||||
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
|
||||
public void UsingDataContractSerializer<T>(T objectInstance, Action<Stream> codeThatChecks)
|
||||
{
|
||||
UsingDataContractSerializer(typeof(T), objectInstance, codeThatChecks);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
|
||||
/// <see cref="DataContractJsonSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to serialize. It cannot be <c>null</c>.</param>
|
||||
/// <param name="objectInstance">The value to serialize.</param>
|
||||
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
|
||||
public static void UsingDataContractJsonSerializer(Type type, object objectInstance, Action<Stream> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="Stream"/>, serializes <paramref name="objectInstance"/> to it using
|
||||
/// <see cref="DataContractJsonSerializer"/>, rewinds the stream and calls <see cref="codeThatChecks"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to serialize.</typeparam>
|
||||
/// <param name="objectInstance">The value to serialize.</param>
|
||||
/// <param name="codeThatChecks">Code to check the contents of the stream.</param>
|
||||
public void UsingDataContractJsonSerializer<T>(T objectInstance, Action<Stream> codeThatChecks)
|
||||
{
|
||||
UsingDataContractJsonSerializer(typeof(T), objectInstance, codeThatChecks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// MSTest utility for testing code operating against a stream.
|
||||
/// </summary>
|
||||
public class StreamAssert
|
||||
{
|
||||
private static StreamAssert singleton = new StreamAssert();
|
||||
|
||||
public static StreamAssert Singleton { get { return singleton; } }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="MemoryStream"/>, invokes <paramref name="codeThatWrites"/> to write to it,
|
||||
/// rewinds the stream to the beginning and invokes <paramref name="codeThatReads"/>.
|
||||
/// </summary>
|
||||
/// <param name="codeThatWrites">Code to write to the stream. It cannot be <c>null</c>.</param>
|
||||
/// <param name="codeThatReads">Code that reads from the stream. It cannot be <c>null</c>.</param>
|
||||
public void WriteAndRead(Action<MemoryStream> codeThatWrites, Action<Stream> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="Stream"/>, invokes <paramref name="codeThatWrites"/> to write to it,
|
||||
/// rewinds the stream to the beginning and invokes <paramref name="codeThatReads"/> to obtain
|
||||
/// the result to return from this method.
|
||||
/// </summary>
|
||||
/// <param name="codeThatWrites">Code to write to the stream. It cannot be <c>null</c>.</param>
|
||||
/// <param name="codeThatReads">Code that reads from the stream and returns the result. It cannot be <c>null</c>.</param>
|
||||
/// <returns>The value returned from <paramref name="codeThatReads"/>.</returns>
|
||||
public static object WriteAndReadResult(Action<Stream> codeThatWrites, Func<Stream, object> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="Stream"/>, invokes <paramref name="codeThatWrites"/> to write to it,
|
||||
/// rewinds the stream to the beginning and invokes <paramref name="codeThatReads"/> to obtain
|
||||
/// the result to return from this method.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the result expected.</typeparam>
|
||||
/// <param name="codeThatWrites">Code to write to the stream. It cannot be <c>null</c>.</param>
|
||||
/// <param name="codeThatReads">Code that reads from the stream and returns the result. It cannot be <c>null</c>.</param>
|
||||
/// <returns>The value returned from <paramref name="codeThatReads"/>.</returns>
|
||||
public T WriteAndReadResult<T>(Action<Stream> codeThatWrites, Func<Stream, object> codeThatReads)
|
||||
{
|
||||
return (T)WriteAndReadResult(codeThatWrites, codeThatReads);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// MSTest assert class to make assertions about tests using <see cref="Task"/>.
|
||||
/// </summary>
|
||||
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; } }
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the given task has been started. TAP guidelines are that all
|
||||
/// <see cref="Task"/> objects returned from public API's have been started.
|
||||
/// </summary>
|
||||
/// <param name="task">The <see cref="Task"/> to test.</param>
|
||||
public void IsStarted(Task task)
|
||||
{
|
||||
Assert.NotNull(task);
|
||||
Assert.True(task.Status != TaskStatus.Created);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="task">The <see cref="Task"/> to test.</param>
|
||||
public void Succeeds(Task task)
|
||||
{
|
||||
IsStarted(task);
|
||||
task.Wait(timeOutMs);
|
||||
AggregateException aggregateException = task.Exception;
|
||||
Exception innerException = aggregateException == null ? null : aggregateException.InnerException;
|
||||
Assert.Null(innerException);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the given task completes successfully and returns a result.
|
||||
/// Use this overload for a generic <see cref="Task"/> 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.
|
||||
/// </summary>
|
||||
/// <param name="task">The <see cref="Task"/> to test.</param>
|
||||
/// <returns>The result from that task.</returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the given task completes successfully and returns a <typeparamref name="T"/> result.
|
||||
/// This method will block the current thread waiting for the task, but will timeout if it does not complete.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The result of the <see cref="Task"/>.</typeparam>
|
||||
/// <param name="task">The <see cref="Task"/> to test.</param>
|
||||
/// <returns>The result from that task.</returns>
|
||||
public T SucceedsWithResult<T>(Task<T> task)
|
||||
{
|
||||
Succeeds(task);
|
||||
return task.Result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the given <see cref="Task"/> completes successfully and yields
|
||||
/// the expected result.
|
||||
/// </summary>
|
||||
/// <param name="task">The <see cref="Task"/> to test.</param>
|
||||
/// <param name="expectedObj">The expected result.</param>
|
||||
public void ResultEquals(Task task, object expectedObj)
|
||||
{
|
||||
object actualObj = SucceedsWithResult(task);
|
||||
Assert.Equal(expectedObj, actualObj);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asserts the given <see cref="Task"/> completes successfully and yields
|
||||
/// the expected result.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type the task will return.</typeparam>
|
||||
/// <param name="task">The task to test.</param>
|
||||
/// <param name="expectedObj">The expected result.</param>
|
||||
public void ResultEquals<T>(Task<T> task, T expectedObj)
|
||||
{
|
||||
T actualObj = SucceedsWithResult<T>(task);
|
||||
Assert.Equal(expectedObj, actualObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// MSTest utility for testing that a given type has the expected properties such as being public, sealed, etc.
|
||||
/// </summary>
|
||||
public class TypeAssert
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies a set of type properties to test for using the <see cref="CheckProperty"/> method.
|
||||
/// This enumeration has a <see cref="FlagsAttribute"/> attribute that allows a bitwise combination of its member values.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum TypeProperties
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that the type must be abstract.
|
||||
/// </summary>
|
||||
IsAbstract = 0x1,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be a class.
|
||||
/// </summary>
|
||||
IsClass = 0x2,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be a COM object.
|
||||
/// </summary>
|
||||
IsComObject = 0x4,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be disposable.
|
||||
/// </summary>
|
||||
IsDisposable = 0x8,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be an enum.
|
||||
/// </summary>
|
||||
IsEnum = 0x10,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be a generic type.
|
||||
/// </summary>
|
||||
IsGenericType = 0x20,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be a generic type definition.
|
||||
/// </summary>
|
||||
IsGenericTypeDefinition = 0x40,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be an interface.
|
||||
/// </summary>
|
||||
IsInterface = 0x80,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be nested and declared private.
|
||||
/// </summary>
|
||||
IsNestedPrivate = 0x100,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be nested and declared public.
|
||||
/// </summary>
|
||||
IsNestedPublic = 0x200,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be public.
|
||||
/// </summary>
|
||||
IsPublic = 0x400,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be sealed.
|
||||
/// </summary>
|
||||
IsSealed = 0x800,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be visible outside the assembly.
|
||||
/// </summary>
|
||||
IsVisible = 0x1000,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be static.
|
||||
/// </summary>
|
||||
IsStatic = TypeAssert.TypeProperties.IsAbstract | TypeAssert.TypeProperties.IsSealed,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the type must be a public, visible class.
|
||||
/// </summary>
|
||||
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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to test for properties.</typeparam>
|
||||
/// <param name="typeProperties">The set of type properties to test for.</param>
|
||||
public void HasProperties<T>(TypeProperties typeProperties)
|
||||
{
|
||||
HasProperties(typeof(T), typeProperties);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to test for properties.</typeparam>
|
||||
/// <typeparam name="TIsAssignableFrom">Verify that the type to test is assignable from this type.</typeparam>
|
||||
/// <param name="typeProperties">The set of type properties to test for.</param>
|
||||
public void HasProperties<T, TIsAssignableFrom>(TypeProperties typeProperties)
|
||||
{
|
||||
HasProperties(typeof(T), typeProperties, typeof(TIsAssignableFrom));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to test for properties.</param>
|
||||
/// <param name="typeProperties">The set of type properties to test for.</param>
|
||||
public void HasProperties(Type type, TypeProperties typeProperties)
|
||||
{
|
||||
HasProperties(type, typeProperties, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="type">The type to test for properties.</param>
|
||||
/// <param name="typeProperties">The set of type properties to test for.</param>
|
||||
/// <param name="isAssignableFrom">Verify that the type to test is assignable from this type.</param>
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Assert class that compares two XML strings for equality. Namespaces are ignored during comparison
|
||||
/// </summary>
|
||||
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<XAttribute> Normalize(IEnumerable<XAttribute> attributes)
|
||||
{
|
||||
return attributes
|
||||
.Where((attrib) => !attrib.IsNamespaceDeclaration)
|
||||
.Select((attrib) => new XAttribute(Encode(attrib.Name), attrib.Value))
|
||||
.OrderBy(a => a.Name.ToString());
|
||||
}
|
||||
|
||||
private static IEnumerable<XElement> Normalize(IEnumerable<XElement> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<T, TProp>(Expression<Func<T, TProp>> 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, TValue>(TInstance instance, Func<TInstance, TValue> getFunc, Action<TInstance, TValue> setFunc, TValue valueToSet, TValue valueToCheck)
|
||||
{
|
||||
setFunc(instance, valueToSet);
|
||||
TValue newValue = getFunc(instance);
|
||||
Assert.Equal(valueToCheck, newValue);
|
||||
}
|
||||
|
||||
private static void TestPropertyValue<TInstance, TValue>(TInstance instance, Func<TInstance, TValue> getFunc, Action<TInstance, TValue> setFunc, TValue value)
|
||||
{
|
||||
TestPropertyValue(instance, getFunc, setFunc, value, value);
|
||||
}
|
||||
|
||||
public void Property<T, TResult>(T instance, Expression<Func<T, TResult>> propertyGetter, TResult expectedDefaultValue, bool allowNull = false, TResult roundTripTestValue = null) where TResult : class
|
||||
{
|
||||
PropertyInfo property = GetPropertyInfo(propertyGetter);
|
||||
Func<T, TResult> getFunc = (obj) => (TResult)property.GetValue(obj, index: null);
|
||||
Action<T, TResult> 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, TResult>(T instance, Expression<Func<T, TResult>> propertyGetter, TResult expectedDefaultValue,
|
||||
TResult? minLegalValue, TResult? illegalLowerValue,
|
||||
TResult? maxLegalValue, TResult? illegalUpperValue,
|
||||
TResult roundTripTestValue) where TResult : struct
|
||||
{
|
||||
PropertyInfo property = GetPropertyInfo(propertyGetter);
|
||||
Func<T, TResult> getFunc = (obj) => (TResult)property.GetValue(obj, index: null);
|
||||
Action<T, TResult> 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, TResult>(T instance, Expression<Func<T, TResult?>> propertyGetter, TResult? expectedDefaultValue,
|
||||
TResult? minLegalValue, TResult? illegalLowerValue,
|
||||
TResult? maxLegalValue, TResult? illegalUpperValue,
|
||||
TResult roundTripTestValue) where TResult : struct
|
||||
{
|
||||
PropertyInfo property = GetPropertyInfo(propertyGetter);
|
||||
Func<T, TResult?> getFunc = (obj) => (TResult?)property.GetValue(obj, index: null);
|
||||
Action<T, TResult?> 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>(T instance, Expression<Func<T, bool>> propertyGetter, bool expectedDefaultValue)
|
||||
{
|
||||
PropertyInfo property = GetPropertyInfo(propertyGetter);
|
||||
Func<T, bool> getFunc = (obj) => (bool)property.GetValue(obj, index: null);
|
||||
Action<T, bool> setFunc = (obj, value) => property.SetValue(obj, value, index: null);
|
||||
|
||||
Assert.Equal(expectedDefaultValue, getFunc(instance));
|
||||
|
||||
TestPropertyValue(instance, getFunc, setFunc, !expectedDefaultValue);
|
||||
}
|
||||
|
||||
public void EnumProperty<T, TResult>(T instance, Expression<Func<T, TResult>> propertyGetter, TResult expectedDefaultValue, TResult illegalValue, TResult roundTripTestValue) where TResult : struct
|
||||
{
|
||||
PropertyInfo property = GetPropertyInfo(propertyGetter);
|
||||
Func<T, TResult> getFunc = (obj) => (TResult)property.GetValue(obj, index: null);
|
||||
Action<T, TResult> 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, TResult>(T instance, Expression<Func<T, TResult>> propertyGetter, TResult expectedDefaultValue, TResult roundTripTestValue) where TResult : struct
|
||||
{
|
||||
PropertyInfo property = GetPropertyInfo(propertyGetter);
|
||||
Func<T, TResult> getFunc = (obj) => (TResult)property.GetValue(obj, index: null);
|
||||
Action<T, TResult> setFunc = (obj, value) => property.SetValue(obj, value, index: null);
|
||||
|
||||
Assert.Equal(expectedDefaultValue, getFunc(instance));
|
||||
|
||||
TestPropertyValue(instance, getFunc, setFunc, roundTripTestValue);
|
||||
}
|
||||
|
||||
public void StringProperty<T>(T instance, Expression<Func<T, string>> propertyGetter, string expectedDefaultValue,
|
||||
bool allowNullAndEmpty = true, bool treatNullAsEmpty = true)
|
||||
{
|
||||
PropertyInfo property = GetPropertyInfo(propertyGetter);
|
||||
Func<T, string> getFunc = (obj) => (string)property.GetValue(obj, index: null);
|
||||
Action<T, string> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue