/// <summary> /// Verifies that the command behaves correct when invoked with a null argument. /// </summary> /// <param name="command">The command whose behavior must be examined.</param> /// <remarks> /// <para> /// The Verify method attempts to invoke the <paramref name="command" /> instance's /// <see cref="IGuardClauseCommand.Execute" /> with <see langword="null" />. The expected /// result is that this action throws an <see cref="ArgumentNullException" /> with proper parameter name, /// in which case the expected behavior is considered verified. If any other exception is thrown, or /// if no exception is thrown at all, the verification fails and an exception is thrown. /// </para> /// <para> /// The behavior is only asserted if the command's /// <see cref="IGuardClauseCommand.RequestedType" /> is nullable. In case of value types, /// no action is performed. /// </para> /// </remarks> public void Verify(IGuardClauseCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (!command.RequestedType.IsClass && !command.RequestedType.IsInterface) { return; } try { command.Execute(null); } catch (ArgumentNullException e) { if (string.Equals(e.ParamName, command.RequestedParameterName, StringComparison.Ordinal)) { return; } throw command.CreateException("null", e); } catch (Exception e) { throw command.CreateException("null", e); } throw command.CreateException("null"); }
/// <summary> /// Verifies that the command behaves correct when invoked with a null argument. /// </summary> /// <param name="command">The command whose behavior must be examined.</param> /// <remarks> /// <para> /// The Verify method attempts to invoke the <paramref name="command" /> instance's /// <see cref="IGuardClauseCommand.Execute" /> with <see langword="null" />. The expected /// result is that this action throws an <see cref="ArgumentNullException" />, in which /// case the expected behavior is considered verified. If any other exception is thrown, or /// if no exception is thrown at all, the verification fails and an exception is thrown. /// </para> /// <para> /// The behavior is only asserted if the command's /// <see cref="IGuardClauseCommand.RequestedType" /> is nullable. In case of value types, /// no action is performed. /// </para> /// </remarks> public void Verify(IGuardClauseCommand command) { if (command == null) { throw new ArgumentNullException("command"); } if (!command.RequestedType.IsClass && !command.RequestedType.IsInterface) { return; } try { command.Execute(null); } catch (ArgumentNullException) { return; } catch (Exception e) { throw command.CreateException("null", e); } throw command.CreateException("null"); }
public bool ContainsParameters(IGuardClauseCommand command) { if (command == null) { throw new ArgumentNullException("command"); } var expectedCommand = command as ReflectionExceptionUnwrappingCommand; MethodInvokeCommand methodCommand = null; if (expectedCommand == null) { methodCommand = command as MethodInvokeCommand; } else { methodCommand = expectedCommand.Command as MethodInvokeCommand; } if (methodCommand != null) { return(ParameterNames.Any(p => p == methodCommand.ParameterInfo.Name)); } return(false); }
private static IEnumerable <object> GetParameters(IGuardClauseCommand commmand) { var methodInvokeCommand = (MethodInvokeCommand)((ReflectionExceptionUnwrappingCommand)commmand).Command; var indexedReplacement = (IndexedReplacement <object>)methodInvokeCommand.Expansion; return(indexedReplacement.Source); }
public void Verify(IGuardClauseCommand command) { ParameterValidation.IsNotNull(command, nameof(command)); if (command.RequestedType.IsClass || command.RequestedType.IsInterface) { var newCommand = this.guardClauseExtensions.CreateExtendedCommand(this.specimenBuilder, command); var expectedExceptionThrown = false; try { newCommand.Execute(null); } catch (ArgumentNullException) { expectedExceptionThrown = true; } catch (Exception ex) { throw newCommand.CreateException("null", ex); } if (!expectedExceptionThrown) { throw newCommand.CreateException("null"); } } }
/// <summary> /// Verifies the behavior of the command when invoked with <see cref="Double" /> less than 0. /// </summary> /// <param name="command">The command whose behavior must be examined.</param> /// <remarks> /// <para> /// This method encapsulates the behavior which is expected when a method or constructor is /// invoked with <see cref="Double" /> less than 0 as one of the method arguments. In that case it's /// expected that invoking <paramref name="command" /> with Double less than 0 throws an /// <see cref="ArgumentException" />, causing the Verify method to succeed. If other /// exceptions are thrown, or no exception is thrown when invoking the command, the Verify /// method throws an exception. /// </para> /// </remarks> public void Verify(IGuardClauseCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (command.RequestedType != typeof(decimal)) { return; } try { command.Execute(Convert.ToDecimal(Convert.ToDouble(new Random().Next(int.MinValue, -1)))); } catch (ArgumentOutOfRangeException) { return; } catch (Exception e) { throw command.CreateException("\"Decimal.Negative\"", e); } throw command.CreateException("\"Decimal.Negative\""); }
/// <summary> /// Verifies the behavior of the command by delegating the implementation to all /// <see cref="BehaviorExpectations" />. /// </summary> /// <param name="command">The command whose behavior must be examined.</param> public void Verify(IGuardClauseCommand command) { foreach (var be in this.BehaviorExpectations) { be.Verify(command); } }
/// <summary> /// Verifies the behavior of the command when invoked with <see cref="Guid.Empty" />. /// </summary> /// <param name="command">The command whose behavior must be examined.</param> /// <remarks> /// <para> /// This method encapsulates the behavior which is expected when a method or constructor is /// invoked with <see cref="Guid.Empty" /> as one of the method arguments. In that case it's /// expected that invoking <paramref name="command" /> with Guid.Empty throws an /// <see cref="ArgumentException" />, causing the Verify method to succeed. If other /// exceptions are thrown, or no exception is thrown when invoking the command, the Verify /// method throws an exception. /// </para> /// </remarks> public void Verify(IGuardClauseCommand command) { if (command == null) { throw new ArgumentNullException("command"); } if (command.RequestedType != typeof(Guid)) { return; } try { command.Execute(Guid.Empty); } catch (ArgumentException) { return; } catch (Exception e) { throw command.CreateException("\"Guid.Empty\"", e); } throw command.CreateException("\"Guid.Empty\""); }
/// <summary> /// Verifies that the command behaves correct when invoked with a null argument. /// </summary> /// <param name="command">The command whose behavior must be examined.</param> /// <remarks> /// <para> /// The Verify method attempts to invoke the <paramref name="command" /> instance's /// <see cref="IGuardClauseCommand.Execute" /> with <see langword="null" />. The expected /// result is that this action throws an <see cref="ArgumentNullException" />, in which /// case the expected behavior is considered verified. If any other exception is thrown, or /// if no exception is thrown at all, the verification fails and an exception is thrown. /// </para> /// <para> /// The behavior is only asserted if the command's /// <see cref="IGuardClauseCommand.RequestedType" /> is nullable. In case of value types, /// no action is performed. /// </para> /// </remarks> public void Verify(IGuardClauseCommand command) { if (command == null) throw new ArgumentNullException("command"); if (!command.RequestedType.IsClass && !command.RequestedType.IsInterface) { return; } try { command.Execute(null); } catch (ArgumentNullException) { return; } catch (Exception e) { throw command.CreateException("null", e); } throw command.CreateException("null"); }
public void Verify(IGuardClauseCommand command) { if (command == null) throw new ArgumentNullException("command"); if (_filter.ContainsParameters(command)) { return; } InnerBehaviorExpectation.Verify(command); }
public void CommandIsCorrect() { // Arrange var expectedCommand = new DelegatingGuardClauseCommand(); var sut = new ReflectionExceptionUnwrappingCommand(expectedCommand); // Act IGuardClauseCommand result = sut.Command; // Assert Assert.Equal(expectedCommand, result); }
public void CommandIsCorrect() { // Fixture setup var expectedCommand = new DelegatingGuardClauseCommand(); var sut = new ReflectionExceptionUnwrappingCommand(expectedCommand); // Exercise system IGuardClauseCommand result = sut.Command; // Verify outcome Assert.Equal(expectedCommand, result); // Teardown }
public void Verify(IGuardClauseCommand command) { if (command == null) { throw new ArgumentNullException("command"); } if (_filter.ContainsParameters(command)) { return; } InnerBehaviorExpectation.Verify(command); }
/// <summary> /// Creates a new <see cref="IGuardClauseCommand"/> from the specified /// <see cref="IGuardClauseCommand"/> that replaces the expansion with one that uses /// <see cref="ParameterInfo"/> instead of <see cref="Type"/> and supports async method. /// </summary> /// <param name="specimenBuilder">The anonymous object creation service used to workaround /// a problem with guard clause assertions.</param> /// <param name="command">The <see cref="IGuardClauseCommand"/> that will be executed /// as part of the assertion.</param> /// <returns>A new instance of <see cref="IGuardClauseCommand"/> with the newly supplied /// features.</returns> /// <exception cref="ArgumentNullException"><paramref name="specimenBuilder"/>, or /// <paramref name="command"/> is <see langword="null"/>.</exception> public IGuardClauseCommand CreateExtendedCommand( ISpecimenBuilder specimenBuilder, IGuardClauseCommand command) { ParameterValidation.IsNotNull(specimenBuilder, nameof(specimenBuilder)); ParameterValidation.IsNotNull(command, nameof(command)); MethodInvokeCommand methodInvokeCommand; this.TryGetMethodInvokeCommand(command, out methodInvokeCommand); var expansion = methodInvokeCommand?.Expansion as IndexedReplacement <object>; IGuardClauseCommand commandToExecute; if (methodInvokeCommand != null && expansion != null) { var recreatedExpansion = RecreateExpansion(specimenBuilder, methodInvokeCommand.Method, expansion); var instanceMethod = methodInvokeCommand.Method as InstanceMethod; var staticMethod = methodInvokeCommand.Method as StaticMethod; var asyncAttribute = instanceMethod?.Method?.GetCustomAttribute <AsyncStateMachineAttribute>(false) ?? staticMethod?.Method?.GetCustomAttribute <AsyncStateMachineAttribute>(false); if (asyncAttribute != null) { commandToExecute = new ReflectionExceptionUnwrappingCommand(new AsyncMethodInvokeCommand( methodInvokeCommand.Method, recreatedExpansion, methodInvokeCommand.ParameterInfo, this.asyncTaskTimeout)); } else { commandToExecute = new ReflectionExceptionUnwrappingCommand(new MethodInvokeCommand( methodInvokeCommand.Method, recreatedExpansion, methodInvokeCommand.ParameterInfo)); } } else { commandToExecute = command; } return(commandToExecute); }
/// <summary> /// Verifies that the command behaves correct when invoked with a null argument. /// </summary> /// <param name="command">The command whose behavior must be examined.</param> /// <remarks> /// <para> /// The Verify method attempts to invoke the <paramref name="command" /> instance's /// <see cref="IGuardClauseCommand.Execute" /> with <see langword="null" />. The expected /// result is that this action throws an <see cref="ArgumentNullException" /> with proper parameter name, /// in which case the expected behavior is considered verified. If a <see cref="string" /> argument is provided and /// an <see cref="ArgumentException"/> is thrown the expected behavior is also considered verified. If any other /// exception is thrown, or if no exception is thrown at all, the verification fails and an exception is thrown. /// </para> /// <para> /// The behavior is only asserted if the command's /// <see cref="IGuardClauseCommand.RequestedType" /> is nullable. In case of value types, /// no action is performed. /// </para> /// </remarks> public void Verify(IGuardClauseCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (!command.RequestedType.IsClass && !command.RequestedType.IsInterface) { return; } try { command.Execute(null); } catch (Exception exception) when (exception is ArgumentNullException || (exception is ArgumentException && command.RequestedType == typeof(string))) { var castedException = exception is ArgumentNullException ? exception as ArgumentNullException : exception as ArgumentException; if (string.Equals(castedException !.ParamName, command.RequestedParameterName, StringComparison.Ordinal)) { return; } throw command.CreateException( "<nullOrEmpty>", string.Format( CultureInfo.InvariantCulture, "Guard Clause prevented it, however the thrown exception contains invalid parameter name. Ensure you pass correct parameter name to the ArgumentNullException constructor.{0}. Expected parameter name: {1}{0}Actual parameter name: {2}", Environment.NewLine, command.RequestedParameterName, castedException.ParamName), exception); } catch (Exception e) { throw command.CreateException("null", e); } throw command.CreateException("null"); }
public void Verify(IGuardClauseCommand command) { try { command.Execute(null); } catch (ArgumentNullException) { return; } catch (ArgumentException) { return; } throw new GuardClauseException(); }
/// <summary> /// Attempts to retrieve an instance of <see cref="MethodInvokeCommand"/> from an instance /// of <see cref="IGuardClauseCommand"/>. /// </summary> /// <param name="command">The <see cref="IGuardClauseCommand"/> to attempt to extract an /// instance of <see cref="MethodInvokeCommand"/> from.</param> /// <param name="methodInvokeCommand">Will contain an instance of /// <see cref="MethodInvokeCommand"/> if one is found; otherwise <see langword="null"/>. /// </param> /// <returns><see langword="true"/> if an instance of <see cref="MethodInvokeCommand"/> was /// found on the specified <see cref="IGuardClauseCommand"/>; otherwise /// <see langword="false"/>.</returns> public bool TryGetMethodInvokeCommand( IGuardClauseCommand command, out MethodInvokeCommand methodInvokeCommand) { ParameterValidation.IsNotNull(command, nameof(command)); methodInvokeCommand = (command as ReflectionExceptionUnwrappingCommand)?.Command as MethodInvokeCommand; if (methodInvokeCommand == null && command.GetType() == TaskReturnType) { methodInvokeCommand = (TaskReturnType.GetField( nameof(command), BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(command) as ReflectionExceptionUnwrappingCommand)?.Command as MethodInvokeCommand; } return(methodInvokeCommand != null); }
public void Verify(IGuardClauseCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (!command.RequestedType.IsClass && !command.RequestedType.IsInterface) { return; } try { command.Execute(null); } catch (ArgumentNullException e) { if (string.Equals(e.ParamName, command.RequestedParameterName, StringComparison.Ordinal)) { return; } throw command.CreateException( "<null>", string.Format(CultureInfo.InvariantCulture, "Guard Clause prevented it, however the thrown exception contains invalid parameter name. " + "Ensure you pass correct parameter name to the ArgumentNullException constructor.{0}" + "Expected parameter name: {1}{0}Actual parameter name: {2}", Environment.NewLine, command.RequestedParameterName, e.ParamName), e); } catch (ArgumentOutOfRangeException) { return; } throw command.CreateException("null"); }
public bool ContainsParameters(IGuardClauseCommand command) { if (command == null) throw new ArgumentNullException("command"); var expectedCommand = command as ReflectionExceptionUnwrappingCommand; MethodInvokeCommand methodCommand = null; if (expectedCommand == null) { methodCommand = command as MethodInvokeCommand; } else { methodCommand = expectedCommand.Command as MethodInvokeCommand; } if (methodCommand != null) { return ParameterNames.Any(p => p == methodCommand.ParameterInfo.Name); } return false; }
/// <summary> /// Verifies the behavior of the command when invoked with <see cref="Int32" /> less than 0. /// </summary> /// <param name="command">The command whose behavior must be examined.</param> /// <remarks> /// <para> /// This method encapsulates the behavior which is expected when a method or constructor is /// invoked with <see cref="Int32" /> less than 0 as one of the method arguments. In that case it's /// expected that invoking <paramref name="command" /> with Int32 less than 0 throws an /// <see cref="ArgumentException" />, causing the Verify method to succeed. If other /// exceptions are thrown, or no exception is thrown when invoking the command, the Verify /// method throws an exception. /// </para> /// </remarks> public void Verify(IGuardClauseCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (command.RequestedType != typeof(int)) { return; } try { var random = new Random(); if (_exceptions.Length != 0) { var value = random.Next(int.MinValue, -1); while (_exceptions.Contains(value)) { value = random.Next(int.MinValue, -1); } command.Execute(value); } else { command.Execute(random.Next(int.MinValue, -1)); } } catch (ArgumentOutOfRangeException) { return; } catch (Exception e) { throw command.CreateException("\"Int32.Negative\"", e); } throw command.CreateException("\"Int32.Negative\""); }
/// <summary> /// Verifies the behavior of the command when invoked with <see cref="Guid.Empty" />. /// </summary> /// <param name="command">The command whose behavior must be examined.</param> /// <remarks> /// <para> /// This method encapsulates the behavior which is expected when a method or constructor is /// invoked with <see cref="Guid.Empty" /> as one of the method arguments. In that case it's /// expected that invoking <paramref name="command" /> with Guid.Empty throws an /// <see cref="ArgumentException" />, causing the Verify method to succeed. If other /// exceptions are thrown, or no exception is thrown when invoking the command, the Verify /// method throws an exception. /// </para> /// </remarks> public void Verify(IGuardClauseCommand command) { if (command == null) throw new ArgumentNullException("command"); if (command.RequestedType != typeof(Guid)) return; try { command.Execute(Guid.Empty); } catch (ArgumentException) { return; } catch (Exception e) { throw command.CreateException("\"Guid.Empty\"", e); } throw command.CreateException("\"Guid.Empty\""); }
public void Verify(IGuardClauseCommand command) { if (!command.RequestedType.IsClass && !command.RequestedType.IsInterface) { return; } try { command.Execute(" "); } catch (ArgumentException) { return; } catch (Exception e) { throw command.CreateException("whitespace", e); } throw command.CreateException("whitespace"); }
public void Verify(IGuardClauseCommand command) { ParameterValidation.IsNotNull(command, nameof(command)); MethodInvokeCommand methodInvokeCommand; if (this.guardClauseExtensions.TryGetMethodInvokeCommand( command, out methodInvokeCommand) && methodInvokeCommand.ParameterInfo.Name == this.ParameterName) { var newCommand = this.guardClauseExtensions.CreateExtendedCommand(this.specimenBuilder, command); foreach (var value in this.Values) { var expectedExceptionThrown = false; try { newCommand.Execute(value); } catch (Exception ex) when(ex.GetType() == typeof(T)) { expectedExceptionThrown = true; } catch (Exception ex) { throw newCommand.CreateException(Invariant($"\"{value.ToString()}\""), ex); } if (!expectedExceptionThrown) { throw newCommand.CreateException(Invariant($"\"{value.ToString()}\"")); } } } }
public void Verify(IGuardClauseCommand command) { Assertions.Add(() => nullAndEmptyGuidBehaviorExpectation.Verify(command)); }
/// <summary> /// Initializes a new instance of the <see cref="ReflectionExceptionUnwrappingCommand" /> /// class. /// </summary> /// <param name="command">The decorated command.</param> public ReflectionExceptionUnwrappingCommand(IGuardClauseCommand command) { this.command = command; }
public void Verify(IGuardClauseCommand command) { this.OnVerify(command); }
public TaskReturnMethodInvokeCommand(IGuardClauseCommand command) { this.command = command; }
public IteratorMethodInvokeCommand(IGuardClauseCommand command) { this.command = command; }