@@ -58,5 +58,47 @@ void testFullCycle() {
5858 context .handleEvent (); // Yellow -> Red
5959 assertTrue (context .getCurrentState () instanceof RedLightState );
6060 }
61+
62+ // Test invalid state transition
63+ @ Test
64+ void testInvalidStateTransition () {
65+ context .setState (new RedLightState ());
66+ try {
67+ // This should fail, as it doesn't make sense for Red to handle an event again.
68+ context .handleEvent ();
69+ } catch (IllegalStateException e ) {
70+ assertTrue (true , "Handled invalid state transition." );
71+ }
72+ }
73+
74+ // Test state reset
75+ @ Test
76+ void testStateReset () {
77+ context .setState (new YellowLightState ());
78+ context .handleEvent (); // Yellow -> Red
79+ assertTrue (context .getCurrentState () instanceof RedLightState );
80+
81+ context .setState (new GreenLightState ());
82+ context .handleEvent (); // Green -> Yellow
83+ assertTrue (context .getCurrentState () instanceof YellowLightState );
84+
85+ context .setState (new RedLightState ());
86+ context .handleEvent (); // Red -> Green
87+ assertTrue (context .getCurrentState () instanceof GreenLightState );
88+ }
89+
90+ // Test manually setting the state
91+ @ Test
92+ void testManualStateSet () {
93+ context .setState (new GreenLightState ());
94+ assertTrue (context .getCurrentState () instanceof GreenLightState );
95+
96+ context .setState (new YellowLightState ());
97+ assertTrue (context .getCurrentState () instanceof YellowLightState );
98+
99+ context .setState (new RedLightState ());
100+ assertTrue (context .getCurrentState () instanceof RedLightState );
101+ }
61102}
62103
104+
0 commit comments