|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Tests for <c>WHILE cond stmt</c>, <c>BREAK</c>, and <c>CONTINUE</c>. |
| 7 | +/// Covers iteration with mutated cond, the one-statement-body footgun, |
| 8 | +/// nested loops (BREAK exits innermost), BREAK / CONTINUE scope check |
| 9 | +/// (Msg 135 / Msg 136 fire even from un-taken IF branches — real SQL |
| 10 | +/// Server's compile-time check), @@ROWCOUNT=0 at every exit path, |
| 11 | +/// non-boolean cond (Msg 4145), WHILE in un-taken IF (skip-mode), and |
| 12 | +/// the simulator's iteration cap. Behavior probed against SQL Server |
| 13 | +/// 2025 (2026-05-11). |
| 14 | +/// </summary> |
| 15 | +[TestClass] |
| 16 | +public sealed class WhileLoopTests |
| 17 | +{ |
| 18 | + [TestMethod] |
| 19 | + public void BasicCounter_RunsToCompletion() |
| 20 | + => AreEqual(3, new Simulation().ExecuteScalar<int>(""" |
| 21 | + declare @i int = 0; |
| 22 | + while @i < 3 set @i = @i + 1; |
| 23 | + select @i |
| 24 | + """)); |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Body is exactly one statement — same footgun as IF. The second |
| 28 | + /// statement after the body runs once *after* the loop, not per |
| 29 | + /// iteration. Probe-confirmed. |
| 30 | + /// </summary> |
| 31 | + [TestMethod] |
| 32 | + public void BodyOneStatement_SecondStatementEscapesLoop() |
| 33 | + => AreEqual(2, new Simulation().ExecuteScalar<int>(""" |
| 34 | + declare @i int = 0; |
| 35 | + while @i < 2 set @i = @i + 1 select @i |
| 36 | + """)); |
| 37 | + |
| 38 | + [TestMethod] |
| 39 | + public void CondInitiallyFalse_NoIteration() |
| 40 | + => AreEqual(0, new Simulation().ExecuteScalar<int>(""" |
| 41 | + declare @i int = 0; |
| 42 | + while 1 = 0 set @i = 99; |
| 43 | + select @i |
| 44 | + """)); |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + public void NonBooleanCond_Msg4145() |
| 48 | + => new Simulation().AssertSqlError("while 1 select 1", 4145); |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public void NullCond_Msg4145() |
| 52 | + => new Simulation().AssertSqlError("while null select 1", 4145); |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public void EmptyBeginEnd_Msg102() |
| 56 | + => new Simulation().AssertSqlError("while 1=0 begin end", 102); |
| 57 | + |
| 58 | + // ---- BREAK ---- |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void Break_ExitsLoop() |
| 62 | + => AreEqual(2, new Simulation().ExecuteScalar<int>(""" |
| 63 | + declare @i int = 0; |
| 64 | + while 1 = 1 |
| 65 | + begin |
| 66 | + set @i = @i + 1; |
| 67 | + if @i >= 2 break; |
| 68 | + end; |
| 69 | + select @i |
| 70 | + """)); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// BREAK skips remaining statements in the same block — subsequent |
| 74 | + /// statements after BREAK in the body never execute. Probe-confirmed. |
| 75 | + /// </summary> |
| 76 | + [TestMethod] |
| 77 | + public void Break_SkipsRemainingStatementsInBlock() |
| 78 | + => AreEqual(1, new Simulation().ExecuteScalar<int>(""" |
| 79 | + declare @sum int = 0; |
| 80 | + while 1 = 1 |
| 81 | + begin |
| 82 | + set @sum = @sum + 1; |
| 83 | + break; |
| 84 | + set @sum = @sum + 100; |
| 85 | + end; |
| 86 | + select @sum |
| 87 | + """)); |
| 88 | + |
| 89 | + // ---- CONTINUE ---- |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// CONTINUE skips the remainder of the current iteration and re- |
| 93 | + /// evaluates the cond. @hits counts only odd iterations because |
| 94 | + /// even iterations continue before incrementing @hits. |
| 95 | + /// </summary> |
| 96 | + [TestMethod] |
| 97 | + public void Continue_SkipsRemainderAndReevaluates() |
| 98 | + => AreEqual(3, new Simulation().ExecuteScalar<int>(""" |
| 99 | + declare @i int = 0, @hits int = 0; |
| 100 | + while @i < 5 |
| 101 | + begin |
| 102 | + set @i = @i + 1; |
| 103 | + if @i % 2 = 0 continue; |
| 104 | + set @hits = @hits + 1; |
| 105 | + end; |
| 106 | + select @hits |
| 107 | + """)); |
| 108 | + |
| 109 | + // ---- Scope check (Msg 135 / 136) ---- |
| 110 | + |
| 111 | + [TestMethod] |
| 112 | + public void BreakOutsideLoop_Msg135() |
| 113 | + => new Simulation().AssertSqlError("break", 135, "Cannot use a BREAK statement outside the scope of a WHILE statement."); |
| 114 | + |
| 115 | + [TestMethod] |
| 116 | + public void ContinueOutsideLoop_Msg136() |
| 117 | + => new Simulation().AssertSqlError("continue", 136, "Cannot use a CONTINUE statement outside the scope of a WHILE statement."); |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// BREAK inside an IF body (not in a WHILE) → Msg 135. The check is |
| 121 | + /// compile-time in real SQL Server, so it fires even though the IF |
| 122 | + /// might be un-taken at runtime. The simulator parses the IF body |
| 123 | + /// statement-by-statement; BREAK's scope check fires at parse time |
| 124 | + /// regardless of skip mode (probe-confirmed against SQL Server 2025). |
| 125 | + /// </summary> |
| 126 | + [TestMethod] |
| 127 | + public void BreakInsideIfNoLoop_StillMsg135() |
| 128 | + => new Simulation().AssertSqlError("if 1=1 break", 135); |
| 129 | + |
| 130 | + [TestMethod] |
| 131 | + public void BreakInsideUntakenIf_StillMsg135() |
| 132 | + => new Simulation().AssertSqlError("if 1=0 break", 135); |
| 133 | + |
| 134 | + [TestMethod] |
| 135 | + public void BreakInsideBlock_NoLoop_StillMsg135() |
| 136 | + => new Simulation().AssertSqlError("begin break end", 135); |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// BREAK inside an IF inside a WHILE: the LoopDepth check passes |
| 140 | + /// (we're in a WHILE), so no Msg 135. Iteration completes; outer |
| 141 | + /// WHILE keeps iterating. |
| 142 | + /// </summary> |
| 143 | + [TestMethod] |
| 144 | + public void BreakInsideIfInsideLoop_NoError() |
| 145 | + => AreEqual(5, new Simulation().ExecuteScalar<int>(""" |
| 146 | + declare @i int = 0; |
| 147 | + while @i < 5 |
| 148 | + begin |
| 149 | + set @i = @i + 1; |
| 150 | + if 1 = 0 break; |
| 151 | + end; |
| 152 | + select @i |
| 153 | + """)); |
| 154 | + |
| 155 | + // ---- Nested loops ---- |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Inner BREAK exits inner WHILE only. Outer continues iterating. |
| 159 | + /// Reset <c>@inner</c> inside the outer body via SET (not DECLARE — |
| 160 | + /// re-declaring inside a loop body would fire Msg 134 on iteration 2). |
| 161 | + /// </summary> |
| 162 | + [TestMethod] |
| 163 | + public void NestedBreak_ExitsInnerOnly() |
| 164 | + => AreEqual(3, new Simulation().ExecuteScalar<int>(""" |
| 165 | + declare @outer int = 0, @inner int = 0; |
| 166 | + while @outer < 3 |
| 167 | + begin |
| 168 | + set @outer = @outer + 1; |
| 169 | + set @inner = 0; |
| 170 | + while 1 = 1 |
| 171 | + begin |
| 172 | + set @inner = @inner + 1; |
| 173 | + if @inner >= 2 break; |
| 174 | + end; |
| 175 | + end; |
| 176 | + select @outer |
| 177 | + """)); |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// Nested CONTINUE only re-iterates the inner loop. Outer iteration |
| 181 | + /// continues normally after inner WHILE finishes. |
| 182 | + /// </summary> |
| 183 | + [TestMethod] |
| 184 | + public void NestedContinue_ReiteratesInnerOnly() |
| 185 | + => AreEqual(10, new Simulation().ExecuteScalar<int>(""" |
| 186 | + declare @outer int = 0, @inner int = 0, @total int = 0; |
| 187 | + while @outer < 2 |
| 188 | + begin |
| 189 | + set @outer = @outer + 1; |
| 190 | + set @inner = 0; |
| 191 | + while @inner < 4 |
| 192 | + begin |
| 193 | + set @inner = @inner + 1; |
| 194 | + if @inner % 2 = 0 continue; |
| 195 | + set @total = @total + 1; |
| 196 | + end; |
| 197 | + end; |
| 198 | + select @total + (@outer * 3) |
| 199 | + """)); |
| 200 | + |
| 201 | + // ---- WHILE in skipped IF ---- |
| 202 | + |
| 203 | + /// <summary> |
| 204 | + /// WHILE inside an un-taken IF branch: the WHILE never iterates. |
| 205 | + /// Critical because the body is <c>WHILE 1=1</c> (infinite loop) — |
| 206 | + /// if skip-mode failed, the test would hit the iteration cap. |
| 207 | + /// </summary> |
| 208 | + [TestMethod] |
| 209 | + public void WhileInsideSkippedIf_NeverIterates() |
| 210 | + => AreEqual(0, new Simulation().ExecuteScalar<int>(""" |
| 211 | + declare @i int = 0; |
| 212 | + if 1 = 0 while 1 = 1 set @i = 99; |
| 213 | + select @i |
| 214 | + """)); |
| 215 | + |
| 216 | + // ---- @@ROWCOUNT ---- |
| 217 | + |
| 218 | + [TestMethod] |
| 219 | + public void RowCount_AfterWhileNoIter_IsZero() |
| 220 | + => AreEqual(0, new Simulation().ExecuteScalar<int>(""" |
| 221 | + declare @prime int = 1; |
| 222 | + while 1 = 0 set @prime = 99; |
| 223 | + select @@rowcount |
| 224 | + """)); |
| 225 | + |
| 226 | + [TestMethod] |
| 227 | + public void RowCount_AfterWhileWithIters_IsZero() |
| 228 | + => AreEqual(0, new Simulation().ExecuteScalar<int>(""" |
| 229 | + declare @i int = 0; |
| 230 | + while @i < 2 begin set @i = @i + 1 end; |
| 231 | + select @@rowcount |
| 232 | + """)); |
| 233 | + |
| 234 | + [TestMethod] |
| 235 | + public void RowCount_AfterBreakExit_IsZero() |
| 236 | + => AreEqual(0, new Simulation().ExecuteScalar<int>(""" |
| 237 | + declare @i int = 0; |
| 238 | + while 1 = 1 |
| 239 | + begin |
| 240 | + set @i = @i + 1; |
| 241 | + if @i >= 1 break; |
| 242 | + end; |
| 243 | + select @@rowcount |
| 244 | + """)); |
| 245 | + |
| 246 | + // ---- Cond mutation across iterations ---- |
| 247 | + |
| 248 | + [TestMethod] |
| 249 | + public void CondReferencesVariable_MutationVisibleAcrossIters() |
| 250 | + => AreEqual(5, new Simulation().ExecuteScalar<int>(""" |
| 251 | + declare @i int = 0; |
| 252 | + while @i < 5 set @i = @i + 1; |
| 253 | + select @i |
| 254 | + """)); |
| 255 | + |
| 256 | + // ---- Iteration cap ---- |
| 257 | + |
| 258 | + /// <summary> |
| 259 | + /// Simulator-only safety: a runaway WHILE throws after the per-batch |
| 260 | + /// iteration cap is exceeded. Real SQL Server has no such cap — query |
| 261 | + /// timeouts handle this in production — but the simulator surfaces an |
| 262 | + /// explicit error so a buggy test doesn't hang CI. |
| 263 | + /// </summary> |
| 264 | + [TestMethod] |
| 265 | + public void IterationCap_ThrowsAfterLimit() |
| 266 | + { |
| 267 | + var ex = Throws<InvalidOperationException>(() => new Simulation().ExecuteNonQuery(""" |
| 268 | + declare @i int = 0; |
| 269 | + while 1 = 1 set @i = @i + 1 |
| 270 | + """)); |
| 271 | + Contains("iteration cap exceeded", ex.Message, StringComparison.Ordinal); |
| 272 | + } |
| 273 | +} |
0 commit comments