Add type-checker sanity tests for list patterns.
This commit is contained in:
parent
56e90fba21
commit
6649821200
|
@ -0,0 +1,128 @@
|
||||||
|
use crate::{
|
||||||
|
ast::{ModuleKind, TypedModule, UntypedModule},
|
||||||
|
builtins, parser,
|
||||||
|
tipo::error::{Error, Warning},
|
||||||
|
IdGenerator,
|
||||||
|
};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
fn parse(source_code: &str) -> UntypedModule {
|
||||||
|
let kind = ModuleKind::Lib;
|
||||||
|
let (ast, _) = parser::module(source_code, kind).expect("Failed to parse module");
|
||||||
|
ast
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check(ast: UntypedModule) -> Result<TypedModule, (Vec<Warning>, Error)> {
|
||||||
|
let kind = ModuleKind::Lib;
|
||||||
|
|
||||||
|
let id_gen = IdGenerator::new();
|
||||||
|
|
||||||
|
let mut warnings = vec![];
|
||||||
|
|
||||||
|
let mut module_types = HashMap::new();
|
||||||
|
module_types.insert("aiken".to_string(), builtins::prelude(&id_gen));
|
||||||
|
module_types.insert("aiken/builtin".to_string(), builtins::plutus(&id_gen));
|
||||||
|
|
||||||
|
let result = ast.infer(&id_gen, kind, "test/project", &module_types, &mut warnings);
|
||||||
|
|
||||||
|
result.map_err(|e| (warnings, e))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_pattern_1() {
|
||||||
|
let source_code = r#"
|
||||||
|
test foo() {
|
||||||
|
let xs = [1, 2, 3]
|
||||||
|
let [x] = xs
|
||||||
|
x == 1
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
assert!(matches!(
|
||||||
|
check(parse(source_code)),
|
||||||
|
Err((_, Error::NotExhaustivePatternMatch { .. }))
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_pattern_2() {
|
||||||
|
let source_code = r#"
|
||||||
|
test foo() {
|
||||||
|
let xs = [1, 2, 3]
|
||||||
|
let x = when xs is {
|
||||||
|
[x] -> x
|
||||||
|
}
|
||||||
|
x == 1
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
assert!(matches!(
|
||||||
|
check(parse(source_code)),
|
||||||
|
Err((_, Error::NotExhaustivePatternMatch { .. }))
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_pattern_3() {
|
||||||
|
let source_code = r#"
|
||||||
|
test foo() {
|
||||||
|
let xs = [1, 2, 3]
|
||||||
|
let x = when xs is {
|
||||||
|
[x] -> x
|
||||||
|
[x, ..] -> x
|
||||||
|
}
|
||||||
|
x == 1
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
assert!(matches!(
|
||||||
|
check(parse(source_code)),
|
||||||
|
Err((_, Error::NotExhaustivePatternMatch { .. }))
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_pattern_4() {
|
||||||
|
let source_code = r#"
|
||||||
|
test foo() {
|
||||||
|
let xs = [1, 2, 3]
|
||||||
|
let x = when xs is {
|
||||||
|
[] -> 1
|
||||||
|
[x] -> x
|
||||||
|
[x, ..] if x > 10 -> x
|
||||||
|
}
|
||||||
|
x == 1
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
assert!(matches!(
|
||||||
|
check(parse(source_code)),
|
||||||
|
Err((_, Error::NotExhaustivePatternMatch { .. }))
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_pattern_5() {
|
||||||
|
let source_code = r#"
|
||||||
|
test foo() {
|
||||||
|
let xs = [1, 2, 3]
|
||||||
|
let x = when xs is {
|
||||||
|
[] -> 1
|
||||||
|
[_, ..] -> 1
|
||||||
|
}
|
||||||
|
x == 1
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
assert!(check(parse(source_code)).is_ok())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn list_pattern_6() {
|
||||||
|
let source_code = r#"
|
||||||
|
test foo() {
|
||||||
|
let xs = [1, 2, 3]
|
||||||
|
let x = when xs is {
|
||||||
|
[x, ..] -> 1
|
||||||
|
_ -> 1
|
||||||
|
}
|
||||||
|
x == 1
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
assert!(check(parse(source_code)).is_ok())
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod check;
|
||||||
mod format;
|
mod format;
|
||||||
mod lexer;
|
mod lexer;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
|
Loading…
Reference in New Issue