Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Common/src/Interop/Interop.Libraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public static partial class Libraries
public const string Kernel32 = "kernel32.dll";
public const string Ole32 = "ole32.dll";
public const string NtDll = "ntdll.dll";
public const string RichEdit = "RichEd20.DLL";
public const string RichEdit41 = "MsftEdit.DLL";
public const string Shell32 = "shell32.dll";
public const string User32 = "user32.dll";
Expand Down
326 changes: 1 addition & 325 deletions src/Common/tests/InternalUtilitiesForTests/src/AssertExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,12 @@
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Xunit;
using Xunit.Sdk;
using System.Linq;

namespace System
{
public static class AssertExtensions
{
private static bool IsFullFramework => RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework");

public static void Throws<T>(Action action, string message)
where T : Exception
{
Assert.Equal(Assert.Throws<T>(action).Message, message);
}

public static void Throws<T>(string netCoreParamName, string netFxParamName, Action action)
where T : ArgumentException
{
T exception = Assert.Throws<T>(action);

if (netFxParamName == null && IsFullFramework)
{
// Param name varies between NETFX versions -- skip checking it
return;
}

string expectedParamName =
IsFullFramework ?
netFxParamName : netCoreParamName;

if (!RuntimeInformation.FrameworkDescription.StartsWith(".NET Native"))
{
Assert.Equal(expectedParamName, exception.ParamName);
}
}

public static void Throws<T>(string netCoreParamName, string netFxParamName, Func<object> testCode)
where T : ArgumentException
{
T exception = Assert.Throws<T>(testCode);

if (netFxParamName == null && IsFullFramework)
{
// Param name varies between NETFX versions -- skip checking it
return;
}

string expectedParamName =
IsFullFramework ?
netFxParamName : netCoreParamName;

if (!RuntimeInformation.FrameworkDescription.StartsWith(".NET Native"))
{
Assert.Equal(expectedParamName, exception.ParamName);
}
}

public static T Throws<T>(string paramName, Action action)
where T : ArgumentException
{
T exception = Assert.Throws<T>(action);

if (!RuntimeInformation.FrameworkDescription.StartsWith(".NET Native"))
{
Assert.Equal(paramName, exception.ParamName);
}

return exception;
}

public static T Throws<T>(Action action)
where T : Exception
{
T exception = Assert.Throws<T>(action);

return exception;
}

public static T Throws<T>(string paramName, Func<object> testCode)
where T : ArgumentException
{
Expand All @@ -95,255 +21,5 @@ public static T Throws<T>(string paramName, Func<object> testCode)

return exception;
}

public static async Task<T> ThrowsAsync<T>(string paramName, Func<Task> testCode)
where T : ArgumentException
{
T exception = await Assert.ThrowsAsync<T>(testCode);

if (!RuntimeInformation.FrameworkDescription.StartsWith(".NET Native"))
{
Assert.Equal(paramName, exception.ParamName);
}

return exception;
}

public static void Throws<TNetCoreExceptionType, TNetFxExceptionType>(string paramName, Action action)
where TNetCoreExceptionType : ArgumentException
where TNetFxExceptionType : ArgumentException
{
Throws<TNetCoreExceptionType, TNetFxExceptionType>(paramName, paramName, action);
}

public static Exception Throws<TNetCoreExceptionType, TNetFxExceptionType>(Action action)
where TNetCoreExceptionType : Exception
where TNetFxExceptionType : Exception
{
return Throws(typeof(TNetCoreExceptionType), typeof(TNetFxExceptionType), action);
}

public static Exception Throws(Type netCoreExceptionType, Type netFxExceptionType, Action action)
{
if (IsFullFramework)
{
return Assert.Throws(netFxExceptionType, action);
}
else
{
return Assert.Throws(netCoreExceptionType, action);
}
}

public static void Throws<TNetCoreExceptionType, TNetFxExceptionType>(string netCoreParamName, string netFxParamName, Action action)
where TNetCoreExceptionType : ArgumentException
where TNetFxExceptionType : ArgumentException
{
if (IsFullFramework)
{
Throws<TNetFxExceptionType>(netFxParamName, action);
}
else
{
Throws<TNetCoreExceptionType>(netCoreParamName, action);
}
}

public static void ThrowsAny(Type firstExceptionType, Type secondExceptionType, Action action)
{
ThrowsAnyInternal(action, firstExceptionType, secondExceptionType);
}

private static void ThrowsAnyInternal(Action action, params Type[] exceptionTypes)
{
try
{
action();
}
catch (Exception e)
{
Type exceptionType = e.GetType();
if (exceptionTypes.Any(t => t.Equals(exceptionType)))
{
return;
}

throw new XunitException($"Expected one of: ({string.Join<Type>(", ", exceptionTypes)}) -> Actual: ({e.GetType()})");
}

throw new XunitException($"Expected one of: ({string.Join<Type>(", ", exceptionTypes)}) -> Actual: No exception thrown");
}

public static void ThrowsAny<TFirstExceptionType, TSecondExceptionType>(Action action)
where TFirstExceptionType : Exception
where TSecondExceptionType : Exception
{
ThrowsAnyInternal(action, typeof(TFirstExceptionType), typeof(TSecondExceptionType));
}

public static void ThrowsAny<TFirstExceptionType, TSecondExceptionType, TThirdExceptionType>(Action action)
where TFirstExceptionType : Exception
where TSecondExceptionType : Exception
where TThirdExceptionType : Exception
{
ThrowsAnyInternal(action, typeof(TFirstExceptionType), typeof(TSecondExceptionType), typeof(TThirdExceptionType));
}

public static void ThrowsIf<T>(bool condition, Action action)
where T : Exception
{
if (condition)
{
Assert.Throws<T>(action);
}
else
{
action();
}
}

private static string AddOptionalUserMessage(string message, string userMessage)
{
if (userMessage == null)
{
return message;
}
else
{
return $"{message} {userMessage}";
}
}

/// <summary>
/// Tests whether the specified string contains the specified substring
/// and throws an exception if the substring does not occur within the
/// test string or if either string or substring is null.
/// </summary>
/// <param name="value">
/// The string that is expected to contain <paramref name="substring"/>.
/// </param>
/// <param name="substring">
/// The string expected to occur within <paramref name="value"/>.
/// </param>
public static void Contains(string value, string substring)
{
Assert.NotNull(value);
Assert.NotNull(substring);
Assert.Contains(substring, value, StringComparison.Ordinal);
}

/// <summary>
/// Validate that a given value is greater than another value.
/// </summary>
/// <param name="actual">The value that should be greater than <paramref name="greaterThan"/>.</param>
/// <param name="greaterThan">The value that <paramref name="actual"/> should be greater than.</param>
public static void GreaterThan<T>(T actual, T greaterThan, string userMessage = null) where T : IComparable
{
if (actual == null)
{
throw new XunitException(
greaterThan == null
? AddOptionalUserMessage($"Expected: <null> to be greater than <null>.", userMessage)
: AddOptionalUserMessage($"Expected: <null> to be greater than {greaterThan}.", userMessage));
}

if (actual.CompareTo(greaterThan) <= 0)
{
throw new XunitException(AddOptionalUserMessage($"Expected: {actual} to be greater than {greaterThan}", userMessage));
}
}

/// <summary>
/// Validate that a given value is less than another value.
/// </summary>
/// <param name="actual">The value that should be less than <paramref name="lessThan"/>.</param>
/// <param name="lessThan">The value that <paramref name="actual"/> should be less than.</param>
public static void LessThan<T>(T actual, T lessThan, string userMessage = null) where T : IComparable
{
if (actual == null)
{
if (lessThan == null)
{
throw new XunitException(AddOptionalUserMessage($"Expected: <null> to be less than <null>.", userMessage));
}
else
{
// Null is always less than non-null
return;
}
}

if (actual.CompareTo(lessThan) >= 0)
{
throw new XunitException(AddOptionalUserMessage($"Expected: {actual} to be less than {lessThan}", userMessage));
}
}

/// <summary>
/// Validate that a given value is less than or equal to another value.
/// </summary>
/// <param name="actual">The value that should be less than or equal to <paramref name="lessThanOrEqualTo"/></param>
/// <param name="lessThanOrEqualTo">The value that <paramref name="actual"/> should be less than or equal to.</param>
public static void LessThanOrEqualTo<T>(T actual, T lessThanOrEqualTo, string userMessage = null) where T : IComparable
{
// null, by definition is always less than or equal to
if (actual == null)
{
return;
}

if (actual.CompareTo(lessThanOrEqualTo) > 0)
{
throw new XunitException(AddOptionalUserMessage($"Expected: {actual} to be less than or equal to {lessThanOrEqualTo}", userMessage));
}
}

/// <summary>
/// Validate that a given value is greater than or equal to another value.
/// </summary>
/// <param name="actual">The value that should be greater than or equal to <paramref name="greaterThanOrEqualTo"/></param>
/// <param name="greaterThanOrEqualTo">The value that <paramref name="actual"/> should be greater than or equal to.</param>
public static void GreaterThanOrEqualTo<T>(T actual, T greaterThanOrEqualTo, string userMessage = null) where T : IComparable
{
// null, by definition is always less than or equal to
if (actual == null)
{
if (greaterThanOrEqualTo == null)
{
// We're equal
return;
}
else
{
// Null is always less than non-null
throw new XunitException(AddOptionalUserMessage($"Expected: <null> to be greater than or equal to <null>.", userMessage));
}
}

if (actual.CompareTo(greaterThanOrEqualTo) < 0)
{
throw new XunitException(AddOptionalUserMessage($"Expected: {actual} to be greater than or equal to {greaterThanOrEqualTo}", userMessage));
}
}

/// <summary>
/// Validates that the actual byte array is equal to the expected byte array. XUnit only displays the first 5 values
/// of each collection if the test fails. This doesn't display at what point or how the equality assertion failed.
/// </summary>
/// <param name="expected">The byte array that <paramref name="actual"/> should be equal to.</param>
/// <param name="actual"></param>
public static void Equal(byte[] expected, byte[] actual)
{
try
{
Assert.Equal(expected, actual);
}
catch (AssertActualExpectedException)
{
string expectedString = string.Join(", ", expected);
string actualString = string.Join(", ", actual);
throw new AssertActualExpectedException(expectedString, actualString, null);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Runtime.CompilerServices;
using Xunit;

namespace System
{
Expand All @@ -21,7 +19,6 @@ public static partial class PlatformDetection

public static bool HasWindowsShell => IsWindows && IsNotWindowsServerCore && IsNotWindowsNanoServer && IsNotWindowsIoTCore;
public static bool IsUap => IsInAppContainer || IsNetNative;
public static bool IsFullFramework => RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.OrdinalIgnoreCase);
public static bool IsNetNative => RuntimeInformation.FrameworkDescription.StartsWith(".NET Native", StringComparison.OrdinalIgnoreCase);
public static bool IsNetCore => RuntimeInformation.FrameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase);
public static bool IsOSX => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,9 @@ private void DeserializeAssignStatement(IDesignerSerializationManager manager, C

if (rhs is IConvertible ic)
{
// f.FieldType is a type from the reflection (or project target) Universe, while rhs is a runtime type(exists in the visual studio framework) they need to be converted to the same universe for comparison to work. If TargetFrameworkProvider is not available, then we are working with runtime types.
// f.FieldType is a type from the reflection (or project target) universe, while rhs is a runtime type (exists in the Visual Studio framework)
// they need to be converted to the same universe for comparison to work.
// If TargetFrameworkProvider is not available, then we are working with runtime types.
Type fieldType = f.FieldType;
TypeDescriptionProvider tdp = GetTargetFrameworkProviderForType(manager, fieldType);
if (tdp != null)
Expand Down
Loading