Exemple #1
0
        public void AddEntryActionWPrevious_throws_InvalidOperationException_if_already_set()
        {
            var stateMachine = new StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var sut          = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);

            sut.AddEntryAction(sale => Task.Delay(1), previousState: SaleState.Open);

            Assert.Throws <InvalidOperationException>(() => sut.AddEntryAction(sale => Task.Delay(1), SaleState.Open));
        }
        public void AddEntryAction_throws_InvalidOperationException_if_already_set()
        {
            var stateMachine = new StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var sut          = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);

            sut.AddEntryAction((sale, _) => Task.CompletedTask);

            Assert.Throws <InvalidOperationException>(() => sut.AddEntryAction((sale, _) => Task.CompletedTask));
        }
Exemple #3
0
        public void AddEntryAction_throws_ArgumentNullException_if_Action_null()
        {
            var stateMachine = new StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var sut          = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);

            Assert.Throws <ArgumentNullException>(() => sut.AddEntryAction(action: null));
        }
        public async Task ExecuteEntryActionAsync_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 StateMachine <Sale, SaleState, SaleEvent>(sale1 => sale1.State, (sale1, newState) => sale1.State = newState);
            var sut                      = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);
            var entryActionFromOpenFired = false;

            sut.AddEntryAction((sale1, _) => { entryActionFromOpenFired = true; return(Task.CompletedTask); });

            using var cancelSource = new CancellationTokenSource();
            var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.Pay, sale, cancelSource.Token);

            cancelSource.Cancel();

            await sut.ExecuteEntryActionAsync(parameters, transitionResult);

            Assert.False(entryActionFromOpenFired);
        }
        public async Task ExecuteEntryActionAsync_executes_for_super_state()
        {
            var entryActionCalled = false;
            var stateMachine      = new StateMachine <Sale, SaleState, SaleEvent>(sale1 => sale1.State, (sale1, newState) => sale1.State = newState);
            var openConfig        = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.Open, stateMachine);

            openConfig.AddEntryAction((sale1, _) => { entryActionCalled = true; return(Task.CompletedTask); });
            var changeDueConfig = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);

            changeDueConfig.MakeSubStateOf(openConfig);
            var sale = new Sale(saleId: 96)
            {
                State = SaleState.ChangeDue
            };
            var parameters       = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.Pay, sale);
            var transitionResult = new StateTransitionResult <SaleState, SaleEvent>(SaleEvent.Pay
                                                                                    , SaleState.Open
                                                                                    , SaleState.Complete
                                                                                    , SaleState.ChangeDue
                                                                                    , "lastTransitionName");

            await changeDueConfig.ExecuteEntryActionAsync(parameters, transitionResult);

            Assert.True(entryActionCalled);
        }
Exemple #6
0
        public void ExecuteEntryAction_executes_EntryAction_without_previous_state()
        {
            var sale = new Sale(saleID: 96)
            {
                State = SaleState.ChangeDue
            };
            var transitionResult = new StateTransitionResult <SaleState, SaleEvent>(SaleEvent.AddItem
                                                                                    , SaleState.Open
                                                                                    , SaleState.Open
                                                                                    , SaleState.ChangeDue
                                                                                    , "lastTransitionName");
            var stateMachine             = new StateMachine <Sale, SaleState, SaleEvent>(sale1 => sale1.State, (sale1, newState) => sale1.State = newState);
            var sut                      = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);
            var entryActionFromOpenFired = false;

            sut.AddEntryAction(sale1 => { entryActionFromOpenFired = true; });

            sut.ExecuteEntryAction(sale, transitionResult);

            Assert.True(entryActionFromOpenFired);
        }
        public async Task ExecuteEntryActionAsync_executes_EntryAction_without_previous_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 StateMachine <Sale, SaleState, SaleEvent>(sale1 => sale1.State, (sale1, newState) => sale1.State = newState);
            var sut                      = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);
            var entryActionFromOpenFired = false;

            sut.AddEntryAction((sale1, _) => { entryActionFromOpenFired = true; return(Task.CompletedTask); });
            var parameters = new ExecutionParameters <Sale, SaleEvent>(SaleEvent.Pay, sale);

            await sut.ExecuteEntryActionAsync(parameters, transitionResult);

            Assert.True(entryActionFromOpenFired);
        }
Exemple #8
0
        public void ExecuteEntryActionAsync_executes_for_super_state()
        {
            var entryActionCalled = false;
            var stateMachine      = new StateMachine <Sale, SaleState, SaleEvent>(sale1 => sale1.State, (sale1, newState) => sale1.State = newState);
            var openConfig        = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.Open, stateMachine);

            openConfig.AddEntryAction(sale1 => { entryActionCalled = true; });
            var changeDueConfig = new StateConfiguration <Sale, SaleState, SaleEvent>(SaleState.ChangeDue, stateMachine);

            changeDueConfig.MakeSubstateOf(openConfig);
            var sale = new Sale(saleID: 96)
            {
                State = SaleState.ChangeDue
            };
            var transitionResult = new StateTransitionResult <SaleState, SaleEvent>(SaleEvent.Pay
                                                                                    , SaleState.Open
                                                                                    , SaleState.Complete
                                                                                    , SaleState.ChangeDue
                                                                                    , "lastTransitionName");

            changeDueConfig.ExecuteEntryAction(sale, transitionResult);

            Assert.True(entryActionCalled);
        }