Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,106 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn blocking_password_command_preserves_mismatch_error_and_secret_boundaries() {
|
||||
let (project, _) = checked(
|
||||
"flow verify(password: Secret, hash: Text) (valid: Bool)\n| step | kind | do |\n| valid | calc | blocking(dever.crypto.passwordVerify(password, hash)) |\ncmd verify = verify\n",
|
||||
"blocking-command",
|
||||
);
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
let binary = project.sources[0].path.parent().unwrap().join("verify");
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&[
|
||||
"build",
|
||||
path,
|
||||
"--profile",
|
||||
"dev",
|
||||
"--output",
|
||||
binary.to_str().unwrap(),
|
||||
]));
|
||||
let password = "public-test-only-password";
|
||||
let secret = dever_runtime::value::secret::Secret::from_input(password.as_bytes().to_vec());
|
||||
let hash = dever_runtime::crypto::password_hash(&secret).unwrap();
|
||||
for (input, valid) in [(password, true), ("different-test-password", false)] {
|
||||
let output = Command::new(&binary)
|
||||
.args([
|
||||
"main.main.verify",
|
||||
&serde_json::json!({"password": input, "hash": hash}).to_string(),
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
let value: serde_json::Value = serde_json::from_str(&success(output)).unwrap();
|
||||
assert_eq!(value["data"], valid);
|
||||
}
|
||||
let output = Command::new(&binary)
|
||||
.args([
|
||||
"main.main.verify",
|
||||
&serde_json::json!({"password": password, "hash": "invalid"}).to_string(),
|
||||
])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!output.status.success() && output.stdout.is_empty());
|
||||
let error = String::from_utf8(output.stderr).unwrap();
|
||||
assert!(error.contains("VerifyResult.Failed"));
|
||||
assert!(!error.contains(password));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocking_requires_a_known_synchronous_target_and_cannot_hide_in_async_code() {
|
||||
let prefix = r#"
|
||||
identity(value: Int) (out: Int) { out = value }
|
||||
flow verify(password: Secret, hash: Text) (out: Bool)
|
||||
| step | kind | do |
|
||||
| out | flow | dever.crypto.passwordVerify(password, hash) |
|
||||
flow offload(password: Secret, hash: Text) (out: Bool)
|
||||
| step | kind | do |
|
||||
| out | calc | blocking(verify(password, hash)) |
|
||||
"#;
|
||||
let cases = [
|
||||
"flow apply(action: handler(value: Int) (out: Int)) (out: Int)\n| step | kind | do |\n| out | calc | blocking(action(1)) |\nflow bad() (out: Int)\n| step | kind | do |\n| out | flow | apply(identity) |",
|
||||
"flow bad() (out: Int)\n| step | kind | do |\n| out | calc | blocking(identity(1)) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| - | calc | blocking(dever.time.sleep(0)) |",
|
||||
"flow bad() (out: DateTime)\n| step | kind | do |\n| out | calc | blocking(dever.time.now()) |",
|
||||
"flow bad(password: Secret, hash: Text) (out: Bool)\n| step | kind | do |\n| out | calc | parallel(verify(password, hash)) |",
|
||||
"flow bad(password: Secret, hash: Text) (out: Bool)\n| step | kind | do |\n| out | calc | blocking(offload(password, hash)) |",
|
||||
"bad(password: Secret, hash: Text) (out: Bool) { out = blocking(verify(password, hash)) }",
|
||||
"flow bad(password: Secret, hash: Text) (out: Bool)\n| step | kind | do |\n| - | flow | dever.time.sleep(0) |\n| out | flow | verify(password, hash) |",
|
||||
"flow bad(password: Secret, hash: Text) (out: Bool)\n| step | kind | parallel | do |\n| - | flow | work | dever.time.sleep(0) |\n| out | flow | work | verify(password, hash) |",
|
||||
"flow bad(password: Secret, hash: Text) (out: Bool)\n| step | kind | do |\n| out | calc | wait(blocking(verify(password, hash))) |",
|
||||
];
|
||||
for (index, source) in cases.iter().enumerate() {
|
||||
let path = directory(&format!("blocking-invalid-{index}")).join("app.dever");
|
||||
fs::write(&path, format!("{prefix}{source}\n")).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let error = program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.expect(source);
|
||||
assert!(matches!(error.code, "C013" | "C014"), "{index}: {error:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocking_propagates_effects_formats_and_navigates_real_password_operations() {
|
||||
let (project, program) = checked(include_str!("../../native/blocking.dever"), "blocking-docs");
|
||||
let target = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "dever.crypto.app.passwordVerify")
|
||||
.unwrap();
|
||||
assert!(target.effects.blocking && !target.effects.suspends);
|
||||
let entry = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.verify")
|
||||
.unwrap();
|
||||
assert!(entry.effects.tasks && entry.effects.suspends && !entry.effects.blocking);
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.verify"), 1, true).unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
assert!(
|
||||
text.contains("线程池执行同步阻塞调用") && text.contains("dever.crypto.app.passwordVerify")
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn channels_reject_hidden_effects_wrong_payloads_and_wire_escape() {
|
||||
let cases = [
|
||||
"bad() (out: Channel<Int>) { out = channel<Int>(1) }",
|
||||
"flow bad() ()\n| step | kind | do |\n| - | calc | send(channel<Int>(1), 1) |",
|
||||
"flow bad(channel: Channel<Int>) ()\n| step | kind | do |\n| - | calc | send(channel, true) |",
|
||||
"flow bad() (out: Int?)\n| step | kind | do |\n| out | calc | receive(1) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| - | calc | close(1) |",
|
||||
"bad(a: Channel<Int>, b: Channel<Int>) (out: Bool) { out = a == b }",
|
||||
"type Box { value: Channel<Int> }\nbad(a: Box, b: Box) (out: Bool) { out = a == b }",
|
||||
"bad(a: List<Channel<Int>>, b: Channel<Int>) (out: Bool) { out = contains(a, b) }",
|
||||
"flow bad() (out: Channel<Int>)\n| step | kind | do |\n| out | calc | channel<Int>(1) |\ncmd bad = bad",
|
||||
"flow bad(channel: Channel<Int>) ()\n| step | kind | do |\n| - | calc | close(channel) |\ncmd bad = bad",
|
||||
"type Outcome { Ok, error Bad }\nflow bad(channel: Channel<Outcome>) ()\n| step | kind | do |\n| error | calc | Outcome.Bad |\n| - | calc | send(channel, error) |",
|
||||
"rule bad(input: Int) (out: Channel<Int>)\n| input | out |\n| - | channel<Int>(1) |",
|
||||
];
|
||||
for (index, source) in cases.iter().enumerate() {
|
||||
let path = directory(&format!("channel-invalid-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(
|
||||
project.diagnostics.is_empty(),
|
||||
"{index}: {:?}",
|
||||
project.diagnostics
|
||||
);
|
||||
let error = program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.expect(source);
|
||||
assert!(
|
||||
matches!(error.code, "C005" | "C012" | "C013" | "C014"),
|
||||
"{index}: {error:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn channels_format_and_show_queue_close_and_cancel_in_workflows() {
|
||||
let (project, program) = checked(include_str!("../../native/channel.dever"), "channel-docs");
|
||||
let entry = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.source_name.ends_with(".produce"))
|
||||
.unwrap();
|
||||
assert!(entry.effects.channel && entry.effects.suspends && !entry.effects.tasks);
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
success(cli(&["check", path]));
|
||||
let index = dever::compiler::inspect::analyze(&project, Some("main.main.app.produce"), 1, true)
|
||||
.unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
assert!(text.contains("等待空位,移交消息") && text.contains("关闭通道并唤醒等待者"));
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn token_random_effects_follow_actual_calls_and_navigate_bundled_source() {
|
||||
let (project, program) = checked(include_str!("../../native/token.dever"), "token-effects");
|
||||
for name in [
|
||||
"dever.crypto.app.token",
|
||||
"main.main.app.generate",
|
||||
"main.main.app.handled",
|
||||
] {
|
||||
let function = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.source_name == name)
|
||||
.unwrap();
|
||||
assert!(function.effects.random && !function.effects.pure());
|
||||
assert!(!function.effects.blocking && !function.effects.suspends);
|
||||
}
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.generate"), 2, true)
|
||||
.unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
assert!(text.contains("效果") && text.contains("dever.crypto.app.token"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tokens_cannot_hide_random_effects_or_escape_secret_and_failure_boundaries() {
|
||||
let cases = [
|
||||
"bad() (out: Secret) { out = dever.crypto.token(32) }",
|
||||
"flow bad() (out: Bytes)\n| step | kind | do |\n| out | calc | dever.crypto.hmacSha256(dever.crypto.token(32), dever.bytes.fromText(\"message\")) |",
|
||||
"flow bad() (out: Secret)\n| step | kind | do |\n| out | calc | parallel(dever.crypto.token(32)) |",
|
||||
"flow bad() (out: Secret)\n| step | kind | do |\n| out | calc | blocking(dever.crypto.token(32)) |",
|
||||
"flow bad() (out: Secret)\n| step | kind | do |\n| out | flow | dever.crypto.token(32) |\ncmd bad = bad",
|
||||
"flow bad() (out: Text)\n| step | kind | do |\n| key | flow | dever.crypto.token(32) |\n| out | calc | dever.json.encode(key) |",
|
||||
"type Attempt { Ready(value: Secret), error Failed(value: dever.crypto.TokenResult) }\nflow bad() (out: Int)\n| step | kind | do |\n| value: Attempt | flow | result(dever.crypto.token(15)) |\n| out | calc | 1 |",
|
||||
"apply(create: handler(bytes: Int) (value: Secret)) (out: Secret) { out = create(32) }\nflow bad() (out: Secret)\n| step | kind | do |\n| out | calc | apply(dever.crypto.token) |",
|
||||
];
|
||||
for (index, source) in cases.iter().enumerate() {
|
||||
let path = directory(&format!("invalid-token-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(!errors.is_empty(), "{index}: {source}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,382 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn streaming_boundaries_do_not_hide_network_effects_or_discard_failures() {
|
||||
for (index, source) in [
|
||||
"flow bad(client: dever.system.HttpClient, body: AsyncStream<dever.io.ReadEvent>) ()\n| step | kind | do |\n| response | flow | dever.http.openStream(client, dever.http.StreamRequest { method = \"POST\", target = \"/upload\", headers = [], body = body }) |\n| - | calc | close(response.body) |",
|
||||
"bad(socket: dever.system.WebSocket) (state: dever.websocket.ReceiveState) { state = dever.websocket.receive(socket) }",
|
||||
"flow bad(socket: dever.system.WebSocket) ()\n| step | kind | do |\n| ignored | flow | result(dever.websocket.receive(socket)) |",
|
||||
"loop bad(events: AsyncStream<dever.websocket.MessageEvent>) (count: Int)\n| each event in events | result |\n| count | count |",
|
||||
"same(left: dever.system.WebSocket, right: dever.system.WebSocket) (out: Bool) { out = left == right }",
|
||||
].iter().enumerate() {
|
||||
let path = directory(&format!("streaming-boundary-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let diagnostics = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(diagnostics.iter().any(|error| matches!(error.code, "C005" | "C012" | "C013")), "{index}: {diagnostics:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_pool_is_a_shared_network_resource_with_explicit_effects() {
|
||||
let (project, program) = checked(
|
||||
include_str!("../../native/http_pool.dever"),
|
||||
"http-pool-effects",
|
||||
);
|
||||
assert!(program.uses_http() && program.uses_network());
|
||||
let open = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.open")
|
||||
.unwrap();
|
||||
assert!(open.effects.network && open.effects.tasks && open.effects.suspends);
|
||||
let kind = &open.signature.outputs[0].kind;
|
||||
assert_eq!(*kind, dever::compiler::model::Type::HttpClient);
|
||||
assert!(!program.comparable(kind));
|
||||
let fetch = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.fetch")
|
||||
.unwrap();
|
||||
assert!(fetch.effects.network && fetch.effects.suspends && !fetch.effects.tasks);
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.fetch"), 1, true).unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
assert!(text.contains("dever.http.app.request") && text.contains("挂起"));
|
||||
let download = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.download")
|
||||
.unwrap();
|
||||
assert!(download.effects.network && download.effects.suspends && download.effects.stream);
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.download"), 1, true)
|
||||
.unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
assert!(text.contains("dever.http.app.open") && text.contains("挂起"));
|
||||
let path = directory("http-download-unhandled-body").join("app.dever");
|
||||
fs::write(&path, "loop bad(body: AsyncStream<dever.io.ReadEvent>) (count: Int)\n| each event in body | result |\n| count | count |\n").unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(errors.iter().any(|e| e.code == "C012"), "{errors:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_server_binds_handlers_preserves_effects_and_rejects_unsafe_callbacks() {
|
||||
let source = include_str!("../../native/http_server.dever");
|
||||
let (project, program) = checked(source, "http-server-effects");
|
||||
let start = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.startTasks")
|
||||
.unwrap();
|
||||
assert!(
|
||||
start.effects.network
|
||||
&& start.effects.tasks
|
||||
&& start.effects.suspends
|
||||
&& start.effects.clock
|
||||
);
|
||||
assert!(!start.effects.blocking);
|
||||
let (_, streaming) = checked(
|
||||
&format!(
|
||||
"{source}\nloop firstTick(ticks: AsyncStream<Int>) (tick: Int?)\n| each value in ticks | result |\n| tick | first value |\nflow streamRoute(request: dever.http.Request) (response: dever.http.Response)\n| step | kind | do |\n| ticks | flow | dever.task.ticks(1) |\n| tick | loop | firstTick(ticks) |\n| response | calc | echo(request) |\nflow streamServer(listener: dever.system.Listener) ()\n| step | kind | do |\n| - | flow | dever.http.serve(streamRoute, listener, dever.http.defaultLimits()) |"
|
||||
),
|
||||
"http-stream-handler-effects",
|
||||
);
|
||||
assert!(
|
||||
streaming
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.streamServer")
|
||||
.unwrap()
|
||||
.effects
|
||||
.stream
|
||||
);
|
||||
let config = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.startConfig")
|
||||
.unwrap();
|
||||
assert!(config.uses_settings);
|
||||
let start_failure = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.startFailure")
|
||||
.unwrap();
|
||||
assert!(
|
||||
start_failure
|
||||
.failures
|
||||
.iter()
|
||||
.all(|error| !matches!(error, dever::compiler::model::Failure::Api(_)))
|
||||
);
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.startTasks"), 2, true)
|
||||
.unwrap();
|
||||
let index = serde_json::to_string(&index).unwrap();
|
||||
assert!(
|
||||
index.contains("请求处理")
|
||||
&& index.contains("main.main.app.scheduledRoute")
|
||||
&& index.contains("监听器关闭")
|
||||
);
|
||||
for (index, route) in [
|
||||
"bad(request: dever.http.Request) (out: dever.http.Response) { out = echo(request) }",
|
||||
"bad(request: Text) (response: dever.http.Response) { response = dever.http.Response { status = 200, headers = [], body = dever.bytes.fromText(request) } }",
|
||||
"flow bad(request: dever.http.Request) (response: dever.http.Response)\n| step | kind | do |\n| - | flow | dever.io.println(request.target) |\n| response | calc | echo(request) |",
|
||||
"flow bad(request: dever.http.Request) (response: dever.http.Response)\n| step | kind | do |\n| - | calc | blocking(dever.io.println(request.target)) |\n| response | calc | echo(request) |",
|
||||
"flow bad(request: dever.http.Request) (response: dever.http.Response)\n| step | kind | do |\n| file | flow | dever.io.open(request.target) |
|
||||
| - | flow | dever.io.close(file) |\n| response | calc | echo(request) |",
|
||||
].iter().enumerate() {
|
||||
let path = directory(&format!("http-invalid-handler-{index}")).join("app.dever");
|
||||
fs::write(&path, format!("{source}\n{route}\nflow badServer(listener: dever.system.Listener) ()\n| step | kind | do |\n| - | flow | dever.http.serve(bad, listener, dever.http.defaultLimits()) |\n")).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(errors.iter().any(|e| matches!(e.code, "C005" | "C013")), "{index}: {errors:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_send_keeps_network_effects_failures_and_navigation() {
|
||||
let (_, plain) = checked("main() (out: Int) { out = 1 }", "without-http");
|
||||
assert!(!plain.uses_http());
|
||||
let (project, program) = checked(include_str!("../../native/http.dever"), "http-send");
|
||||
assert!(program.uses_http());
|
||||
let fetch = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.fetch")
|
||||
.unwrap();
|
||||
assert!(fetch.effects.network && fetch.effects.suspends && !fetch.effects.blocking);
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.fetch"), 1, true).unwrap();
|
||||
assert!(
|
||||
serde_json::to_string(&index)
|
||||
.unwrap()
|
||||
.contains("dever.http.app.send")
|
||||
);
|
||||
for (index, source) in [
|
||||
"bad(request: dever.http.Request) (out: dever.http.Response) { out = dever.http.send(\"127.0.0.1\", 80, request, dever.http.defaultLimits()) }",
|
||||
"flow bad(request: dever.http.Request) ()\n| step | kind | do |\n| ignored | flow | result(dever.http.send(\"127.0.0.1\", 80, request, dever.http.defaultLimits())) |",
|
||||
].iter().enumerate() {
|
||||
let path = directory(&format!("invalid-http-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
assert!(!program::analyze(&project.declarations, &project.modules).diagnostics.is_empty(), "{source}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tls_configuration_is_pure_shared_opaque_and_navigable() {
|
||||
let (_, plain) = checked("main() (out: Int) { out = 1 }", "without-tls");
|
||||
assert!(!plain.uses_tls());
|
||||
let (_, typed) = checked(
|
||||
"type Config { tls: dever.system.ClientTls? }",
|
||||
"tls-type-only",
|
||||
);
|
||||
assert!(typed.uses_tls());
|
||||
let (project, program) = checked(include_str!("../../native/tls.dever"), "tls-config");
|
||||
assert!(program.uses_tls());
|
||||
for name in ["clientTls", "serverTls", "defaultTls"] {
|
||||
let function = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == format!("main.main.app.{name}"))
|
||||
.unwrap();
|
||||
assert!(
|
||||
!function.effects.network && !function.effects.suspends && !function.effects.blocking
|
||||
);
|
||||
}
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.clientTls"), 1, true)
|
||||
.unwrap();
|
||||
assert!(
|
||||
serde_json::to_string(&index)
|
||||
.unwrap()
|
||||
.contains("dever.tls.app.client")
|
||||
);
|
||||
for (index, source) in [
|
||||
"same(a: dever.system.ClientTls, b: dever.system.ClientTls) (out: Bool) { out = a == b }",
|
||||
"setting config: dever.system.ClientTls\nmain() (out: Int) { out = 1 }",
|
||||
"type Config { tls: dever.system.ServerTls }\nencode(value: Config) (out: Json) { out = dever.json.encode(value) }",
|
||||
"main() (out: dever.system.ClientTls) { out = dever.tls.system() }\ncmd config = main",
|
||||
"ignore(ca: Bytes) () { ignored = result(dever.tls.client(ca)) }",
|
||||
].iter().enumerate() {
|
||||
let path = directory(&format!("invalid-tls-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
assert!(!program::analyze(&project.declarations, &project.modules).diagnostics.is_empty(), "{source}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn network_effects_resources_and_source_navigation_follow_calls() {
|
||||
let (_, plain) = checked("main() (out: Int) { out = 1 }", "without-network-driver");
|
||||
assert!(!plain.uses_network());
|
||||
let (_, typed) = checked(
|
||||
"type Holder { socket: dever.system.Socket? }\nmain() (out: Int) { out = 1 }",
|
||||
"network-type-only",
|
||||
);
|
||||
assert!(typed.uses_network());
|
||||
let (_, minimal) = checked(
|
||||
"flow disconnect(socket: dever.system.Socket) ()\n| step | kind | do |\n| - | flow | dever.net.close(socket) |",
|
||||
"network-transitive-library",
|
||||
);
|
||||
assert!(
|
||||
minimal
|
||||
.definitions
|
||||
.iter()
|
||||
.any(|value| value.name == "dever.io.app.CloseResult")
|
||||
);
|
||||
let (project, program) = checked(include_str!("../../native/net.dever"), "network-effects");
|
||||
for name in ["roundTrip", "dialTimed", "readSome", "send"] {
|
||||
let function = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == format!("main.main.app.{name}"))
|
||||
.unwrap();
|
||||
assert!(
|
||||
function.effects.network && function.effects.suspends && !function.effects.blocking,
|
||||
"{name}"
|
||||
);
|
||||
}
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.roundTrip"), 1, true)
|
||||
.unwrap();
|
||||
let index = serde_json::to_string(&index).unwrap();
|
||||
for name in [
|
||||
"listen",
|
||||
"port",
|
||||
"connect",
|
||||
"accept",
|
||||
"chunks",
|
||||
"close",
|
||||
"closeListener",
|
||||
] {
|
||||
assert!(index.contains(&format!("dever.net.app.{name}")), "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn network_cannot_escape_effects_wire_or_error_obligations() {
|
||||
for (index, source) in [
|
||||
"bad(socket: dever.system.Socket) () { dever.net.close(socket) }",
|
||||
"flow bad(socket: dever.system.Socket) ()\n| step | kind | do |\n| - | calc | parallel(dever.net.close(socket)) |",
|
||||
"flow bad(host: Text) (out: dever.system.Listener)\n| step | kind | do |\n| out | flow | dever.net.listen(host, 0) |\ncmd bad = bad",
|
||||
"same(a: dever.system.Socket, b: dever.system.Socket) (out: Bool) { out = a == b }",
|
||||
"type Box { value: dever.system.Listener }\nserialize(value: Box) (out: Json) { out = dever.json.encode(value) }",
|
||||
"flow bad(listener: dever.system.Listener) ()\n| step | kind | do |\n| events | flow | dever.net.connections(listener) |",
|
||||
"flow bad(socket: dever.system.Socket) (out: dever.io.ReadState)\n| step | kind | do |\n| out | flow | dever.net.read(socket, length(dever.process.arguments())) |",
|
||||
].iter().enumerate() {
|
||||
let path = directory(&format!("invalid-network-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(!errors.is_empty(), "{index}: {source}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn process_arguments_are_explicit_effects_with_bundled_navigation() {
|
||||
let (project, program) = checked(
|
||||
include_str!("../../native/arguments.dever"),
|
||||
"arguments-effects",
|
||||
);
|
||||
let read = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.readArgs")
|
||||
.unwrap();
|
||||
assert!(read.effects.process && !read.effects.blocking && !read.effects.suspends);
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.readArgs"), 1, true)
|
||||
.unwrap();
|
||||
assert!(
|
||||
serde_json::to_string(&index)
|
||||
.unwrap()
|
||||
.contains("dever.process.app.arguments")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stdout_has_an_explicit_blocking_output_effect_and_real_navigation() {
|
||||
let (project, program) = checked(include_str!("../../native/stdout.dever"), "stdout-effects");
|
||||
let print = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.printLine")
|
||||
.unwrap();
|
||||
assert!(print.effects.output && print.effects.blocking && !print.effects.file);
|
||||
let offload = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.offload")
|
||||
.unwrap();
|
||||
assert!(offload.effects.output && offload.effects.suspends && !offload.effects.blocking);
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.printLine"), 1, true)
|
||||
.unwrap();
|
||||
assert!(
|
||||
serde_json::to_string(&index)
|
||||
.unwrap()
|
||||
.contains("dever.io.app.println")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn file_effects_and_workflow_navigation_follow_the_real_bundled_functions() {
|
||||
let (project, program) = checked(include_str!("../../native/file.dever"), "file-effects");
|
||||
let read = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.readSome")
|
||||
.unwrap();
|
||||
assert!(read.effects.file && read.effects.blocking && !read.effects.suspends);
|
||||
let offload = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.offload")
|
||||
.unwrap();
|
||||
assert!(offload.effects.tasks && offload.effects.suspends && !offload.effects.blocking);
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
let index = dever::compiler::inspect::analyze(&project, Some("main.main.app.offload"), 2, true)
|
||||
.unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
assert!(text.contains("线程池执行同步阻塞调用") && text.contains("dever.io.app.read"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn files_cannot_bypass_effects_resource_comparison_or_wire_boundaries() {
|
||||
let cases = [
|
||||
"bad() (out: List<Text>) { out = dever.process.arguments() }",
|
||||
"flow bad() (out: List<Text>)\n| step | kind | do |\n| out | calc | parallel(dever.process.arguments()) |",
|
||||
"bad(text: Text) () { dever.io.println(text) }",
|
||||
"flow bad(text: Text) ()\n| step | kind | do |\n| - | flow | dever.time.sleep(0) |\n| - | flow | dever.io.println(text) |",
|
||||
"flow bad(text: Text) ()\n| step | kind | do |\n| - | calc | parallel(dever.io.println(text)) |",
|
||||
"bad(path: Text) (out: dever.system.File) { out = dever.io.open(path) }",
|
||||
"flow bad(path: Text) (out: dever.io.ReadState)\n| step | kind | do |\n| out | flow | dever.io.read(dever.io.open(path), 1) |",
|
||||
"flow bad(path: Text) (out: dever.system.File)\n| step | kind | do |\n| - | flow | dever.time.sleep(0) |\n| out | flow | dever.io.open(path) |",
|
||||
"flow bad(path: Text) (out: dever.system.File)\n| step | kind | do |\n| out | calc | parallel(dever.io.open(path)) |",
|
||||
"flow bad(path: Text) (out: dever.system.File)\n| step | kind | do |\n| out | flow | dever.io.open(path) |\ncmd bad = bad",
|
||||
"type Holder { file: dever.system.File }\nflow bad(file: dever.system.File) (out: Text)\n| step | kind | do |\n| out | calc | dever.json.encode(Holder { file = file }) |",
|
||||
"same(left: dever.system.File, right: dever.system.File) (out: Bool) { out = left == right }",
|
||||
"type Holder { file: dever.system.File }\nsame(left: Holder, right: Holder) (out: Bool) { out = left == right }",
|
||||
];
|
||||
for (index, source) in cases.iter().enumerate() {
|
||||
let path = directory(&format!("invalid-file-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(!errors.is_empty(), "{index}: {source}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
//! 检查真实项目和实际 handler 实例;拒绝隐藏效果,不以库名代替纯度。
|
||||
use super::*;
|
||||
|
||||
fn checked(source: &str, name: &str) -> (Project, dever::compiler::model::Program) {
|
||||
let root = directory(name);
|
||||
let path = root.join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
analyze(&path)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effects_follow_tables_and_bound_handlers_without_making_pure_calls_async() {
|
||||
let (project, program) = checked(include_str!("../../native/tasks.dever"), "effects");
|
||||
for name in [
|
||||
"delay",
|
||||
"chosen",
|
||||
"eachDelay",
|
||||
"next",
|
||||
"walked",
|
||||
"apply",
|
||||
"handled",
|
||||
"captured",
|
||||
"borrowed",
|
||||
"accumulated",
|
||||
"firstDelay",
|
||||
"walkFrom",
|
||||
] {
|
||||
let function = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|function| function.signature.source_name == format!("main.main.app.{name}"))
|
||||
.unwrap();
|
||||
assert!(
|
||||
function.effects.clock && function.effects.suspends,
|
||||
"{name}"
|
||||
);
|
||||
}
|
||||
for name in ["identity", "advance", "resolve", "measureText"] {
|
||||
let function = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|function| function.signature.source_name == format!("main.main.app.{name}"))
|
||||
.unwrap();
|
||||
assert!(function.effects.pure(), "{name}");
|
||||
}
|
||||
let now = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|function| function.signature.name == "dever.time.app.now")
|
||||
.unwrap();
|
||||
assert!(now.effects.clock && !now.effects.suspends);
|
||||
let generated = emit::emit(&program, &ownership::analyze(&program), &project.sources);
|
||||
let text = generated
|
||||
.values()
|
||||
.map(|file| file.text.as_str())
|
||||
.collect::<String>();
|
||||
assert!(text.contains("pub async fn d_4_main_4_main_3_app_5_delay"));
|
||||
assert!(text.contains("pub fn d_4_main_4_main_3_app_8_identity"));
|
||||
assert!(text.contains("pub async fn d_4_main_4_main_3_app_8_borrowed(v0: &str)"));
|
||||
assert!(!text.contains("tokio::spawn") && !text.contains("Box::pin"));
|
||||
let index = dever::compiler::inspect::analyze(&project, Some("main.main.app.handled"), 3, true)
|
||||
.unwrap();
|
||||
let data = serde_json::to_string(&index).unwrap();
|
||||
assert!(data.contains("挂起") && data.contains("transform = main.main.app.delay"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hidden_effects_are_rejected_in_functions_arguments_guards_and_filters() {
|
||||
let prefix = r#"
|
||||
flow tick() (out: Bool)
|
||||
| step | kind | do |
|
||||
| - | flow | dever.time.sleep(0) |
|
||||
| out | calc | true |
|
||||
identity(value: Bool) (out: Bool) { out = value }
|
||||
type Attempt { Done, error Failed(value: dever.time.SleepResult) }
|
||||
rule finish(value: Attempt) (out: Bool) recover("report whether the delay was accepted")
|
||||
| value | out |
|
||||
| Done | true |
|
||||
| Failed(error) | false |
|
||||
flow safeTick() (out: Bool)
|
||||
| step | kind | do |
|
||||
| attempt: Attempt | flow | result(dever.time.sleep(0)) |
|
||||
| out | rule | finish(attempt) |
|
||||
|
||||
"#;
|
||||
let cases = [
|
||||
"bad() (out: Int) { out = dever.time.unixMillis() }",
|
||||
"bad() (out: Int) { out = dever.time.monotonicNanos() }",
|
||||
"bad() (out: Bool) { out = tick() }",
|
||||
"flow bad() (out: Bool)\n| step | kind | do |\n| out | calc | identity(tick()) |",
|
||||
"flow bad() (out: Bool)\n| step | kind | do |\n| out | calc | tick() and true |",
|
||||
"flow bad() ()\n| step | kind | do |\n| - | check | tick() else Invalid |",
|
||||
"flow bad() (out: List<Bool>)\n| step | kind | do |\n| out | calc | [tick()] |",
|
||||
"rule bad(value: Bool) (out: Bool)\n| value | out |\n| - | identity(tick()) |",
|
||||
"loop bad(values: List<Int>) (out: Int)\n| each value in values | safeTick() | result |\n| out | true | count |",
|
||||
"apply(work: handler() (out: Bool)) (out: Bool) { out = work() }\nflow bad() (out: Bool)\n| step | kind | do |\n| out | calc | apply(tick) |",
|
||||
"loop bad(values: List<Int>) (out: Int)\n| each value in values | safeTick() | result |\n| out | - | count |",
|
||||
];
|
||||
let root = directory("hidden-effects");
|
||||
let path = root.join("app.dever");
|
||||
for source in cases {
|
||||
fs::write(&path, format!("{prefix}\n{source}")).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(
|
||||
project.diagnostics.is_empty(),
|
||||
"{:?}: {source}",
|
||||
project.diagnostics
|
||||
);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(
|
||||
errors.iter().any(|error| error.code == "C013"),
|
||||
"{errors:?}: {source}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn domain_cannot_gain_clock_effects_through_standard_library_or_rule() {
|
||||
let root = directory("domain-effects");
|
||||
let domain = root.join("module/demo/time");
|
||||
fs::create_dir_all(&domain).unwrap();
|
||||
fs::write(
|
||||
domain.join("domain.dever"),
|
||||
"rule current(flag: Bool) (out: DateTime)\n| flag | out |\n| - | dever.time.now() |\n",
|
||||
)
|
||||
.unwrap();
|
||||
let project = Project::load(&root).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(
|
||||
errors.iter().any(|error| error.code == "C013"),
|
||||
"{errors:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn async_standalone_and_cmd_entries_execute_and_propagate_errors() {
|
||||
let root = directory("async-entries");
|
||||
let path = root.join("app.dever");
|
||||
fs::write(&path, "flow main() (out: Int)\n| step | kind | do |\n| - | flow | dever.time.sleep(1) |\n| out | calc | 42 |\n").unwrap();
|
||||
success(cli(&["fmt", path.to_str().unwrap()]));
|
||||
assert_eq!(success(cli(&["run", path.to_str().unwrap()])).trim(), "42");
|
||||
fs::write(&path, "flow pause(millis: Int) (out: Int)\n| step | kind | do |\n| - | flow | dever.time.sleep(millis) |\n| out | calc | millis |\ncmd pause = app.pause\n").unwrap();
|
||||
let (project, program) = analyze(&path);
|
||||
let artifact = build::build(&project, &program, "main", Profile::Dev, None).unwrap();
|
||||
let invoke = |value| {
|
||||
Command::new(&artifact.binary)
|
||||
.args(["main.main.pause", value])
|
||||
.output()
|
||||
.unwrap()
|
||||
};
|
||||
let output = success(invoke(r#"{"millis":1}"#));
|
||||
assert_eq!(
|
||||
serde_json::from_str::<serde_json::Value>(&output).unwrap()["data"],
|
||||
1
|
||||
);
|
||||
let output = invoke(r#"{"millis":-1}"#);
|
||||
assert!(!output.status.success() && output.stdout.is_empty());
|
||||
assert!(String::from_utf8_lossy(&output.stderr).contains("SleepResult.Failed"));
|
||||
}
|
||||
|
||||
mod blocking;
|
||||
mod channel;
|
||||
mod crypto;
|
||||
mod file;
|
||||
mod parallel;
|
||||
mod stream;
|
||||
mod task;
|
||||
@@ -0,0 +1,207 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn concurrent_loops_round_trip_bound_capacity_and_expose_cleanup() {
|
||||
let (project, _) = checked(
|
||||
include_str!("../../native/concurrent.dever"),
|
||||
"concurrent-loop",
|
||||
);
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
let index = dever::compiler::inspect::analyze(&project, Some("main.main.app.ordered"), 1, true)
|
||||
.unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
for label in [
|
||||
"并发容量",
|
||||
"在途工作与待交付结果共用容量",
|
||||
"同一元素内逐行执行",
|
||||
"保留首个观察错误",
|
||||
"按输入顺序交付剩余结果",
|
||||
] {
|
||||
assert!(text.contains(label), "{label}");
|
||||
}
|
||||
let cases = [
|
||||
"loop bad(n: Int) (out: List<Int>)\n| each value in range(0, n) parallel n | result |\n| out | list value |",
|
||||
"loop bad() (out: List<Int>)\n| each value in [1] parallel 0 | result |\n| out | list value |",
|
||||
"loop bad() (out: List<Int>)\n| each value in [1] parallel 65537 | result |\n| out | list value |",
|
||||
"loop bad() (out: Int)\n| each value in [1] parallel 2 | result |\n| out | sum value |",
|
||||
"loop bad() (out: List<Int>)\n| each value in [1] parallel 2 | take | result |\n| out | 1 | list value |",
|
||||
"loop bad() (out: List<Int>)\n| each value in [1] parallel 2 | sort | result |\n| out | [value.asc] | list value |",
|
||||
"loop bad() (out: List<Int>)\n| each value in [1] parallel value | result |\n| out | list value |",
|
||||
"loop bad(file: dever.system.File) ()\n| each value in [1] parallel 2 | result |\n| - | do dever.io.close(file) |",
|
||||
"loop bad(values: Stream<Int>) (out: List<Int>)\n| each value in values parallel 2 | result |\n| out | list value |",
|
||||
];
|
||||
for (index, source) in cases.iter().enumerate() {
|
||||
let path = directory(&format!("invalid-concurrent-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(
|
||||
project.diagnostics.is_empty(),
|
||||
"{index}: {:?}",
|
||||
project.diagnostics
|
||||
);
|
||||
assert!(
|
||||
!program::analyze(&project.declarations, &project.modules)
|
||||
.diagnostics
|
||||
.is_empty(),
|
||||
"{index}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn catching_concurrent_failure_does_not_erase_cancelled_input_obligations() {
|
||||
let source = r#"
|
||||
type Attempt { Ok, error Bad }
|
||||
type Trouble { error Broken }
|
||||
type Caught { Done(values: List<Int>), error Failed(reason: Trouble) }
|
||||
rule resolve(value: Attempt) (out: Int) recover("report failure as -1")
|
||||
| value | out |
|
||||
| Ok | 1 |
|
||||
| Bad | -1 |
|
||||
flow work(value: Attempt, valid: Bool) (out: Int)
|
||||
| step | kind | do |
|
||||
| - | check | valid else Trouble.Broken |
|
||||
| out | rule | resolve(value) |
|
||||
loop jobs(values: List<Attempt>, valid: Bool) (out: List<Int>)
|
||||
| each value in values parallel 2 | result |
|
||||
| out | list work(value, valid) |
|
||||
flow forwarded(values: List<Attempt>, valid: Bool) (out: List<Int>)
|
||||
| step | kind | do |
|
||||
| out | loop | jobs(values, valid) |
|
||||
rule recoverCaught(value: Caught) (out: List<Int>) recover("report failed batch as empty")
|
||||
| value | out |
|
||||
| Done(values) | values |
|
||||
| Failed(reason) | [] |
|
||||
flow bad(values: List<Attempt>, valid: Bool) (out: List<Int>)
|
||||
| step | kind | do |
|
||||
| attempt: Caught | loop | result(jobs(values, valid)) |
|
||||
| out | rule | recoverCaught(attempt) |
|
||||
"#;
|
||||
for target in ["jobs", "forwarded"] {
|
||||
let source = source
|
||||
.replace(
|
||||
"result(jobs(values, valid))",
|
||||
&format!("result({target}(values, valid))"),
|
||||
)
|
||||
.replace(
|
||||
"| attempt: Caught | loop |",
|
||||
if target == "jobs" {
|
||||
"| attempt: Caught | loop |"
|
||||
} else {
|
||||
"| attempt: Caught | flow |"
|
||||
},
|
||||
);
|
||||
let path = directory(&format!("concurrent-cancelled-input-{target}")).join("app.dever");
|
||||
fs::write(&path, &source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(
|
||||
errors
|
||||
.iter()
|
||||
.any(|error| error.code == "C012"
|
||||
&& error.message.contains("failure is not handled")),
|
||||
"{errors:?}"
|
||||
);
|
||||
let valid = source
|
||||
.replace("| attempt: Caught |", "| out | ")
|
||||
.split("| out | rule | recoverCaught")
|
||||
.next()
|
||||
.unwrap()
|
||||
.replace("result(jobs(values, valid))", "jobs(values, valid)")
|
||||
.replace(
|
||||
"result(forwarded(values, valid))",
|
||||
"forwarded(values, valid)",
|
||||
);
|
||||
checked(&valid, &format!("concurrent-propagated-input-{target}"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parallel_groups_round_trip_check_dependencies_and_expose_fork_join_cleanup() {
|
||||
let source = include_str!("../../native/parallel.dever").replace(
|
||||
"| left | flow | read | delayed(11, 5) |",
|
||||
"| left | - | read | delayed(11, 5) |",
|
||||
);
|
||||
let (project, program) = checked(&source, "parallel");
|
||||
let source = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", source]));
|
||||
success(cli(&["fmt", source, "--check"]));
|
||||
let formatted = fs::read_to_string(source).unwrap();
|
||||
assert!(formatted.contains("parallel"));
|
||||
let row = formatted
|
||||
.lines()
|
||||
.find(|row| row.contains("delayed(11, 5)"))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
row.split('|').map(str::trim).collect::<Vec<_>>(),
|
||||
["", "left", "flow", "read", "delayed(11, 5)", ""]
|
||||
);
|
||||
let pure = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name.ends_with(".pureGroup"))
|
||||
.unwrap();
|
||||
assert!(pure.effects.pure());
|
||||
let generated = emit::emit(&program, &ownership::analyze(&program), &project.sources);
|
||||
let text = generated
|
||||
.values()
|
||||
.map(|file| file.text.as_str())
|
||||
.collect::<String>();
|
||||
assert!(text.contains("try_join!(biased;"));
|
||||
assert!(!text.contains("tokio::spawn") && !text.contains("Box::pin"));
|
||||
assert!(text.contains("pub fn d_4_main_4_main_3_app_9_pureGroup"));
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.together"), 0, true)
|
||||
.unwrap();
|
||||
let data = serde_json::to_value(index).unwrap();
|
||||
let flow = &data["flows"][0];
|
||||
for label in [
|
||||
"并发组 read",
|
||||
"等待本组全部步骤成功",
|
||||
"取消其余步骤,等待资源释放",
|
||||
] {
|
||||
assert!(
|
||||
flow["nodes"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.any(|node| node["label"] == label),
|
||||
"{label}"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
flow["edges"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|edge| edge["label"] == "并发推进")
|
||||
.count(),
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parallel_rejects_single_split_guard_dependent_and_hidden_effect_steps() {
|
||||
let root = directory("parallel-errors");
|
||||
let path = root.join("app.dever");
|
||||
let prefix = "identity(value: Int) (out: Int) { out = value }\nflow delayed(value: Int) (out: Int)\n| step | kind | do |\n| - | flow | dever.time.sleep(1) |\n| out | calc | value |\n";
|
||||
for rows in [
|
||||
"| a | calc | group | identity(value) |\n| out | calc | - | a |",
|
||||
"| a | calc | group | identity(value) |\n| b | calc | group | identity(value) |\n| c | calc | - | a |\n| d | calc | group | identity(value) |\n| out | calc | group | identity(value) |",
|
||||
"| - | check | group | value != 0 else Invalid |\n| out | calc | group | identity(value) |",
|
||||
"| a | calc | group | identity(value) |\n| out | calc | group | identity(a) |",
|
||||
"| a | calc | group | identity(delayed(value)) |\n| out | calc | group | identity(value) |",
|
||||
] {
|
||||
fs::write(&path, format!("{prefix}\nflow bad(value: Int) (out: Int)\n| step | kind | parallel | do |\n{rows}\n")).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(
|
||||
errors.iter().any(|error| error.code == "C013"),
|
||||
"{errors:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn synchronous_streams_preserve_lazy_errors_and_show_source_cleanup() {
|
||||
let (project, program) = checked(
|
||||
include_str!("../../native/sync_stream.dever"),
|
||||
"sync-stream-effects",
|
||||
);
|
||||
let first = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "main.main.app.first")
|
||||
.unwrap();
|
||||
assert!(
|
||||
first.effects.blocking
|
||||
&& first.effects.file
|
||||
&& first.effects.stream
|
||||
&& !first.effects.suspends
|
||||
);
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.first"), 1, true).unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
for expected in [
|
||||
"同步拉取文件流的下一元素",
|
||||
"关闭来源流,保留外部文件",
|
||||
"同一次拉取分发给各行",
|
||||
] {
|
||||
assert!(text.contains(expected), "{expected}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn synchronous_streams_cannot_cross_task_or_suspension_boundaries_or_discard_read_errors() {
|
||||
let cases = [
|
||||
"loop bad(values: Stream<dever.io.ReadEvent>) (out: Int)\n| each value in values | result |\n| out | count |",
|
||||
"flow bad(file: dever.system.File) (out: Stream<dever.io.ReadEvent>)\n| step | kind | do |\n| out | calc | blocking(dever.io.chunks(file, 2)) |",
|
||||
"flow consume(values: Stream<dever.io.ReadEvent>) ()\n| step | kind | do |\n| - | calc | close(values) |\nflow bad(values: Stream<dever.io.ReadEvent>) ()\n| step | kind | do |\n| task | calc | run(consume(values)) |\n| - | calc | wait(task) |",
|
||||
"flow bad(values: Stream<dever.io.ReadEvent>) ()\n| step | kind | do |\n| - | flow | dever.time.sleep(1) |\n| - | calc | close(values) |",
|
||||
"type Box { values: Stream<Int> }\nflow bad(box: Box) ()\n| step | kind | do |\n| - | flow | dever.time.sleep(1) |",
|
||||
"type Box { values: Stream<Int> }\nflow bad() (out: Channel<Box>)\n| step | kind | do |\n| out | calc | channel<Box>(2) |",
|
||||
"flow bad(values: List<Stream<Int>>) (out: AsyncStream<Stream<Int>>)\n| step | kind | do |\n| out | calc | stream(values) |",
|
||||
"flow bad(file: dever.system.File) (out: Stream<dever.io.ReadEvent>)\n| step | kind | do |\n| out | flow | dever.io.chunks(file, 2) |\ncmd bad = bad",
|
||||
];
|
||||
for (index, source) in cases.iter().enumerate() {
|
||||
let path = directory(&format!("invalid-sync-stream-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let errors = program::analyze(&project.declarations, &project.modules).diagnostics;
|
||||
assert!(!errors.is_empty(), "{index}: {source}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streams_reject_hidden_operations_error_discard_and_wire_escape() {
|
||||
let cases = [
|
||||
"bad() (out: AsyncStream<Int>) { out = stream([1]) }",
|
||||
"flow bad() (out: AsyncStream<Int>)\n| step | kind | do |\n| out | calc | stream(1) |",
|
||||
"flow bad() (out: AsyncStream<Int>)\n| step | kind | do |\n| out | calc | stream([]) |\ncmd bad = bad",
|
||||
"bad(a: AsyncStream<Int>, b: AsyncStream<Int>) (out: Bool) { out = a == b }",
|
||||
"type Box { values: AsyncStream<Int> }\nbad(a: Box, b: Box) (out: Bool) { out = a == b }",
|
||||
"flow bad() (out: AsyncStream<Int>)\n| step | kind | do |\n| out | calc | dever.system.taskTicks(1) |",
|
||||
"type Attempt { Ok, error Bad }\nflow bad(values: AsyncStream<Attempt>) ()\n| step | kind | do |\n| - | calc | close(values) |",
|
||||
"type Attempt { Ok, error Bad }\nloop bad(values: AsyncStream<Attempt>) (out: Int)\n| each value in values | result |\n| out | count |",
|
||||
"type Attempt { Ok, error Bad }\nloop bad(values: AsyncStream<Attempt>) (out: Attempt?)\n| each value in values | result |\n| out | first value |",
|
||||
"type Attempt { Ok, error Bad }\nflow bad() ()\n| step | kind | do |\n| values | calc | stream([Attempt.Bad]) |\n| - | calc | close(values) |",
|
||||
];
|
||||
for (index, source) in cases.iter().enumerate() {
|
||||
let path = directory(&format!("stream-invalid-{index}")).join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(
|
||||
project.diagnostics.is_empty(),
|
||||
"{index}: {:?}",
|
||||
project.diagnostics
|
||||
);
|
||||
let error = program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.expect(source);
|
||||
assert!(
|
||||
matches!(error.code, "C003" | "C005" | "C012" | "C013" | "C014"),
|
||||
"{index}: {error:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streams_preserve_one_loop_source_and_show_cleanup_and_tick_navigation() {
|
||||
let (project, program) = checked(include_str!("../../native/stream.dever"), "stream-docs");
|
||||
let entry = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.source_name.ends_with(".summary"))
|
||||
.unwrap();
|
||||
assert!(entry.effects.stream && entry.effects.suspends && !entry.effects.tasks);
|
||||
let ticks = program
|
||||
.functions
|
||||
.iter()
|
||||
.find(|f| f.signature.name == "dever.task.app.ticks")
|
||||
.unwrap();
|
||||
assert!(ticks.effects.stream && ticks.effects.clock && !ticks.effects.suspends);
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
success(cli(&["check", path]));
|
||||
let index = dever::compiler::inspect::analyze(&project, Some("main.main.app.summary"), 1, true)
|
||||
.unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
for expected in [
|
||||
"挂起拉取流的下一元素",
|
||||
"同一次拉取分发给各行",
|
||||
"关闭来源流",
|
||||
"取消或运行故障",
|
||||
] {
|
||||
assert!(text.contains(expected), "{expected}");
|
||||
}
|
||||
let source = "type Attempt { Ok(value: Int), error Bad }\nrule handle(value: Attempt) (out: Int) recover(\"record rejected elements\")\n| value | out |\n| Ok(n) | n |\n| Bad | 0 |\nloop handled(values: AsyncStream<Attempt>) (out: Int)\n| each value in values | result |\n| out | sum handle(value) |\n";
|
||||
checked(source, "stream-recovery");
|
||||
}
|
||||
@@ -0,0 +1,403 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn task_handles_are_affine_and_scheduling_is_explicit() {
|
||||
let prefix = "identity(value: Int) (out: Int) { out = value }\nnoop() () {}\n";
|
||||
let cases = [
|
||||
"flow bad() ()\n| step | kind | do |\n| t | calc | run(noop()) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| t | calc | run(noop()) |\n| - | calc | wait(t) |\n| - | calc | stop(t) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| t | calc | run(noop()) |\n| alias | calc | t |\n| - | calc | wait(t) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| t | calc | run(noop()) |\n| list | calc | [t] |\n| - | calc | wait(t) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| t | calc | run(identity(1)) |\n| - | calc | stop(t) |",
|
||||
"flow bad() (out: Int)\n| step | kind | do |\n| t | calc | run(identity(1)) |\n| out | calc | identity(wait(t)) |",
|
||||
"bad() () { t = run(noop())\nwait(t) }",
|
||||
"rule bad(value: Int) (out: Int)\n| value | out |\n| - | wait(run(identity(value))) |",
|
||||
"flow bad() ()\n| step | kind | parallel | do |\n| t | calc | - | run(noop()) |\n| - | calc | join | wait(t) |\n| - | calc | join | stop(t) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| - | calc | run(noop()) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| t | calc | run(length([1])) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| t | calc | run(noop()) |\n| n | calc | identity(t) |\n| - | calc | wait(t) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| t: Task | calc | run(noop()) |\n| - | calc | wait(t) |",
|
||||
];
|
||||
for (index, source) in cases.iter().enumerate() {
|
||||
let root = directory(&format!("task-invalid-{index}"));
|
||||
let path = root.join("app.dever");
|
||||
fs::write(&path, format!("{prefix}{source}\n")).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(
|
||||
project.diagnostics.is_empty(),
|
||||
"{index}: {:?}",
|
||||
project.diagnostics
|
||||
);
|
||||
let error = program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.expect(source);
|
||||
assert!(
|
||||
matches!(error.code, "C004" | "C005" | "C014"),
|
||||
"{index}: {error:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn task_outputs_keep_nominal_error_obligations() {
|
||||
let source = r#"
|
||||
type Outcome { Done, error Failed }
|
||||
rule choose(value: Bool) (out: Outcome)
|
||||
| value | out |
|
||||
| true | Done |
|
||||
| false | Failed |
|
||||
flow bad(value: Bool) (out: Int)
|
||||
| step | kind | do |
|
||||
| t | calc | run(choose(value)) |
|
||||
| received | calc | wait(t) |
|
||||
| out | calc | 1 |
|
||||
"#;
|
||||
let root = directory("task-obligation");
|
||||
let path = root.join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
let error = program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.unwrap();
|
||||
assert_eq!(error.code, "C012", "{error:?}");
|
||||
|
||||
let source = r#"
|
||||
type Outcome { Done, error Failed }
|
||||
noop() () {}
|
||||
rule consume(value: Outcome) () recover("explicitly acknowledge the input failure")
|
||||
| value | do |
|
||||
| Done | noop() |
|
||||
| Failed | noop() |
|
||||
flow cancel(value: Outcome) ()
|
||||
| step | kind | do |
|
||||
| task | calc | run(consume(value)) |
|
||||
| - | calc | stop(task) |
|
||||
"#;
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty());
|
||||
let error = program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.expect("stop may cancel before input recovery executes");
|
||||
assert_eq!(error.code, "C012", "{error:?}");
|
||||
fs::write(&path, source.replace("stop(task)", "wait(task)")).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
program::check(&project.declarations, &project.modules).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn task_program_formats_navigates_and_runs_real_command_entry() {
|
||||
let source = include_str!("../../native/spawn.dever")
|
||||
.lines()
|
||||
.filter(|line| !line.starts_with("cmd "))
|
||||
.collect::<Vec<_>>()
|
||||
.join(
|
||||
"
|
||||
",
|
||||
);
|
||||
let (project, _) = checked(&source, "task-entry");
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
let output = success(cli(&["run", path]));
|
||||
assert_eq!(output.trim(), "42");
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.main"), 1, true).unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
assert!(text.contains("派生任务") && text.contains("消费任务"));
|
||||
let commands = format!(
|
||||
"{}\n{}",
|
||||
include_str!("../../native/spawn.dever"),
|
||||
r#"
|
||||
type PrivateFailure { error Rejected(detail: Text) }
|
||||
reject(detail: Text) () { fail(PrivateFailure.Rejected(detail)) }
|
||||
flow rejected(detail: Text) ()
|
||||
| step | kind | do |
|
||||
| tasks | calc | group(1) |
|
||||
| - | calc | run(tasks, reject(detail)) |
|
||||
| - | calc | wait(tasks) |
|
||||
cmd rejected = rejected
|
||||
denied(value: Int) () {
|
||||
check value > 0 else Forbidden
|
||||
}
|
||||
cmd denied = denied
|
||||
"#
|
||||
);
|
||||
fs::write(path, commands).unwrap();
|
||||
let (project, program) = analyze(&project.sources[0].path);
|
||||
let decisions = ownership::analyze(&program);
|
||||
let mut generated = emit::emit(&program, &decisions, &project.sources);
|
||||
generated
|
||||
.get_mut("main.rs")
|
||||
.unwrap()
|
||||
.text
|
||||
.push_str(&emit::application::emit(&program, &decisions));
|
||||
let artifact = build::compile(
|
||||
&project.root,
|
||||
&program,
|
||||
&generated,
|
||||
Profile::Dev,
|
||||
&project,
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
let binary = project.sources[0].path.parent().unwrap().join("commands");
|
||||
artifact.publish(&binary).unwrap();
|
||||
drop(artifact);
|
||||
let output = Command::new(&binary)
|
||||
.args(["main.main.main", "{}"])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(success(output).contains("42"));
|
||||
let output = Command::new(&binary)
|
||||
.args(["main.main.overflow", r#"{"value":9223372036854775807}"#])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!output.status.success() && output.stdout.is_empty());
|
||||
assert!(String::from_utf8_lossy(&output.stderr).contains("arithmetic fault"));
|
||||
let output = Command::new(&binary)
|
||||
.args(["main.main.rejected", r#"{"detail":"private-value"}"#])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!output.status.success() && output.stdout.is_empty());
|
||||
assert_eq!(
|
||||
String::from_utf8(output.stderr).unwrap().trim(),
|
||||
"main.main.app.PrivateFailure.Rejected"
|
||||
);
|
||||
let output = Command::new(&binary)
|
||||
.args(["main.main.denied", r#"{"value":0}"#])
|
||||
.output()
|
||||
.unwrap();
|
||||
assert!(!output.status.success() && output.stdout.is_empty());
|
||||
assert_eq!(
|
||||
String::from_utf8(output.stderr).unwrap().trim(),
|
||||
"Forbidden"
|
||||
);
|
||||
fs::write(path, "identity(value: Int) (out: Int) { out = value }\nflow main() (out: Int)\n| step | kind | do |\n| t | calc | run(identity(42)) |\n| out | calc | wait(t) |\n").unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
assert_eq!(success(cli(&["run", path])).trim(), "42");
|
||||
let (project, program) = checked(
|
||||
"setting label: Text\nidentity(value: Int) (out: Int) { out = value }\nflow main() (out: Int)\n| step | kind | do |\n| t | calc | run(identity(42)) |\n| received | calc | wait(t) |\n| out | calc | received + length(label) |\n",
|
||||
"task-local-settings",
|
||||
);
|
||||
let generated = emit::emit(&program, &ownership::analyze(&program), &project.sources);
|
||||
assert!(
|
||||
!generated["wire.rs"].text.contains("Arc<SettingValues>"),
|
||||
"settings used only by the caller need no shared allocation"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combinations_reject_aliases_contract_mismatches_and_overlapping_groups() {
|
||||
let prefix = "identity(value: Int) (out: Int) { out = value }\nfallback() (out: Int) { out = 1 }\nother() (different: Int) { different = 1 }\nnoop() () {}\n";
|
||||
let cases = [
|
||||
"| t | calc | run(identity(1)) |\n| out | calc | race(t, t) |",
|
||||
"| out | calc | race(run(identity(1))) |",
|
||||
"| out | calc | timeout(run(identity(1)), 1, identity) |",
|
||||
"| out | calc | timeout(run(identity(1)), 1, other) |",
|
||||
"| out | calc | race(run(identity(1)), run(other())) |",
|
||||
"| g | calc | group(1) |\n| - | calc | run(g, identity(1)) |\n| - | calc | wait(g) |\n| out | calc | 1 |",
|
||||
"| g | calc | group(1) |\n| alias | calc | g |\n| - | calc | wait(g) |\n| out | calc | 1 |",
|
||||
"| g | calc | group(1) |\n| - | calc | wait(g) |\n| - | calc | run(g, noop()) |\n| out | calc | 1 |",
|
||||
"| g | calc | group(1) |\n| out | calc | 1 |",
|
||||
];
|
||||
for (index, body) in cases.iter().enumerate() {
|
||||
let root = directory(&format!("combination-invalid-{index}"));
|
||||
let path = root.join("app.dever");
|
||||
fs::write(
|
||||
&path,
|
||||
format!("{prefix}flow bad() (out: Int)\n| step | kind | do |\n{body}\n"),
|
||||
)
|
||||
.unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let error = program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.expect(body);
|
||||
assert_eq!(error.code, "C014", "{index}: {error:?}");
|
||||
}
|
||||
let source = format!(
|
||||
"{prefix}flow bad() ()\n| step | kind | parallel | do |\n| g | calc | - | group(2) |\n| - | calc | both | run(g, noop()) |\n| - | calc | both | run(g, noop()) |\n| - | calc | - | wait(g) |\n"
|
||||
);
|
||||
let path = directory("group-overlap").join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert_eq!(
|
||||
program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.unwrap()
|
||||
.code,
|
||||
"C014"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combinations_only_discharge_errors_on_proven_completed_paths() {
|
||||
let prefix = r#"
|
||||
type Outcome { Done, error Failed }
|
||||
noop() () {}
|
||||
rule consume(value: Outcome) () recover("acknowledge an input failure")
|
||||
| value | do |
|
||||
| Done | noop() |
|
||||
| Failed | noop() |
|
||||
"#;
|
||||
let cases = [
|
||||
(
|
||||
"| - | calc | timeout(run(consume(value)), 1, noop) |",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"| - | calc | race(run(consume(value)), run(noop())) |",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"| - | calc | race(run(consume(value)), run(consume(value))) |",
|
||||
true,
|
||||
),
|
||||
(
|
||||
"| g | calc | group(1) |\n| - | calc | run(g, consume(value)) |\n| - | calc | stop(g) |",
|
||||
false,
|
||||
),
|
||||
(
|
||||
"| g | calc | group(1) |\n| - | calc | run(g, consume(value)) |\n| - | calc | wait(g) |",
|
||||
true,
|
||||
),
|
||||
("| - | calc | wait(run(consume(value))) |", true),
|
||||
("| - | calc | parallel(consume(value)) |", true),
|
||||
];
|
||||
for (index, (body, valid)) in cases.iter().enumerate() {
|
||||
let path = directory(&format!("combination-obligation-{index}")).join("app.dever");
|
||||
fs::write(
|
||||
&path,
|
||||
format!("{prefix}flow handle(value: Outcome) ()\n| step | kind | do |\n{body}\n"),
|
||||
)
|
||||
.unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let result = program::check(&project.declarations, &project.modules);
|
||||
if *valid {
|
||||
result.unwrap();
|
||||
} else {
|
||||
assert_eq!(result.err().expect(body).code, "C012");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combinations_format_and_inspect_checked_task_workflows() {
|
||||
let (project, _) = checked(
|
||||
include_str!("../../native/combinations.dever"),
|
||||
"combination-docs",
|
||||
);
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
let index = dever::compiler::inspect::analyze(&project, None, 1, true).unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
for label in ["限时等待", "首个就绪结果", "等待组内空位", "消费任务"] {
|
||||
assert!(text.contains(label), "missing {label}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn captured_combination_failure_cannot_erase_cancelled_input_obligations() {
|
||||
let prefix = r#"
|
||||
type Outcome { Done, error Failed }
|
||||
type Problem { error Invalid }
|
||||
type Captured { Done, error Failed(reason: Problem) }
|
||||
noop() () {}
|
||||
failNow() () { fail(Problem.Invalid) }
|
||||
rule consume(value: Outcome) () recover("acknowledge this input")
|
||||
| value | do |
|
||||
| Done | noop() |
|
||||
| Failed | noop() |
|
||||
rule acknowledge(value: Captured) () recover("acknowledge the combination failure only")
|
||||
| value | do |
|
||||
| Done | noop() |
|
||||
| Failed(reason) | noop() |
|
||||
"#;
|
||||
for (index, steps) in [
|
||||
"| result: Captured | calc | result(race(run(consume(value)), run(failNow()))) |",
|
||||
"| result: Captured | calc | result(timeout(run(consume(value)), 1, failNow)) |",
|
||||
"| g | calc | group(2) |\n| - | calc | run(g, consume(value)) |\n| - | calc | run(g, failNow()) |\n| result: Captured | calc | result(wait(g)) |",
|
||||
].iter().enumerate() {
|
||||
let path = directory(&format!("captured-combination-input-{index}")).join("app.dever");
|
||||
fs::write(&path, format!("{prefix}flow bad(value: Outcome) ()\n| step | kind | do |\n{steps}\n| - | rule | acknowledge(result) |\n")).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
assert_eq!(program::check(&project.declarations, &project.modules).err().expect(steps).code, "C012");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cpu_parallel_rejects_effectful_targets_hidden_calls_and_task_consumption() {
|
||||
let prefix = r#"
|
||||
identity(value: Int) (out: Int) { out = value }
|
||||
flow clock() (out: DateTime)
|
||||
| step | kind | do |
|
||||
| out | flow | dever.time.now() |
|
||||
flow waiting() ()
|
||||
| step | kind | do |
|
||||
| - | flow | dever.time.sleep(0) |
|
||||
flow taskWork() (out: Int)
|
||||
| step | kind | do |
|
||||
| t | calc | run(identity(1)) |
|
||||
| out | calc | wait(t) |
|
||||
"#;
|
||||
let cases = [
|
||||
"flow bad() (out: DateTime)\n| step | kind | do |\n| out | calc | parallel(clock()) |",
|
||||
"flow bad() ()\n| step | kind | do |\n| - | calc | parallel(waiting()) |",
|
||||
"flow bad() (out: Int)\n| step | kind | do |\n| out | calc | parallel(taskWork()) |",
|
||||
"bad() (out: Int) { out = parallel(identity(1)) }",
|
||||
"flow bad() (out: Int)\n| step | kind | do |\n| out | calc | identity(parallel(identity(1))) |",
|
||||
"flow bad() (out: Int)\n| step | kind | do |\n| out | calc | wait(parallel(identity(1))) |",
|
||||
"flow apply(action: handler() ()) ()\n| step | kind | do |\n| - | calc | parallel(action()) |\nflow bad() ()\n| step | kind | do |\n| - | flow | apply(waiting) |",
|
||||
];
|
||||
for (index, source) in cases.iter().enumerate() {
|
||||
let path = directory(&format!("worker-invalid-{index}")).join("app.dever");
|
||||
fs::write(&path, format!("{prefix}{source}\n")).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty(), "{:?}", project.diagnostics);
|
||||
let error = program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.expect(source);
|
||||
assert!(matches!(error.code, "C013" | "C014"), "{index}: {error:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cpu_parallel_preserves_ranges_and_nominal_error_obligations() {
|
||||
let path = directory("worker-infallible-capture").join("app.dever");
|
||||
fs::write(&path, "type Problem { error Invalid }\ntype Attempt { Done(value: Int), error Failed(reason: Problem) }\nidentity(value: Int) (out: Int) { out = value }\nflow bad() (out: Attempt)\n| step | kind | do |\n| out | calc | result(parallel(identity(1))) |\n").unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert!(project.diagnostics.is_empty());
|
||||
let error = program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.expect("infallible call needs no recovery");
|
||||
assert_eq!(error.code, "C012");
|
||||
assert_eq!(error.message, "result on an infallible call is redundant");
|
||||
checked(
|
||||
"positive() (out: Int >= 1) { out = 1 }\nflow main() (out: Int >= 1)\n| step | kind | do |\n| out | calc | parallel(positive()) |\n",
|
||||
"worker-range",
|
||||
);
|
||||
let source = "type Outcome { Done, error Failed }\nrule choose(value: Bool) (out: Outcome)\n| value | out |\n| true | Done |\n| false | Failed |\nflow bad(value: Bool) (out: Int)\n| step | kind | do |\n| received | calc | parallel(choose(value)) |\n| out | calc | 1 |\n";
|
||||
let path = directory("worker-output-obligation").join("app.dever");
|
||||
fs::write(&path, source).unwrap();
|
||||
let project = Project::load(&path).unwrap();
|
||||
assert_eq!(
|
||||
program::check(&project.declarations, &project.modules)
|
||||
.err()
|
||||
.unwrap()
|
||||
.code,
|
||||
"C012"
|
||||
);
|
||||
let (project, _) = checked(include_str!("../../native/worker.dever"), "worker-docs");
|
||||
let path = project.sources[0].path.to_str().unwrap();
|
||||
success(cli(&["fmt", path]));
|
||||
success(cli(&["fmt", path, "--check"]));
|
||||
let index =
|
||||
dever::compiler::inspect::analyze(&project, Some("main.main.app.together"), 1, true)
|
||||
.unwrap();
|
||||
let text = serde_json::to_string(&index).unwrap();
|
||||
assert!(text.contains("线程池执行纯计算") && text.contains("main.main.app.pair"));
|
||||
}
|
||||
Reference in new issue
Block a user