Skip to content

Commit 8af84d0

Browse files
committed
Added additional tests for the parser, also test that semicolons inside strings and comments are ignored.
1 parent 208fae4 commit 8af84d0

7 files changed

+139
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, /* block comment ; with semicolon */
4+
Column1 INTEGER
5+
)

tests/res/raw/complex.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ CREATE TABLE Entity2
1010
INSERT INTO Entity2
1111
(
1212
Id,
13-
Column,
13+
Column, /* --> ; <-- */
1414
Column2
1515
)
1616
SELECT Id,
@@ -25,7 +25,7 @@ ALTER TABLE Entity2 RENAME TO Entity;
2525
/* Add some --sample-- data */
2626
INSERT INTO Entity2
2727
(
28-
Id,
28+
Id, --;'/*;*/--
2929
Col/*not sure if anyone would ever be insane enough to do this*/umn,
3030
Column2--,
3131
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE Entity1
2+
(
3+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, -- line comment ; with semicolon
4+
Column1 INTEGER
5+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
INSERT INTO Entity
2+
(
3+
Id,
4+
Column1,
5+
Column2
6+
)
7+
VALUES
8+
(
9+
1,
10+
'some ; text',
11+
'some ; text'
12+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
INSERT INTO Entity
2+
(
3+
Id,
4+
Column1,
5+
Column2
6+
)
7+
VALUES
8+
(
9+
1,
10+
'some text',
11+
'some text'
12+
);

tests/res/raw/whitespace.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
CREATE TABLE Entity1
2+
3+
4+
(
5+
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
Column1 INTEGER
36+
);

tests/src/com/activeandroid/test/parser/ParserTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ public void testTwoStatements() throws IOException {
3636
assertEquals(sql2, commands.get(1));
3737
}
3838

39+
/**
40+
* Should reduce unnecessary whitespace.
41+
* @throws IOException
42+
*/
43+
public void testWhitespace() throws IOException {
44+
45+
final InputStream stream = this.getStream(R.raw.whitespace);
46+
List<String> commands = SqlParser.parse(stream);
47+
48+
assertEquals(1, commands.size());
49+
assertEquals(sql1, commands.get(0));
50+
}
51+
3952
/**
4053
* Should be able to parse a multi-line statement that has an embedded line comment.
4154
* @throws IOException
@@ -62,6 +75,19 @@ public void testLineCommentWithString() throws IOException {
6275
assertEquals(sql1, commands.get(0));
6376
}
6477

78+
/**
79+
* Should be able to handle a line comment that contains a semicolon.
80+
* @throws IOException
81+
*/
82+
public void testLineCommentWithSemicolon() throws IOException {
83+
84+
final InputStream stream = this.getStream(R.raw.line_comment_with_semicolon);
85+
List<String> commands = SqlParser.parse(stream);
86+
87+
assertEquals(1, commands.size());
88+
assertEquals(sql1, commands.get(0));
89+
}
90+
6591
/**
6692
* Should ignore a block comment end token inside a line comment.
6793
* @throws IOException
@@ -101,6 +127,19 @@ public void testBlockCommentWithString() throws IOException {
101127
assertEquals(sql1, commands.get(0));
102128
}
103129

130+
/**
131+
* Should be able to handle a block comment that contains a semicolon.
132+
* @throws IOException
133+
*/
134+
public void testBlockCommentWithSemicolon() throws IOException {
135+
136+
final InputStream stream = this.getStream(R.raw.block_comment_with_semicolon);
137+
List<String> commands = SqlParser.parse(stream);
138+
139+
assertEquals(1, commands.size());
140+
assertEquals(sql1, commands.get(0));
141+
}
142+
104143
/**
105144
* Should ignore a line comment token inside a block comment.
106145
* @throws IOException
@@ -156,6 +195,34 @@ public void testStringWithBlockComment() throws IOException {
156195
assertEquals(sql, commands.get(0));
157196
}
158197

198+
/**
199+
* Should ignore semicolons inside strings.
200+
* @throws IOException
201+
*/
202+
public void testStringWithSemicolon() throws IOException {
203+
final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, 'some ; text', 'some ; text' )";
204+
205+
final InputStream stream = this.getStream(R.raw.string_with_semicolon);
206+
List<String> commands = SqlParser.parse(stream);
207+
208+
assertEquals(1, commands.size());
209+
assertEquals(sql, commands.get(0));
210+
}
211+
212+
/**
213+
* Should not clobber whitespace in strings.
214+
* @throws IOException
215+
*/
216+
public void testStringWithWhitespace() throws IOException {
217+
final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, 'some\t\t\ttext', 'some text' )";
218+
219+
final InputStream stream = this.getStream(R.raw.string_with_whitespace);
220+
List<String> commands = SqlParser.parse(stream);
221+
222+
assertEquals(1, commands.size());
223+
assertEquals(sql, commands.get(0));
224+
}
225+
159226
/**
160227
* Should be able to handle a script that contains anything nasty I can thing of right now.
161228
* @throws IOException

0 commit comments

Comments
 (0)