1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::schema::{Assertion, Namespace, TestCase, TestDocument};
use codegen::{Function, Module, Scope};

/// Defines a test code generation object
pub struct Generator {
    pub test_document: TestDocument,
}

impl Generator {
    /// Generates a `Scope` from the `Generator`'s `test_document`
    pub fn generate_scope(&self) -> Scope {
        test_document_to_scope(&self.test_document)
    }
}

/// Converts a `TestDocument` into a `Scope`
fn test_document_to_scope(test_document: &TestDocument) -> Scope {
    let mut scope = Scope::new();
    for namespace in &test_document.namespaces {
        scope.push_module(namespace_to_module(namespace));
    }
    for test in &test_document.test_cases {
        scope.push_fn(test_case_to_function(test));
    }
    scope
}

/// Converts a `Namespace` into a `Module`
fn namespace_to_module(namespace: &Namespace) -> Module {
    let mut module = Module::new(&*namespace.name);
    for ns in &namespace.namespaces {
        module.push_module(namespace_to_module(ns));
    }
    for test in &namespace.test_cases {
        module.push_fn(test_case_to_function(test));
    }
    module
}

/// Converts a test case into a testing `Function`
fn test_case_to_function(test_case: &TestCase) -> Function {
    let mut test_fn: Function = Function::new(&test_case.test_name);
    test_fn.attr("test");
    test_fn.line(format!("let statement = r#\"{}\"#;", &test_case.statement));
    for assertion in &test_case.assertions {
        match assertion {
            Assertion::SyntaxSuccess => {
                test_fn.line("let res = partiql_parser::Parser::default().parse(statement);");
                test_fn.line(r#"assert!(res.is_ok(), "For `{}`, expected `Ok(_)`, but was `{:#?}`", statement, res);"#);
            }
            Assertion::SyntaxFail => {
                test_fn.line("let res = partiql_parser::Parser::default().parse(statement);");
                test_fn.line(r#"assert!(res.is_err(), "For `{}`, expected `Err(_)`, but was `{:#?}`", statement, res);"#);
            }
            Assertion::NotYetImplemented => {
                // for `NotYetImplemented` assertions, add the 'ignore' annotation to the test case
                test_fn.attr("ignore = \"not yet implemented\"");
            }
        }
    }
    test_fn
}

#[cfg(test)]
mod tests {
    // TODO: add tests checking the conversions between test structs and CodeGen functions
    //  https://github.com/partiql/partiql-lang-rust/issues/101
}