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::>() .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"), "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")); }