Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,81 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn constants_keep_precise_values_and_ranges_without_runtime_checks() {
|
||||
let program = check(
|
||||
r#"
|
||||
type State { Ready, error Broken }
|
||||
const count: Int >= 1 = 4
|
||||
const alias = count
|
||||
const rate: Decimal = alias
|
||||
const minimum = -9223372036854775808
|
||||
const fraction = 0.1000000000000000000000000000000001
|
||||
const empty: Text? = null
|
||||
const state: State = Ready
|
||||
const failure = State.Broken
|
||||
f() (out: Int >= 1) { out = alias }
|
||||
g() (out: Decimal) { out = rate + fraction }
|
||||
stop() () { fail(failure) }
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(program.functions[0].constant_references.len(), 1);
|
||||
assert_eq!(program.functions[1].constant_references.len(), 2);
|
||||
assert_eq!(
|
||||
program.functions[2].failures,
|
||||
[Failure::Choice(1, 1)].into()
|
||||
);
|
||||
rejected(
|
||||
"const count = 4\nf() () { check count >= 1 else Invalid }",
|
||||
"C012",
|
||||
"redundant",
|
||||
);
|
||||
rejected(
|
||||
"type State { Ready, error Broken }\nconst broken = State.Broken\nf() (out: Int) { ignored = broken\nout = 1 }",
|
||||
"C012",
|
||||
"failure is not handled",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constants_reject_dynamic_values_cycles_duplicates_and_invalid_literals() {
|
||||
for text in [
|
||||
"const value = [1]",
|
||||
"const value = 1 + 2",
|
||||
"const value = length(\"text\")",
|
||||
"const value = not true",
|
||||
"type Item { value: Int }\nconst value = Item { value = 1 }",
|
||||
] {
|
||||
rejected(text, "C005", "scalar literal");
|
||||
}
|
||||
rejected("const value = null", "C005", "concrete scalar");
|
||||
rejected("const value = Ready", "C004", "unknown");
|
||||
rejected("const value: Int >= 1 = 0", "C008", "cannot prove");
|
||||
rejected("const value: Int = true", "C005", "expected");
|
||||
rejected("const value = 1e999", "C008", "finite");
|
||||
rejected("const value = 9223372036854775808", "C008", "outside");
|
||||
rejected("const value = 1\nconst value = 2", "C001", "duplicate");
|
||||
rejected(
|
||||
"const value = 1\nvalue() (n: Int) { n = 2 }",
|
||||
"C001",
|
||||
"share a name",
|
||||
);
|
||||
rejected("const value = value", "C010", "cyclic constant");
|
||||
rejected(
|
||||
"const first = second\nconst second = 1",
|
||||
"C004",
|
||||
"preceding",
|
||||
);
|
||||
rejected(
|
||||
"type State { Ready, Item(value: Int) }\nconst state = State.Ready",
|
||||
"C005",
|
||||
"payload-free",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_names_do_not_turn_into_constant_references() {
|
||||
let program = check("const value = 4\nf(value: Int) (out: Int) { out = value }").unwrap();
|
||||
assert!(program.functions[0].constant_references.is_empty());
|
||||
check("const value = 4\ntype Item { value: Int }\nf(input: Item) (out: Int) { out = input.value }").unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user