Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,132 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn folds_keep_errors_across_fields_and_short_circuit() {
|
||||
let prefix = "type State { Ready, error Broken }\ntype Pair { left: State, right: State }\nswap(previous: Pair) (out: Pair) { out = Pair { left = previous.right, right = previous.left } }\nloop rotate(values: List<Int>, seed: Pair) (out: Pair)\n| each value in values | result |\n| out | fold(acc = seed, swap(acc)) |\n";
|
||||
check(prefix).unwrap();
|
||||
check(&format!("{prefix}retained(values: List<Int>, seed: Pair) (out: Pair) {{ ignored = rotate(values, seed)\nout = seed }}")).unwrap();
|
||||
rejected(
|
||||
&format!(
|
||||
"{prefix}missing(values: List<Int>, seed: Pair) (out: State) {{ ignored = rotate(values, seed)\nout = seed.left }}"
|
||||
),
|
||||
"C012",
|
||||
"failure is not handled",
|
||||
);
|
||||
rejected(
|
||||
&format!(
|
||||
"{prefix}partial(values: List<Int>, seed: Pair) (out: State) {{ pair = rotate(values, seed)\nout = pair.left }}"
|
||||
),
|
||||
"C012",
|
||||
"failure is not handled",
|
||||
);
|
||||
let stop = "type State { Ready, error Broken }\nadd(value: State, previous: List<State>) (acc: List<State>, stop: Bool) { acc = append(previous, value)\nstop = true }\nloop first(values: List<State>) (out: List<State>)\n| each value in values | result |\n| out | foldUntil(acc = [], add(value, acc)) |\n";
|
||||
rejected(stop, "C012", "failure is not handled");
|
||||
check(
|
||||
&stop
|
||||
.replace(
|
||||
"(out: List<State>)\n| each",
|
||||
"(out: List<State>, all: List<State>)\n| each",
|
||||
)
|
||||
.replace(
|
||||
"foldUntil(acc = [], add(value, acc)) |",
|
||||
"foldUntil(acc = [], add(value, acc)) |\n| all | list value |",
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
check("type State { Ready, error Broken }\nnext(previous: State) (out: State) recover(\"discard optional state explicitly\") { out = State.Ready }\nloop reset(values: List<Int>, seed: State) (out: State)\n| each value in values | result |\n| out | fold(acc = seed, next(acc)) |\n").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fold_handler_binding_and_formatting_share_the_checked_call() {
|
||||
let source = "add(value: Int, previous: Int) (out: Int) { out = value + previous }\nloop collect(values: List<Int>, update: handler(value: Int, previous: Int) (out: Int)) (out: Int)\n| each value in values | result |\n| out | fold(acc = 0, update(value, acc)) |\nstart(values: List<Int>) (out: Int) { out = collect(values, add) }\n";
|
||||
check(source).unwrap();
|
||||
let text = Source::new(0, "app.dever", source.into());
|
||||
let (tokens, diagnostics, _) = lex::scan_with_diagnostics(&text, 0..source.len());
|
||||
assert!(diagnostics.is_empty(), "{diagnostics:?}");
|
||||
let parsed = parse::parse(tokens);
|
||||
let formatted = dever::compiler::format::format(&parsed.declarations);
|
||||
check(&formatted).unwrap();
|
||||
assert!(formatted.contains("fold(acc = 0, update(value, acc))"));
|
||||
let text = Source::new(0, "app.dever", formatted.clone());
|
||||
let (tokens, diagnostics, _) = lex::scan_with_diagnostics(&text, 0..formatted.len());
|
||||
assert!(diagnostics.is_empty(), "{diagnostics:?}");
|
||||
let parsed = parse::parse(tokens);
|
||||
assert_eq!(
|
||||
formatted,
|
||||
dever::compiler::format::format(&parsed.declarations)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folds_use_output_context_and_require_a_named_typed_update() {
|
||||
check("add(value: Int, previous: List<Int>) (out: List<Int>) { out = append(previous, value) }\nloop collect(values: List<Int>) (out: List<Int>)\n| each value in values | result |\n| out | fold(acc = [], add(value, acc)) |\n").unwrap();
|
||||
check("find(value: Int) (acc: Int?, stop: Bool) { acc = value\nstop = value > 0 }\nloop search(values: List<Int>) (out: Int?)\n| each value in values | result |\n| out | foldUntil(previous = null, find(value)) |\n").unwrap();
|
||||
for source in [
|
||||
"loop f(values: List<Int>) (out: Int)\n| each value in values | result |\n| out | fold(acc = 0, acc + value) |\n",
|
||||
"add(x: Int, y: Int) (out: Int) { out = x + y }\nloop f(values: List<Int>) (out: Int)\n| each value in values | result |\n| out | fold(value = 0, add(value, value)) |\n",
|
||||
"add(x: Int, y: Int) (out: Int) { out = x + y }\nloop f(values: List<Int>) (out: Int)\n| each value in values | result |\n| out | fold(out = 0, add(value, out)) |\n",
|
||||
"next(value: Int) (acc: Int, done: Bool) { acc = value\ndone = true }\nloop f(values: List<Int>) (out: Int)\n| each value in values | result |\n| out | foldUntil(acc = 0, next(value)) |\n",
|
||||
"next(value: Int) (acc: Text) { acc = \"x\" }\nloop f(values: List<Int>) (out: Int)\n| each value in values | result |\n| out | fold(acc = 0, next(value)) |\n",
|
||||
] {
|
||||
assert_eq!(check(source).err().unwrap().code, "C005", "{source}");
|
||||
}
|
||||
rejected(
|
||||
"next(x: Int, y: Int) (out: Int) { out = x + y }\nloop f(values: List<Int>) (out: Int)\n| each value in values | result |\n| out | fold(acc = value, next(value, acc)) |\n",
|
||||
"C004",
|
||||
"unknown",
|
||||
);
|
||||
rejected(
|
||||
"next(x: Int, y: Int) (out: Int) { out = x + y }\nloop f(values: List<Int>) (out: Int)\n| each value in values | acc | result |\n| out | - | fold(acc = 0, next(value, acc)) |\n",
|
||||
"C004",
|
||||
"unknown",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accumulator_ranges_hold_for_every_iteration_including_empty_input() {
|
||||
let positive = "next(value: Int >= 0, previous: Int >= 0) (out: Int >= 0) { out = value + previous }\nloop f(values: List<Int>) (out: Int >= 0)\n| each value in values | value | result |\n| out | >= 0 | fold(acc = 0, next(value, acc)) |\n";
|
||||
check(positive).unwrap();
|
||||
rejected(
|
||||
&positive.replace("acc = 0", "acc = -1"),
|
||||
"C008",
|
||||
"cannot prove",
|
||||
);
|
||||
rejected(
|
||||
&positive.replace("(out: Int >= 0) {", "(out: Int) {"),
|
||||
"C008",
|
||||
"preserves",
|
||||
);
|
||||
rejected(
|
||||
"next(value: Int, previous: Int >= 0 and <= 0) (out: Int) { out = value + previous }\nloop f(values: List<Int>) (out: Int)\n| each value in values | result |\n| out | fold(acc = 0, next(value, acc)) |\n",
|
||||
"C008",
|
||||
"cannot prove",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accumulator_failures_are_checked_after_the_initial_iteration() {
|
||||
let prefix = "type State { Ready, error Broken }\nrule next(flag: Bool) (out: State)\n| flag | out |\n| true | State.Ready |\n| false | State.Broken |\n";
|
||||
rejected(
|
||||
&format!(
|
||||
"{prefix}add(flag: Bool, previous: List<State>) (out: List<State>) {{ out = append(previous, next(flag)) }}\nloop growing(values: List<Bool>, seed: List<State>) (out: List<State>)\n| each value in values | result |\n| out | fold(acc = seed, add(value, acc)) |\nmissing(values: List<Bool>, seed: List<State>) (out: List<State>) {{ ignored = growing(values, seed)\nout = seed }}"
|
||||
),
|
||||
"C012",
|
||||
"failure is not handled",
|
||||
);
|
||||
rejected(
|
||||
&format!(
|
||||
"{prefix}loop f(values: List<Bool>) (out: State)\n| each value in values | result |\n| out | fold(acc = State.Ready, next(value)) |\n"
|
||||
),
|
||||
"C012",
|
||||
"earlier accumulator failure",
|
||||
);
|
||||
check("type State { Ready, error Broken }\nready() (out: State) { out = State.Ready }\nloop f(values: List<Int>) (out: State)\n| each value in values | result |\n| out | fold(acc = State.Ready, ready()) |\n").unwrap();
|
||||
check("type State { Ready, error Broken }\nkeep(previous: State) (out: State) { out = previous }\nloop f(values: List<Int>, seed: State) (out: State)\n| each value in values | result |\n| out | fold(acc = seed, keep(acc)) |\ng(values: List<Int>, seed: State) (out: State) { ignored = f(values, seed)\nout = seed }").unwrap();
|
||||
let collect = "type State { Ready, error Broken }\nadd(value: State, previous: List<State>) (out: List<State>) { out = append(previous, value) }\nloop f(values: List<State>) (out: List<State>)\n| each value in values | result |\n| out | fold(acc = [], add(value, acc)) |\n";
|
||||
check(collect).unwrap();
|
||||
rejected(
|
||||
&format!("{collect}g(values: List<State>) () {{ ignored = f(values) }}"),
|
||||
"C012",
|
||||
"failure is not handled",
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user