From 136e23dea0863146f59c868c214ef405152cf36b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 8 Sep 2026 08:21:52 +0000 Subject: [PATCH] Add tests for yaml module registration and functions Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- stdlib/src/yaml.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/stdlib/src/yaml.rs b/stdlib/src/yaml.rs index 92d9c527..9527e663 100644 --- a/stdlib/src/yaml.rs +++ b/stdlib/src/yaml.rs @@ -66,3 +66,70 @@ impl StdlibRegistry { ); } } + +#[cfg(test)] +mod tests { + use super::*; + use std::cell::RefCell; + use std::rc::Rc; + use techscript_runtime::RuntimeContext; + use indexmap::IndexMap; + + #[test] + fn test_register_yaml() { + let mut registry = StdlibRegistry { + modules: std::collections::HashMap::new(), + }; + registry.register_yaml(); + assert!(registry.has_module("std.yaml")); + } + + #[test] + fn test_yaml_parse() { + let mut registry = StdlibRegistry { + modules: std::collections::HashMap::new(), + }; + registry.register_yaml(); + let module = registry.get_module("std.yaml").unwrap(); + let parse_func = module.exports.get("parse").unwrap(); + + let mut ctx = RuntimeContext::new(techscript_runtime::RuntimeConfig::default()); + let yaml_str = RuntimeValue::Str("key1: value1\n# comment\nkey2: value2".to_string()); + + let result = parse_func.call(&mut ctx, vec![yaml_str]).unwrap(); + + if let RuntimeValue::Map { entries, .. } = result { + let entries = entries.borrow(); + assert_eq!(entries.len(), 2); + assert_eq!(entries.get("key1").unwrap().try_into_string().unwrap(), "value1"); + assert_eq!(entries.get("key2").unwrap().try_into_string().unwrap(), "value2"); + } else { + panic!("Expected Map"); + } + } + + #[test] + fn test_yaml_stringify() { + let mut registry = StdlibRegistry { + modules: std::collections::HashMap::new(), + }; + registry.register_yaml(); + let module = registry.get_module("std.yaml").unwrap(); + let stringify_func = module.exports.get("stringify").unwrap(); + + let mut ctx = RuntimeContext::new(techscript_runtime::RuntimeConfig::default()); + + let mut map = IndexMap::new(); + map.insert("key1".to_string(), RuntimeValue::Str("value1".to_string())); + map.insert("key2".to_string(), RuntimeValue::Str("value2".to_string())); + + let map_val = RuntimeValue::Map { + entries: Rc::new(RefCell::new(map)), + is_const: false, + }; + + let result = stringify_func.call(&mut ctx, vec![map_val]).unwrap(); + + assert_eq!(result.try_into_string().unwrap(), "key1: value1\nkey2: value2\n"); + } +}