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, 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, seed: Pair) (out: Pair) {{ ignored = rotate(values, seed)\nout = seed }}")).unwrap(); rejected( &format!( "{prefix}missing(values: List, seed: Pair) (out: State) {{ ignored = rotate(values, seed)\nout = seed.left }}" ), "C012", "failure is not handled", ); rejected( &format!( "{prefix}partial(values: List, 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) (acc: List, stop: Bool) { acc = append(previous, value)\nstop = true }\nloop first(values: List) (out: List)\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)\n| each", "(out: List, all: List)\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, 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, 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) (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) (out: List) { out = append(previous, value) }\nloop collect(values: List) (out: List)\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) (out: Int?)\n| each value in values | result |\n| out | foldUntil(previous = null, find(value)) |\n").unwrap(); for source in [ "loop f(values: List) (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) (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) (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) (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) (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) (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) (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) (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) (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) (out: List) {{ out = append(previous, next(flag)) }}\nloop growing(values: List, seed: List) (out: List)\n| each value in values | result |\n| out | fold(acc = seed, add(value, acc)) |\nmissing(values: List, seed: List) (out: List) {{ ignored = growing(values, seed)\nout = seed }}" ), "C012", "failure is not handled", ); rejected( &format!( "{prefix}loop f(values: List) (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) (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, seed: State) (out: State)\n| each value in values | result |\n| out | fold(acc = seed, keep(acc)) |\ng(values: List, seed: State) (out: State) { ignored = f(values, seed)\nout = seed }").unwrap(); let collect = "type State { Ready, error Broken }\nadd(value: State, previous: List) (out: List) { out = append(previous, value) }\nloop f(values: List) (out: List)\n| each value in values | result |\n| out | fold(acc = [], add(value, acc)) |\n"; check(collect).unwrap(); rejected( &format!("{collect}g(values: List) () {{ ignored = f(values) }}"), "C012", "failure is not handled", ); }