use std::io::Write;
use std::process::Command;
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 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_imports_resolve() {
let f = write_temp_file(
"module userapp\n\
\n\
use core.convert.{From, Into, TryFrom, Displayable, ConvertError, Celsius, Fahrenheit}\n\
\n\
public fn convert(c: Celsius) -> Fahrenheit {\n\
\x20\x20Fahrenheit.from(c)\n\
}\n\
\n\
public fn wrap(msg: String) -> ConvertError {\n\
\x20\x20ConvertError { message: msg }\n\
}\n",
);
let output = bock_bin().arg("check").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "core.convert imports resolve");
}
#[test]
fn check_stdlib_from_resolves() {
let f = write_temp_file(
"module userapp\n\
\n\
use core.convert.{Celsius, Fahrenheit}\n\
\n\
public fn to_f(c: Celsius) -> Fahrenheit {\n\
\x20\x20Fahrenheit.from(c)\n\
}\n",
);
let output = bock_bin().arg("check").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "stdlib From[Celsius] for Fahrenheit resolves");
}
#[test]
fn from_rejects_wrong_arg() {
let f = write_temp_file(
"module userapp\n\
\n\
use core.convert.{Fahrenheit}\n\
\n\
public fn bad(s: String) -> Fahrenheit {\n\
\x20\x20Fahrenheit.from(s)\n\
}\n",
);
let output = bock_bin().arg("check").arg(f.path()).output().unwrap();
assert_exit_code(&output, 1, "from rejects wrong argument type");
}
#[test]
fn user_from_enables_into_and_from() {
let f = write_temp_file(
"module userapp\n\
\n\
use core.convert.{From, Into}\n\
\n\
public record Meter {\n\
\x20\x20value: Float\n\
}\n\
\n\
public record Foot {\n\
\x20\x20value: Float\n\
}\n\
\n\
impl From[Meter] for Foot {\n\
\x20\x20public fn from(value: Meter) -> Foot {\n\
\x20\x20\x20\x20Foot { value: value.value * 3.28 }\n\
\x20\x20}\n\
}\n\
\n\
public fn via_from(m: Meter) -> Foot {\n\
\x20\x20Foot.from(m)\n\
}\n\
\n\
public fn via_into(m: Meter) -> Foot {\n\
\x20\x20m.into()\n\
}\n",
);
let output = bock_bin().arg("check").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "user From enables .into() and .from()");
}
#[test]
fn into_to_unrelated_target_errors() {
let f = write_temp_file(
"module userapp\n\
\n\
use core.convert.{From}\n\
\n\
public record Meter {\n\
\x20\x20value: Float\n\
}\n\
\n\
public record Foot {\n\
\x20\x20value: Float\n\
}\n\
\n\
impl From[Meter] for Foot {\n\
\x20\x20public fn from(value: Meter) -> Foot {\n\
\x20\x20\x20\x20Foot { value: value.value * 3.28 }\n\
\x20\x20}\n\
}\n\
\n\
public fn bad(m: Meter) -> String {\n\
\x20\x20m.into()\n\
}\n",
);
let output = bock_bin().arg("check").arg(f.path()).output().unwrap();
assert_exit_code(&output, 1, ".into() to unrelated target errors");
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stderr.contains("E4012") || stdout.contains("E4012"),
"expected E4012, stdout: {stdout}\nstderr: {stderr}"
);
}
#[test]
fn run_prints_convert_error_message() {
let f = write_temp_file(
"module main\n\
\n\
use core.convert.{convert_error}\n\
\n\
public fn main() {\n\
\x20\x20println(convert_error(\"out of range\").message)\n\
}\n",
);
let output = bock_bin().arg("run").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "run core.convert main");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("out of range"),
"expected `out of range` in stdout, got: {stdout}\nstderr: {}",
String::from_utf8_lossy(&output.stderr),
);
}
#[test]
fn run_user_associated_from_dispatches() {
let f = write_temp_file(
"module main\n\
\n\
record Meter { value: Float }\n\
record Foot { value: Float }\n\
\n\
impl From[Meter] for Foot {\n\
\x20\x20public fn from(value: Meter) -> Foot {\n\
\x20\x20\x20\x20Foot { value: value.value * 3.28 }\n\
\x20\x20}\n\
}\n\
\n\
fn main() -> Void {\n\
\x20\x20let m: Meter = Meter { value: 10.0 }\n\
\x20\x20let f: Foot = Foot.from(m)\n\
\x20\x20println(\"foot=${f.value}\")\n\
}\n",
);
let output = bock_bin().arg("run").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "run user associated From.from");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("foot=32.8"),
"expected `foot=32.8` in stdout, got: {stdout}\nstderr: {}",
String::from_utf8_lossy(&output.stderr),
);
}
#[test]
fn run_user_to_string_shadows_builtin() {
let f = write_temp_file(
"module main\n\
\n\
record Money { cents: Int }\n\
\n\
impl Displayable for Money {\n\
\x20\x20public fn to_string(self) -> String {\n\
\x20\x20\x20\x20\"cents=${self.cents}\"\n\
\x20\x20}\n\
}\n\
\n\
fn main() -> Void {\n\
\x20\x20let m: Money = Money { cents: 42 }\n\
\x20\x20println(m.to_string())\n\
}\n",
);
let output = bock_bin().arg("run").arg(f.path()).output().unwrap();
assert_exit_code(&output, 0, "run user to_string shadows builtin");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("cents=42") && !stdout.contains("Money {"),
"expected user `cents=42` (not default record formatting), got: {stdout}\nstderr: {}",
String::from_utf8_lossy(&output.stderr),
);
}