test(check): validator errors and warning

This commit is contained in:
rvcas 2023-02-15 22:21:20 -05:00 committed by Lucas
parent a311531508
commit 7b0faa7c1c
3 changed files with 82 additions and 6 deletions

View File

@ -12,9 +12,10 @@ fn parse(source_code: &str) -> UntypedModule {
ast ast
} }
fn check(ast: UntypedModule) -> Result<TypedModule, (Vec<Warning>, Error)> { fn check_module(
let kind = ModuleKind::Lib; ast: UntypedModule,
kind: ModuleKind,
) -> Result<(Vec<Warning>, TypedModule), (Vec<Warning>, Error)> {
let id_gen = IdGenerator::new(); let id_gen = IdGenerator::new();
let mut warnings = vec![]; 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); 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] #[test]

View File

@ -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())] #[error("Validators requires at least {} arguments.", at_least.to_string().purple().bold())]
#[diagnostic(code("illegal::validator_arity"))] #[diagnostic(code("illegal::validator_arity"))]
WrongValidatorArity { IncorrectValidatorArity {
at_least: u8, at_least: u8,
#[label("not enough arguments")] #[label("not enough arguments")]
location: Span, location: Span,

View File

@ -278,7 +278,7 @@ fn infer_definition(
let typed_params = typed_fun.arguments.drain(0..params_length).collect(); let typed_params = typed_fun.arguments.drain(0..params_length).collect();
if typed_fun.arguments.len() < 2 { if typed_fun.arguments.len() < 2 {
return Err(Error::WrongValidatorArity { return Err(Error::IncorrectValidatorArity {
at_least: 2, at_least: 2,
location: typed_fun.location, location: typed_fun.location,
}); });