126 lines
5.0 KiB
Rust
126 lines
5.0 KiB
Rust
use super::*;
|
|
|
|
#[test]
|
|
fn domain_cannot_define_flow_even_without_io() {
|
|
let source = Source::new(
|
|
0,
|
|
"domain.dever",
|
|
"flow f() (out: Int)\n| step | kind | do |\n| out | calc | 1 |\n".into(),
|
|
);
|
|
let (tokens, diagnostics, _) = lex::scan_with_diagnostics(&source, 0..source.text.len());
|
|
assert!(diagnostics.is_empty(), "{diagnostics:?}");
|
|
let parsed = parse::parse(tokens);
|
|
assert!(parsed.diagnostics.is_empty());
|
|
let mut module = Module::standalone();
|
|
module.role = dever::compiler::project::Role::Domain;
|
|
let error = program::check(&parsed.declarations, &[module])
|
|
.err()
|
|
.unwrap();
|
|
assert_eq!(error.code, "C003");
|
|
}
|
|
|
|
#[test]
|
|
fn flow_uses_shared_range_refinement_and_real_call_kinds() {
|
|
let source = "rule price(remote: Bool) (fee: Int)\n| remote | fee |\n| true | 15 |\n| false | 8 |\nflow quote(remote: Bool, quantity: Int) (total: Int)\n| step | kind | do |\n| - | check | quantity > 0 else Invalid |\n| fee | rule | price(remote) |\n| total | calc | fee * quantity |\nflow start(remote: Bool, quantity: Int) (total: Int)\n| step | kind | do |\n| total | flow | quote(remote, quantity) |\n";
|
|
let program = check(source).unwrap();
|
|
let Body::Statements {
|
|
flow: Some(steps), ..
|
|
} = &program.functions[1].body
|
|
else {
|
|
panic!()
|
|
};
|
|
assert_eq!(
|
|
steps.iter().map(|step| step.kind).collect::<Vec<_>>(),
|
|
[StepKind::Check, StepKind::Rule, StepKind::Calc]
|
|
);
|
|
assert_eq!(
|
|
program.functions[2].failures,
|
|
[Failure::Api(ApiError::Invalid)].into()
|
|
);
|
|
rejected(
|
|
&source.replace("| fee | rule |", "| fee | calc |"),
|
|
"C005",
|
|
"kind must be rule",
|
|
);
|
|
rejected(
|
|
&source.replace("| total | flow |", "| total | calc |"),
|
|
"C005",
|
|
"kind must be flow",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn flow_bindings_are_unique_forward_only_and_complete() {
|
|
rejected(
|
|
"flow f(value: Int) (out: Int)\n| step | kind | do |\n| value | calc | 1 |\n| out | calc | value |\n",
|
|
"C005",
|
|
"cannot shadow",
|
|
);
|
|
rejected(
|
|
"flow f() (out: Int)\n| step | kind | do |\n| out | calc | 1 |\n| out | calc | 2 |\n",
|
|
"C005",
|
|
"must be new",
|
|
);
|
|
rejected(
|
|
"flow f() (out: Int)\n| step | kind | do |\n| out | calc | later |\n| later | calc | 2 |\n",
|
|
"C004",
|
|
"unassigned",
|
|
);
|
|
rejected(
|
|
"flow f() (out: Int)\n| step | kind | do |\n| other | calc | 1 |\n",
|
|
"C005",
|
|
"not assigned",
|
|
);
|
|
rejected(
|
|
"flow first(flag: Bool) (out: Int)\n| step | kind | do |\n| out | rule | second(flag) |\nrule second(flag: Bool) (out: Int)\n| flag | out |\n| true | first(flag) |\n| false | 0 |\n",
|
|
"C010",
|
|
"recursive",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn discarding_success_does_not_discard_failure_or_create_useless_values() {
|
|
let source = "work(value: Int) (answer: Int) { check value > 0 else Invalid\nanswer = value }\nflow f(value: Int) ()\n| step | kind | do |\n| - | calc | work(value) |\n";
|
|
let program = check(source).unwrap();
|
|
assert_eq!(
|
|
program.functions[1].failures,
|
|
[Failure::Api(ApiError::Invalid)].into()
|
|
);
|
|
rejected(
|
|
"flow f() ()\n| step | kind | do |\n| - | calc | 3 + 4 |\n",
|
|
"C005",
|
|
"discarded",
|
|
);
|
|
rejected(
|
|
"identity(value: Int) (answer: Int) { answer = value }\nflow f() ()\n| step | kind | do |\n| - | calc | identity(1) |\n",
|
|
"C005",
|
|
"discarded",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn flow_capture_and_handler_binding_keep_concrete_failure_obligations() {
|
|
let source = "type Reply { Ready(value: Int), error Rejected }\nwork(value: Int) (answer: Int) { check value > 0 else Reply.Rejected\nanswer = value }\nflow attempt(value: Int) (reply: Reply)\n| step | kind | do |\n| reply: Reply | calc | result(work(value)) |\nflow apply(value: Int, transform: handler(value: Int) (answer: Int)) (answer: Int)\n| step | kind | do |\n| answer | calc | transform(value) |\nrule identity(value: Int) (answer: Int)\n| value | answer |\n| - | value |\nflow start(value: Int) (answer: Int)\n| step | kind | do |\n| first | flow | apply(value, work) |\n| answer | flow | apply(first, identity) |\n";
|
|
let program = check(source).unwrap();
|
|
assert_eq!(
|
|
program
|
|
.functions
|
|
.iter()
|
|
.find(|f| f.signature.source_name.ends_with(".start"))
|
|
.unwrap()
|
|
.failures,
|
|
[Failure::Choice(1, 1)].into()
|
|
);
|
|
rejected(
|
|
&source
|
|
.replace("| reply: Reply |", "| discarded: Reply |")
|
|
.replace(
|
|
"| discarded: Reply | calc | result(work(value)) |",
|
|
"| discarded: Reply | calc | result(work(value)) |\n| reply | calc | Ready(0) |",
|
|
),
|
|
"C012",
|
|
"not handled",
|
|
);
|
|
check("type Reply { Ready(value: Int), error Rejected }\nrule work(value: Int) (answer: Int)\n| value | answer |\n| > 0 | value |\n| <= 0 | fail(Reply.Rejected) |\nflow capture(value: Int) (reply: Reply)\n| step | kind | do |\n| reply | rule | result(work(value)) |\n").unwrap();
|
|
}
|