use std::io::Write;
use std::process::{Command, Output, Stdio};
use std::time::{Duration, Instant};
use tempfile::NamedTempFile;
fn bock_bin() -> Command {
Command::new(env!("CARGO_BIN_EXE_bock"))
}
fn write_temp_file(content: &str) -> NamedTempFile {
let mut f = NamedTempFile::with_suffix(".bock").unwrap();
f.write_all(content.as_bytes()).unwrap();
f.flush().unwrap();
f
}
fn run_with_timeout(mut cmd: Command, timeout: Duration) -> Option<Output> {
let mut child = cmd
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("failed to spawn bock");
let start = Instant::now();
loop {
match child.try_wait().expect("try_wait failed") {
Some(_status) => {
return Some(
child
.wait_with_output()
.expect("wait_with_output after exit failed"),
);
}
None => {
if start.elapsed() >= timeout {
let _ = child.kill();
let _ = child.wait();
return None;
}
std::thread::sleep(Duration::from_millis(20));
}
}
}
}
fn assert_exit_code(output: &std::process::Output, expected: i32, ctx: &str) {
assert_eq!(
output.status.code(),
Some(expected),
"{ctx}: expected exit {expected}, got {}\nstdout: {}\nstderr: {}",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
}
#[test]
fn check_resolves_iter_traits_and_drive() {
let f = write_temp_file(
"module userapp\n\
\n\
use core.iter.{Iterator, Iterable, ListIterator, list_iter}\n\
\n\
public fn sum_via_drive() -> Int {\n\
\x20\x20let mut it: ListIterator[Int] = list_iter([1, 2, 3])\n\
\x20\x20let mut total: Int = 0\n\
\x20\x20loop {\n\
\x20\x20\x20\x20match it.next() {\n\
\x20\x20\x20\x20\x20\x20Some(x) => {\n\
\x20\x20\x20\x20\x20\x20\x20\x20total = total + x\n\
\x20\x20\x20\x20\x20\x20}\n\
\x20\x20\x20\x20\x20\x20None => break\n\
\x20\x20\x20\x20}\n\
\x20\x20}\n\
\x20\x20total\n\
}\n",
);
let output = bock_bin().arg("check").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "core.iter traits + drive resolve");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("no errors"), "stdout: {stdout}");
}
#[test]
fn check_combinators_typecheck() {
let f = write_temp_file(
"module userapp\n\
\n\
use core.iter.{ListIterator, list_iter, to_list, count, fold, map, filter, take}\n\
\n\
public fn all() -> Int {\n\
\x20\x20let listed: List[Int] = to_list(list_iter([1, 2, 3]))\n\
\x20\x20let n: Int = count(list_iter([1, 2, 3]))\n\
\x20\x20let summed: Int = fold(list_iter([1, 2, 3]), 0, (acc, x) => acc + x)\n\
\x20\x20let doubled: List[Int] = map(list_iter([1, 2, 3]), (x) => x * 2)\n\
\x20\x20let kept: List[Int] = filter(list_iter([1, 2, 3, 4]), (x) => x > 2)\n\
\x20\x20let taken: List[Int] = take(list_iter([1, 2, 3, 4]), 2)\n\
\x20\x20n + summed\n\
}\n",
);
let output = bock_bin().arg("check").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "core.iter combinators type-check");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("no errors"), "stdout: {stdout}");
}
#[test]
fn run_drive_loop_accumulates_total() {
let f = write_temp_file(
"module main\n\
\n\
use core.iter.{ListIterator, list_iter}\n\
\n\
public fn main() {\n\
\x20\x20let mut it: ListIterator[Int] = list_iter([7, 8, 9])\n\
\x20\x20let mut total: Int = 0\n\
\x20\x20loop {\n\
\x20\x20\x20\x20match it.next() {\n\
\x20\x20\x20\x20\x20\x20Some(x) => {\n\
\x20\x20\x20\x20\x20\x20\x20\x20total = total + x\n\
\x20\x20\x20\x20\x20\x20}\n\
\x20\x20\x20\x20\x20\x20None => break\n\
\x20\x20\x20\x20}\n\
\x20\x20}\n\
\x20\x20println(\"total=${total}\")\n\
}\n",
);
let mut cmd = bock_bin();
cmd.arg("run").arg(f.path());
let output = run_with_timeout(cmd, Duration::from_secs(30))
.expect("`bock run` hung: core.iter ListIterator cursor did not advance");
assert_exit_code(&output, 0, "run core.iter drive loop");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("total=24"),
"expected `total=24` in stdout, got: {stdout}\nstderr: {}",
String::from_utf8_lossy(&output.stderr),
);
}
#[test]
fn run_single_next_yields_first() {
let f = write_temp_file(
"module main\n\
\n\
use core.iter.{ListIterator, list_iter}\n\
\n\
public fn main() {\n\
\x20\x20let mut it: ListIterator[Int] = list_iter([7, 8, 9])\n\
\x20\x20match it.next() {\n\
\x20\x20\x20\x20Some(x) => {\n\
\x20\x20\x20\x20\x20\x20println(\"first=${x}\")\n\
\x20\x20\x20\x20}\n\
\x20\x20\x20\x20None => {\n\
\x20\x20\x20\x20\x20\x20println(\"empty\")\n\
\x20\x20\x20\x20}\n\
\x20\x20}\n\
}\n",
);
let output = bock_bin().arg("run").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "run core.iter single next");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("first=7"),
"expected `first=7` in stdout, got: {stdout}\nstderr: {}",
String::from_utf8_lossy(&output.stderr),
);
}
#[test]
fn check_for_over_user_iterable_desugars() {
let f = write_temp_file(
"module userapp\n\
\n\
use core.iter.{list_iter}\n\
\n\
public record Bag {\n\
\x20\x20items: List[Int]\n\
}\n\
\n\
impl Iterable[Int] for Bag {\n\
\x20\x20public fn iter(self) -> ListIterator[Int] {\n\
\x20\x20\x20\x20list_iter(self.items)\n\
\x20\x20}\n\
}\n\
\n\
public fn sum(bag: Bag) -> Int {\n\
\x20\x20let mut total: Int = 0\n\
\x20\x20for x in bag {\n\
\x20\x20\x20\x20total = total + x\n\
\x20\x20}\n\
\x20\x20total\n\
}\n",
);
let output = bock_bin().arg("check").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "for-over-Iterable desugar type-checks");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("no errors"), "stdout: {stdout}");
}