@@ -66,3 +66,82 @@ impl StdlibRegistry {
6666 ) ;
6767 }
6868}
69+
70+ #[ cfg( test) ]
71+ mod tests {
72+ use super :: * ;
73+ use techscript_runtime:: context:: { RuntimeConfig , RuntimeContext } ;
74+
75+ #[ test]
76+ fn test_register_xml ( ) {
77+ let mut registry = StdlibRegistry :: default ( ) ;
78+ registry. register_xml ( ) ;
79+
80+ let module = registry
81+ . modules
82+ . get ( "std.xml" )
83+ . expect ( "Module std.xml should be registered" ) ;
84+ assert_eq ! ( module. name, "std.xml" ) ;
85+ assert_eq ! ( module. version, "1.0.0" ) ;
86+ assert ! ( module. required_capabilities. is_empty( ) ) ;
87+ assert ! ( module. exports. contains_key( "parse" ) ) ;
88+ assert ! ( module. exports. contains_key( "stringify" ) ) ;
89+ }
90+
91+ #[ test]
92+ fn test_xml_parse ( ) {
93+ let mut registry = StdlibRegistry :: default ( ) ;
94+ registry. register_xml ( ) ;
95+
96+ let module = registry. modules . get ( "std.xml" ) . unwrap ( ) ;
97+ let parse_fn = module. exports . get ( "parse" ) . unwrap ( ) ;
98+
99+ let mut ctx = RuntimeContext :: new ( RuntimeConfig :: default ( ) ) ;
100+
101+ // Valid XML
102+ let args = vec ! [ RuntimeValue :: Str ( "<item>hello</item>" . to_string( ) ) ] ;
103+ let result = parse_fn. call ( & mut ctx, args) . unwrap ( ) ;
104+
105+ if let RuntimeValue :: Map { entries, .. } = result {
106+ let map = entries. borrow ( ) ;
107+ assert_eq ! ( map. get( "item" ) . and_then( |v| v. as_string( ) ) , Some ( "hello" ) ) ;
108+ } else {
109+ panic ! ( "parse should return a Map" ) ;
110+ }
111+
112+ // Non-matching XML input (returns empty map)
113+ let plain_str = vec ! [ RuntimeValue :: Str ( "not xml" . to_string( ) ) ] ;
114+ let result = parse_fn. call ( & mut ctx, plain_str) . unwrap ( ) ;
115+ if let RuntimeValue :: Map { entries, .. } = result {
116+ assert ! ( entries. borrow( ) . is_empty( ) ) ;
117+ } else {
118+ panic ! ( "parse should return a Map even for non-matching strings" ) ;
119+ }
120+ }
121+
122+ #[ test]
123+ fn test_xml_stringify ( ) {
124+ let mut registry = StdlibRegistry :: default ( ) ;
125+ registry. register_xml ( ) ;
126+
127+ let module = registry. modules . get ( "std.xml" ) . unwrap ( ) ;
128+ let stringify_fn = module. exports . get ( "stringify" ) . unwrap ( ) ;
129+
130+ let mut ctx = RuntimeContext :: new ( RuntimeConfig :: default ( ) ) ;
131+
132+ let mut map = IndexMap :: new ( ) ;
133+ map. insert ( "name" . to_string ( ) , RuntimeValue :: Str ( "Alice" . to_string ( ) ) ) ;
134+ let map_val = RuntimeValue :: Map {
135+ entries : Rc :: new ( RefCell :: new ( map) ) ,
136+ is_const : false ,
137+ } ;
138+
139+ let result = stringify_fn. call ( & mut ctx, vec ! [ map_val] ) . unwrap ( ) ;
140+ assert_eq ! ( result. as_string( ) , Some ( "<name>Alice</name>" ) ) ;
141+
142+ // Non-map argument returns empty string
143+ let non_map = vec ! [ RuntimeValue :: Int ( 42 ) ] ;
144+ let result = stringify_fn. call ( & mut ctx, non_map) . unwrap ( ) ;
145+ assert_eq ! ( result. as_string( ) , Some ( "" ) ) ;
146+ }
147+ }
0 commit comments