Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Aug 3, 2023
1 parent c18f8f1 commit 8f32c71
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,58 @@ impl FromIterator<ResourceId> for ResourceSet {
Self(HashSet::from_iter(iter))
}
}

#[cfg(test)]
mod test {
use crate::resource::SignatureError;
use crate::types::type_param::{TypeArg, TypeArgError, TypeParam};
use crate::types::{ClassicType, HashableType, PrimType, SimpleType, TypeTag};

use super::{TypeDef, TypeDefTag};

#[test]
fn test_instantiate_typedef() {
let def = TypeDef {
name: "MyType".into(),
params: vec![TypeParam::ClassicType],
resource: Some("MyRsrc".into()),
description: "Some parameterised type".into(),
tag: TypeDefTag::FromParams(vec![0]),
};
let typ: SimpleType = def
.instantiate_concrete(vec![TypeArg::ClassicType(ClassicType::F64)])
.unwrap()
.into();
assert_eq!(typ.tag(), TypeTag::Classic);
let typ2: SimpleType = def
.instantiate_concrete([TypeArg::ClassicType(ClassicType::Hashable(
HashableType::String,
))])
.unwrap()
.into();
assert_eq!(typ2.tag(), TypeTag::Hashable);

// And some bad arguments...firstly, wrong kind of TypeArg:
assert_eq!(
def.instantiate_concrete([TypeArg::HashableType(HashableType::String)]),
Err(SignatureError::TypeArgMismatch(TypeArgError::TypeMismatch(
TypeArg::HashableType(HashableType::String),
TypeParam::ClassicType
)))
);
// Too few arguments:
assert_eq!(
def.instantiate_concrete([]).unwrap_err(),
SignatureError::TypeArgMismatch(TypeArgError::WrongNumber(0, 1))
);
// Too many arguments:
assert_eq!(
def.instantiate_concrete([
TypeArg::ClassicType(ClassicType::F64),
TypeArg::ClassicType(ClassicType::F64),
])
.unwrap_err(),
SignatureError::TypeArgMismatch(TypeArgError::WrongNumber(2, 1))
);
}
}

0 comments on commit 8f32c71

Please sign in to comment.