#![allow(clippy::unwrap_used, clippy::expect_used)]
use crate::core::constants::SUPPORTED_EXTENSIONS;
use crate::project::{StdLibLoader, file_loader};
use crate::semantic::Workspace;
use crate::syntax::SyntaxFile;
use once_cell::sync::Lazy;
use std::path::PathBuf;
use std::sync::Arc;
static SHARED_STDLIB_WORKSPACE: Lazy<Arc<SharedStdlibData>> = Lazy::new(|| {
let loader = StdLibLoader::new();
let mut workspace = Workspace::<SyntaxFile>::new();
loader.load(&mut workspace).expect("Failed to load stdlib");
let paths =
file_loader::collect_file_paths(&loader.stdlib_path).expect("Failed to collect file paths");
Arc::new(SharedStdlibData {
file_count: workspace.file_paths().count(),
has_stdlib: workspace.has_stdlib(),
collected_paths: paths,
})
});
struct SharedStdlibData {
file_count: usize,
has_stdlib: bool,
collected_paths: Vec<PathBuf>,
}
#[test]
fn test_stdlib_loader_creation() {
let loader = StdLibLoader::new();
assert_eq!(loader.stdlib_path, PathBuf::from("sysml.library"));
let custom_loader = StdLibLoader::with_path(PathBuf::from("/custom/path"));
assert_eq!(custom_loader.stdlib_path, PathBuf::from("/custom/path"));
}
#[test]
fn test_load_missing_directory() {
let loader = StdLibLoader::with_path(PathBuf::from("/nonexistent/path"));
let mut workspace = Workspace::<SyntaxFile>::new();
let result = loader.load(&mut workspace);
assert!(
result.is_ok(),
"Loading missing directory should succeed gracefully"
);
assert!(
!workspace.has_stdlib(),
"Stdlib should not be marked as loaded"
);
}
#[test]
fn test_load_actual_stdlib() {
let data = &*SHARED_STDLIB_WORKSPACE;
assert!(data.has_stdlib, "Stdlib should be marked as loaded");
}
#[test]
fn test_collect_file_paths() {
let data = &*SHARED_STDLIB_WORKSPACE;
let sysml_files: Vec<_> = data
.collected_paths
.iter()
.filter(|p| p.extension().and_then(|e| e.to_str()) == Some("sysml"))
.collect();
assert!(
!sysml_files.is_empty(),
"Should find at least one .sysml file in stdlib"
);
assert_eq!(
sysml_files.len(),
58,
"Expected exactly 58 .sysml files in stdlib, found {}",
sysml_files.len()
);
}
#[test]
fn test_supported_extensions_only() {
let data = &*SHARED_STDLIB_WORKSPACE;
let unsupported: Vec<_> = data
.collected_paths
.iter()
.filter(|path| {
!path
.extension()
.and_then(std::ffi::OsStr::to_str)
.is_some_and(|e| SUPPORTED_EXTENSIONS.contains(&e))
})
.collect();
assert!(
unsupported.is_empty(),
"Found {} paths with unsupported extensions: {:?}",
unsupported.len(),
unsupported
);
}
#[test]
fn test_parallel_loading() {
let data1 = &*SHARED_STDLIB_WORKSPACE;
let data2 = &*SHARED_STDLIB_WORKSPACE;
assert_eq!(data1.file_count, data2.file_count);
assert!(data1.has_stdlib);
assert!(data2.has_stdlib);
}
#[test]
fn test_files_added_to_workspace() {
let data = &*SHARED_STDLIB_WORKSPACE;
assert!(
data.file_count == 94,
"Expected 94 files in workspace after loading stdlib, found {}",
data.file_count
);
assert!(data.has_stdlib);
}
#[test]
fn test_kerml_files_handled() {
let data = &*SHARED_STDLIB_WORKSPACE;
let kerml_count = data
.collected_paths
.iter()
.filter(|p| p.extension().and_then(std::ffi::OsStr::to_str) == Some("kerml"))
.count();
assert!(
kerml_count == 36,
"Expected 36 .kerml files, found {kerml_count}"
);
}
#[test]
fn test_lazy_loading_behavior() {
let data = &*SHARED_STDLIB_WORKSPACE;
let mut loader = StdLibLoader::new();
let mut workspace = Workspace::<SyntaxFile>::new();
assert!(!workspace.has_stdlib(), "Stdlib should not be loaded yet");
assert!(!loader.is_loaded(), "Loader should report not loaded");
assert_eq!(
workspace.file_count(),
0,
"Should have no files before lazy load"
);
loader.ensure_loaded(&mut workspace).unwrap();
assert!(
workspace.has_stdlib(),
"Should be loaded after ensure_loaded"
);
assert!(loader.is_loaded(), "Loader should report loaded");
assert_eq!(
workspace.file_count(),
data.file_count,
"Should have same file count as shared fixture"
);
let count_before = workspace.file_count();
loader.ensure_loaded(&mut workspace).unwrap();
assert_eq!(
workspace.file_count(),
count_before,
"Should not reload on second ensure_loaded call"
);
}
#[test]
fn test_eager_vs_lazy_equivalence() {
let data = &*SHARED_STDLIB_WORKSPACE;
assert_eq!(data.file_count, 94, "Shared fixture should have 94 files");
assert!(data.has_stdlib, "Shared fixture should have stdlib loaded");
}
#[test]
fn test_lazy_avoids_reloading() {
let mut loader = StdLibLoader::new();
let mut workspace = Workspace::<SyntaxFile>::new();
workspace.mark_stdlib_loaded();
loader.ensure_loaded(&mut workspace).unwrap();
assert!(workspace.has_stdlib());
}