use pg_parse::Error;
#[test]
fn it_reports_cursorpos_for_a_token_error() {
let err = pg_parse::parse("CREATE TABL public.foo (id int)").unwrap_err();
match err {
Error::ParseError { message, cursorpos } => {
assert_eq!(message, "syntax error at or near \"TABL\"");
assert_eq!(cursorpos, 8);
}
other => panic!("expected ParseError, got {other:?}"),
}
}
#[test]
fn it_reports_cursorpos_at_end_of_input() {
let sql = "SELECT * FROM"; let err = pg_parse::parse(sql).unwrap_err();
match err {
Error::ParseError { message, cursorpos } => {
assert_eq!(message, "syntax error at end of input");
assert_eq!(cursorpos, sql.chars().count() as i32 + 1);
assert_eq!(cursorpos, 14);
}
other => panic!("expected ParseError, got {other:?}"),
}
}
#[test]
fn it_reports_cursorpos_as_character_offset() {
let sql = "SELECT 'éé' FRM x";
let err = pg_parse::parse(sql).unwrap_err();
match err {
Error::ParseError { cursorpos, .. } => {
assert_eq!(
cursorpos, 17,
"should be the character offset, not byte offset"
);
let char_index = (cursorpos - 1) as usize;
let byte_offset = sql
.char_indices()
.nth(char_index)
.map(|(byte, _)| byte)
.unwrap_or(sql.len());
assert_eq!(&sql[byte_offset..], "x");
}
other => panic!("expected ParseError, got {other:?}"),
}
}