test: add ResourceTable tests (#4185)

This commit is contained in:
ecyrbe 2020-02-29 18:35:45 +01:00 committed by GitHub
parent a29343c7d6
commit 199fb195f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -83,3 +83,60 @@ impl ResourceTable {
pub trait Resource: Downcast + Any + Send {}
impl<T> Resource for T where T: Downcast + Any + Send {}
impl_downcast!(Resource);
#[cfg(test)]
mod tests {
use super::*;
struct FakeResource {
not_empty: u128,
}
impl FakeResource {
fn new(value: u128) -> FakeResource {
FakeResource { not_empty: value }
}
}
#[test]
fn test_create_resource_table_default() {
let table = ResourceTable::default();
assert_eq!(table.map.len(), 0);
}
#[test]
fn test_add_to_resource_table_not_empty() {
let mut table = ResourceTable::default();
table.add("fake1", Box::new(FakeResource::new(1)));
table.add("fake2", Box::new(FakeResource::new(2)));
assert_eq!(table.map.len(), 2);
}
#[test]
fn test_add_to_resource_table_are_contiguous() {
let mut table = ResourceTable::default();
let rid1 = table.add("fake1", Box::new(FakeResource::new(1)));
let rid2 = table.add("fake2", Box::new(FakeResource::new(2)));
assert_eq!(rid1 + 1, rid2);
}
#[test]
fn test_get_from_resource_table_is_what_was_given() {
let mut table = ResourceTable::default();
let rid = table.add("fake", Box::new(FakeResource::new(7)));
let resource = table.get::<FakeResource>(rid);
assert_eq!(resource.unwrap().not_empty, 7);
}
#[test]
fn test_remove_from_resource_table() {
let mut table = ResourceTable::default();
let rid1 = table.add("fake1", Box::new(FakeResource::new(1)));
let rid2 = table.add("fake2", Box::new(FakeResource::new(2)));
assert_eq!(table.map.len(), 2);
table.close(rid1);
assert_eq!(table.map.len(), 1);
table.close(rid2);
assert_eq!(table.map.len(), 0);
}
}