Skip to content

Commit fc33e0f

Browse files
committed
Add some tests
1 parent de2b69b commit fc33e0f

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/parser.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6751,4 +6751,31 @@ mod tests {
67516751
r#"UPDATE test SET name = $1, value = $2, where = $3, create = $4, is_default = $5, classification = $6, sort = $7 WHERE id = $8"#
67526752
);
67536753
}
6754+
6755+
#[test]
6756+
fn test_tokenizer_error_loc() {
6757+
let sql = "foo '";
6758+
let ast = Parser::parse_sql(&GenericDialect, sql);
6759+
assert_eq!(
6760+
ast,
6761+
Err(ParserError::TokenizerError(
6762+
"Unterminated string literal at Line: 1, Column 5".to_string()
6763+
))
6764+
);
6765+
}
6766+
6767+
#[test]
6768+
fn test_parser_error_loc() {
6769+
// TODO: Once we thread token locations through the parser, we should update this
6770+
// test to assert the locations of the referenced token
6771+
let sql = "SELECT this is a syntax error";
6772+
let ast = Parser::parse_sql(&GenericDialect, sql);
6773+
assert_eq!(
6774+
ast,
6775+
Err(ParserError::ParserError(
6776+
"Expected [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: a"
6777+
.to_string()
6778+
))
6779+
);
6780+
}
67546781
}

src/tokenizer.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,7 +1615,25 @@ mod tests {
16151615
compare(expected, tokens);
16161616
}
16171617

1618-
fn compare(expected: Vec<Token>, actual: Vec<Token>) {
1618+
#[test]
1619+
fn tokenize_with_location() {
1620+
let sql = "SELECT a,\n b";
1621+
let dialect = GenericDialect {};
1622+
let mut tokenizer = Tokenizer::new(&dialect, sql);
1623+
let tokens = tokenizer.tokenize_with_location().unwrap();
1624+
let expected = vec![
1625+
TokenWithLocation::new(Token::make_keyword("SELECT"), 1, 1),
1626+
TokenWithLocation::new(Token::Whitespace(Whitespace::Space), 1, 7),
1627+
TokenWithLocation::new(Token::make_word("a", None), 1, 8),
1628+
TokenWithLocation::new(Token::Comma, 1, 9),
1629+
TokenWithLocation::new(Token::Whitespace(Whitespace::Newline), 1, 10),
1630+
TokenWithLocation::new(Token::Whitespace(Whitespace::Space), 2, 1),
1631+
TokenWithLocation::new(Token::make_word("b", None), 2, 2),
1632+
];
1633+
compare(expected, tokens);
1634+
}
1635+
1636+
fn compare<T: PartialEq + std::fmt::Debug>(expected: Vec<T>, actual: Vec<T>) {
16191637
//println!("------------------------------");
16201638
//println!("tokens = {:?}", actual);
16211639
//println!("expected = {:?}", expected);

0 commit comments

Comments
 (0)