123 lines
6.2 KiB
Rust
123 lines
6.2 KiB
Rust
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");
|
|
}
|