Skip to content

Commit af6c400

Browse files
committed
Add some tests
1 parent 414f261 commit af6c400

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/parser.rs

+27
Original file line numberDiff line numberDiff line change
@@ -6592,4 +6592,31 @@ mod tests {
65926592
r#"UPDATE test SET name = $1, value = $2, where = $3, create = $4, is_default = $5, classification = $6, sort = $7 WHERE id = $8"#
65936593
);
65946594
}
6595+
6596+
#[test]
6597+
fn test_tokenizer_error_loc() {
6598+
let sql = "foo '";
6599+
let ast = Parser::parse_sql(&GenericDialect, sql);
6600+
assert_eq!(
6601+
ast,
6602+
Err(ParserError::TokenizerError(
6603+
"Unterminated string literal at Line: 1, Column 5".to_string()
6604+
))
6605+
);
6606+
}
6607+
6608+
#[test]
6609+
fn test_parser_error_loc() {
6610+
// TODO: Once we thread token locations through the parser, we should update this
6611+
// test to assert the locations of the referenced token
6612+
let sql = "SELECT this is a syntax error";
6613+
let ast = Parser::parse_sql(&GenericDialect, sql);
6614+
assert_eq!(
6615+
ast,
6616+
Err(ParserError::ParserError(
6617+
"Expected [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: a"
6618+
.to_string()
6619+
))
6620+
);
6621+
}
65956622
}

src/tokenizer.rs

+19-1
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)