Add all type parsing tests
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::vec;
|
||||
|
||||
use mclangc::{common::{Loc, loc::LocBox}, parser::{self, ast::{Ident, Keyword, Program, Punctuation, TokenType, statement::{Enum, Struct}, typ::Type}}, tokeniser::Token, validator::predefined::{BuiltinType, get_builtin_from_name, load_builtin}};
|
||||
use anyhow::bail;
|
||||
use mclangc::{common::{Loc, loc::LocBox}, parser::{self, ast::{Ident, Keyword, Program, Punctuation, TokenType, statement::{Enum, Struct}, typ::Type}}, tokeniser::Token, validator::{self, predefined::{BuiltinType, get_builtin_from_name, load_builtin}}};
|
||||
|
||||
|
||||
|
||||
@@ -20,9 +21,63 @@ fn get_prog() -> Program {
|
||||
name: Ident::new("MyEnum"),
|
||||
fields: vec![],
|
||||
}));
|
||||
prog.types.insert(Ident::new("MyType"), LocBox::new(&loc, Type::Owned(Ident::new("MyStruct"))));
|
||||
prog.types.insert(Ident::new("MyType2"), LocBox::new(&loc, Type::Owned(Ident::new("MyType"))));
|
||||
prog.types.insert(Ident::new("MyType3"), LocBox::new(&loc, Type::Owned(Ident::new("MyType2")).as_ref()));
|
||||
prog.types.insert(Ident::new("MyTypeInf"), LocBox::new(&loc, Type::Owned(Ident::new("MyTypeInf"))));
|
||||
prog.types.insert(Ident::new("MyTypeInfIndirect"), LocBox::new(&loc, Type::Owned(Ident::new("MyType4"))));
|
||||
prog.types.insert(Ident::new("MyType4"), LocBox::new(&loc, Type::Owned(Ident::new("MyType5"))));
|
||||
prog.types.insert(Ident::new("MyType5"), LocBox::new(&loc, Type::Owned(Ident::new("MyType4"))));
|
||||
|
||||
prog
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_type_alias_simple() {
|
||||
let mut tokens = vec![
|
||||
Token::new_test(TokenType::Ident(Ident::new("MyType")))
|
||||
];
|
||||
let ret = parser::typ::parse_type(&mut tokens);
|
||||
let mut prog = get_prog();
|
||||
let ret = validator::validate_type(&mut prog, &ret.unwrap());
|
||||
assert!(ret.is_ok());
|
||||
assert_eq!(ret.unwrap(), Type::Owned(Ident::new("MyStruct")))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_type_alias_deep() {
|
||||
let mut tokens = vec![
|
||||
Token::new_test(TokenType::Ident(Ident::new("MyType3")))
|
||||
];
|
||||
let ret = parser::typ::parse_type(&mut tokens);
|
||||
let mut prog = get_prog();
|
||||
let ret = validator::validate_type(&mut prog, &ret.unwrap());
|
||||
assert!(ret.is_ok());
|
||||
assert_eq!(ret.unwrap(), Type::Owned(Ident::new("MyStruct")))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_type_alias_recursive_simple() {
|
||||
let mut tokens = vec![
|
||||
Token::new_test(TokenType::Ident(Ident::new("MyTypeInf")))
|
||||
];
|
||||
let ret = parser::typ::parse_type(&mut tokens);
|
||||
let mut prog = get_prog();
|
||||
let ret = validator::validate_type(&mut prog, &ret.unwrap());
|
||||
assert!(ret.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_type_alias_recursive_indirect() {
|
||||
let mut tokens = vec![
|
||||
Token::new_test(TokenType::Ident(Ident::new("MyType4")))
|
||||
];
|
||||
let ret = parser::typ::parse_type(&mut tokens);
|
||||
let mut prog = get_prog();
|
||||
let ret = validator::validate_type(&mut prog, &ret.unwrap());
|
||||
assert!(ret.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_type_named_struct() {
|
||||
let mut tokens = vec![
|
||||
|
||||
Reference in New Issue
Block a user