|
| 1 | +using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert; |
| 2 | + |
| 3 | +namespace SqlServerSimulator; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Tests for the <c>PRINT</c> statement. The simulator parses + evaluates |
| 7 | +/// the operand (so type / coercion errors surface naturally) and discards |
| 8 | +/// the result — <see cref="SimulatedDbConnection"/> doesn't expose an |
| 9 | +/// <c>InfoMessage</c> event because <c>DbConnection</c> doesn't define one |
| 10 | +/// and there's no demonstrated need for the public surface yet. The tests |
| 11 | +/// verify side-effect-free behavior (<c>@@ROWCOUNT</c> reset, skip-mode |
| 12 | +/// suppression, runtime errors from the operand path) rather than message |
| 13 | +/// capture. Behavior probed against SQL Server 2025 (2026-05-11). |
| 14 | +/// </summary> |
| 15 | +[TestClass] |
| 16 | +public sealed class PrintStatementTests |
| 17 | +{ |
| 18 | + [TestMethod] |
| 19 | + public void Print_StringLiteral_DoesNotThrow() |
| 20 | + => _ = new Simulation().ExecuteNonQuery("print 'hello'"); |
| 21 | + |
| 22 | + [TestMethod] |
| 23 | + public void Print_Null_DoesNotThrow() |
| 24 | + => _ = new Simulation().ExecuteNonQuery("print null"); |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void Print_Integer_DoesNotThrow() |
| 28 | + => _ = new Simulation().ExecuteNonQuery("print 42"); |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + public void Print_Decimal_DoesNotThrow() |
| 32 | + => _ = new Simulation().ExecuteNonQuery("print 1.5"); |
| 33 | + |
| 34 | + [TestMethod] |
| 35 | + public void Print_Float_DoesNotThrow() |
| 36 | + => _ = new Simulation().ExecuteNonQuery("print cast(1.5 as float)"); |
| 37 | + |
| 38 | + [TestMethod] |
| 39 | + public void Print_Variable_DoesNotThrow() |
| 40 | + => _ = new Simulation().ExecuteNonQuery("declare @v varchar(10) = 'hi'; print @v"); |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public void Print_Expression_DoesNotThrow() |
| 44 | + => _ = new Simulation().ExecuteNonQuery("print 5 + 3"); |
| 45 | + |
| 46 | + [TestMethod] |
| 47 | + public void Print_StringConcat_DoesNotThrow() |
| 48 | + => _ = new Simulation().ExecuteNonQuery("print 'a' + 'b'"); |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public void Print_Case_DoesNotThrow() |
| 52 | + => _ = new Simulation().ExecuteNonQuery("print case when 1=1 then 'y' else 'n' end"); |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// PRINT evaluates the operand normally, so the <c>+</c> operator's |
| 56 | + /// int-side promotion still kicks in — <c>'val=' + 5</c> tries to parse |
| 57 | + /// <c>'val='</c> as int and raises Msg 245. Probe-confirmed: real SQL |
| 58 | + /// Server raises the same Msg 245. |
| 59 | + /// </summary> |
| 60 | + [TestMethod] |
| 61 | + public void Print_StringPlusInt_Msg245() |
| 62 | + => new Simulation().AssertSqlError("print 'val=' + 5", 245); |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Probe-confirmed: PRINT resets <c>@@ROWCOUNT</c> to 0 — the next |
| 66 | + /// statement reads 0 regardless of whatever the prior statement set. |
| 67 | + /// </summary> |
| 68 | + [TestMethod] |
| 69 | + public void Print_Resets_RowCount_To_Zero() |
| 70 | + { |
| 71 | + using var reader = new Simulation().ExecuteReader(""" |
| 72 | + select 1 union all select 2 union all select 3; |
| 73 | + print 'between'; |
| 74 | + select @@rowcount as rc |
| 75 | + """); |
| 76 | + // Drain the first result set (the SELECT … UNION ALL). |
| 77 | + while (reader.Read()) { } |
| 78 | + IsTrue(reader.NextResult()); |
| 79 | + IsTrue(reader.Read()); |
| 80 | + AreEqual(0, reader.GetInt32(0)); |
| 81 | + } |
| 82 | + |
| 83 | + // ---- Skip-mode interaction ---- |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// In an un-taken IF branch, PRINT's operand isn't evaluated — so an |
| 87 | + /// otherwise-error-raising expression inside the un-taken branch is |
| 88 | + /// silently skipped (matches every other statement parser's skip-mode |
| 89 | + /// gate). |
| 90 | + /// </summary> |
| 91 | + [TestMethod] |
| 92 | + public void Print_InUntakenIf_OperandNotEvaluated() |
| 93 | + => _ = new Simulation().ExecuteNonQuery("if 1=0 print 'val=' + 5"); |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public void Print_InTakenIf_StillEvaluates() |
| 97 | + => new Simulation().AssertSqlError("if 1=1 print 'val=' + 5", 245); |
| 98 | + |
| 99 | + [TestMethod] |
| 100 | + public void Print_InUntakenElse_OperandNotEvaluated() |
| 101 | + => _ = new Simulation().ExecuteNonQuery("if 1=1 select 'taken' else print 'val=' + 5"); |
| 102 | + |
| 103 | + [TestMethod] |
| 104 | + public void Print_AfterReturn_NotEvaluated() |
| 105 | + => _ = new Simulation().ExecuteNonQuery("return; print 'val=' + 5"); |
| 106 | + |
| 107 | + [TestMethod] |
| 108 | + public void Print_InBlock_BeforeReturn_Evaluates() |
| 109 | + => new Simulation().AssertSqlError( |
| 110 | + "begin print 'val=' + 5; return; end", |
| 111 | + 245); |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// PRINT inside a WHILE evaluates each iteration. Verify by including |
| 115 | + /// an operand that would always error if reached past the BREAK gate. |
| 116 | + /// </summary> |
| 117 | + [TestMethod] |
| 118 | + public void Print_InWhile_RunsEachIteration() |
| 119 | + { |
| 120 | + // Loop runs twice, then BREAKs; PRINT inside fires on both runs. |
| 121 | + _ = new Simulation().ExecuteNonQuery(""" |
| 122 | + declare @i int = 0; |
| 123 | + while @i < 2 |
| 124 | + begin |
| 125 | + set @i = @i + 1; |
| 126 | + print @i; |
| 127 | + end |
| 128 | + """); |
| 129 | + } |
| 130 | + |
| 131 | + // ---- Statement composition / dispatch ---- |
| 132 | + |
| 133 | + [TestMethod] |
| 134 | + public void Multiple_Prints_AllRun() |
| 135 | + => _ = new Simulation().ExecuteNonQuery("print 'a'; print 'b'; print 'c'"); |
| 136 | + |
| 137 | + [TestMethod] |
| 138 | + public void Print_Then_Select_SelectReturnsRow() |
| 139 | + { |
| 140 | + using var reader = new Simulation().ExecuteReader("print 'x'; select 1 as v"); |
| 141 | + IsTrue(reader.Read()); |
| 142 | + AreEqual(1, reader.GetInt32(0)); |
| 143 | + IsFalse(reader.Read()); |
| 144 | + } |
| 145 | + |
| 146 | + [TestMethod] |
| 147 | + public void Select_Then_Print_SelectReturnsRow() |
| 148 | + { |
| 149 | + using var reader = new Simulation().ExecuteReader("select 1 as v print 'x'"); |
| 150 | + IsTrue(reader.Read()); |
| 151 | + AreEqual(1, reader.GetInt32(0)); |
| 152 | + IsFalse(reader.Read()); |
| 153 | + } |
| 154 | + |
| 155 | + [TestMethod] |
| 156 | + public void Print_BareReturnsAfter_BatchContinues() |
| 157 | + { |
| 158 | + using var reader = new Simulation().ExecuteReader("print 'before'; select 'after'"); |
| 159 | + IsTrue(reader.Read()); |
| 160 | + AreEqual("after", reader.GetString(0)); |
| 161 | + } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// PRINT inside a rolled-back transaction is a no-op for the simulator |
| 165 | + /// (output is discarded anyway). The point of this test is to verify |
| 166 | + /// PRINT doesn't interact badly with the undo log or transaction state. |
| 167 | + /// </summary> |
| 168 | + [TestMethod] |
| 169 | + public void Print_InRolledBackTransaction_NoStateLeak() |
| 170 | + { |
| 171 | + var sim = new Simulation(); |
| 172 | + _ = sim.ExecuteNonQuery("begin tran; print 'inside'; rollback"); |
| 173 | + // Subsequent statement on a fresh connection should work normally. |
| 174 | + AreEqual(1, sim.ExecuteScalar<int>("select 1")); |
| 175 | + } |
| 176 | +} |
0 commit comments