View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package integration.tests.loops;
7   
8   import org.junit.jupiter.api.Assertions;
9   import org.junit.jupiter.api.Test;
10  
11  import integration.tests.CoverageTest;
12  
13  class WhileStatementsTest extends CoverageTest {
14      WhileStatements tested;
15  
16      @Test
17      void whileBlockInSeparateLines() {
18          tested.whileBlockInSeparateLines();
19  
20          assertLines(17, 22, 4);
21          assertLine(17, 1, 1, 1);
22          assertLine(19, 1, 1, 6);
23          assertLine(20, 1, 1, 5);
24          assertLine(22, 1, 1, 1);
25      }
26  
27      @Test
28      void whileBlockInSingleLine() {
29          tested.whileBlockInSingleLine(0);
30          tested.whileBlockInSingleLine(1);
31          tested.whileBlockInSingleLine(2);
32  
33          assertLines(25, 28, 2);
34          assertLine(26, 2, 2, 6);
35          assertLine(28, 1, 1, 3);
36      }
37  
38      @Test
39      void whileWithIfElse() {
40          tested.whileWithIfElse(0);
41          tested.whileWithIfElse(1);
42          tested.whileWithIfElse(2);
43  
44          assertLines(131, 142, 5);
45      }
46  
47      @Test
48      void whileWithContinue() {
49          tested.whileWithContinue(0);
50          tested.whileWithContinue(1);
51          tested.whileWithContinue(2);
52  
53          assertLines(31, 40, 6);
54          assertLine(31, 1, 1, 6);
55          assertLine(32, 1, 1, 3);
56          assertLine(33, 1, 1, 2);
57          assertLine(34, 1, 1, 2);
58          assertLine(37, 1, 1, 1);
59          assertLine(40, 1, 1, 3);
60      }
61  
62      @Test
63      void whileWithBreak() {
64          tested.whileWithBreak(0);
65          tested.whileWithBreak(1);
66          tested.whileWithBreak(2);
67  
68          assertLines(44, 52, 5);
69          assertLine(44, 2, 2, 4);
70          assertLine(45, 1, 1, 3);
71          assertLine(46, 1, 1, 2);
72          assertLine(49, 1, 1, 1);
73          assertLine(52, 1, 1, 3);
74      }
75  
76      @Test
77      void nestedWhile() {
78          tested.nestedWhile(0, 2);
79          tested.nestedWhile(1, 1);
80  
81          assertLines(56, 63, 4);
82          assertLine(56, 2, 2, 3);
83          assertLine(57, 1, 1, 1);
84          assertLine(58, 1, 0, 0);
85          assertLine(61, 1, 1, 1);
86          assertLine(63, 1, 1, 2);
87      }
88  
89      @Test
90      void doWhileInSeparateLines() {
91          tested.doWhileInSeparateLines();
92  
93          assertLines(66, 71, 4);
94          assertLine(66, 1, 1, 1);
95          assertLine(69, 1, 1, 3);
96          assertLine(70, 2, 2, 3);
97          assertLine(71, 1, 1, 1);
98      }
99  
100     @Test
101     void bothKindsOfWhileCombined() {
102         tested.bothKindsOfWhileCombined(0, 0);
103         tested.bothKindsOfWhileCombined(0, 2);
104         tested.bothKindsOfWhileCombined(1, 1);
105 
106         assertLines(76, 84, 4);
107         assertLine(76, 1, 1, 5);
108         assertLine(79, 2, 2, 5);
109         assertLine(82, 1, 1, 4);
110         assertLine(84, 2, 2, 4);
111     }
112 
113     @Test
114     void whileTrueEndingWithAnIf() {
115         tested.whileTrueEndingWithAnIf(0);
116 
117         // TODO: assertions
118     }
119 
120     @Test
121     void whileTrueStartingWithAnIf() {
122         tested.whileTrueStartingWithAnIf(0);
123 
124         // TODO: assertions
125     }
126 
127     @Test
128     void whileTrueWithoutExitCondition() {
129         Assertions.assertThrows(IllegalStateException.class, () -> {
130             tested.whileTrueWithoutExitCondition();
131         });
132     }
133 
134     @Test
135     public void whileTrueContainingTryFinally() {
136         Assertions.assertThrows(IllegalStateException.class, () -> {
137             tested.whileTrueContainingTryFinally();
138         });
139     }
140 }