Skip to content

Commit b30119d

Browse files
authored
Merge pull request #88 from Tcode-Motion/jules/test-csv-module-1184424780295221576
🧪 testing improvement: Add tests for csv module
2 parents 56071f4 + 2138831 commit b30119d

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

‎stdlib/src/csv.rs‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,112 @@ impl StdlibRegistry {
7373
);
7474
}
7575
}
76+
77+
#[cfg(test)]
78+
mod tests {
79+
use super::*;
80+
use std::cell::RefCell;
81+
use std::rc::Rc;
82+
use techscript_runtime::context::{RuntimeConfig, RuntimeContext};
83+
use techscript_runtime::value::RuntimeValue;
84+
85+
#[test]
86+
fn test_csv_register() {
87+
let mut registry = StdlibRegistry::default();
88+
registry.register_csv();
89+
90+
let module = registry
91+
.modules
92+
.get("std.csv")
93+
.expect("Module std.csv should be registered");
94+
assert_eq!(module.name, "std.csv");
95+
assert!(module.exports.contains_key("parse"));
96+
assert!(module.exports.contains_key("stringify"));
97+
}
98+
99+
#[test]
100+
fn test_csv_parse() {
101+
let mut registry = StdlibRegistry::default();
102+
registry.register_csv();
103+
104+
let module = registry.modules.get("std.csv").unwrap();
105+
let parse_fn = module.exports.get("parse").unwrap();
106+
107+
let mut ctx = RuntimeContext::new(RuntimeConfig::default());
108+
let args = vec![RuntimeValue::Str("a,b,c\n1,2,3".to_string())];
109+
110+
let result = parse_fn.call(&mut ctx, args).unwrap();
111+
112+
if let RuntimeValue::List { items, .. } = result {
113+
let rows = items.borrow();
114+
assert_eq!(rows.len(), 2);
115+
116+
// row 1
117+
if let RuntimeValue::List {
118+
items: r1_items, ..
119+
} = &rows[0]
120+
{
121+
let r1 = r1_items.borrow();
122+
assert_eq!(r1[0].try_into_string().unwrap(), "a");
123+
assert_eq!(r1[1].try_into_string().unwrap(), "b");
124+
assert_eq!(r1[2].try_into_string().unwrap(), "c");
125+
} else {
126+
panic!("Row 1 is not a list");
127+
}
128+
129+
// row 2
130+
if let RuntimeValue::List {
131+
items: r2_items, ..
132+
} = &rows[1]
133+
{
134+
let r2 = r2_items.borrow();
135+
assert_eq!(r2[0].try_into_string().unwrap(), "1");
136+
assert_eq!(r2[1].try_into_string().unwrap(), "2");
137+
assert_eq!(r2[2].try_into_string().unwrap(), "3");
138+
} else {
139+
panic!("Row 2 is not a list");
140+
}
141+
} else {
142+
panic!("Result is not a list");
143+
}
144+
}
145+
146+
#[test]
147+
fn test_csv_stringify() {
148+
let mut registry = StdlibRegistry::default();
149+
registry.register_csv();
150+
151+
let module = registry.modules.get("std.csv").unwrap();
152+
let stringify_fn = module.exports.get("stringify").unwrap();
153+
154+
let mut ctx = RuntimeContext::new(RuntimeConfig::default());
155+
156+
let r1 = vec![
157+
RuntimeValue::Str("a".to_string()),
158+
RuntimeValue::Str("b".to_string()),
159+
RuntimeValue::Str("c".to_string()),
160+
];
161+
let row1 = RuntimeValue::List {
162+
items: Rc::new(RefCell::new(r1)),
163+
is_const: false,
164+
};
165+
let r2 = vec![
166+
RuntimeValue::Int(1),
167+
RuntimeValue::Int(2),
168+
RuntimeValue::Int(3),
169+
];
170+
let row2 = RuntimeValue::List {
171+
items: Rc::new(RefCell::new(r2)),
172+
is_const: false,
173+
};
174+
175+
let args = vec![RuntimeValue::List {
176+
items: Rc::new(RefCell::new(vec![row1, row2])),
177+
is_const: false,
178+
}];
179+
180+
let result = stringify_fn.call(&mut ctx, args).unwrap();
181+
182+
assert_eq!(result.try_into_string().unwrap(), "a,b,c\n1,2,3");
183+
}
184+
}

0 commit comments

Comments
 (0)