|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Tests for the bare <c>RETURN</c> statement (the value-form |
| 7 | +/// <c>RETURN N</c> is reserved for stored procedures / functions, neither |
| 8 | +/// of which is modeled — value form always raises Msg 178). Covers |
| 9 | +/// batch-exit semantics, propagation through IF / BEGIN…END / WHILE, |
| 10 | +/// the compile-time Msg 178 check (fires even in un-taken IF, same |
| 11 | +/// pattern as BREAK Msg 135), and skip-mode interactions. Behavior |
| 12 | +/// probed against SQL Server 2025 (2026-05-11). |
| 13 | +/// </summary> |
| 14 | +[TestClass] |
| 15 | +public sealed class ReturnStatementTests |
| 16 | +{ |
| 17 | + [TestMethod] |
| 18 | + public void BareReturn_ExitsBatch() |
| 19 | + { |
| 20 | + using var reader = new Simulation().ExecuteReader("select 'before'; return; select 'after'"); |
| 21 | + IsTrue(reader.Read()); |
| 22 | + AreEqual("before", reader.GetString(0)); |
| 23 | + IsFalse(reader.Read()); |
| 24 | + IsFalse(reader.NextResult()); |
| 25 | + } |
| 26 | + |
| 27 | + [TestMethod] |
| 28 | + public void BareReturn_AtEndOfBatch_Clean() |
| 29 | + { |
| 30 | + // ExecuteNonQuery returns -1 when no DML / DDL ran; the assertion |
| 31 | + // is just that the batch completed without throwing. |
| 32 | + _ = new Simulation().ExecuteNonQuery("return"); |
| 33 | + } |
| 34 | + |
| 35 | + [TestMethod] |
| 36 | + public void ReturnInTakenIf_ExitsBatch() |
| 37 | + { |
| 38 | + using var reader = new Simulation().ExecuteReader("if 1=1 return; select 'after'"); |
| 39 | + IsFalse(reader.Read()); |
| 40 | + IsFalse(reader.NextResult()); |
| 41 | + } |
| 42 | + |
| 43 | + [TestMethod] |
| 44 | + public void ReturnInUntakenIf_StatementAfterRuns() |
| 45 | + => AreEqual("after", new Simulation().ExecuteScalar( |
| 46 | + "if 1=0 return; select 'after'")); |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public void ReturnInElseBranch_ExitsBatch() |
| 50 | + { |
| 51 | + using var reader = new Simulation().ExecuteReader( |
| 52 | + "if 1=0 select 'then' else return; select 'after'"); |
| 53 | + IsFalse(reader.Read()); |
| 54 | + IsFalse(reader.NextResult()); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Probe-confirmed: RETURN inside a WHILE exits the entire batch, not |
| 59 | + /// just the loop (unlike BREAK). |
| 60 | + /// </summary> |
| 61 | + [TestMethod] |
| 62 | + public void ReturnInWhile_ExitsBatchNotJustLoop() |
| 63 | + { |
| 64 | + using var reader = new Simulation().ExecuteReader(""" |
| 65 | + declare @i int = 0; |
| 66 | + while @i < 100 |
| 67 | + begin |
| 68 | + set @i = @i + 1; |
| 69 | + if @i = 3 return; |
| 70 | + end; |
| 71 | + select 'after' |
| 72 | + """); |
| 73 | + IsFalse(reader.Read()); |
| 74 | + IsFalse(reader.NextResult()); |
| 75 | + } |
| 76 | + |
| 77 | + [TestMethod] |
| 78 | + public void ReturnInNestedWhile_ExitsAllLoops() |
| 79 | + { |
| 80 | + using var reader = new Simulation().ExecuteReader(""" |
| 81 | + while 1=1 |
| 82 | + begin |
| 83 | + while 1=1 |
| 84 | + begin |
| 85 | + return; |
| 86 | + end; |
| 87 | + end; |
| 88 | + select 'after' |
| 89 | + """); |
| 90 | + IsFalse(reader.Read()); |
| 91 | + IsFalse(reader.NextResult()); |
| 92 | + } |
| 93 | + |
| 94 | + [TestMethod] |
| 95 | + public void ReturnInBlock_SkipsSiblings() |
| 96 | + { |
| 97 | + using var reader = new Simulation().ExecuteReader(""" |
| 98 | + begin |
| 99 | + select 'a'; |
| 100 | + return; |
| 101 | + select 'b'; |
| 102 | + end; |
| 103 | + select 'after' |
| 104 | + """); |
| 105 | + IsTrue(reader.Read()); |
| 106 | + AreEqual("a", reader.GetString(0)); |
| 107 | + IsFalse(reader.Read()); |
| 108 | + IsFalse(reader.NextResult()); |
| 109 | + } |
| 110 | + |
| 111 | + [TestMethod] |
| 112 | + public void MultipleReturn_FirstWins() |
| 113 | + { |
| 114 | + _ = new Simulation().ExecuteNonQuery("return; return; return"); |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Bare RETURN followed by a statement-starting keyword is bare RETURN |
| 119 | + /// — the SELECT begins a new statement (which gets abandoned because |
| 120 | + /// the batch is exiting). Probe-confirmed against SQL Server 2025. |
| 121 | + /// </summary> |
| 122 | + [TestMethod] |
| 123 | + public void ReturnFollowedByStatementKeyword_IsBareReturn() |
| 124 | + { |
| 125 | + // SELECT 1 never runs (batch exits via RETURN); we just verify the |
| 126 | + // batch parses + completes without throwing Msg 178. |
| 127 | + using var reader = new Simulation().ExecuteReader("return select 1"); |
| 128 | + IsFalse(reader.Read()); |
| 129 | + IsFalse(reader.NextResult()); |
| 130 | + } |
| 131 | + |
| 132 | + // ---- Msg 178: value-form RETURN ---- |
| 133 | + |
| 134 | + [TestMethod] |
| 135 | + public void ReturnInteger_Msg178() |
| 136 | + => new Simulation().AssertSqlError("return 5", 178, |
| 137 | + "A RETURN statement with a return value cannot be used in this context."); |
| 138 | + |
| 139 | + [TestMethod] |
| 140 | + public void ReturnZero_Msg178() |
| 141 | + => new Simulation().AssertSqlError("return 0", 178); |
| 142 | + |
| 143 | + [TestMethod] |
| 144 | + public void ReturnNull_Msg178() |
| 145 | + => new Simulation().AssertSqlError("return null", 178); |
| 146 | + |
| 147 | + [TestMethod] |
| 148 | + public void ReturnString_Msg178() |
| 149 | + => new Simulation().AssertSqlError("return 'abc'", 178); |
| 150 | + |
| 151 | + [TestMethod] |
| 152 | + public void ReturnVariable_Msg178() |
| 153 | + => new Simulation().AssertSqlError("declare @v int = 7; return @v", 178); |
| 154 | + |
| 155 | + [TestMethod] |
| 156 | + public void ReturnExpression_Msg178() |
| 157 | + => new Simulation().AssertSqlError("return (1+2)", 178); |
| 158 | + |
| 159 | + [TestMethod] |
| 160 | + public void ReturnParenWrapped_Msg178() |
| 161 | + => new Simulation().AssertSqlError("return(1)", 178); |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// Compile-time check: Msg 178 fires even when the RETURN is in an |
| 165 | + /// un-taken IF branch. Probe-confirmed — same semantics as BREAK Msg 135. |
| 166 | + /// </summary> |
| 167 | + [TestMethod] |
| 168 | + public void ReturnValueInUntakenIf_StillMsg178() |
| 169 | + => new Simulation().AssertSqlError("if 1=0 return 5", 178); |
| 170 | + |
| 171 | + [TestMethod] |
| 172 | + public void ReturnValueInUntakenIf_BeforeFollowingStmt_StillMsg178() |
| 173 | + => new Simulation().AssertSqlError("if 1=0 return 5; select 'ok'", 178); |
| 174 | +} |
0 commit comments