public static Format ( object value ) : string | ||
value | object | The value to be formatted. |
Résultat | string |
static string ParameterToDisplayValue(string parameterName, object parameterValue) => $"{parameterName}: {ArgumentFormatter.Format(parameterValue)}";
static string ParameterToDisplayValue(string parameterName, object parameterValue) { return(String.Format("{0}: {1}", parameterName, ArgumentFormatter.Format(parameterValue))); }
/// <summary> /// Creates a new instance of the <see cref="ThrowsException"/> class. Call this constructor /// when an exception of the wrong type was thrown. /// </summary> /// <param name="expectedType">The type of the exception that was expected</param> /// <param name="actual">The actual exception that was thrown</param> public ThrowsException(Type expectedType, Exception actual) : this(expectedType, ArgumentFormatter.Format(actual.GetType()), actual.Message, actual.StackTrace, actual) { }
/// <summary> /// Creates a new instance of the <see cref="EmptyException"/> class. /// </summary> public EmptyException(IEnumerable collection) : base("<empty>", ArgumentFormatter.Format(collection), "Assert.Empty() Failure") { }
/// <summary> /// Resolves argument values for the test method, including support for optional method /// arguments. /// </summary> /// <param name="testMethod">The test method to resolve.</param> /// <param name="arguments">The user-supplied method arguments.</param> /// <returns>The argument values</returns> public static object[] ResolveMethodArguments(this MethodBase testMethod, object[] arguments) { var parameters = testMethod.GetParameters(); bool hasParamsParameter = false; // Params can only be added at the end of the parameter list if (parameters.Length > 0) { hasParamsParameter = parameters[parameters.Length - 1].GetCustomAttribute(typeof(ParamArrayAttribute)) != null; } var nonOptionalParameterCount = parameters.Count(p => !p.IsOptional); if (hasParamsParameter) { nonOptionalParameterCount--; } // We can't call a method if we provided fewer parameters than the number of non-optional parameters in the method. if (arguments.Length < nonOptionalParameterCount) { return(arguments); } // We can't call a non-params method if we have provided more parameters than the total number of parameters in the method. if (!hasParamsParameter && arguments.Length > parameters.Length) { return(arguments); } var newArguments = new object[parameters.Length]; var resolvedArgumentsCount = 0; if (hasParamsParameter) { var paramsParameter = parameters[parameters.Length - 1]; var paramsElementType = paramsParameter.ParameterType.GetElementType(); if (arguments.Length < parameters.Length) { // Didn't include the params parameter var emptyParamsArray = Array.CreateInstance(paramsElementType, 0); newArguments[newArguments.Length - 1] = emptyParamsArray; } else if (arguments.Length == parameters.Length && (arguments[arguments.Length - 1] == null || (arguments[arguments.Length - 1].GetType().IsArray&& arguments[arguments.Length - 1].GetType().GetElementType() == paramsElementType))) { // Passing null or the same type array as the params parameter newArguments[newArguments.Length - 1] = arguments[arguments.Length - 1]; resolvedArgumentsCount = 1; } else { // Parameters need adjusting into an array var paramsArrayLength = arguments.Length - parameters.Length + 1; var paramsArray = Array.CreateInstance(paramsElementType, paramsArrayLength); try { Array.Copy(arguments, parameters.Length - 1, paramsArray, 0, paramsArray.Length); } catch (InvalidCastException) { throw new InvalidOperationException($"The arguments for this test method did not match the parameters: {ArgumentFormatter.Format(arguments)}"); } newArguments[newArguments.Length - 1] = paramsArray; resolvedArgumentsCount = paramsArrayLength; } } // If the argument has been provided, pass the argument value for (var i = 0; i < arguments.Length - resolvedArgumentsCount; i++) { newArguments[i] = TryConvertObject(arguments[i], parameters[i].ParameterType); } // If the argument has not been provided, pass the default value int unresolvedParametersCount = hasParamsParameter ? parameters.Length - 1 : parameters.Length; for (var i = arguments.Length; i < unresolvedParametersCount; i++) { var parameter = parameters[i]; if (parameter.HasDefaultValue) { newArguments[i] = parameter.DefaultValue; } else { newArguments[i] = parameter.ParameterType.GetTypeInfo().GetDefaultValue(); } } return(newArguments); }
static string ParameterToDisplayValue(string parameterName, object parameterValue) { return(string.Format(CultureInfo.CurrentCulture, "{0}: {1}", parameterName, ArgumentFormatter.Format(parameterValue))); }
/// <summary> /// Resolves argument values for the test method, including support for optional method /// arguments. /// </summary> /// <param name="testMethod">The test method to resolve.</param> /// <param name="arguments">The user-supplied method arguments.</param> /// <returns>The argument values</returns> public static object?[] ResolveMethodArguments( this MethodBase testMethod, object?[] arguments) { Guard.ArgumentNotNull(nameof(testMethod), testMethod); var parameters = testMethod.GetParameters(); var hasParamsParameter = false; // Params can only be added at the end of the parameter list if (parameters.Length > 0) { hasParamsParameter = parameters[parameters.Length - 1].GetCustomAttribute(typeof(ParamArrayAttribute)) != null; } var nonOptionalParameterCount = parameters.Count(p => !p.IsOptional); if (hasParamsParameter) { nonOptionalParameterCount--; } // We can't call a method if we provided fewer parameters than the number of non-optional parameters in the method. if (arguments.Length < nonOptionalParameterCount) { return(arguments); } // We can't call a non-params method if we have provided more parameters than the total number of parameters in the method. if (!hasParamsParameter && arguments.Length > parameters.Length) { return(arguments); } var newArguments = new object?[parameters.Length]; var resolvedArgumentsCount = 0; if (hasParamsParameter) { var paramsParameter = parameters[parameters.Length - 1]; var paramsElementType = paramsParameter.ParameterType.GetElementType(); if (paramsElementType == null) { throw new InvalidOperationException("Cannot determine params element type"); } if (arguments.Length < parameters.Length) { // Didn't include the params parameter var emptyParamsArray = Array.CreateInstance(paramsElementType, 0); newArguments[newArguments.Length - 1] = emptyParamsArray; } else if (arguments.Length == parameters.Length && (arguments[arguments.Length - 1] == null || (arguments[arguments.Length - 1] !.GetType().IsArray&& arguments[arguments.Length - 1] !.GetType().GetElementType() == paramsElementType))) { // Passing null or the same type array as the params parameter newArguments[newArguments.Length - 1] = arguments[arguments.Length - 1]; resolvedArgumentsCount = 1; } else { // Parameters need adjusting into an array var paramsArrayLength = arguments.Length - parameters.Length + 1; var paramsArray = Array.CreateInstance(paramsElementType, paramsArrayLength); try { Array.Copy(arguments, parameters.Length - 1, paramsArray, 0, paramsArray.Length); } catch (ArrayTypeMismatchException) { throw new InvalidOperationException($"The arguments for this test method did not match the parameters: {ArgumentFormatter.Format(arguments)}"); } catch (InvalidCastException) { throw new InvalidOperationException($"The arguments for this test method did not match the parameters: {ArgumentFormatter.Format(arguments)}"); } newArguments[newArguments.Length - 1] = paramsArray; resolvedArgumentsCount = paramsArrayLength; } }
public ThrowsException(Type expectedType, Exception actual) #if XUNIT_NULLABLE : this(expectedType, ArgumentFormatter.Format(actual.GetType()) !, actual.Message, actual.StackTrace, actual)