474 lines
15 KiB
Rust
474 lines
15 KiB
Rust
//! 真实前端、所有权分析、Rust 生成、Cargo 编译和原生执行,共用锁定依赖。
|
|
|
|
use dever::compiler::{
|
|
build::Profile,
|
|
check::{ownership, program},
|
|
emit,
|
|
project::Project,
|
|
};
|
|
use std::{
|
|
fs,
|
|
path::{Path, PathBuf},
|
|
process::Command,
|
|
};
|
|
|
|
#[path = "native/calendar.rs"]
|
|
mod calendar;
|
|
#[path = "native/constants.rs"]
|
|
mod constants;
|
|
#[path = "native/uuid.rs"]
|
|
mod uuid;
|
|
#[path = "native/websocket.rs"]
|
|
mod websocket;
|
|
|
|
#[test]
|
|
fn blocks_execute_only_the_selected_branch_and_finish_other_aggregates() {
|
|
let binary = build(
|
|
"blocks",
|
|
include_str!("native/blocks.dv"),
|
|
Some(
|
|
r#"
|
|
let lines = [@Line@ { f0: 30, f1: true }, @Line@ { f0: 50, f1: false }];
|
|
assert_eq!(@checkout@(1, false, &lines).unwrap(), 80);
|
|
assert_eq!(@display@(None), "guest");
|
|
assert_eq!(@display@(Some(String::from("author"))), "author");
|
|
let stats = @cartStats@(&lines);
|
|
assert_eq!((stats.f0, stats.f1), (30, 2));
|
|
let orders = vec![vec![], lines.to_vec(), lines.to_vec()];
|
|
assert_eq!(@discountTotal@(&orders), 60);
|
|
assert_eq!(@discountTotal@(&[]), 0);
|
|
let refunds = [@Refund@ { f0: 1, f1: false }, @Refund@ { f0: 2, f1: true }, @Refund@ { f0: 0, f1: false }];
|
|
assert_eq!(@runRefunds@(&refunds).unwrap(), Some(2));
|
|
assert_eq!(@runRefunds@(&refunds[..1]).unwrap(), None);
|
|
assert!(@runRefunds@(&refunds[2..]).is_err());
|
|
assert_eq!(@checkout@(0, true, &[]).unwrap(), 0);
|
|
assert!(@checkout@(0, false, &lines).is_err());
|
|
"#,
|
|
),
|
|
Profile::Dev,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn http_pool_executes_shared_clients_and_scope_cleanup() {
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"http-pool",
|
|
include_str!("native/http_pool.dever"),
|
|
Some(include_str!("native/http_pool.rs.txt")),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert!(output.stdout.is_empty());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn http_server_executes_static_sync_async_task_and_setting_handlers() {
|
|
let fixtures = Path::new(env!("CARGO_MANIFEST_DIR")).join("../test/fixtures/tls");
|
|
let assertions = include_str!("native/http_server.rs.txt")
|
|
.replace("@tls_fixtures@", &format!("{fixtures:?}"));
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"http-server",
|
|
include_str!("native/http_server.dever"),
|
|
Some(&assertions),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert!(output.stdout.is_empty());
|
|
assert!(String::from_utf8_lossy(&output.stderr).contains("http-server.dever:"));
|
|
assert!(
|
|
String::from_utf8_lossy(&output.stderr).contains("ApiError")
|
|
|| String::from_utf8_lossy(&output.stderr).contains("Invalid")
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn http_send_executes_real_requests_and_task_transfer() {
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"http-send",
|
|
include_str!("native/http.dever"),
|
|
Some(include_str!("native/http.rs.txt")),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn tls_configuration_executes_with_real_certificates_and_task_transfer() {
|
|
let fixtures = Path::new(env!("CARGO_MANIFEST_DIR")).join("../test/fixtures/tls");
|
|
let assertions =
|
|
include_str!("native/tls.rs.txt").replace("@tls_fixtures@", &format!("{fixtures:?}"));
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"tls",
|
|
include_str!("native/tls.dever"),
|
|
Some(&assertions),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn tcp_executes_real_connections_streams_timeouts_and_task_transfer() {
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"net",
|
|
include_str!("native/net.dever"),
|
|
Some(include_str!("native/net.rs.txt")),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn process_arguments_keep_order_unicode_errors_and_direct_rust_allocation_cost() {
|
|
use std::{ffi::OsString, os::unix::ffi::OsStringExt};
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"arguments",
|
|
include_str!("native/arguments.dever"),
|
|
Some(include_str!("native/arguments.rs.txt")),
|
|
profile,
|
|
);
|
|
for arguments in [
|
|
vec![],
|
|
vec![
|
|
OsString::from(""),
|
|
OsString::from("雪😀"),
|
|
OsString::from("--flag"),
|
|
OsString::from("a b"),
|
|
],
|
|
vec![OsString::from("long".repeat(4096))],
|
|
vec![OsString::from_vec(vec![b's', 0xff])],
|
|
] {
|
|
let output = Command::new(&binary).args(arguments).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn standard_library_executes_unicode_numeric_boundaries_and_rounding_faults() {
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"standard",
|
|
include_str!("native/standard.dever"),
|
|
Some(include_str!("native/standard.rs.txt")),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn json_library_executes_raw_values_documents_and_nominal_errors() {
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"json",
|
|
include_str!("native/json.dever"),
|
|
Some(include_str!("native/json.rs.txt")),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn json_command_boundaries_validate_dynamic_subtrees_and_preserve_raw_numbers() {
|
|
let binary = build(
|
|
"json-command",
|
|
include_str!("native/json.dever"),
|
|
None,
|
|
Profile::Dev,
|
|
);
|
|
let run = |name: &str, input: &str| Command::new(&binary).args([name, input]).output().unwrap();
|
|
let data = r#"{"label":"雪","value":{"number":1e9999},"optional":[null,true]}"#;
|
|
let input = format!(r#"{{"value":{data}}}"#);
|
|
let output = run("main.main.fragment", &input);
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert_eq!(
|
|
String::from_utf8(output.stdout).unwrap().trim(),
|
|
format!(r#"{{"code":0,"message":"ok","data":{data}}}"#)
|
|
);
|
|
let output = run("main.main.bundle", &input);
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert_eq!(
|
|
String::from_utf8(output.stdout).unwrap().trim(),
|
|
format!(r#"{{"code":0,"message":"ok","data":{{"status":1,"out":{data}}}}}"#)
|
|
);
|
|
for invalid in [
|
|
input.replace("1e9999", "1,\"number\":2"),
|
|
input.replace("1e9999", "[1,]"),
|
|
input.replace("\"label\":", "\"extra\":0,\"label\":"),
|
|
input.replace("\"label\":\"雪\",", ""),
|
|
] {
|
|
let output = run("main.main.fragment", &invalid);
|
|
assert!(!output.status.success(), "{invalid}");
|
|
assert!(output.stdout.is_empty());
|
|
}
|
|
for (depth, valid) in [(63, true), (64, false)] {
|
|
let raw = format!("{}0{}", "[".repeat(depth), "]".repeat(depth));
|
|
let output = run("main.main.raw", &format!(r#"{{"value":{raw}}}"#));
|
|
assert_eq!(
|
|
output.status.success(),
|
|
valid,
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn bytes_library_executes_shared_slices_conversions_errors_and_direct_loops() {
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"bytes",
|
|
include_str!("native/bytes.dever"),
|
|
Some(include_str!("native/bytes.rs.txt")),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn sequential_loops_execute_aggregates_order_short_circuit_and_failure() {
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"iteration",
|
|
include_str!("native/iteration.dever"),
|
|
Some(include_str!("native/iteration.rs.txt")),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn planned_tree_walk_executes_cycle_deduplication_and_missing_node_failures() {
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"tree",
|
|
concat!(
|
|
include_str!("../../examples/next/workflows/module/demo/tree/flow/tree.dv"),
|
|
include_str!("../../examples/next/workflows/module/demo/tree/rule/tree.dv"),
|
|
include_str!("../../examples/next/workflows/module/demo/tree/loop/tree.dv"),
|
|
),
|
|
Some(include_str!("native/walk_tree.rs.txt")),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
fn build(name: &str, source: &str, assertions: Option<&str>, profile: Profile) -> PathBuf {
|
|
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
|
|
let directory = root.join(format!(
|
|
"../target/next-native-cases/{}-{name}-{}",
|
|
std::process::id(),
|
|
profile.directory()
|
|
));
|
|
fs::create_dir_all(&directory).unwrap();
|
|
let path = directory.join(format!("{name}.dever"));
|
|
fs::write(&path, source).unwrap();
|
|
let project = Project::load(&path).unwrap();
|
|
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
|
let program = program::check(&project.declarations, &project.modules).unwrap();
|
|
let decisions = ownership::analyze(&program);
|
|
let mut generated = emit::emit(&program, &decisions, &project.sources);
|
|
for file in generated.values() {
|
|
for mapping in &file.mappings {
|
|
assert!(mapping.start < mapping.end && mapping.end <= file.text.len());
|
|
assert!(mapping.source.end <= project.sources[mapping.source.source].text.len());
|
|
}
|
|
}
|
|
let mut harness = assertions
|
|
.unwrap_or("")
|
|
.replace(
|
|
"@allocation_counter@",
|
|
include_str!("native/allocation.rs.txt"),
|
|
)
|
|
.replace(
|
|
"@source_path@",
|
|
&format!("{:?}", project.sources[0].path.to_string_lossy()),
|
|
);
|
|
for name in program
|
|
.functions
|
|
.iter()
|
|
.map(|f| &f.signature.name)
|
|
.chain(program.definitions.iter().map(|d| &d.name))
|
|
{
|
|
let short = name.rsplit('.').next().unwrap();
|
|
harness = harness.replace(&format!("@{short}@"), &emit::symbol(name));
|
|
}
|
|
generated
|
|
.get_mut("main.rs")
|
|
.unwrap()
|
|
.text
|
|
.push_str(&if assertions.is_some() {
|
|
format!("fn main() {{\n{harness}\n}}")
|
|
} else {
|
|
emit::application::emit(&program, &decisions)
|
|
});
|
|
let artifact =
|
|
dever::compiler::build::compile(&directory, &program, &generated, profile, &project, None)
|
|
.unwrap();
|
|
let binary = directory.join("program");
|
|
artifact.publish(&binary).unwrap();
|
|
binary
|
|
}
|
|
|
|
#[test]
|
|
fn generated_values_rules_failures_and_faults_execute_natively() {
|
|
for profile in [Profile::Dev, Profile::Release] {
|
|
let binary = build(
|
|
"semantics",
|
|
include_str!("native/semantics.dever"),
|
|
Some(include_str!("native/semantics.rs.txt")),
|
|
profile,
|
|
);
|
|
let output = Command::new(binary).output().unwrap();
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn command_wire_validates_once_and_preserves_protocol() {
|
|
use dever_runtime::wire::serde_json::{self, json};
|
|
let binary = build(
|
|
"commands",
|
|
include_str!("native/commands.dever"),
|
|
None,
|
|
Profile::Dev,
|
|
);
|
|
let run = |name: &str, input: &str| Command::new(&binary).args([name, input]).output().unwrap();
|
|
let valid = r#"{"input":{"name":"中文","price":"0.1","quantity":3,"state":"Active"}}"#;
|
|
let output = run("main.main.bill", valid);
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
assert_eq!(
|
|
serde_json::from_slice::<serde_json::Value>(&output.stdout).unwrap(),
|
|
json!({"code":0,"message":"ok","data":{"total":"0.3","item":{"name":"中文","price":"0.1","quantity":3,"state":"Active","note":null}}})
|
|
);
|
|
for input in [
|
|
"[]".into(),
|
|
"{}".into(),
|
|
valid.replace("3,", "0,"),
|
|
valid.replace("\"0.1\"", "0.1"),
|
|
valid.replace("\"0.1\"", "\"NaN\""),
|
|
valid.replace("\"Active\"", "\"启用\""),
|
|
valid.replace("\"Active\"", "{\"Active\":null}"),
|
|
valid.replace("\"name\":", "\"name\":\"duplicate\",\"name\":"),
|
|
valid.replace("\"name\":", "\"extra\":true,\"name\":"),
|
|
format!("{valid} false"),
|
|
] {
|
|
let output = run("main.main.bill", &input);
|
|
assert!(!output.status.success(), "accepted: {input}");
|
|
assert!(output.stdout.is_empty(), "failure cannot emit success");
|
|
}
|
|
let output = run("main.main.guarded", r#"{"value":0}"#);
|
|
assert!(!output.status.success());
|
|
assert!(String::from_utf8_lossy(&output.stderr).contains("Invalid"));
|
|
let output = run("main.main.overflow", r#"{"value":9223372036854775807}"#);
|
|
assert!(!output.status.success());
|
|
assert!(String::from_utf8_lossy(&output.stderr).contains("commands.dever:"));
|
|
let output = run("main.main.nonfinite", r#"{"value":0}"#);
|
|
assert!(!output.status.success());
|
|
assert!(String::from_utf8_lossy(&output.stderr).contains("non-finite"));
|
|
assert!(!run("unknown", "{}").status.success());
|
|
}
|
|
|
|
#[path = "native/settings.rs"]
|
|
mod settings;
|
|
|
|
#[path = "native/crypto.rs"]
|
|
mod crypto;
|
|
|
|
#[path = "native/tasks.rs"]
|
|
mod tasks;
|
|
|
|
#[path = "native/parallel.rs"]
|
|
mod parallel;
|