use super::super::common::*;
#[test]
fn generic_pack_parsing() {
with_parse(
r#"
function f<a...>(...: a...)
end
type A = (a...) -> b...
"#,
ParseOptions::default(),
|result| {
let result = result.unwrap();
let [source_function, ty] = statement_kinds(result.root.as_slice()).exact();
let source_function = source_function
.as_function_declaration()
.expect("expected function declaration")
.function;
let ty = ty.as_type_alias().expect("expected type alias").ty;
let TypeKind::Function {
arg_types,
return_types,
..
} = ty.kind()
else {
panic!("expected function type alias");
};
assert!(matches!(
source_function.vararg_annotation.map(|annotation| annotation.kind()),
Some(TypePackKind::Generic { generic_name: name, .. }) if name == "a"
));
assert!(
matches!(arg_types, TypeList { types, tail_type: Some(tail) } if types.is_empty() && matches!(&tail.kind(), TypePackKind::Generic { generic_name: name, .. } if name == "a")
)
);
assert!(
matches!(&return_types.kind(), TypePackKind::Generic { generic_name: name, .. } if name == "b"
)
);
},
);
}
#[test]
fn generic_function_declaration_parsing() {
with_parse_ok_with_declarations("declare function f<a, b, c...>()", |result| {
let [statement] = statement_kinds(result.root.as_slice()).exact();
let declaration = statement
.as_declare_function()
.expect("expected generic function declaration");
assert_eq!(declaration.generics.len(), 2);
assert_eq!(declaration.generic_packs.len(), 1);
});
}
#[test]
fn parse_type_pack_type_parameters() {
parse_ok(
r#"
type Packed<T...> = () -> T...
type A<X...> = Packed<X...>
type B<X...> = Packed<...number>
type C<X...> = Packed<(number, X...)>
"#,
);
}