1
use std::collections::HashSet;
2
use std::error::Error;
3

            
4
use glob::glob;
5

            
6
/// Discovers test files with specific extensions and prints test cases for them
7
pub fn discover_tests() -> Result<(), Box<dyn Error>> {
8
    // Discover different types of test files
9
    discover_files_with_extension("mcrl2", "examples/mCRL2/**/*.mcrl2")?;
10
    discover_files_with_extension("mcf", "examples/mCRL2/**/*.mcf")?;
11
    discover_files_with_extension("dataspec", "examples/REC/**/*.dataspec")?;
12

            
13
    Ok(())
14
}
15

            
16
/// Discovers files matching a pattern and generates test cases for them
17
/// Returns the number of unique files found
18
fn discover_files_with_extension(ext_name: &str, pattern: &str) -> Result<(), Box<dyn Error>> {
19
    // Track seen filenames to avoid duplicates
20
    let mut seen_filenames = HashSet::new();
21

            
22
    // Glob returns a Result<impl Iterator<Item=Result<PathBuf>>>
23
    match glob(pattern) {
24
        Ok(paths) => {
25
            for path_result in paths {
26
                match path_result {
27
                    Ok(path) => {
28
                        // Get the relative path and filename
29
                        let path_str = path.to_string_lossy();
30
                        // Normalize path separators to forward slashes for cross-platform compatibility
31
                        let normalized_path = path_str.replace('\\', "/");
32
                        let filename = path.file_name().unwrap_or_default().to_string_lossy();
33

            
34
                        // Replace spaces with underscores and convert to lowercase for consistent test naming
35
                        let sanitized_filename = filename.replace(' ', "_").to_lowercase();
36

            
37
                        // Only generate test case if this filename hasn't been seen before
38
                        if !seen_filenames.contains(&sanitized_filename) {
39
                            // Generate the test case string with normalized path
40
                            println!(
41
                                "#[test_case(include_str!(\"../../../{normalized_path}\"), \"tests/snapshot/result_{sanitized_filename}\" ; \"{sanitized_filename}\")]"
42
                            );
43

            
44
                            // Add sanitized filename to set of seen filenames
45
                            seen_filenames.insert(sanitized_filename);
46
                        }
47
                    }
48
                    Err(e) => {
49
                        return Err(Box::new(e));
50
                    }
51
                }
52
            }
53
        }
54
        Err(e) => return Err(Box::new(e)),
55
    }
56

            
57
    // Additional debug assertion for specific file type
58
    debug_assert!(!seen_filenames.is_empty(), "No {ext_name} test files were discovered");
59

            
60
    Ok(())
61
}