|
| 1 | +#![cfg(test)] |
| 2 | +//! Property-based tests for the lexer/parser (issue #222, phase 1-2). |
| 3 | +//! |
| 4 | +//! Example-based tests can only cover inputs the author thought of. These |
| 5 | +//! generate thousands of inputs per run and assert invariants that must hold |
| 6 | +//! for *every* input, which is where malformed-SQL handling tends to break. |
| 7 | +
|
| 8 | +use proptest::prelude::*; |
| 9 | + |
| 10 | +use crate::engine::lexer::predule::Tokenizer; |
| 11 | +use crate::engine::parser::context::ParserContext; |
| 12 | +use crate::engine::parser::predule::Parser; |
| 13 | + |
| 14 | +/// Parse a statement the way the engine does: tokenize, then parse. |
| 15 | +fn try_parse(sql: &str) -> crate::errors::Result<Vec<crate::engine::ast::SQLStatement>> { |
| 16 | + let tokens = Tokenizer::string_to_tokens(sql.to_owned())?; |
| 17 | + Parser::new(tokens).parse(ParserContext::default()) |
| 18 | +} |
| 19 | + |
| 20 | +/// Arbitrary text, including control characters, quotes and unbalanced |
| 21 | +/// delimiters. |
| 22 | +fn arbitrary_sql_text() -> impl Strategy<Value = String> { |
| 23 | + proptest::string::string_regex(r#"[a-zA-Z0-9_ ,;'"()\-*=<>\.\r\n\t]{0,80}"#) |
| 24 | + .expect("valid regex") |
| 25 | +} |
| 26 | + |
| 27 | +/// Fragments drawn from real SQL keywords, so the generator spends more of its |
| 28 | +/// budget on inputs that reach deeper into the parser instead of being |
| 29 | +/// rejected by the tokenizer immediately. |
| 30 | +fn sql_like_fragment() -> impl Strategy<Value = String> { |
| 31 | + let keyword = prop::sample::select(vec![ |
| 32 | + "SELECT", "FROM", "WHERE", "INSERT", "INTO", "VALUES", "UPDATE", "SET", "DELETE", "CREATE", |
| 33 | + "TABLE", "DATABASE", "INDEX", "DROP", "ALTER", "AND", "OR", "NOT", "NULL", "PRIMARY", |
| 34 | + "KEY", "ORDER", "BY", "GROUP", "JOIN", "ON", "AS", "(", ")", ",", ";", "*", "=", "'x'", |
| 35 | + "1", "foo", |
| 36 | + ]); |
| 37 | + |
| 38 | + prop::collection::vec(keyword, 0..12).prop_map(|parts| parts.join(" ")) |
| 39 | +} |
| 40 | + |
| 41 | +proptest! { |
| 42 | + #![proptest_config(ProptestConfig::with_cases(1024))] |
| 43 | + |
| 44 | + /// The parser must never panic, however malformed the input. Any input it |
| 45 | + /// cannot handle has to come back as an `Err`, not an abort — a panic here |
| 46 | + /// would take down the connection task that is parsing the statement. |
| 47 | + #[test] |
| 48 | + fn parser_never_panics_on_arbitrary_text(sql in arbitrary_sql_text()) { |
| 49 | + let _ = try_parse(&sql); |
| 50 | + } |
| 51 | + |
| 52 | + /// Same invariant, but with inputs built from real SQL keywords so the |
| 53 | + /// generator reaches the statement parsers rather than failing at the |
| 54 | + /// tokenizer. |
| 55 | + #[test] |
| 56 | + fn parser_never_panics_on_sql_like_input(sql in sql_like_fragment()) { |
| 57 | + let _ = try_parse(&sql); |
| 58 | + } |
| 59 | + |
| 60 | + /// Parsing is a pure function of its input: the same text must always |
| 61 | + /// produce the same outcome. This guards against parser state leaking |
| 62 | + /// across runs through shared or cached state. |
| 63 | + #[test] |
| 64 | + fn parsing_is_deterministic(sql in sql_like_fragment()) { |
| 65 | + let first = try_parse(&sql); |
| 66 | + let second = try_parse(&sql); |
| 67 | + |
| 68 | + prop_assert_eq!(first.is_ok(), second.is_ok()); |
| 69 | + |
| 70 | + if let (Ok(first), Ok(second)) = (first, second) { |
| 71 | + prop_assert_eq!(format!("{:?}", first), format!("{:?}", second)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Leading and trailing whitespace must not change the parse result. |
| 76 | + #[test] |
| 77 | + fn surrounding_whitespace_is_insignificant(sql in sql_like_fragment()) { |
| 78 | + let bare = try_parse(&sql); |
| 79 | + let padded = try_parse(&format!(" \t{sql}\n ")); |
| 80 | + |
| 81 | + prop_assert_eq!(bare.is_ok(), padded.is_ok()); |
| 82 | + |
| 83 | + if let (Ok(bare), Ok(padded)) = (bare, padded) { |
| 84 | + prop_assert_eq!(format!("{:?}", bare), format!("{:?}", padded)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /// The tokenizer must terminate and must not invent trailing tokens for |
| 89 | + /// input that is only whitespace. |
| 90 | + #[test] |
| 91 | + fn whitespace_only_input_yields_no_statements( |
| 92 | + spaces in proptest::string::string_regex(r"[ \t\r\n]{0,40}").expect("valid regex") |
| 93 | + ) { |
| 94 | + let parsed = try_parse(&spaces); |
| 95 | + prop_assert!(parsed.is_ok(), "whitespace should parse, got {:?}", parsed.err()); |
| 96 | + prop_assert!(parsed.unwrap().is_empty()); |
| 97 | + } |
| 98 | +} |
0 commit comments