357 lines
17 KiB
Rust
357 lines
17 KiB
Rust
use super::*;
|
|
|
|
#[test]
|
|
fn walks_bind_a_named_step_and_preserve_the_concrete_state_contract() {
|
|
let next = "rule next(state: Int) (out: Walk<Int, Int>)\n| state | out |\n| > 0 | Next(state, state - 1) |\n| <= 0 | Done |\n";
|
|
check(&format!("{next}loop values(seed: Int) (out: List<Int>)\n| each value in walk(seed, next) | result |\n| out | list value |\n")).unwrap();
|
|
check(&format!("{next}loop values(seed: Int, step: handler(state: Int) (out: Walk<Int, Int>)) (out: List<Int>)\n| each value in walk(seed, step) | result |\n| out | list value |\nstart(seed: Int) (out: List<Int>) {{ out = values(seed, next) }}")).unwrap();
|
|
rejected(
|
|
"next(state: Int) (out: Walk<Int, Text>) { out = Next(state, \"\") }\nloop values() (out: List<Int>)\n| each value in walk(0, next) | result |\n| out | list value |\n",
|
|
"C005",
|
|
"preserve its state type",
|
|
);
|
|
rejected(
|
|
"next(state: Int >= 0) (out: Walk<Int, Int>) { out = Done }\nloop values() (out: List<Int>)\n| each value in walk(0, next) | result |\n| out | list value |\n",
|
|
"C008",
|
|
"after every Next",
|
|
);
|
|
rejected(
|
|
"next(state: Int) (out: Int) { out = state }\nloop values() (out: List<Int>)\n| each value in walk(0, next) | result |\n| out | list value |\n",
|
|
"C005",
|
|
"Walk<T, S>",
|
|
);
|
|
rejected(
|
|
"loop values(seed: Int) (out: List<Int>)\n| each value in walk(seed, values) | result |\n| out | list value |\n",
|
|
"C010",
|
|
"recursive",
|
|
);
|
|
check("walk(seed: Int) (out: List<Int>) { out = [seed] }\nloop values(seed: Int) (out: List<Int>)\n| each value in walk(seed) | result |\n| out | list value |\n").unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn walks_do_not_invent_future_failures_or_drop_current_state_failures() {
|
|
let next = "type State { Ready, error Broken }\nrule next(state: Int) (out: Walk<State, Int>)\n| state | out |\n| > 0 | Next(State.Broken, state - 1) |\n| <= 0 | Done |\n";
|
|
let first = format!(
|
|
"{next}loop first(seed: Int) (out: State?)\n| each value in walk(seed, next) | result |\n| out | first value |\n"
|
|
);
|
|
check(&first).unwrap();
|
|
rejected(
|
|
&format!("{first}missing(seed: Int) () {{ ignored = first(seed) }}"),
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
rejected(
|
|
&format!(
|
|
"{next}loop mixed(seed: Int) (out: State?, count: Int)\n| each value in walk(seed, next) | result |\n| out | first value |\n| count | count |\n"
|
|
),
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
let next = "type State { Ready, error Broken }\nrule next(state: State) (out: Walk<Int, State>)\n| state | out |\n| Ready | Next(1, State.Broken) |\n| Broken | fail(state) |\n";
|
|
rejected(
|
|
&format!(
|
|
"{next}loop first() (out: Int?)\n| each value in walk(State.Ready, next) | result |\n| out | first value |\n"
|
|
),
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
check(&format!("{next}loop all() (out: Int)\n| each value in walk(State.Ready, next) | result |\n| out | sum value |\n")).unwrap();
|
|
}
|
|
use dever::compiler::check::ownership;
|
|
|
|
#[test]
|
|
fn indexing_has_a_nominal_duplicate_failure_and_preserves_elements() {
|
|
let prefix = "loop unique(values: List<Int>) (out: Map<Int, Int>)\n| each value in values | result |\n| out | index value |\n";
|
|
let program = check(prefix).unwrap();
|
|
assert_eq!(
|
|
program.functions[0].failures,
|
|
[Failure::Choice(COLLECTION_ERROR, 0)].into()
|
|
);
|
|
check(&format!("{prefix}type Result {{ Ready(value: Map<Int, Int>), error Duplicate(reason: dever.collection.Error) }}\nf(values: List<Int>) (out: Result) {{ out = result(unique(values)) }}")).unwrap();
|
|
rejected(
|
|
&format!(
|
|
"{prefix}type Result {{ Ready(value: Map<Int, Int>), error Duplicate }}\nf(values: List<Int>) (out: Result) {{ out = result(unique(values)) }}"
|
|
),
|
|
"C005",
|
|
"nominally matching",
|
|
);
|
|
check("type State { Ready, error Broken }\nloop indexed(values: List<State>) (out: Map<Int, State>)\n| each value in values | result |\n| out | index 0 |\n").unwrap();
|
|
rejected(
|
|
"type State { Ready, error Broken }\nloop indexed(values: List<State>) (out: Map<Int, State>)\n| each value in values | value | result |\n| out | Ready | index 0 |\n",
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
check(
|
|
"rule raise(value: dever.collection.Error) ()\n| value | do |\n| DuplicateKey | fail(value) |\n",
|
|
)
|
|
.unwrap();
|
|
for kind in ["Map<Int, Text>", "Map<Int, List<Int>>", "Map<Text, Int>"] {
|
|
rejected(
|
|
&format!(
|
|
"loop f(values: List<Int>) (out: {kind})\n| each value in values | result |\n| out | index value |\n"
|
|
),
|
|
"C005",
|
|
"",
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn grouping_keeps_source_elements_types_and_error_obligations() {
|
|
let prefix = "type State { Ready, error Broken }\n";
|
|
check(&format!("{prefix}loop all(values: List<State>) (out: Map<Int, List<State>>)\n| each value in values | result |\n| out | group 0 |\n")).unwrap();
|
|
rejected(
|
|
&format!(
|
|
"{prefix}loop missing(values: List<State>) (out: Map<Int, List<State>>)\n| each value in values | value | result |\n| out | Ready | group 0 |\n"
|
|
),
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
for (kind, columns, result) in [
|
|
("Map<Int, Int>", "", "group value"),
|
|
("Map<Int, List<Text>>", "", "group value"),
|
|
("Map<Text, List<Int>>", "", "group value"),
|
|
("Map<Int, List<Int>>", "take |", "1 | group value"),
|
|
("Map<Int, List<Int>>", "sort |", "[value.asc] | group value"),
|
|
] {
|
|
assert_eq!(check(&format!("loop f(values: List<Int>) (out: {kind})\n| each value in values | {columns} result |\n| out | {result} |\n")).err().unwrap().code, "C005");
|
|
}
|
|
let program = check("loop names(values: List<Text>) (out: Map<Int, List<Text>>)\n| each value in values | result |\n| out | group length(value) |\n").unwrap();
|
|
assert_eq!(ownership::analyze(&program)[0].parameters, [Storage::Owned]);
|
|
}
|
|
|
|
#[test]
|
|
fn loops_bind_each_output_once_and_share_the_call_kind() {
|
|
let program = check("loop total(values: List<Int>) (out: Int)\n| each value in values | result |\n| out | sum value |\nflow start(values: List<Int>) (out: Int)\n| step | kind | do |\n| out | loop | total(values) |\n").unwrap();
|
|
assert!(matches!(program.functions[0].body, Body::Loop(_)));
|
|
for source in [
|
|
"loop f(values: List<Int>) (n: Int)\n| each value in values | result |\n| n | count |\n| n | count |\n",
|
|
"loop f(values: List<Int>) (n: Int, missing: Int)\n| each value in values | result |\n| n | count |\n",
|
|
"loop f(values: List<Int>) (n: Int)\n| each value in values | result |\n| wrong | count |\n",
|
|
"loop f(values: List<Int>) (n: Bool)\n| each value in values | result |\n| n | sum value |\n",
|
|
"loop f(values: List<Float>) (n: Float?)\n| each value in values | result |\n| n | min value |\n",
|
|
"loop f(values: List<Int>) (n: Int)\n| each values in values | result |\n| n | count |\n",
|
|
"loop f(values: Int) (n: Int)\n| each value in values | result |\n| n | count |\n",
|
|
"loop f() (n: Int)\n| each value in range(1) | result |\n| n | count |\n",
|
|
"loop f(values: List<Int>) ()\n| each value in values | result |\n| - | count |\n",
|
|
"id(value: Int) (out: Int) { out = value }\nloop f(values: List<Int>) ()\n| each value in values | result |\n| - | do id(value) |\n",
|
|
] {
|
|
assert_eq!(check(source).err().unwrap().code, "C005", "{source}");
|
|
}
|
|
rejected(
|
|
"loop f(values: List<Int>) (n: Int >= 1)\n| each value in values | result |\n| n | count |\n",
|
|
"C008",
|
|
"empty input",
|
|
);
|
|
rejected(
|
|
"loop f(values: List<Int>) (n: Int, total: Int)\n| each value in values | result |\n| n | count |\n| total | sum n |\n",
|
|
"C004",
|
|
"unknown",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn loops_cannot_erase_error_elements_through_count_or_short_circuit() {
|
|
let prefix = "type State { Ready, error Broken }\n";
|
|
for aggregate in ["count", "first value"] {
|
|
let kind = if aggregate == "count" {
|
|
"Int"
|
|
} else {
|
|
"State?"
|
|
};
|
|
rejected(
|
|
&format!(
|
|
"{prefix}loop f(values: List<State>) (out: {kind})\n| each value in values | result |\n| out | {aggregate} |\n"
|
|
),
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
}
|
|
check(&format!("{prefix}loop f(values: List<State>) (out: List<State>)\n| each value in values | result |\n| out | list value |\n")).unwrap();
|
|
check(&format!("{prefix}rule consume(value: State) (out: Int)\n| value | out |\n| Ready | 1 |\n| Broken | fail(State.Broken) |\nloop f(values: List<State>) (out: Int)\n| each value in values | result |\n| out | sum consume(value) |\n")).unwrap();
|
|
rejected(
|
|
&format!(
|
|
"{prefix}loop empty(values: List<State>) (out: List<State>)\n| each value in values | result |\n| out | list value |\nf() (n: Int) {{ values: List<State> = []\nignored = append(empty(values), State.Broken)\nn = 1 }}"
|
|
),
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
rejected(
|
|
&format!(
|
|
"{prefix}loop f(values: List<Int>, state: State) (out: List<State>)\n| each value in values | result |\n| out | list state |\n"
|
|
),
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn loop_ownership_borrows_captures_and_moves_elements_only_at_last_use() {
|
|
let program = check("loop sizes(values: List<Text>) (n: Int)\n| each value in values | result |\n| n | sum length(value) |\nloop copied(values: List<Text>) (out: List<Text>)\n| each value in values | result |\n| out | list value |\nloop repeated(values: List<Int>, label: Text) (out: List<Text>)\n| each value in values | result |\n| out | list label |\n").unwrap();
|
|
let decisions = ownership::analyze(&program);
|
|
assert_eq!(decisions[0].parameters, [Storage::Borrowed]);
|
|
assert!(
|
|
!decisions[0]
|
|
.reads
|
|
.values()
|
|
.any(|read| *read == Access::Clone)
|
|
);
|
|
assert_eq!(decisions[1].parameters, [Storage::Owned]);
|
|
assert!(
|
|
!decisions[1]
|
|
.reads
|
|
.values()
|
|
.any(|read| *read == Access::Clone)
|
|
);
|
|
assert_eq!(
|
|
decisions[2].parameters,
|
|
[Storage::Borrowed, Storage::Borrowed]
|
|
);
|
|
assert!(
|
|
decisions[2]
|
|
.reads
|
|
.values()
|
|
.any(|read| *read == Access::Clone)
|
|
);
|
|
}
|
|
#[test]
|
|
fn filters_are_infallible_typed_and_cannot_hide_error_elements() {
|
|
for (source, message) in [
|
|
(
|
|
"loop f(values: List<Int>) (n: Int)\n| each value in values | value | result |\n| n | other | count |\n",
|
|
"other",
|
|
),
|
|
(
|
|
"loop f(values: List<Int>) (n: Int)\n| each value in values | value | result |\n| n | true | count |\n",
|
|
"input type",
|
|
),
|
|
(
|
|
"checked(value: Int) (out: Int) { check value > 0 else Invalid\nout = value }\nloop f(values: List<Int>) (n: Int)\n| each value in values | checked(value) | result |\n| n | > 0 | count |\n",
|
|
"must not propagate",
|
|
),
|
|
] {
|
|
rejected(source, "C005", message);
|
|
}
|
|
rejected(
|
|
"type State { Ready, error Broken }\nloop f(values: List<State>) (out: List<State>)\n| each value in values | value | result |\n| out | Ready | list value |\n",
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
check("loop f(values: List<Text>) (n: Int)\n| each value in values | length(value) | result |\n| n | >= 1 | count |\n").unwrap();
|
|
}
|
|
#[test]
|
|
fn filter_facts_remove_repeated_checks_without_leaking_between_rows() {
|
|
check("positive(value: Int >= 1) (out: Int) { out = value }\nloop f(values: List<Int>) (out: List<Int>)\n| each value in values | value | result |\n| out | > 0 | list positive(value) |\n").unwrap();
|
|
rejected(
|
|
"positive(value: Int >= 1) (out: Int) { out = value }\nloop f(values: List<Int>) (first: List<Int>, second: List<Int>)\n| each value in values | value | result |\n| first | > 0 | list positive(value) |\n| second | - | list positive(value) |\n",
|
|
"C008",
|
|
"range",
|
|
);
|
|
check("loop f(values: List<Text?>) (out: List<Text>)\n| each value in values | value | result |\n| out | \"甲\" | list value |\n").unwrap();
|
|
}
|
|
#[test]
|
|
fn filter_payload_bindings_keep_variant_types_ranges_and_row_scope() {
|
|
let prefix = "type Event { Named(label: Text), Tagged(label: Text), Number(label: Int) }\n";
|
|
check(&format!("{prefix}loop f(values: List<Event>) (out: List<Text>)\n| each value in values | value | result |\n| out | Named(label), Tagged(label) | list label |\n")).unwrap();
|
|
for (condition, message) in [
|
|
("Named(value)", "shadows"),
|
|
("Named(label), Number(label)", "same names and types"),
|
|
("Named(label), -", "same names and types"),
|
|
("Named(label), Tagged(other)", "same names and types"),
|
|
] {
|
|
rejected(
|
|
&format!(
|
|
"{prefix}loop f(values: List<Event>) (out: List<Text>)\n| each value in values | value | result |\n| out | {condition} | list label |\n"
|
|
),
|
|
"C005",
|
|
message,
|
|
);
|
|
}
|
|
rejected(
|
|
&format!(
|
|
"{prefix}loop f(values: List<Event>) (labels: List<Text>, out: List<Text>)\n| each value in values | value | result |\n| labels | Named(label) | list label |\n| out | - | list label |\n"
|
|
),
|
|
"C004",
|
|
"unknown",
|
|
);
|
|
}
|
|
#[test]
|
|
fn filters_keep_every_error_variant_obligation_across_all_rows() {
|
|
let prefix = "type State { Ready, error Offline, error Denied }\n";
|
|
check(&format!("{prefix}loop f(values: List<State>) (offline: List<State>, denied: List<State>)\n| each value in values | value | result |\n| offline | Offline | list value |\n| denied | Denied | list value |\n")).unwrap();
|
|
check("type State { Ready, error Broken }\nloop f(values: List<State>) ()\n| each value in values | value | result |\n| - | Broken | do fail(value) |\n").unwrap();
|
|
rejected(
|
|
&format!(
|
|
"{prefix}loop f(values: List<State>) (offline: List<State>)\n| each value in values | value | result |\n| offline | Offline | list value |\n"
|
|
),
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
rejected(
|
|
"type State { Ready, error Broken }\nloop f(values: List<State>, enabled: Bool) (out: List<State>)\n| each value in values | value | enabled | result |\n| out | Broken | true | list value |\n",
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
rejected(
|
|
"type State { Ready, error Broken }\nloop f(values: List<State>) (out: State?)\n| each value in values | value | result |\n| out | Broken | first value |\n",
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
}
|
|
#[test]
|
|
fn take_is_list_only_initialized_from_inputs_and_cannot_discard_errors() {
|
|
rejected(
|
|
"loop f(values: List<Int>) (out: Int)\n| each value in values | take | result |\n| out | 1 | count |\n",
|
|
"C005",
|
|
"list rows",
|
|
);
|
|
rejected(
|
|
"loop f(values: List<Int>, size: Int) (out: List<Int>)\n| each value in values | take | result |\n| out | size | list value |\n",
|
|
"C008",
|
|
"nonnegative",
|
|
);
|
|
rejected(
|
|
"loop f(values: List<Int>) (out: List<Int>)\n| each value in values | take | result |\n| out | value | list value |\n",
|
|
"C004",
|
|
"unknown",
|
|
);
|
|
for size in ["0", "1"] {
|
|
rejected(
|
|
&format!(
|
|
"type State {{ Ready, error Broken }}\nloop f(values: List<State>) (out: List<State>)\n| each value in values | take | result |\n| out | {size} | list value |\n"
|
|
),
|
|
"C012",
|
|
"failure is not handled",
|
|
);
|
|
}
|
|
}
|
|
#[test]
|
|
fn sorting_requires_explicit_directions_and_infallible_total_order_keys() {
|
|
for (kind, keys, message) in [
|
|
("Int", "value.asc", "list of asc"),
|
|
("Int", "[value]", ".asc or .desc"),
|
|
("Int", "[value.random]", ".asc or .desc"),
|
|
("Int", "[]", "at least one key"),
|
|
("Float", "[value.asc]", "total order"),
|
|
("Text?", "[value.asc]", "total order"),
|
|
] {
|
|
rejected(
|
|
&format!(
|
|
"loop f(values: List<{kind}>) (out: List<{kind}>)\n| each value in values | sort | result |\n| out | {keys} | list value |\n"
|
|
),
|
|
"C005",
|
|
message,
|
|
);
|
|
}
|
|
rejected(
|
|
"loop f(values: List<Int>) (out: Int)\n| each value in values | sort | result |\n| out | [value.asc] | count |\n",
|
|
"C005",
|
|
"list rows",
|
|
);
|
|
rejected(
|
|
"positive(value: Int) (out: Int) { check value > 0 else Invalid\nout = value }\nloop f(values: List<Int>) (out: List<Int>)\n| each value in values | sort | result |\n| out | [positive(value).asc] | list value |\n",
|
|
"C005",
|
|
"must not propagate",
|
|
);
|
|
}
|