From 7b0faa7c1cc2d8623241314e0ba296432f3faa09 Mon Sep 17 00:00:00 2001 From: rvcas Date: Wed, 15 Feb 2023 22:21:20 -0500 Subject: [PATCH] test(check): validator errors and warning --- crates/aiken-lang/src/tests/check.rs | 84 ++++++++++++++++++++++++++-- crates/aiken-lang/src/tipo/error.rs | 2 +- crates/aiken-lang/src/tipo/infer.rs | 2 +- 3 files changed, 82 insertions(+), 6 deletions(-) diff --git a/crates/aiken-lang/src/tests/check.rs b/crates/aiken-lang/src/tests/check.rs index e6b46c2c..cadd75a2 100644 --- a/crates/aiken-lang/src/tests/check.rs +++ b/crates/aiken-lang/src/tests/check.rs @@ -12,9 +12,10 @@ fn parse(source_code: &str) -> UntypedModule { ast } -fn check(ast: UntypedModule) -> Result, Error)> { - let kind = ModuleKind::Lib; - +fn check_module( + ast: UntypedModule, + kind: ModuleKind, +) -> Result<(Vec, TypedModule), (Vec, Error)> { let id_gen = IdGenerator::new(); let mut warnings = vec![]; @@ -25,7 +26,82 @@ fn check(ast: UntypedModule) -> Result, 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, TypedModule), (Vec, Error)> { + check_module(ast, ModuleKind::Lib) +} + +fn check_validator( + ast: UntypedModule, +) -> Result<(Vec, TypedModule), (Vec, 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] diff --git a/crates/aiken-lang/src/tipo/error.rs b/crates/aiken-lang/src/tipo/error.rs index 2d4c0722..878aee77 100644 --- a/crates/aiken-lang/src/tipo/error.rs +++ b/crates/aiken-lang/src/tipo/error.rs @@ -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, diff --git a/crates/aiken-lang/src/tipo/infer.rs b/crates/aiken-lang/src/tipo/infer.rs index 348063b9..dc476f5e 100644 --- a/crates/aiken-lang/src/tipo/infer.rs +++ b/crates/aiken-lang/src/tipo/infer.rs @@ -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, });