more fixes, not there yet tho

This commit is contained in:
filipriec
2025-07-05 10:00:04 +02:00
parent a757acf51c
commit 4c57b562e6
11 changed files with 1407 additions and 270 deletions

View File

@@ -184,96 +184,3 @@ pub mod prelude {
pub use crate::functions::*;
pub use crate::utils::{TypeConverter, ScriptAnalyzer, DecimalPrecision};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_transformation() {
let steel_decimal = SteelDecimal::new();
let script = "(+ 1.5 2.3)";
let transformed = steel_decimal.transform(script);
assert_eq!(transformed, "(decimal-add \"1.5\" \"2.3\")");
}
#[test]
fn test_with_variables() {
let mut variables = HashMap::new();
variables.insert("x".to_string(), "10.5".to_string());
variables.insert("y".to_string(), "20.3".to_string());
let steel_decimal = SteelDecimal::with_variables(variables);
let script = "(+ $x $y)";
let transformed = steel_decimal.transform(script);
assert_eq!(transformed, "(decimal-add (get-var \"x\") (get-var \"y\"))");
}
#[test]
fn test_function_registration() {
let steel_decimal = SteelDecimal::new();
let mut vm = Engine::new();
steel_decimal.register_functions(&mut vm);
let script = "(decimal-add \"1.5\" \"2.3\")";
let result = vm.compile_and_run_raw_program(script.to_string());
assert!(result.is_ok());
}
#[test]
fn test_parse_and_execute() {
let steel_decimal = SteelDecimal::new();
let script = "(+ 1.5 2.3)";
let result = steel_decimal.parse_and_execute(script);
assert!(result.is_ok());
}
#[test]
fn test_script_validation() {
let steel_decimal = SteelDecimal::new();
// Valid script
assert!(steel_decimal.validate_script("(+ 1.5 2.3)").is_ok());
// Invalid script - unbalanced parentheses
assert!(steel_decimal.validate_script("(+ 1.5 2.3").is_err());
}
#[test]
fn test_variable_validation() {
let steel_decimal = SteelDecimal::new();
// Script with undefined variable
assert!(steel_decimal.validate_script("(+ $x 2.3)").is_err());
// Script with defined variable
let mut variables = HashMap::new();
variables.insert("x".to_string(), "10.5".to_string());
let steel_decimal = SteelDecimal::with_variables(variables);
assert!(steel_decimal.validate_script("(+ $x 2.3)").is_ok());
}
#[test]
fn test_complex_expressions() {
let steel_decimal = SteelDecimal::new();
let script = "(+ (* 2.5 3.0) (/ 15.0 3.0))";
let transformed = steel_decimal.transform(script);
let expected = "(decimal-add (decimal-mul \"2.5\" \"3.0\") (decimal-div \"15.0\" \"3.0\"))";
assert_eq!(transformed, expected);
}
#[test]
fn test_dependency_extraction() {
let steel_decimal = SteelDecimal::new();
let script = "(+ $x $y $z)";
let dependencies = steel_decimal.extract_dependencies(script);
assert_eq!(dependencies.len(), 3);
assert!(dependencies.contains("x"));
assert!(dependencies.contains("y"));
assert!(dependencies.contains("z"));
}
}

View File

@@ -114,63 +114,3 @@ impl Default for ScriptParser {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_basic_math_transformation() {
let parser = ScriptParser::new();
let input = "(+ 1.5 2.3)";
let expected = "(decimal-add \"1.5\" \"2.3\")";
let result = parser.transform(input);
assert_eq!(result, expected);
}
#[test]
fn test_complex_expression() {
let parser = ScriptParser::new();
let input = "(+ (* 2 3) (/ 10 2))";
let expected = "(decimal-add (decimal-mul \"2\" \"3\") (decimal-div \"10\" \"2\"))";
let result = parser.transform(input);
assert_eq!(result, expected);
}
#[test]
fn test_variable_replacement() {
let parser = ScriptParser::new();
let input = "(+ $x $y)";
let expected = "(decimal-add (get-var \"x\") (get-var \"y\"))";
let result = parser.transform(input);
assert_eq!(result, expected);
}
#[test]
fn test_negative_numbers() {
let parser = ScriptParser::new();
let input = "(+ -1.5 2.3)";
let expected = "(decimal-add \"-1.5\" \"2.3\")";
let result = parser.transform(input);
assert_eq!(result, expected);
}
#[test]
fn test_scientific_notation() {
let parser = ScriptParser::new();
let input = "(+ 1.5e2 2.3E-1)";
let expected = "(decimal-add \"1.5e2\" \"2.3E-1\")";
let result = parser.transform(input);
assert_eq!(result, expected);
}
}

View File

@@ -229,53 +229,3 @@ impl Default for FunctionRegistryBuilder {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_function_registration() {
let mut vm = Engine::new();
FunctionRegistry::register_all(&mut vm);
// Test that functions are registered by running a simple script
let script = r#"(decimal-add "1.5" "2.3")"#;
let result = vm.compile_and_run_raw_program(script.to_string());
assert!(result.is_ok());
}
#[test]
fn test_selective_registration() {
let mut vm = Engine::new();
FunctionRegistryBuilder::new()
.basic_arithmetic(true)
.advanced_math(false)
.trigonometric(false)
.register(&mut vm);
// Test that basic functions work
let script = r#"(decimal-add "1.5" "2.3")"#;
let result = vm.compile_and_run_raw_program(script.to_string());
assert!(result.is_ok());
}
#[test]
fn test_variable_registration() {
let mut vm = Engine::new();
let mut variables = HashMap::new();
variables.insert("x".to_string(), "10.5".to_string());
variables.insert("y".to_string(), "20.3".to_string());
FunctionRegistryBuilder::new()
.with_variables(variables)
.register(&mut vm);
// Test variable access
let script = r#"(get-var "x")"#;
let result = vm.compile_and_run_raw_program(script.to_string());
assert!(result.is_ok());
}
}

View File

@@ -158,70 +158,3 @@ impl DecimalPrecision {
Ok(decimal.normalize().to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_steel_val_to_decimal() {
let string_val = SteelVal::StringV("123.456".into());
let result = TypeConverter::steel_val_to_decimal(&string_val);
assert!(result.is_ok());
assert_eq!(result.unwrap().to_string(), "123.456");
let int_val = SteelVal::IntV(42);
let result = TypeConverter::steel_val_to_decimal(&int_val);
assert!(result.is_ok());
assert_eq!(result.unwrap().to_string(), "42");
}
#[test]
fn test_decimal_to_steel_val() {
let decimal = Decimal::from_str("123.456").unwrap();
let result = TypeConverter::decimal_to_steel_val(decimal);
if let SteelVal::StringV(s) = result {
assert_eq!(s.to_string(), "123.456");
} else {
panic!("Expected StringV");
}
}
#[test]
fn test_validate_decimal_string() {
assert!(TypeConverter::validate_decimal_string("123.456").is_ok());
assert!(TypeConverter::validate_decimal_string("invalid").is_err());
}
#[test]
fn test_script_analyzer() {
let script = r#"(decimal-add "1.5" "2.3")"#;
assert!(ScriptAnalyzer::contains_decimal_functions(script));
assert_eq!(ScriptAnalyzer::count_function_calls(script, "decimal-add"), 1);
let literals = ScriptAnalyzer::extract_string_literals(script);
assert_eq!(literals, vec!["1.5", "2.3"]);
}
#[test]
fn test_decimal_precision() {
let result = DecimalPrecision::set_precision("123.456789", 2);
assert!(result.is_ok());
assert_eq!(result.unwrap(), "123.46");
let places = DecimalPrecision::get_decimal_places("123.456");
assert!(places.is_ok());
assert_eq!(places.unwrap(), 3);
}
#[test]
fn test_type_conversions() {
assert_eq!(TypeConverter::i64_to_decimal_string(42), "42");
assert_eq!(TypeConverter::u64_to_decimal_string(42), "42");
let f64_result = TypeConverter::f64_to_decimal_string(123.456);
assert!(f64_result.is_ok());
}
}