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 SutIsContextualCommand() { // Fixture setup var dummyCommand = new DelegatingGuardClauseCommand(); // Exercise system var sut = new ReflectionExceptionUnwrappingCommand(dummyCommand); // Verify outcome Assert.IsAssignableFrom <IGuardClauseCommand>(sut); // Teardown }
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 RequestedParamNameIsCorrect() { const string expected = "foo"; var commandStub = new DelegatingGuardClauseCommand { RequestedParameterName = expected }; var sut = new ReflectionExceptionUnwrappingCommand(commandStub); var actual = sut.RequestedParameterName; Assert.Equal(expected, actual); }
public void ContextTypeIsCorrect(Type type) { // Arrange var cmd = new DelegatingGuardClauseCommand { RequestedType = type }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Act var result = sut.RequestedType; // Assert Assert.Equal(type, result); }
public void ExecuteRethrowsNormalException() { // Arrange var cmd = new DelegatingGuardClauseCommand { OnExecute = v => { throw new InvalidOperationException(); } }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Act & Assert var dummyValue = new object(); Assert.Throws <InvalidOperationException>(() => sut.Execute(dummyValue)); }
public void ContextTypeIsCorrect(Type type) { // Fixture setup var cmd = new DelegatingGuardClauseCommand { RequestedType = type }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Exercise system var result = sut.RequestedType; // Verify outcome Assert.Equal(type, result); // Teardown }
public void ExecuteRethrowsNormalException() { // Fixture setup var cmd = new DelegatingGuardClauseCommand { OnExecute = v => { throw new InvalidOperationException(); } }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Exercise system and verify outcome var dummyValue = new object(); Assert.Throws <InvalidOperationException>(() => sut.Execute(dummyValue)); // Teardown }
public void ExecuteUnwrapsAndThrowsInnerExceptionFromTargetInvocationException() { // Arrange var expectedException = new InvalidOperationException(); var cmd = new DelegatingGuardClauseCommand { OnExecute = v => { throw new TargetInvocationException(expectedException); } }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Act & Assert var dummyValue = new object(); var e = Assert.Throws <InvalidOperationException>(() => sut.Execute(dummyValue)); Assert.Equal(expectedException, e); }
public void CreateExceptionReturnsCorrectResult() { // Arrange var value = Guid.NewGuid().ToString(); var expected = new Exception(); var cmd = new DelegatingGuardClauseCommand { OnCreateException = v => value == v ? expected : new Exception() }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Act var result = sut.CreateException(value); // Assert Assert.Equal(expected, result); }
public void ExecuteExecutesDecoratedCommand() { // Arrange var mockVerified = false; var expectedValue = new object(); var cmd = new DelegatingGuardClauseCommand { OnExecute = v => mockVerified = expectedValue.Equals(v) }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Act sut.Execute(expectedValue); // Assert Assert.True(mockVerified, "Mock verified."); }
public void ExecuteUnwrapsAndThrowsInnerExceptionFromTargetInvocationException() { // Fixture setup var expectedException = new InvalidOperationException(); var cmd = new DelegatingGuardClauseCommand { OnExecute = v => { throw new TargetInvocationException(expectedException); } }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Exercise system and verify outcome var dummyValue = new object(); var e = Assert.Throws <InvalidOperationException>(() => sut.Execute(dummyValue)); Assert.Equal(expectedException, e); // Teardown }
public void ExecuteExecutesDecoratedCommand() { // Fixture setup var mockVerified = false; var expectedValue = new object(); var cmd = new DelegatingGuardClauseCommand { OnExecute = v => mockVerified = expectedValue.Equals(v) }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Exercise system sut.Execute(expectedValue); // Verify outcome Assert.True(mockVerified, "Mock verified."); // Teardown }
public void CreateExceptionReturnsCorrectResult() { // Fixture setup var value = Guid.NewGuid().ToString(); var expected = new Exception(); var cmd = new DelegatingGuardClauseCommand { OnCreateException = v => value == v ? expected : new Exception() }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Exercise system var result = sut.CreateException(value); // Verify outcome Assert.Equal(expected, result); // Teardown }
/// <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); }
public void CreateExceptionWithFailureReasonReturnsCorrectResult() { // Arrange var value = Guid.NewGuid().ToString(); var failureReason = Guid.NewGuid().ToString(); var inner = new Exception(); var expected = new Exception(); var cmd = new DelegatingGuardClauseCommand { OnCreateExceptionWithFailureReason = (v, r, e) => v == value && r == failureReason && e == inner ? expected : new Exception() }; var sut = new ReflectionExceptionUnwrappingCommand(cmd); // Act var result = sut.CreateException(value, failureReason, inner); // Assert Assert.Equal(expected, result); }
public void TryGetMethodInvokeCommandHasTaskTypeMethodInvokeCommandReturnsCommand() { var sut = new SutAlias(); var testHelperMethod = typeof(GuardClauseExtensionsTests) .GetMethod( nameof(StaticCommandHelperMethod), BindingFlags.Static | BindingFlags.NonPublic); var innerCommand = new ReflectionExceptionUnwrappingCommand(new MethodInvokeCommand( new Mock <IMethod>().Object, new Mock <IExpansion <object> >().Object, testHelperMethod.GetParameters().First())); var command = (IGuardClauseCommand)Activator.CreateInstance(TaskReturnType, innerCommand); MethodInvokeCommand methodInvokeCommand; var actual = sut.TryGetMethodInvokeCommand(command, out methodInvokeCommand); Assert.True(actual); Assert.NotNull(methodInvokeCommand); }