Skip to content

Commit c9b4b50

Browse files
committed
Single-line comment tokenization accuracy improved.
1 parent 77357ee commit c9b4b50

3 files changed

Lines changed: 36 additions & 66 deletions

File tree

SqlServerSimulator.Tests/SimpleCommandTests.cs

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,83 +27,45 @@ public void CommandNotInitialized()
2727
[TestMethod]
2828
public void EmptyCommand()
2929
{
30-
using var connection = new Simulation().CreateDbConnection();
31-
connection.Open();
32-
33-
using var command = connection.CreateCommand("");
34-
35-
var x = Throws<InvalidOperationException>(command.ExecuteReader);
30+
var x = Throws<InvalidOperationException>(new Simulation().CreateCommand("").ExecuteReader);
3631
AreEqual("ExecuteReader: CommandText property has not been initialized", x.Message);
3732
}
3833

3934
[TestMethod]
4035
public void SpaceCommand()
4136
{
42-
using var connection = new Simulation().CreateDbConnection();
43-
connection.Open();
44-
45-
using var command = connection.CreateCommand(" ");
46-
using var reader = command.ExecuteReader();
47-
48-
IsFalse(reader.Read());
37+
IsFalse(new Simulation().ExecuteReader(" ").Read());
4938
}
5039

5140
[TestMethod]
5241
public void IncompleteSingleLineCommentCommand()
5342
{
54-
using var connection = new Simulation().CreateDbConnection();
55-
connection.Open();
56-
57-
using var command = connection.CreateCommand("-");
58-
_ = Throws<DbException>(command.ExecuteReader);
59-
}
60-
61-
[TestMethod]
62-
public void SingleLineEmptyCommentCommand()
63-
{
64-
using var connection = new Simulation().CreateDbConnection();
65-
connection.Open();
66-
67-
using var command = connection.CreateCommand("--");
68-
using var reader = command.ExecuteReader();
69-
70-
IsFalse(reader.Read());
43+
_ = Throws<DbException>(() => new Simulation().ExecuteReader("-"));
7144
}
7245

7346
[TestMethod]
74-
public void SingleLineCommentCommand()
47+
[DataRow("--")]
48+
[DataRow("--Test")]
49+
[DataRow("--Test \n")]
50+
[DataRow("--Test \r")]
51+
[DataRow("--Test \r\n")]
52+
[DataRow("--Test\n")]
53+
[DataRow("--Test\r")]
54+
[DataRow("--Test\r\n")]
55+
public void LineComment(string comment)
7556
{
76-
using var connection = new Simulation().CreateDbConnection();
77-
connection.Open();
78-
79-
using var command = connection.CreateCommand("-- Test");
80-
using var reader = command.ExecuteReader();
81-
82-
IsFalse(reader.Read());
83-
}
84-
85-
[TestMethod]
86-
public void SingleLineCommentWithNewlineCommand()
87-
{
88-
using var connection = new Simulation().CreateDbConnection();
89-
connection.Open();
90-
91-
using var command = connection.CreateCommand("-- Test\n");
92-
using var reader = command.ExecuteReader();
93-
94-
IsFalse(reader.Read());
95-
}
96-
97-
[TestMethod]
98-
public void SingleLineCommentWithCarriageReturnCommand()
99-
{
100-
using var connection = new Simulation().CreateDbConnection();
101-
connection.Open();
102-
103-
using var command = connection.CreateCommand("-- Test\r");
104-
using var reader = command.ExecuteReader();
105-
106-
IsFalse(reader.Read());
57+
IsNull(new Simulation().ExecuteScalar($"{comment}"));
58+
AreEqual(1, new Simulation().ExecuteScalar<int>($"select 1 {comment}"));
59+
AreEqual(2, new Simulation().ExecuteScalar<int>($"select 2{comment}"));
60+
AreEqual(3, new Simulation().ExecuteScalar<int>($"select 3\n{comment}"));
61+
AreEqual(4, new Simulation().ExecuteScalar<int>($"select 4 \n{comment}"));
62+
AreEqual(5, new Simulation().ExecuteScalar<int>($"select 5\r{comment}"));
63+
AreEqual(6, new Simulation().ExecuteScalar<int>($"select 6 \r{comment}"));
64+
AreEqual(7, new Simulation().ExecuteScalar<int>($"select 7\r\n{comment}"));
65+
AreEqual(8, new Simulation().ExecuteScalar<int>($"select 8 \r\n{comment}"));
66+
AreEqual(9, new Simulation().ExecuteScalar<int>($"{comment}\nselect 9"));
67+
AreEqual(10, new Simulation().ExecuteScalar<int>($"{comment}\rselect 10"));
68+
AreEqual(11, new Simulation().ExecuteScalar<int>($"{comment}\r\nselect 11"));
10769
}
10870

10971
[TestMethod]

SqlServerSimulator/Parser/Token.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ abstract class Token
1414

1515
private protected Token(string command, int index, int length)
1616
{
17+
System.Diagnostics.Debug.Assert(index >= 0);
18+
System.Diagnostics.Debug.Assert(length > 0);
1719
System.Diagnostics.Debug.Assert(index + length <= command.Length);
20+
1821
this.command = command;
1922
this.index = index;
2023
this.length = length;

SqlServerSimulator/Parser/Tokens/Comment.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ sealed class Comment : Token
44
{
55
private Comment(string command, int index, int length) : base(command, index, length)
66
{
7+
System.Diagnostics.Debug.Assert(length >= 2);
8+
System.Diagnostics.Debug.Assert(command[index] is '/' or '-' && command[index + 1] is '*' or '-');
9+
System.Diagnostics.Debug.Assert(command[index] is not '/' || length >= 4);
710
}
811

912
/// <summary>
1013
/// Parses a single-line comment (`-- ...`) where the initial `--` has already been consumed.
1114
/// </summary>
12-
/// <param name="start">The position of the opening `/`.</param>
13-
/// <param name="index">Initially after the opening `*`, updated to just after the closing `/`.</param>
15+
/// <param name="start">The position of the `--`.</param>
16+
/// <param name="index">Initially after the opening `--`, updated to just after the end of the line.</param>
1417
/// <param name="command">The raw command to parse.</param>
1518
/// <returns>A <see cref="Comment"/>.</returns>
1619
public static Comment ParseSingleLine(int start, ref int index, string command)
@@ -21,11 +24,13 @@ public static Comment ParseSingleLine(int start, ref int index, string command)
2124
{
2225
case '\r':
2326
case '\n':
24-
return new Comment(command, start, --index);
27+
return new Comment(command, start, index-- - start);
2528
}
2629
}
2730

28-
return new Comment(command, start, --index);
31+
index--;
32+
33+
return new Comment(command, start, 2);
2934
}
3035

3136
/// <summary>

0 commit comments

Comments
 (0)