Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions stdlib/src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Loading