mod util; #[cfg(feature = "local-rt")] mod tests { use jlrs::{ convert::to_symbol::ToSymbol, data::{ managed::{type_var::TypeVar, union::Union, union_all::UnionAll}, types::{ abstract_type::{ AbstractArray, AbstractChar, AbstractString, Integer, Real, RefTypeConstructor, }, construct_type::{ ArrayTypeConstructor, ConstantIsize, ConstructType, Name, TypeVarConstructor, TypeVarName, TypeVars, UnionTypeConstructor, }, }, }, prelude::*, tvar, tvars, }; use super::util::JULIA; fn construct_array_type() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>().scope(|mut frame| { let ty = > as ConstructType>::construct_type(&mut frame); assert!(ty.is::()); let inner_ty = unsafe {ty.cast_unchecked::()}; assert!(inner_ty.is::()); let elem_param = inner_ty.parameter(0).unwrap(); assert_eq!(elem_param, AbstractChar::construct_type(&mut frame)); assert!(elem_param.cast::().unwrap().is_abstract()); let rank_param = inner_ty.parameter(1).unwrap(); assert_eq!(rank_param.unbox::().unwrap(), 2); Ok(()) }) .unwrap(); }); } fn construct_unranked_array_type() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>().scope(|mut frame| { let ty = >> as ConstructType>::construct_type(&mut frame); let ua = ty.cast::().unwrap(); let base = ua.base_type(); let elem_param = base.parameter(0).unwrap(); assert_eq!(elem_param, AbstractChar::construct_type(&mut frame)); let rank_param = base.parameter(1).unwrap(); assert!(rank_param.is::()); Ok(()) }) .unwrap(); }); } fn construct_untyped_array_type() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>().scope(|mut frame| { let ty = >, ConstantIsize<1>> as ConstructType>::construct_type(&mut frame); let ua = ty.cast::().unwrap(); let base = ua.base_type(); let elem_param = base.parameter(0).unwrap(); assert!(elem_param.is::()); let rank_param = base.parameter(1).unwrap(); assert_eq!(rank_param.unbox::().unwrap(), 1); Ok(()) }) .unwrap(); }); } fn construct_array_type_with_bounded_type() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { let ty = , AbstractChar>, ConstantIsize<1>, > as ConstructType>::construct_type(&mut frame); let ua = ty.cast::().unwrap(); let base = ua.base_type(); let elem_param = base.parameter(0).unwrap(); assert!(elem_param.is::()); let rank_param = base.parameter(1).unwrap(); assert_eq!(rank_param.unbox::().unwrap(), 1); Ok(()) }) .unwrap(); }); } fn construct_union_type() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>().scope(|mut frame| { let ty = as ConstructType>::construct_type(&mut frame); let un = ty.cast::().unwrap(); let variants = un.variants(); assert_eq!(variants.len(), 2); let abstr_char_ty = AbstractChar::construct_type(&mut frame); let integer_ty = Integer::construct_type(&mut frame); if variants[0] == abstr_char_ty { assert!(variants[1] == integer_ty); } else { assert!(variants[0] == integer_ty); assert!(variants[1] == abstr_char_ty); } Ok(()) }) .unwrap(); }); } fn construct_union_type_three_variants() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { let ty = , > as ConstructType>::construct_type(&mut frame); let un = ty.cast::().unwrap(); let variants = un.variants(); assert_eq!(variants.len(), 3); Ok(()) }) .unwrap(); }); } fn construct_union_type_overlapping_variants() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { let ty = , > as ConstructType>::construct_type(&mut frame); let un = ty.cast::().unwrap(); let variants = un.variants(); assert_eq!(variants.len(), 2); // Integer <: Real Ok(()) }) .unwrap(); }); } fn construct_with_env() { JULIA.with(|j| { let mut frame = StackFrame::new(); let mut jlrs = j.borrow_mut(); jlrs.instance(&mut frame) .returning::>() .scope(|mut frame| { type Foo = encode_as_constant_bytes!("Foo"); type Env = tvars!( tvar!(Foo; Integer), tvar!('M'), tvar!('A'; AbstractArray) ); type Ty = RefTypeConstructor; let sym = Foo::symbol(&frame); assert_eq!(sym.as_str().unwrap(), "Foo"); let env = Env::into_env(&mut frame); let ty = Ty::construct_type_with_env(&mut frame, &env); assert!(ty.is::()); let ua = unsafe { ty.cast_unchecked::() }; let unwrapped_ty = ua.body().cast::().unwrap(); let param = unwrapped_ty.parameter(0).unwrap(); assert!(param.is::()); let tvar = unsafe { param.cast_unchecked::() }; let env_param = env.get("A".to_symbol(&frame)).unwrap(); assert_eq!(tvar.as_value(), env_param); Ok(()) }) .unwrap(); }); } #[test] fn type_construction_tests() { construct_array_type(); construct_unranked_array_type(); construct_untyped_array_type(); construct_array_type_with_bounded_type(); construct_union_type(); construct_union_type_three_variants(); construct_union_type_overlapping_variants(); construct_with_env(); } }