public void Execute_SecondCallAfterException_Executes() { SafeExecutionHelpers.RevertToDefaultImplementation(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); SafeExecutionHelpers.Implementation = mockHelpers.Object; Exception exception = new Exception(); int times = 0; void MockTask(string text) { if (times++ == 0) { throw exception; //Throws only on first try } } ICommand command = new SafeCommand <string>(MockTask); //Assert.Throws<Exception>(()=>command.Execute("test")); //First run command.Execute("test"); mockHelpers.Verify(h => h.HandleException <Exception>(exception, null)); //Second run command.Execute("test"); Assert.Equal(2, times); SafeExecutionHelpers.RevertToDefaultImplementation(); }
public void SafeContinueWith_OnExceptionExceptionSet_HandlesException() { SpecificException specificException = new SpecificException(); Action <Exception> onException = new Mock <Action <Exception> >().Object; SafeExecutionHelpers.Initialize(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); SafeExecutionHelpers.SetImplementation(mockHelpers.Object); var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false); var sut = new SafeTask(); sut.SafeContinueWith <Exception>( Task.Factory.StartNew( () => throw specificException, CancellationToken.None, TaskCreationOptions.None, dts), onException, //The crux dts); dts.RunTasksUntilIdle(); Assert.Contains(specificException, dts.Exceptions); mockHelpers.Verify(h => h.HandleException(specificException, onException)); SafeExecutionHelpers.RevertToDefaultImplementation(); }
public void SafeContinueWith_NothingSet_HandleExceptionRuns() { SpecificException specificException = new SpecificException(); SafeExecutionHelpers.RevertToDefaultImplementation(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); SafeExecutionHelpers.Implementation = mockHelpers.Object; var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false); var sut = new SafeTask(); sut.SafeContinueWith <Exception>( Task.Factory.StartNew( () => throw specificException, CancellationToken.None, TaskCreationOptions.None, dts), null, dts); dts.RunTasksUntilIdle(); Assert.Contains(specificException, dts.Exceptions); mockHelpers.Verify(h => h.HandleException <Exception>(specificException, null)); SafeExecutionHelpers.RevertToDefaultImplementation(); }
public void HandleException_SpecificException_TargetThrowsDifferentException_NotHandledByGivenDelegate() { bool isHandled = false; SafeExecutionHelpers.HandleException <SpecificException>(new DifferentException(), (ex) => isHandled = true); Assert.False(isHandled); }
public void SafeContinueWith_DefaultExceptionHandlerSet_HandlesException() { SpecificException specificException = new SpecificException(); Action <Exception> defaultExceptionHandler = new Mock <Action <Exception> >().Object; SafeExecutionHelpers.RevertToDefaultImplementation(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); //Crux - DefaultHandler returns non-null delegate mockHelpers.SetupGet(h => h.Settings.DefaultExceptionHandler).Returns(defaultExceptionHandler); SafeExecutionHelpers.Implementation = mockHelpers.Object; var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false); var sut = new SafeTask(); sut.SafeContinueWith <Exception>( Task.Factory.StartNew( () => throw specificException, CancellationToken.None, TaskCreationOptions.None, dts), null, dts); dts.RunTasksUntilIdle(); Assert.Contains(specificException, dts.Exceptions); mockHelpers.Verify(h => h.HandleException <Exception>(specificException, null)); SafeExecutionHelpers.Implementation = mockHelpers.Object; }
public void HandleException_SpecificException_TargetThrowsSpecificException_IsHandledByGivenDelegate() { BeforeEachTest(); bool isHandled = false; SafeExecutionHelpers.HandleException <SpecificException>(new SpecificException(), (ex) => isHandled = true); Assert.True(isHandled); AfterEachTest(); }
public void HandleException_SpecificExceptionThrown_NoHandlers_Throws() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); //MockForms.Init(); //For Device.BeginInvokeOnMainThread var exception = new Exception(); var handler = new Mock <Action <SpecificException> >(); Assert.Throws <SafeExecutionHelpersException>(() => SafeExecutionHelpers.HandleException(exception, handler.Object)); }
public void HandleException_SpecificExceptionThrown_NoHandlers_Throws() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var exception = new Exception(); var handler = new Mock <Action <SpecificException> >(); Assert.Throws <SafeExecutionHelpersException>(() => SafeExecutionHelpers.HandleException(exception, handler.Object)); }
public void HandleException_SpecificException_TargetThrowsDifferentException_NotHandledByGivenDelegate() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.SetDefaultExceptionHandler((ex) => { }); bool isHandled = false; SafeExecutionHelpers.HandleException <SpecificException>(new DifferentException(), (ex) => isHandled = true); Assert.False(isHandled); }
public void HandleException_Exception_ExceptionOnException_Handled() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var exception = new Exception(); var handler = new Mock <Action <Exception> >(); SafeExecutionHelpers.HandleException(exception, handler.Object); handler.Verify(h => h.Invoke(exception)); }
public void HandleException_Exception_SpecificExceptionOnException_NotHandled() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.SetDefaultExceptionHandler((ex) => { }); var exception = new Exception(); var handler = new Mock <Action <SpecificException> >(); SafeExecutionHelpers.HandleException(exception, handler.Object); Assert.Empty(handler.Invocations); }
public void HandleException_Exception_SpecificExceptionOnException_NotHandled() { SafeExecutionHelpers.Initialize(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var exception = new Exception(); var handler = new Mock <Action <SpecificException> >(); SafeExecutionHelpers.HandleException(exception, handler.Object); Assert.Empty(handler.Invocations); }
public void HandleException_SpecificException_SpecificExceptionOnException_Handled() { SafeExecutionHelpers.Initialize(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var exception = new SpecificException(); var handler = new Mock <Action <SpecificException> >(); SafeExecutionHelpers.HandleException(exception, handler.Object); handler.Verify(h => h.Invoke(exception)); }
public void HandleException_ShouldRethrow_Throws() { Xamarin.Forms.Mocks.MockForms.Init(); //For Device.BeginInvokeOnMainThread SafeExecutionHelpers.Initialize(shouldAlwaysRethrowException: true); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var exception = new NullReferenceException(); var handler = new Mock <Action <Exception> >(); Assert.Throws <NullReferenceException>(() => SafeExecutionHelpers.HandleException(exception, handler.Object)); handler.Verify(h => h.Invoke(exception)); }
public void HandleException_ShouldRethrow_Throws() { SafeExecutionHelpers.RevertToDefaultImplementation(); //MockForms.Init(); //For Device.BeginInvokeOnMainThread SafeExecutionHelpers.Configure(s => s.ShouldAlwaysRethrowException = true); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var exception = new NullReferenceException(); var handler = new Mock <Action <Exception> >(); Assert.Throws <NullReferenceException>(() => SafeExecutionHelpers.HandleException(exception, handler.Object)); handler.Verify(h => h.Invoke(exception)); }
public void HandleException_DefaultHandlerSet_Exception_ExceptionOnException_DefaultNotInvoked() { SafeExecutionHelpers.Initialize(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var defaultHandler = new Mock <Action <Exception> >(); SafeExecutionHelpers.SetDefaultExceptionHandler(defaultHandler.Object); var exception = new Exception(); SafeExecutionHelpers.HandleException(exception, new Mock <Action <Exception> >().Object); Assert.Empty(defaultHandler.Invocations); }
public void HandleException_DefaultHandlerSet_Exception_SpecificExceptionOnException__DefaultInvoked() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var defaultHandler = new Mock <Action <Exception> >(); SafeExecutionHelpers.SetDefaultExceptionHandler(defaultHandler.Object); var exception = new Exception(); var handler = new Mock <Action <SpecificException> >(); SafeExecutionHelpers.HandleException(exception, handler.Object); Assert.NotEmpty(defaultHandler.Invocations); }
public void Execute_TargetThrows_HandlesException() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); SafeExecutionHelpers.Implementation = mockHelpers.Object; Exception exception = new Exception(); var command = new SafeCommand(executeAction: () => throw exception); //Assert.Throws<Exception>(() => command.Execute(null)); command.Execute(null); mockHelpers.Verify(h => h.HandleException <Exception>(exception, null)); SafeExecutionHelpers.RevertToDefaultImplementation(); }
public App() { InitializeComponent(); //ZM: SafeCommand setup -> Log unhandled exceptions, // preventing app crashes SafeExecutionHelpers.SetDefaultExceptionHandler( (ex) => Debug.WriteLine($"{ex.Source}: {ex.Message}")); //ZM: No need to regester dependencies because the // built-in injection engine will use smart-resolve by // default. This can be turned-off for enterprise apps //ZM: Wan't to use 3rd party dependecy injection instead? //... here are two examples //UseLightInject(); //OR //UseAutofac(); MainPage = new AppShell(); }
private void AfterEachTest() { SafeExecutionHelpers.Initialize(false); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); }
private void BeforeEachTest() { SafeExecutionHelpers.Configure(s => s.ShouldAlwaysRethrowException = false); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); }
private void AfterEachTest() { SafeExecutionHelpers.RevertToDefaultImplementation(); SafeExecutionHelpers.Configure(s => s.ShouldAlwaysRethrowException = false); SafeExecutionHelpers.RemoveDefaultExceptionHandler(); }