use serde_json::json;
fn run_blots(code: &str, inputs: Option<serde_json::Value>) -> String {
use std::io::Write;
use std::process::{Command, Stdio};
let mut args = vec![
"run".to_string(),
"-p".to_string(),
"blots".to_string(),
"--".to_string(),
"-e".to_string(),
];
if let Some(inputs) = &inputs {
args.push("-i".to_string());
args.push(inputs.to_string());
}
let mut child = Command::new("cargo")
.args(&args)
.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");
stdin.flush().expect("Failed to flush stdin");
drop(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 function_source(output_json: &str, key: &str) -> String {
let json: serde_json::Value = serde_json::from_str(output_json)
.unwrap_or_else(|_| panic!("Failed to parse JSON: {}", output_json));
json[key]["__blots_function"]
.as_str()
.unwrap_or_else(|| panic!("No __blots_function for key {} in: {}", key, output_json))
.to_string()
}
#[test]
fn test_output_function_keeps_input_reference_symbolic() {
let output = run_blots("output f = () => #x * 2", Some(json!({ "x": 42 })));
assert_eq!(function_source(&output, "f"), "() => #x * 2");
}
#[test]
fn test_output_function_keeps_inputs_field_symbolic() {
let output = run_blots("output f = () => inputs.x * 2", Some(json!({ "x": 42 })));
assert_eq!(function_source(&output, "f"), "() => inputs.x * 2");
}
#[test]
fn test_output_function_mixes_inlined_capture_and_input_reference() {
let output = run_blots(
"k = 7\noutput f = y => k * y + #x",
Some(json!({ "x": 42 })),
);
assert_eq!(function_source(&output, "f"), "(y) => 7 * y + #x");
}
#[test]
fn test_round_trip_input_reference_rebinds_to_consuming_inputs() {
let producer_output = run_blots("output f = y => #x + y", Some(json!({ "x": 42 })));
let producer_json: serde_json::Value = serde_json::from_str(&producer_output).unwrap();
let consumer_inputs = json!({ "x": 100, "f": producer_json["f"] });
let consumer_output = run_blots("output result = inputs.f(1)", Some(consumer_inputs));
let result: serde_json::Value = serde_json::from_str(&consumer_output).unwrap();
assert_eq!(result["result"], 101.0);
}
#[test]
fn test_round_trip_inputs_field_rebinds_to_consuming_inputs() {
let producer_output = run_blots("output f = () => inputs.x * 2", Some(json!({ "x": 42 })));
let producer_json: serde_json::Value = serde_json::from_str(&producer_output).unwrap();
let consumer_inputs = json!({ "x": 100, "f": producer_json["f"] });
let consumer_output = run_blots("output result = inputs.f()", Some(consumer_inputs));
let result: serde_json::Value = serde_json::from_str(&consumer_output).unwrap();
assert_eq!(result["result"], 200.0);
}
#[test]
fn test_input_function_with_input_reference_called_through_via() {
let inputs = json!({
"x": 10,
"scale": { "__blots_function": "(n) => n * #x" }
});
let output = run_blots("output result = [1, 2, 3] via inputs.scale", Some(inputs));
let result: serde_json::Value = serde_json::from_str(&output).unwrap();
assert_eq!(result["result"], json!([10.0, 20.0, 30.0]));
}
#[test]
fn test_input_function_with_input_reference_called_through_map() {
let inputs = json!({
"x": 10,
"scale": { "__blots_function": "(n) => n * #x" }
});
let output = run_blots("output result = map([1, 2, 3], inputs.scale)", Some(inputs));
let result: serde_json::Value = serde_json::from_str(&output).unwrap();
assert_eq!(result["result"], json!([10.0, 20.0, 30.0]));
}
#[test]
fn test_input_function_with_input_reference_called_from_user_lambda() {
let inputs = json!({
"x": 10,
"scale": { "__blots_function": "(n) => n * #x" }
});
let code = "apply = (g, v) => g(v)\noutput result = apply(inputs.scale, 4)";
let output = run_blots(code, Some(inputs));
let result: serde_json::Value = serde_json::from_str(&output).unwrap();
assert_eq!(result["result"], 40.0);
}