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
extern crate proc_macro;
use lark_test::TestPath;
use std::fmt::Write;
use std::path::PathBuf;

use proc_macro::TokenStream;

#[proc_macro]
pub fn generate_tests(_item: TokenStream) -> TokenStream {
    let mut result = String::new();

    let base_path = PathBuf::from("tests/test_files");

    for TestPath {
        relative_test_path,
        test_path,
        is_dir,
    } in lark_test::search_files(&base_path)
    {
        // TODO -- make this `foo::bar` -- maybe move the `search_files` logic
        // into here?

        // "foo/bar_baz.lark" becomes `foo__bar_baz`
        let test_name = relative_test_path
            .with_extension("")
            .display()
            .to_string()
            .replace("/", "__")
            .replace("\\", "__");

        write!(
            result,
            "{}",
            unindent::unindent(&format!(
                r#"
                    #[test]
                    fn r#{test_name}() {{
                        lark_test::run_test_harness(
                            {relative_test_path:?},
                            {test_path:?},
                            {is_dir:?},
                            std::env::var("LARK_BLESS").is_ok(),
                        );
                    }}
                "#,
                test_name = test_name,
                relative_test_path = relative_test_path.to_str().unwrap(),
                test_path = test_path.to_str().unwrap(),
                is_dir = is_dir,
            )),
        )
        .unwrap();
    }

    result.parse().unwrap()
}