@@ -99,6 +99,59 @@ void testManualStateSet() {
9999 context .setState (new RedLightState ());
100100 assertTrue (context .getCurrentState () instanceof RedLightState );
101101 }
102+
103+ // Additional tests for edge cases
104+
105+ // Test if state is correctly set in the middle of a cycle
106+ @ Test
107+ void testMidCycleStateSet () {
108+ context .handleEvent (); // Red -> Green
109+ assertTrue (context .getCurrentState () instanceof GreenLightState );
110+
111+ // Set state manually in the middle of the cycle
112+ context .setState (new YellowLightState ());
113+ assertTrue (context .getCurrentState () instanceof YellowLightState );
114+
115+ context .handleEvent (); // Yellow -> Red
116+ assertTrue (context .getCurrentState () instanceof RedLightState );
117+ }
118+
119+ // Test if context properly resets after complete cycle
120+ @ Test
121+ void testContextResetAfterFullCycle () {
122+ context .handleEvent (); // Red -> Green
123+ context .handleEvent (); // Green -> Yellow
124+ context .handleEvent (); // Yellow -> Red
125+
126+ // Reset and verify again
127+ context .setState (new GreenLightState ());
128+ assertTrue (context .getCurrentState () instanceof GreenLightState );
129+
130+ context .handleEvent (); // Green -> Yellow
131+ assertTrue (context .getCurrentState () instanceof YellowLightState );
132+
133+ context .handleEvent (); // Yellow -> Red
134+ assertTrue (context .getCurrentState () instanceof RedLightState );
135+ }
136+
137+ // Test if the initial state remains unchanged after multiple cycles
138+ @ Test
139+ void testMultipleCycles () {
140+ context .handleEvent (); // Red -> Green
141+ context .handleEvent (); // Green -> Yellow
142+ context .handleEvent (); // Yellow -> Red
143+
144+ // Perform another full cycle and ensure the state remains correct
145+ context .handleEvent (); // Red -> Green
146+ assertTrue (context .getCurrentState () instanceof GreenLightState );
147+
148+ context .handleEvent (); // Green -> Yellow
149+ assertTrue (context .getCurrentState () instanceof YellowLightState );
150+
151+ context .handleEvent (); // Yellow -> Red
152+ assertTrue (context .getCurrentState () instanceof RedLightState );
153+ }
102154}
103155
104156
157+
0 commit comments