Skip to content

Commit 136e23d

Browse files
Add tests for yaml module registration and functions
Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com>
1 parent 514e6c0 commit 136e23d

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

‎stdlib/src/yaml.rs‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,70 @@ impl StdlibRegistry {
6666
);
6767
}
6868
}
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use super::*;
73+
use std::cell::RefCell;
74+
use std::rc::Rc;
75+
use techscript_runtime::RuntimeContext;
76+
use indexmap::IndexMap;
77+
78+
#[test]
79+
fn test_register_yaml() {
80+
let mut registry = StdlibRegistry {
81+
modules: std::collections::HashMap::new(),
82+
};
83+
registry.register_yaml();
84+
assert!(registry.has_module("std.yaml"));
85+
}
86+
87+
#[test]
88+
fn test_yaml_parse() {
89+
let mut registry = StdlibRegistry {
90+
modules: std::collections::HashMap::new(),
91+
};
92+
registry.register_yaml();
93+
let module = registry.get_module("std.yaml").unwrap();
94+
let parse_func = module.exports.get("parse").unwrap();
95+
96+
let mut ctx = RuntimeContext::new(techscript_runtime::RuntimeConfig::default());
97+
let yaml_str = RuntimeValue::Str("key1: value1\n# comment\nkey2: value2".to_string());
98+
99+
let result = parse_func.call(&mut ctx, vec![yaml_str]).unwrap();
100+
101+
if let RuntimeValue::Map { entries, .. } = result {
102+
let entries = entries.borrow();
103+
assert_eq!(entries.len(), 2);
104+
assert_eq!(entries.get("key1").unwrap().try_into_string().unwrap(), "value1");
105+
assert_eq!(entries.get("key2").unwrap().try_into_string().unwrap(), "value2");
106+
} else {
107+
panic!("Expected Map");
108+
}
109+
}
110+
111+
#[test]
112+
fn test_yaml_stringify() {
113+
let mut registry = StdlibRegistry {
114+
modules: std::collections::HashMap::new(),
115+
};
116+
registry.register_yaml();
117+
let module = registry.get_module("std.yaml").unwrap();
118+
let stringify_func = module.exports.get("stringify").unwrap();
119+
120+
let mut ctx = RuntimeContext::new(techscript_runtime::RuntimeConfig::default());
121+
122+
let mut map = IndexMap::new();
123+
map.insert("key1".to_string(), RuntimeValue::Str("value1".to_string()));
124+
map.insert("key2".to_string(), RuntimeValue::Str("value2".to_string()));
125+
126+
let map_val = RuntimeValue::Map {
127+
entries: Rc::new(RefCell::new(map)),
128+
is_const: false,
129+
};
130+
131+
let result = stringify_func.call(&mut ctx, vec![map_val]).unwrap();
132+
133+
assert_eq!(result.try_into_string().unwrap(), "key1: value1\nkey2: value2\n");
134+
}
135+
}

0 commit comments

Comments
 (0)