public void AddTriggerActionWRequest_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.AddTriggerAction <string>(SaleEvent.Pay, (sale, stringParam, _) => Task.CompletedTask); Assert.Throws <InvalidOperationException>(() => sut.AddTriggerAction <string>(SaleEvent.Pay, (sale, stringParam, _) => Task.CompletedTask)); }
public async Task FireTriggerAsync_executes_superState_if_currentState_not_successful() { var sale = new Sale(saleID: 96) { State = SaleState.ChangeDue }; 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 changeDueState = new StateConfigurationAsync <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine); var openStatePayTriggerFired = false; openState .AddTriggerAction(SaleEvent.Pay, (sale1, _) => { openStatePayTriggerFired = true; return(Task.CompletedTask); }) .AddTransition(SaleEvent.Pay, SaleState.Complete, name: "openStatePay"); changeDueState.AddSuperstate(openState); var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.Pay, sale); var result = await changeDueState.FireTriggerAsync(parameters); Assert.True(openStatePayTriggerFired); Assert.True(result.WasTransitioned); Assert.Equal(SaleState.Complete, sale.State); Assert.Equal(SaleState.Complete, result.CurrentState); Assert.Equal(SaleState.ChangeDue, result.PreviousState); Assert.Equal(SaleState.ChangeDue, result.StartingState); Assert.Equal("openStatePay", result.LastTransitionName); }
public void AddTriggerActionWRequest_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.AddTriggerAction <string>(SaleEvent.AddItem, action: null)); }