93 lines
3.2 KiB
Rust
93 lines
3.2 KiB
Rust
use crate::tera_integration::RhaiTeraIntegration;
|
|
use std::collections::HashMap;
|
|
use rhai::Dynamic;
|
|
|
|
/// Test the dynamic loading of Rhai scripts and functions
|
|
pub fn test_dynamic_loading() -> Result<(), String> {
|
|
println!("\n=== TESTING DYNAMIC FUNCTION LOADING ===\n");
|
|
|
|
// Create a new RhaiTeraIntegration
|
|
let mut integration = RhaiTeraIntegration::new();
|
|
|
|
println!("Loading Rhai scripts...");
|
|
|
|
// Load the original scripts
|
|
integration.load_script("string_utils", "src/tera_integration/scripts/string_utils.rhai")?;
|
|
integration.load_script("math_utils", "src/tera_integration/scripts/math_utils.rhai")?;
|
|
|
|
// Load the new test script (which wasn't in the original hardcoded lists)
|
|
integration.load_script("test_utils", "src/tera_integration/scripts/test_utils.rhai")?;
|
|
|
|
// Get function names
|
|
let function_names = integration.get_function_names();
|
|
|
|
// Print all available functions
|
|
println!("\nAll available functions:");
|
|
for name in &function_names {
|
|
println!(" - {}", name);
|
|
}
|
|
|
|
// Group functions by their script prefix to display nicely
|
|
let scripts = group_functions_by_script(&function_names);
|
|
|
|
// Display functions by script
|
|
display_functions_by_script(&scripts);
|
|
|
|
// Test some functions from each script
|
|
println!("\nDynamic function testing:");
|
|
|
|
// Test original string utils functions
|
|
test_function(&integration, "string_utils:capitalize", vec![Dynamic::from("hello world")])?;
|
|
|
|
// Test original math utils functions
|
|
test_function(&integration, "math_utils:format_number", vec![Dynamic::from(1234567)])?;
|
|
|
|
// Test new functions from test_utils
|
|
test_function(&integration, "test_utils:reverse_string", vec![Dynamic::from("hello world")])?;
|
|
|
|
test_function(&integration, "test_utils:count_words",
|
|
vec![Dynamic::from("this is a test sentence")])?;
|
|
|
|
test_function(&integration, "test_utils:factorial",
|
|
vec![Dynamic::from(5)])?;
|
|
|
|
println!("\n=== DYNAMIC FUNCTION LOADING TEST COMPLETE ===\n");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
// Helper function to group functions by script
|
|
fn group_functions_by_script(function_names: &[String]) -> HashMap<String, Vec<String>> {
|
|
let mut scripts = HashMap::new();
|
|
|
|
for name in function_names {
|
|
let parts: Vec<&str> = name.split(':').collect();
|
|
if parts.len() == 2 {
|
|
let script = parts[0].to_string();
|
|
let func = parts[1].to_string();
|
|
|
|
scripts.entry(script).or_insert_with(Vec::new).push(func);
|
|
}
|
|
}
|
|
|
|
scripts
|
|
}
|
|
|
|
// Helper function to display functions grouped by script
|
|
fn display_functions_by_script(scripts: &HashMap<String, Vec<String>>) {
|
|
println!("\nFunctions by script:");
|
|
|
|
for (script, funcs) in scripts {
|
|
println!(" {}:", script);
|
|
for func in funcs {
|
|
println!(" - {}", func);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Helper function to test a function and display its result
|
|
fn test_function(integration: &RhaiTeraIntegration, name: &str, args: Vec<Dynamic>) -> Result<(), String> {
|
|
let result = integration.call_function(name, args)?;
|
|
println!(" {}() => {}", name, result);
|
|
Ok(())
|
|
} |