Add object-oriented records example

This commit is contained in:
Michael Schimmel
2026-02-26 21:54:26 +01:00
parent bf74795e01
commit 512193febc
4 changed files with 96 additions and 4 deletions
+41
View File
@@ -427,6 +427,47 @@ mod tests {
);
}
#[test]
fn test_optimizer_upvalue_inlining_bug_repro() {
let env = Environment::new();
let source = r#"
(do
(def make-counter (fn [init]
(do
(def val init)
{
:inc (fn [] (assign val (+ val 1)))
:get (fn [] val)
})))
(def c (make-counter 10))
((.inc c))
((.get c)))
"#;
let res = env.run_script(source);
assert!(res.is_ok(), "Optimizer bug triggered: {:?}", res.err());
assert_eq!(format!("{}", res.unwrap()), "11");
}
#[test]
fn test_optimizer_destructuring_inlining_and_mutation() {
let env = Environment::new();
// 1. Test: Destructuring definition should allow inlining if not mutated
let source_inline = "(do (def [x y] [10 20]) (+ x y))";
let res_inline = env.run_script(source_inline).unwrap();
assert_eq!(format!("{}", res_inline), "30");
// 2. Test: Destructuring definition should NOT be inlined if mutated (Regression Test)
let source_mutation = r#"
(do
(def [a b] [1 2])
(def f (fn [] (assign a (+ a b))))
(f)
a)
"#;
let res_mutation = env.run_script(source_mutation).unwrap();
assert_eq!(format!("{}", res_mutation), "3");
}
#[test]
fn test_record_basics() {
let env = Environment::new();