Files
core/test/rust/native.rs
T
2026-11-03 03:32:07 +00:00

164 lines
4.7 KiB
Rust

use dever::compiler::{build, project::Project};
use std::path::{Path, PathBuf};
struct Directory(PathBuf);
#[path = "native/application/mod.rs"]
mod application;
#[path = "native/dependency/mod.rs"]
mod dependency;
#[path = "native/bytes.rs"]
mod bytes;
#[path = "native/calendar.rs"]
mod calendar;
#[path = "native/collection.rs"]
mod collection;
#[path = "native/crypto.rs"]
mod crypto;
#[path = "native/handler.rs"]
mod handler;
#[path = "native/http.rs"]
mod http;
#[path = "native/id.rs"]
mod id;
#[path = "native/inspection.rs"]
mod inspection;
#[path = "native/io/mod.rs"]
mod io;
#[path = "native/json.rs"]
mod json;
#[path = "native/net.rs"]
mod net;
#[path = "native/numeric/mod.rs"]
mod numeric;
#[path = "native/package.rs"]
mod package;
#[path = "native/port/mod.rs"]
mod port;
#[path = "native/record.rs"]
mod record;
#[path = "native/standard.rs"]
mod standard;
#[path = "native/storage.rs"]
mod storage;
#[path = "native/task/mod.rs"]
mod task;
#[path = "native/testing.rs"]
mod testing;
#[path = "native/text.rs"]
mod text;
#[path = "native/toolchain/mod.rs"]
mod toolchain;
#[path = "native/uuid.rs"]
mod uuid;
impl Directory {
fn new() -> Self {
let unique = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let path =
std::env::temp_dir().join(format!("dever-native-{}-{unique}", std::process::id()));
std::fs::create_dir(&path).unwrap();
Self(path)
}
}
impl Drop for Directory {
fn drop(&mut self) {
std::fs::remove_dir_all(&self.0).unwrap();
}
}
fn fixture() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../test/rust/fixtures/core.dever")
}
#[test]
fn native_values_and_fault_locations() {
let directory = Directory::new();
let project = Project::load(&fixture()).unwrap();
let program = project.check().unwrap();
let success = build::build(
&program,
&project.sources,
"verify",
&directory.0.join("success"),
)
.unwrap();
let result = std::process::Command::new(&success).output().unwrap();
assert!(
result.status.success(),
"{}",
String::from_utf8_lossy(&result.stderr)
);
assert_eq!(result.stdout, b"true\n");
let before = std::fs::read(&success).unwrap();
assert!(build::build(&program, &project.sources, "verify", &success).is_err());
assert_eq!(std::fs::read(&success).unwrap(), before);
let failure = build::build(
&program,
&project.sources,
"overflowCheck",
&directory.0.join("failure"),
)
.unwrap();
let result = std::process::Command::new(failure).output().unwrap();
assert!(!result.status.success());
assert!(result.stdout.is_empty());
let stderr = String::from_utf8(result.stderr).unwrap();
let source = &project.sources[0];
let offset = source.text.find("value + 1").unwrap();
let (line, column) = source.position(offset);
assert!(
stderr.contains(&format!("core.dever:{line}:{column}:")),
"{stderr}"
);
assert!(stderr.contains("integer arithmetic fault"), "{stderr}");
}
#[test]
fn native_choices_nullable_values_and_typed_failures() {
let directory = Directory::new();
let project = Project::load(&fixture().with_file_name("choice.dever")).unwrap();
let program = project.check().unwrap();
for (entry, expected) in [
("verify", None),
("failureEntry", Some("Result.Failed")),
("faultEntry", Some("integer arithmetic fault")),
("argumentEntry", Some("Problem.Denied")),
] {
let binary =
build::build(&program, &project.sources, entry, &directory.0.join(entry)).unwrap();
let result = std::process::Command::new(binary).output().unwrap();
if let Some(message) = expected {
assert!(!result.status.success());
assert!(result.stdout.is_empty());
let stderr = String::from_utf8(result.stderr).unwrap();
assert!(stderr.contains(message), "{stderr}");
let needle = match entry {
"faultEntry" => "value + 1",
"argumentEntry" => "Problem.Denied(value)",
_ => "Result.Failed(\"negative\")",
};
let source = &project.sources[0];
let (line, column) = source.position(source.text.find(needle).unwrap());
assert!(
stderr.contains(&format!("choice.dever:{line}:{column}:")),
"{stderr}"
);
} else {
assert!(
result.status.success(),
"{}",
String::from_utf8_lossy(&result.stderr)
);
assert_eq!(result.stdout, b"true\n");
}
}
}