Skip to content

Commit

Permalink
Added additional tests for the parser, also test that semicolons insi…
Browse files Browse the repository at this point in the history
…de strings and comments are ignored.
  • Loading branch information
mpfeiffermway committed Apr 17, 2014
1 parent 208fae4 commit 8af84d0
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 2 deletions.
5 changes: 5 additions & 0 deletions tests/res/raw/block_comment_with_semicolon.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, /* block comment ; with semicolon */
Column1 INTEGER
)
4 changes: 2 additions & 2 deletions tests/res/raw/complex.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE Entity2
INSERT INTO Entity2
(
Id,
Column,
Column, /* --> ; <-- */
Column2
)
SELECT Id,
Expand All @@ -25,7 +25,7 @@ ALTER TABLE Entity2 RENAME TO Entity;
/* Add some --sample-- data */
INSERT INTO Entity2
(
Id,
Id, --;'/*;*/--
Col/*not sure if anyone would ever be insane enough to do this*/umn,
Column2--,
)
Expand Down
5 changes: 5 additions & 0 deletions tests/res/raw/line_comment_with_semicolon.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Entity1
(
Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL, -- line comment ; with semicolon
Column1 INTEGER
)
12 changes: 12 additions & 0 deletions tests/res/raw/string_with_semicolon.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INSERT INTO Entity
(
Id,
Column1,
Column2
)
VALUES
(
1,
'some ; text',
'some ; text'
);
12 changes: 12 additions & 0 deletions tests/res/raw/string_with_whitespace.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INSERT INTO Entity
(
Id,
Column1,
Column2
)
VALUES
(
1,
'some text',
'some text'
);
36 changes: 36 additions & 0 deletions tests/res/raw/whitespace.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
CREATE TABLE Entity1


(












Id INTEGER AUTOINCREMENT PRIMARY KEY NOT NULL,

















Column1 INTEGER
);
67 changes: 67 additions & 0 deletions tests/src/com/activeandroid/test/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ public void testTwoStatements() throws IOException {
assertEquals(sql2, commands.get(1));
}

/**
* Should reduce unnecessary whitespace.
* @throws IOException
*/
public void testWhitespace() throws IOException {

final InputStream stream = this.getStream(R.raw.whitespace);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql1, commands.get(0));
}

/**
* Should be able to parse a multi-line statement that has an embedded line comment.
* @throws IOException
Expand All @@ -62,6 +75,19 @@ public void testLineCommentWithString() throws IOException {
assertEquals(sql1, commands.get(0));
}

/**
* Should be able to handle a line comment that contains a semicolon.
* @throws IOException
*/
public void testLineCommentWithSemicolon() throws IOException {

final InputStream stream = this.getStream(R.raw.line_comment_with_semicolon);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql1, commands.get(0));
}

/**
* Should ignore a block comment end token inside a line comment.
* @throws IOException
Expand Down Expand Up @@ -101,6 +127,19 @@ public void testBlockCommentWithString() throws IOException {
assertEquals(sql1, commands.get(0));
}

/**
* Should be able to handle a block comment that contains a semicolon.
* @throws IOException
*/
public void testBlockCommentWithSemicolon() throws IOException {

final InputStream stream = this.getStream(R.raw.block_comment_with_semicolon);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql1, commands.get(0));
}

/**
* Should ignore a line comment token inside a block comment.
* @throws IOException
Expand Down Expand Up @@ -156,6 +195,34 @@ public void testStringWithBlockComment() throws IOException {
assertEquals(sql, commands.get(0));
}

/**
* Should ignore semicolons inside strings.
* @throws IOException
*/
public void testStringWithSemicolon() throws IOException {
final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, 'some ; text', 'some ; text' )";

final InputStream stream = this.getStream(R.raw.string_with_semicolon);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql, commands.get(0));
}

/**
* Should not clobber whitespace in strings.
* @throws IOException
*/
public void testStringWithWhitespace() throws IOException {
final String sql = "INSERT INTO Entity ( Id, Column1, Column2 ) VALUES ( 1, 'some\t\t\ttext', 'some text' )";

final InputStream stream = this.getStream(R.raw.string_with_whitespace);
List<String> commands = SqlParser.parse(stream);

assertEquals(1, commands.size());
assertEquals(sql, commands.get(0));
}

/**
* Should be able to handle a script that contains anything nasty I can thing of right now.
* @throws IOException
Expand Down

0 comments on commit 8af84d0

Please sign in to comment.