use serde_json::Value;
fn run_blots_eval(code: &str) -> String {
use std::io::Write;
use std::process::{Command, Stdio};
let mut child = Command::new("cargo")
.args(["run", "-p", "blots", "--", "-e"])
.env("CARGO_TERM_COLOR", "never")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("Failed to spawn blots");
if let Some(mut stdin) = child.stdin.take() {
stdin
.write_all(code.as_bytes())
.expect("Failed to write to stdin");
}
let output = child.wait_with_output().expect("Failed to wait for blots");
assert!(
output.status.success(),
"Command failed with stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8_lossy(&output.stdout).trim().to_string()
}
fn parse_function_output(json_str: &str) -> String {
let json: Value = serde_json::from_str(json_str)
.unwrap_or_else(|_| panic!("Failed to parse JSON: {}", json_str));
if let Some(obj) = json.as_object()
&& let Some((_, value)) = obj.iter().next()
&& let Some(func_obj) = value.as_object()
&& let Some(func_src) = func_obj.get("__blots_function")
&& let Some(src) = func_src.as_str()
{
return src.to_string();
}
panic!("Could not extract function source from: {}", json_str);
}
#[test]
fn test_lambda_parameter_not_substituted_simple() {
let code = "x = 42\noutput increment = x => x + 1";
let output = run_blots_eval(code);
let func_src = parse_function_output(&output);
assert_eq!(
func_src, "(x) => x + 1",
"Lambda parameter should not be substituted with outer scope value"
);
assert!(
!func_src.contains("42"),
"Lambda body should not contain the value '42'"
);
}
#[test]
fn test_lambda_multiple_parameters_not_substituted() {
let code = "a = 10\nb = 20\noutput add = (a, b) => a + b";
let output = run_blots_eval(code);
let func_src = parse_function_output(&output);
assert_eq!(
func_src, "(a, b) => a + b",
"Lambda parameters should not be substituted"
);
assert!(
!func_src.contains("10"),
"Lambda body should not contain value '10'"
);
assert!(
!func_src.contains("20"),
"Lambda body should not contain value '20'"
);
}
#[test]
fn test_curried_lambda_captures_outer_preserves_inner() {
let code = "output curry_add = x => y => x + y";
let output = run_blots_eval(code);
let func_src = parse_function_output(&output);
assert_eq!(func_src, "(x) => (y) => x + y");
}
#[test]
fn test_partial_application_captures_value_preserves_parameter() {
let code = "curry_add = x => y => x + y\noutput add_five = curry_add(5)";
let output = run_blots_eval(code);
let func_src = parse_function_output(&output);
assert_eq!(func_src, "(y) => 5 + y");
assert!(
!func_src.contains("x"),
"Captured variable 'x' should be substituted with value"
);
assert!(func_src.contains("y"), "Parameter 'y' should be preserved");
}
#[test]
fn test_nested_lambdas_preserve_parameters() {
let code = "x = 100\noutput outer = x => (y => x + y)";
let output = run_blots_eval(code);
let func_src = parse_function_output(&output);
assert_eq!(func_src, "(x) => (y) => x + y");
assert!(
!func_src.contains("100"),
"Outer scope value should not pollute lambda"
);
}
#[test]
fn test_lambda_in_conditional_preserves_parameters() {
let code = "x = 42\noutput check = x => if x > 0 then \"positive\" else \"negative\"";
let output = run_blots_eval(code);
let func_src = parse_function_output(&output);
assert_eq!(
func_src,
"(x) => if x > 0 then \"positive\" else \"negative\""
);
assert!(
!func_src.contains("42"),
"Lambda body should not contain outer scope value"
);
}
#[test]
fn test_higher_order_function_composition() {
let code = "x = 99\noutput compose = (f, g) => x => f(g(x))";
let output = run_blots_eval(code);
let func_src = parse_function_output(&output);
assert_eq!(func_src, "(f, g) => (x) => f(g(x))");
assert!(
!func_src.contains("99"),
"Lambda should not capture outer scope value with same name as parameter"
);
}
#[test]
fn test_multiple_lambdas_same_parameter_name() {
let code = "x = 1000\ndouble = x => x * 2\noutput triple = x => x * 3";
let output = run_blots_eval(code);
let func_src = parse_function_output(&output);
assert_eq!(func_src, "(x) => x * 3");
assert!(
!func_src.contains("1000"),
"Parameter 'x' should not be replaced with outer value"
);
}
#[test]
fn test_lambda_with_list_operations_preserves_parameters() {
let code = "x = 50\noutput mapper = x => (x via y => y * 2)";
let output = run_blots_eval(code);
let func_src = parse_function_output(&output);
assert!(
func_src.contains("x"),
"Outer parameter 'x' should be preserved"
);
assert!(
func_src.contains("y"),
"Inner parameter 'y' should be preserved"
);
assert!(
!func_src.contains("50"),
"Should not contain outer scope value"
);
}