Skip to content

Commit cae0910

Browse files
committed
Improved support for block comments.
1 parent 7f98886 commit cae0910

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

SqlServerSimulator.Tests/SimpleCommandTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,23 @@ public void SingleLineCommentWithCarriageReturnCommand()
107107
}
108108

109109
[TestMethod]
110-
public void BlockComment()
110+
[DataRow("/* */")]
111+
[DataRow("/**/")]
112+
[DataRow("/***/")]
113+
[DataRow("/** **/")]
114+
[DataRow("/*\r*/")]
115+
[DataRow("/*\n*/")]
116+
[DataRow("/*\r\n*/")]
117+
public void BlockComment(string comment)
111118
{
112-
AreEqual(-1, new Simulation().ExecuteNonQuery("/* */"));
119+
IsNull(new Simulation().ExecuteScalar($"{comment}"));
120+
AreEqual(1, new Simulation().ExecuteScalar<int>($"select 1 {comment}"));
121+
AreEqual(2, new Simulation().ExecuteScalar<int>($"select 2{comment}"));
122+
AreEqual(3, new Simulation().ExecuteScalar<int>($"{comment}select 3"));
123+
AreEqual(4, new Simulation().ExecuteScalar<int>($"{comment} select 4"));
124+
AreEqual(5, new Simulation().ExecuteScalar<int>($"select{comment}5"));
125+
AreEqual(6, new Simulation().ExecuteScalar<int>($"select {comment}6"));
126+
AreEqual(7, new Simulation().ExecuteScalar<int>($"select{comment} 7"));
127+
AreEqual(8, new Simulation().ExecuteScalar<int>($"select {comment} 8"));
113128
}
114129
}

SqlServerSimulator/Parser/ParserContext.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,22 @@ public void MoveNextRequired()
121121
throw SimulatedSqlException.SyntaxErrorNear(previous);
122122
}
123123

124+
/// <summary>
125+
/// Updates <see cref="Token"/> with the next usable token in <see cref="commandText"/>.
126+
/// </summary>
127+
/// <remarks>
128+
/// <see cref="Whitespace"/> and <see cref="Comment"/> tokens are skipped.
129+
/// <see cref="index"/> is updated to the position of the next token.
130+
/// </remarks>
131+
/// <returns>True if another token was found, otherwise false.</returns>
124132
private bool MoveNext()
125133
{
126134
while (Tokenizer.NextToken(commandText, ref index) is Token token)
127135
{
128136
if (token is Whitespace)
129137
continue;
138+
if (token is Comment)
139+
continue;
130140

131141
#if DEBUG
132142
tokens.Add(token);

SqlServerSimulator/Parser/Tokenizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private static Token ParseForwardSlashOrComment(string command, ref int index)
149149
while (++index < command.Length)
150150
{
151151
if (command[index] == '*' && command.Length >= index + 2 && command[index + 1] == '/')
152-
return new Comment(command, start, index += 2);
152+
return new Comment(command, start, index++ - start);
153153
}
154154

155155
throw SimulatedSqlException.MissingEndCommentMark();

SqlServerSimulator/Simulation.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ internal IEnumerable<SimulatedStatementOutcome> CreateResultSetsForCommand(Simul
3636
{
3737
switch (context.Token)
3838
{
39-
case Comment:
40-
continue;
41-
4239
case Operator { Character: ';' }:
4340
continue;
4441

0 commit comments

Comments
 (0)