Skip to main content

cufflink_sql_parse/
golden.rs

1//! The golden corpus: statements accepted and refused, and the answer this
2//! release gives each, committed so that every implementation of the parse
3//! (the parser process, the platform, the test host) is checked against
4//! fixed bytes rather than against itself.
5//!
6//! An answer is a tree's canonical text, or `!` and the refusal's kind
7//! (`!syntax`, `!too_large`, `!unavailable`).
8
9use cufflink_types::sql_parse::ParseError;
10
11/// The statements past the probe corpus, one a line.
12pub const STATEMENTS: &str = include_str!("../golden/statements.sql");
13
14/// The answer to each statement of [`cases`], one a line, in order.
15pub const ANSWERS: &str = include_str!("../golden/answers.txt");
16
17/// The probe corpus's statements, then [`STATEMENTS`].
18pub fn statements() -> impl Iterator<Item = &'static str> {
19    crate::PROBE
20        .lines()
21        .chain(STATEMENTS.lines())
22        .filter(|line| !line.trim().is_empty())
23}
24
25/// Each statement with its committed answer.
26pub fn cases() -> impl Iterator<Item = (&'static str, &'static str)> {
27    statements().zip(ANSWERS.lines())
28}
29
30/// A parse's answer as the corpus writes it.
31pub fn answer(parsed: &Result<String, ParseError>) -> String {
32    match parsed {
33        Ok(text) => text.clone(),
34        Err(error) => format!("!{}", kind(error)),
35    }
36}
37
38/// A refusal's kind, as the wire names it.
39pub fn kind(error: &ParseError) -> &'static str {
40    match error {
41        ParseError::Syntax(_) => "syntax",
42        ParseError::TooLarge(_) => "too_large",
43        ParseError::Unavailable(_) => "unavailable",
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn every_statement_has_one_answer() {
53        assert_eq!(statements().count(), ANSWERS.lines().count());
54        assert!(cases()
55            .all(|(_, answer)| answer.starts_with('{')
56                || ["!syntax", "!too_large"].contains(&answer)));
57        assert!(cases().any(|(_, answer)| answer == "!syntax"));
58        assert!(cases().any(|(_, answer)| answer == "!too_large"));
59    }
60}