Skip to content

Commit

Permalink
test: Add test for query which blocks on chumsky (#1982)
Browse files Browse the repository at this point in the history
* test: Add test for query which blocks on chumsky

Based on #1978.

It will block tests, probably until timeout

* Add a very minimal test
  • Loading branch information
max-sixty authored Feb 28, 2023
1 parent ea4c708 commit 800c7a7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
6 changes: 4 additions & 2 deletions prql-compiler/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ pub fn lexer() -> impl Parser<char, Vec<(Token, std::ops::Range<usize>)>, Error
literal,
keyword,
ident,
))
.recover_with(skip_then_retry_until([]).skip_start());
));
// TODO: Add this back when https://github.com/zesterer/chumsky/issues/301
// is fixed.
// .recover_with(skip_then_retry_until([]).skip_start());

let comment = just('#').then(none_of('\n').repeated());
let comments = comment
Expand Down
39 changes: 37 additions & 2 deletions prql-compiler/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ pub fn parse(source: &str) -> Result<Vec<Stmt>> {
}

fn convert_lexer_error(source: &str, e: Cheap<char>) -> Error {
let found = source[e.span()].to_string();
// TODO: is there a neater way of taking a span? We want to take it based on
// the chars, not the bytes, so can't just index into the str.
let found = source
.chars()
.skip(e.span().start)
.take(e.span().end() - e.span().start)
.collect();
let span = common::into_span(e.span());

Error::new(Reason::Unexpected { found }).with_span(span)
Expand Down Expand Up @@ -164,7 +170,7 @@ mod test {

use super::*;
use anyhow::anyhow;
use insta::assert_yaml_snapshot;
use insta::{assert_debug_snapshot, assert_yaml_snapshot};

fn parse_expr(source: &str) -> Result<Expr, Vec<anyhow::Error>> {
let tokens = Parser::parse(&lexer::lexer(), source).map_err(|errs| {
Expand Down Expand Up @@ -2206,4 +2212,33 @@ join s=salaries [==id]
Param: 2_any_text
"###);
}

#[test]
fn test_error_unicode_string() {
// Test various unicode strings successfully parse errors. We were
// getting loops in the lexer before.
parse("s’ ").unwrap_err();
parse("s’").unwrap_err();
parse(" s’").unwrap_err();
parse(" ’ s").unwrap_err();
parse("’s").unwrap_err();
parse("👍 s’").unwrap_err();

let source = "Mississippi has four S’s and four I’s.";
assert_debug_snapshot!(parse(source).unwrap_err(), @r###"
Errors(
[
Error {
span: Some(
span-chars-22-23,
),
reason: Unexpected {
found: "’",
},
help: None,
},
],
)
"###);
}
}
10 changes: 10 additions & 0 deletions prql-compiler/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2939,6 +2939,16 @@ fn test_errors() {
· ╰─── `take` expected int or range, but found 1.8
───╯
"###);

assert_display_snapshot!(compile("Mississippi has four S’s and four I’s.").unwrap_err(), @r###"
Error:
╭─[:1:23]
1 │ Mississippi has four S’s and four I’s.
· ┬
· ╰── unexpected ’
───╯
"###);
}

#[test]
Expand Down

0 comments on commit 800c7a7

Please sign in to comment.