test(check): validator errors and warning
This commit is contained in:
parent
a311531508
commit
7b0faa7c1c
|
@ -12,9 +12,10 @@ fn parse(source_code: &str) -> UntypedModule {
|
|||
ast
|
||||
}
|
||||
|
||||
fn check(ast: UntypedModule) -> Result<TypedModule, (Vec<Warning>, Error)> {
|
||||
let kind = ModuleKind::Lib;
|
||||
|
||||
fn check_module(
|
||||
ast: UntypedModule,
|
||||
kind: ModuleKind,
|
||||
) -> Result<(Vec<Warning>, TypedModule), (Vec<Warning>, Error)> {
|
||||
let id_gen = IdGenerator::new();
|
||||
|
||||
let mut warnings = vec![];
|
||||
|
@ -25,7 +26,82 @@ fn check(ast: UntypedModule) -> Result<TypedModule, (Vec<Warning>, Error)> {
|
|||
|
||||
let result = ast.infer(&id_gen, kind, "test/project", &module_types, &mut warnings);
|
||||
|
||||
result.map_err(|e| (warnings, e))
|
||||
result
|
||||
.map(|o| (warnings.clone(), o))
|
||||
.map_err(|e| (warnings, e))
|
||||
}
|
||||
|
||||
fn check(ast: UntypedModule) -> Result<(Vec<Warning>, TypedModule), (Vec<Warning>, Error)> {
|
||||
check_module(ast, ModuleKind::Lib)
|
||||
}
|
||||
|
||||
fn check_validator(
|
||||
ast: UntypedModule,
|
||||
) -> Result<(Vec<Warning>, TypedModule), (Vec<Warning>, Error)> {
|
||||
check_module(ast, ModuleKind::Validator)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validator_illegal_return_type() {
|
||||
let source_code = r#"
|
||||
validator foo {
|
||||
fn(d, r, c) {
|
||||
1
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
assert!(matches!(
|
||||
check_validator(parse(source_code)),
|
||||
Err((_, Error::ValidatorMustReturnBool { .. }))
|
||||
))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validator_illegal_arity() {
|
||||
let source_code = r#"
|
||||
validator foo {
|
||||
fn(c) {
|
||||
True
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
assert!(matches!(
|
||||
check_validator(parse(source_code)),
|
||||
Err((_, Error::IncorrectValidatorArity { .. }))
|
||||
))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validator_correct_form() {
|
||||
let source_code = r#"
|
||||
validator foo {
|
||||
fn(d, r, c) {
|
||||
True
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
assert!(check_validator(parse(source_code)).is_ok())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validator_in_lib_warning() {
|
||||
let source_code = r#"
|
||||
validator foo {
|
||||
fn(c) {
|
||||
True
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
let (warnings, _) = check(parse(source_code)).unwrap();
|
||||
|
||||
assert!(matches!(
|
||||
warnings[0],
|
||||
Warning::ValidatorInLibraryModule { .. }
|
||||
))
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -775,7 +775,7 @@ The best thing to do from here is to remove it."#))]
|
|||
|
||||
#[error("Validators requires at least {} arguments.", at_least.to_string().purple().bold())]
|
||||
#[diagnostic(code("illegal::validator_arity"))]
|
||||
WrongValidatorArity {
|
||||
IncorrectValidatorArity {
|
||||
at_least: u8,
|
||||
#[label("not enough arguments")]
|
||||
location: Span,
|
||||
|
|
|
@ -278,7 +278,7 @@ fn infer_definition(
|
|||
let typed_params = typed_fun.arguments.drain(0..params_length).collect();
|
||||
|
||||
if typed_fun.arguments.len() < 2 {
|
||||
return Err(Error::WrongValidatorArity {
|
||||
return Err(Error::IncorrectValidatorArity {
|
||||
at_least: 2,
|
||||
location: typed_fun.location,
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue