|
| 1 | +//! Script execution engines for validation scripts |
| 2 | +//! |
| 3 | +//! This module provides support for executing validation scripts in multiple languages: |
| 4 | +//! - Lua (via mlua) |
| 5 | +//! - JavaScript (via quickjs) |
| 6 | +//! - Python (via pyo3) |
| 7 | +//! - Regular expressions (via regex) |
| 8 | +//! - UUID generation (via uuid) |
| 9 | +
|
| 10 | +pub mod js_engine; |
| 11 | +pub mod lua_engine; |
| 12 | +pub mod python_engine; |
| 13 | +pub mod utilities; |
| 14 | + |
| 15 | +#[cfg(test)] |
| 16 | +mod dependency_tests { |
| 17 | + use std::time::Instant; |
| 18 | + |
| 19 | + #[tokio::test] |
| 20 | + async fn test_mlua_basic_execution() { |
| 21 | + let lua = mlua::Lua::new(); |
| 22 | + let result: String = lua.load("return 'Hello from Lua'").eval().unwrap(); |
| 23 | + assert_eq!(result, "Hello from Lua"); |
| 24 | + } |
| 25 | + |
| 26 | + #[tokio::test] |
| 27 | + async fn test_quickjs_basic_execution() { |
| 28 | + let runtime = rquickjs::Runtime::new().unwrap(); |
| 29 | + let context = rquickjs::Context::full(&runtime).unwrap(); |
| 30 | + |
| 31 | + context.with(|ctx| { |
| 32 | + let result: String = ctx.eval("'Hello from JavaScript'").unwrap(); |
| 33 | + assert_eq!(result, "Hello from JavaScript"); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + #[test] |
| 38 | + fn test_pyo3_basic_execution() { |
| 39 | + pyo3::Python::with_gil(|py| { |
| 40 | + let result = py.eval_bound("'Hello from Python'", None, None).unwrap(); |
| 41 | + assert_eq!(result.to_string(), "Hello from Python"); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + #[test] |
| 46 | + fn test_regex_pattern_matching() { |
| 47 | + let re = regex::Regex::new(r"hello").unwrap(); |
| 48 | + assert!(re.is_match("hello world")); |
| 49 | + assert!(!re.is_match("goodbye world")); |
| 50 | + } |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn test_uuid_generation() { |
| 54 | + let id = uuid::Uuid::new_v4(); |
| 55 | + assert_eq!(id.get_version(), Some(uuid::Version::Random)); |
| 56 | + |
| 57 | + // Test serialization |
| 58 | + let serialized = serde_json::to_string(&id).unwrap(); |
| 59 | + let deserialized: uuid::Uuid = serde_json::from_str(&serialized).unwrap(); |
| 60 | + assert_eq!(id, deserialized); |
| 61 | + } |
| 62 | + |
| 63 | + // Performance benchmarks |
| 64 | + #[tokio::test] |
| 65 | + async fn test_mlua_performance() { |
| 66 | + let lua = mlua::Lua::new(); |
| 67 | + let start = Instant::now(); |
| 68 | + let _result: String = lua.load("return 'Performance test'").eval().unwrap(); |
| 69 | + let duration = start.elapsed(); |
| 70 | + |
| 71 | + // Should be under 1ms for simple scripts |
| 72 | + assert!( |
| 73 | + duration.as_millis() < 10, |
| 74 | + "Lua execution took {}ms, expected <10ms", |
| 75 | + duration.as_millis() |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + #[tokio::test] |
| 80 | + async fn test_quickjs_performance() { |
| 81 | + let runtime = rquickjs::Runtime::new().unwrap(); |
| 82 | + let context = rquickjs::Context::full(&runtime).unwrap(); |
| 83 | + |
| 84 | + let start = Instant::now(); |
| 85 | + context.with(|ctx| { |
| 86 | + let _result: String = ctx.eval("'Performance test'").unwrap(); |
| 87 | + }); |
| 88 | + let duration = start.elapsed(); |
| 89 | + |
| 90 | + // Should be under 5ms for simple scripts |
| 91 | + assert!( |
| 92 | + duration.as_millis() < 10, |
| 93 | + "JavaScript execution took {}ms, expected <10ms", |
| 94 | + duration.as_millis() |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_pyo3_performance() { |
| 100 | + let start = Instant::now(); |
| 101 | + pyo3::Python::with_gil(|py| { |
| 102 | + let _result = py.eval_bound("'Performance test'", None, None).unwrap(); |
| 103 | + }); |
| 104 | + let duration = start.elapsed(); |
| 105 | + |
| 106 | + // Should be under 30ms for simple scripts (allowing for system variability) |
| 107 | + assert!( |
| 108 | + duration.as_millis() < 30, |
| 109 | + "Python execution took {}ms, expected <30ms", |
| 110 | + duration.as_millis() |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn test_regex_performance() { |
| 116 | + let start = Instant::now(); |
| 117 | + let re = regex::Regex::new(r"test").unwrap(); |
| 118 | + let _result = re.is_match("performance test"); |
| 119 | + let duration = start.elapsed(); |
| 120 | + |
| 121 | + // Should be under 0.1ms for simple patterns |
| 122 | + assert!( |
| 123 | + duration.as_micros() < 1000, |
| 124 | + "Regex matching took {}μs, expected <1000μs", |
| 125 | + duration.as_micros() |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_uuid_performance() { |
| 131 | + let start = Instant::now(); |
| 132 | + let _id = uuid::Uuid::new_v4(); |
| 133 | + let duration = start.elapsed(); |
| 134 | + |
| 135 | + // Should be under 0.01ms per UUID |
| 136 | + assert!( |
| 137 | + duration.as_micros() < 100, |
| 138 | + "UUID generation took {}μs, expected <100μs", |
| 139 | + duration.as_micros() |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + // Error handling tests |
| 144 | + #[tokio::test] |
| 145 | + async fn test_mlua_error_handling() { |
| 146 | + let lua = mlua::Lua::new(); |
| 147 | + let result = lua.load("invalid_syntax(").eval::<String>(); |
| 148 | + assert!(result.is_err()); |
| 149 | + } |
| 150 | + |
| 151 | + #[tokio::test] |
| 152 | + async fn test_quickjs_error_handling() { |
| 153 | + let runtime = rquickjs::Runtime::new().unwrap(); |
| 154 | + let context = rquickjs::Context::full(&runtime).unwrap(); |
| 155 | + |
| 156 | + context.with(|ctx| { |
| 157 | + let result = ctx.eval::<String, &str>("invalid_syntax("); |
| 158 | + assert!(result.is_err()); |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_pyo3_error_handling() { |
| 164 | + pyo3::Python::with_gil(|py| { |
| 165 | + let result = py.eval_bound("invalid_syntax(", None, None); |
| 166 | + assert!(result.is_err()); |
| 167 | + }); |
| 168 | + } |
| 169 | + |
| 170 | + #[test] |
| 171 | + fn test_regex_error_handling() { |
| 172 | + #[allow(clippy::invalid_regex)] |
| 173 | + let result = regex::Regex::new(r"[invalid"); |
| 174 | + assert!(result.is_err()); |
| 175 | + } |
| 176 | +} |
0 commit comments