示例#1
0
        public void Can_transition_between_states()
        {
            using (new StateProviderFixture())
            {
                StateProvider.Setup <ThreeValidStaticStates>();

                var data  = new ThreeValidStatesData();
                var actor = new ThreeValidStaticStates();

                actor.SetState <ThreeValidStaticStates.StateA>(data);
                actor.SetState <ThreeValidStaticStates.StateB>(data);
                actor.SetState <ThreeValidStaticStates.StateC>(data);

                Assert.True(data.BeginStateA);
                Assert.True(data.EndStateA);
                Assert.True(data.BeginStateB);
                Assert.True(data.EndStateB);

                // last state was C
                Assert.Equal(typeof(ThreeValidStaticStates.StateC), actor.CurrentState.GetType());
                Assert.Equal("ThreeValidStaticStates (StateC)", actor.DisplayName);

                // all states A and C have names
                Assert.NotNull(actor.GetState <ThreeValidStaticStates.StateA>().Name);
                Assert.NotNull(actor.GetState <ThreeValidStaticStates.StateC>().Name);

                // can lookup state by name
                Assert.Equal(typeof(ThreeValidStaticStates.StateA), actor.GetStateByName("StateA").GetType());

                // can enumerate all states
                var allStates = StateProvider.GetAllStatesByType <ThreeValidStaticStates>();
                Assert.Equal(4, allStates.Count);
            }
        }
示例#2
0
 public void Duplicate_state_method_throws()
 {
     using (new StateProviderFixture())
     {
         Assert.Throws <DuplicateStateMethodException>(() => { StateProvider.Setup <DuplicateStateMethods>(); });
     }
 }
示例#3
0
 public void Calling_setup_once_does_not_throw()
 {
     using (new StateProviderFixture())
     {
         StateProvider.Setup(typeof(NoStates));
     }
 }
示例#4
0
 public void Can_inherit_states_and_pass_null_context()
 {
     using (new StateProviderFixture())
     {
         StateProvider.Setup <StateInheritance>();
         var actor = new StateInheritance();
         actor.SetState <StateInheritance.StateA>();
     }
 }
示例#5
0
 public void Calling_setup_after_clear_does_not_throw()
 {
     using (new StateProviderFixture())
     {
         StateProvider.Setup(typeof(NoStates));
         StateProvider.Clear();
         StateProvider.Setup(typeof(NoStates));
     }
 }
示例#6
0
 public void Missing_state_method_throws()
 {
     using (new StateProviderFixture())
     {
         Assert.Throws <UnusedStateMethodsException>(() =>
         {
             StateProvider.Setup <MissingStateForStateMethod>();
         });
     }
 }
示例#7
0
 public void Calling_setup_twice_throws()
 {
     using (new StateProviderFixture())
     {
         Assert.Throws <AlreadyInitializedException>(() =>
         {
             StateProvider.Setup(typeof(NoStates));
             StateProvider.Setup(typeof(NoStates));
         });
     }
 }
示例#8
0
        public void Can_inherit_state_data()
        {
            using (new StateProviderFixture())
            {
                StateProvider.Setup <StateDataInheritance>();

                var data = new BaseStateData {
                    A = true
                };
                var actor = new StateDataInheritance();
                actor.SetState <StateDataInheritance.StateA>(data);
                Assert.False(data.A);
            }
        }
示例#9
0
        public void BeforeBegin_failure_blocks_state_transition()
        {
            using (new StateProviderFixture())
            {
                StateProvider.Setup <ThreeValidStaticStates>();

                var data = new ThreeValidStatesData {
                    AllowBeginStateC = false
                };
                var actor = new ThreeValidStaticStates();

                Assert.Equal(typeof(StateProvider.State), actor.CurrentState.GetType());
                actor.SetState <ThreeValidStaticStates.StateC>(data);
                Assert.Equal(typeof(StateProvider.State), actor.CurrentState.GetType());
            }
        }
示例#10
0
        public void Reentrant_state_ends_itself()
        {
            using (new StateProviderFixture())
            {
                StateProvider.Setup <ThreeValidStaticStates>();

                var data  = new ThreeValidStatesData();
                var actor = new ThreeValidStaticStates();

                actor.SetState <ThreeValidStaticStates.StateA>(data);
                actor.SetState <ThreeValidStaticStates.StateA>(data, true);

                Assert.True(data.BeginStateA);
                Assert.True(data.EndStateA);
            }
        }
示例#11
0
        public void Non_reentrant_state_does_nothing_when_reentering()
        {
            using (new StateProviderFixture())
            {
                StateProvider.Setup <ThreeValidStaticStates>();

                var data  = new ThreeValidStatesData();
                var actor = new ThreeValidStaticStates();

                actor.SetState <ThreeValidStaticStates.StateA>(data);
                actor.SetState <ThreeValidStaticStates.StateA>(data);

                Assert.True(data.BeginStateA);
                Assert.False(data.EndStateA);
            }
        }
示例#12
0
        public void Update_respects_current_state_and_is_optional()
        {
            using (new StateProviderFixture())
            {
                StateProvider.Setup <ThreeValidStaticStates>();

                var data  = new ThreeValidStatesData();
                var actor = new ThreeValidStaticStates();

                Assert.Equal(0, data.TicksA);
                Assert.Equal(0, data.TicksB);

                actor.SetState <ThreeValidStaticStates.StateA>(data);
                actor.Update(data);
                actor.SetState <ThreeValidStaticStates.StateB>(data);
                actor.Update(data);
                actor.SetState <ThreeValidStaticStates.StateC>(data);
                actor.Update(data);

                Assert.Equal(1, data.TicksA);
                Assert.Equal(1, data.TicksB);
            }
        }