public void TransitionExample()
        {
            var currentState = new CartStateEmpty() as ICartState;

            //add some products from the UI, say
            currentState = TransitionAddProduct(currentState, Product.ProductX);
            currentState = TransitionAddProduct(currentState, Product.ProductY);

            //pay from the UI, say
            var cartStateActive = currentState as CartStateActive; //CAST!
            if (cartStateActive == null)
            {
                Assert.Fail("Expected CartStateActive here");
            }

            const decimal paidAmount = 12.34m;
            currentState = cartStateActive.Pay(paidAmount);

            var cartStatePaid = currentState as CartStatePaid; //CAST!
            if (cartStatePaid == null)
            {
                Assert.Fail("Expected CartStatePaid here");
            }

            var itemCount = cartStatePaid.Items.Count();
            Assert.That(itemCount, Is.EqualTo(2));

            var actualPaidAmount = cartStatePaid.Amount;
            Assert.That(actualPaidAmount, Is.EqualTo(paidAmount));
        }
        public void TransitionExample()
        {
            var currentState = new CartStateEmpty() as ICartState;

            //add some products from the UI, say
            currentState = TransitionAddProduct(currentState, Product.ProductX);
            currentState = TransitionAddProduct(currentState, Product.ProductY);

            //pay from the UI, say
            const decimal paidAmount = 12.34m;
            currentState = currentState.Transition(
                cartStateEmpty => cartStateEmpty,
                cartStateActive => cartStateActive.Pay(paidAmount),
                cartStatePaid => cartStatePaid);

            var isPaidState = currentState.Func(cartStateEmpty => false, cartStateActive => false, cartStatePaid => true);
            Assert.IsTrue(isPaidState);

            var itemCount = currentState.Func(cartStateEmpty => -1, cartStateActive => -1, cartStatePaid => cartStatePaid.Items.Count());
            Assert.That(itemCount, Is.EqualTo(2));

            var actualPaidAmount = currentState.Func(cartStateEmpty => -1, cartStateActive => -1, cartStatePaid => cartStatePaid.Amount);
            Assert.That(actualPaidAmount, Is.EqualTo(paidAmount));
        }
 public ICartState VisitEmpty(CartStateEmpty empty)
 {
     return empty;
 }
 public ICartState VisitEmpty(CartStateEmpty empty)
 {
     empty.AddItem(productToAdd); return empty;
 }
        public void WhenEmptyCartAndAddItemExpectActiveCartWithOneItem()
        {
            var emptyCart = new CartStateEmpty();

            var newState = emptyCart.Add(Product.ProductX);

            var isActiveState = newState.Func(cartStateEmpty => false, cartStateActive => true, cartStatePaid => false);
            Assert.IsTrue(isActiveState);

            var itemCount = newState.Func(cartStateEmpty => -1, cartStateActive => cartStateActive.Items.Count(), cartStatePaid => -1);
            Assert.That(itemCount, Is.EqualTo(1));
        }
        public void WhenEmptyCartAndAddItemExpectActiveCartWithOneItem()
        {
            // arrange
            var emptyCart = new CartStateEmpty();

            // act
            var newState = emptyCart.Add(Product.ProductX);

            // assert
            var activeState = newState as CartStateActive; //CAST!
            if (activeState != null)
            {
                var itemCount = activeState.Items.Count();
                Assert.That(itemCount, Is.EqualTo(1));
            }
            else
            {
                Assert.Fail("Expect ActiveState");
            }
        }