169 lines
6.5 KiB
Rust
169 lines
6.5 KiB
Rust
//! 检查真实项目和实际 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;
|