public void AddExitActionWNextState_throws_InvalidOperationException_if_already_set() { var stateMachine = new StateMachineAsync <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState); var sut = new StateConfigurationAsync <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine); sut.AddExitAction((sale, _) => Task.CompletedTask, SaleState.Complete); Assert.Throws <InvalidOperationException>(() => sut.AddExitAction((sale, _) => Task.CompletedTask, SaleState.Complete)); }
public async Task ExecuteExitActionAsync_does_not_execute_if_cancelled() { var sale = new Sale(saleID: 96) { State = SaleState.ChangeDue }; var transitionResult = new StateTransitionResult <SaleState, SaleEvent>(SaleEvent.Pay , SaleState.Open , SaleState.Open , SaleState.ChangeDue , "lastTransitionName"); var stateMachine = new StateMachineAsync <Sale, SaleState, SaleEvent>(sale1 => sale1.State, (sale1, newState) => sale1.State = newState); var openState = new StateConfigurationAsync <Sale, SaleState, SaleEvent>(SaleState.Open, stateMachine); var openExitActionFired = false; openState.AddExitAction((sale1, _) => { openExitActionFired = true; return(Task.CompletedTask); }); using (var cancelSource = new CancellationTokenSource()) { var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.Pay, sale, cancelSource.Token); cancelSource.Cancel(); await openState.ExecuteExitActionAsync(parameters, transitionResult); } Assert.False(openExitActionFired); }
public void AddExitActionWNextState_throws_ArgumentNullException_if_Action_null() { var stateMachine = new StateMachineAsync <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState); var sut = new StateConfigurationAsync <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine); Assert.Throws <ArgumentNullException>(() => sut.AddExitAction(action: null, nextState: SaleState.ChangeDue)); }
public async Task ExecuteExitActionAsync_executes_based_on_next_state() { var sale = new Sale(saleID: 96) { State = SaleState.ChangeDue }; var transitionResult = new StateTransitionResult <SaleState, SaleEvent>(SaleEvent.Pay , SaleState.Open , SaleState.Open , SaleState.ChangeDue , "lastTransitionName"); var stateMachine = new StateMachineAsync <Sale, SaleState, SaleEvent>(sale1 => sale1.State, (sale1, newState) => sale1.State = newState); var openState = new StateConfigurationAsync <Sale, SaleState, SaleEvent>(SaleState.Open, stateMachine); var openExitActionFired = false; openState.AddExitAction((sale1, _) => { openExitActionFired = true; return(Task.CompletedTask); }, SaleState.ChangeDue); var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.Pay, sale); await openState.ExecuteExitActionAsync(parameters, transitionResult); Assert.True(openExitActionFired); }