Add tests to lyra-resource

This commit is contained in:
SeanOMik 2023-09-21 09:36:44 -04:00
parent 5828d00b74
commit b5079a659a
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
5 changed files with 98 additions and 18 deletions

View File

@ -9,18 +9,3 @@ pub use texture::*;
pub mod loader;
pub use loader::*;
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}

View File

@ -51,7 +51,11 @@ impl ResourceLoader for TextureLoader {
file.read_to_end(&mut buf)?;
// load the image and construct Resource<Texture>
let image = image::load_from_memory(&buf)?;
let image = image::load_from_memory(&buf)
.map_err(|e| match e {
ImageError::IoError(e) => LoaderError::IOError(e),
_ => LoaderError::DecodingError(e.into()),
})?;
let texture = Texture {
image,
};
@ -59,4 +63,45 @@ impl ResourceLoader for TextureLoader {
Ok(Arc::new(res))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn get_image(path: &str) -> String {
let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap();
format!("{manifest}/test_files/img/{path}")
}
/// Ensure that `does_support_file` works
#[test]
fn check_support() {
let loader = TextureLoader::default();
let extensions = loader.extensions();
let fake_paths: Vec<String> = extensions.iter().map(|e| format!("a.{}", e)).collect();
for path in fake_paths.iter() {
assert!(loader.does_support_file(&path));
}
}
#[test]
fn check_unsupport() {
let loader = TextureLoader::default();
assert_eq!(loader.does_support_file("test.gltf"), false);
}
/// Tests loading an image
#[test]
fn image_load() {
let loader = TextureLoader::default();
loader.load(&get_image("squiggles.png")).unwrap();
}
#[test]
fn image_load_unsupported() {
let loader = TextureLoader::default();
assert!(loader.load(&get_image("squiggles.gltf")).is_err());
}
}

View File

@ -2,7 +2,7 @@ use std::sync::Arc;
use uuid::Uuid;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ResourceState {
Loading,
Ready,

View File

@ -39,7 +39,6 @@ impl From<LoaderError> for RequestError {
}
}
#[derive(Default)]
pub struct ResourceManager {
resources: HashMap<String, Arc<dyn ResourceStorage>>,
loaders: Vec<Box<dyn ResourceLoader>>,
@ -80,4 +79,55 @@ impl ResourceManager {
}
}
}
}
#[cfg(test)]
mod tests {
use std::io;
use crate::{Texture, ResourceState};
use super::*;
fn get_image(path: &str) -> String {
let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap();
format!("{manifest}/test_files/img/{path}")
}
#[test]
fn load_image() {
let mut man = ResourceManager::new();
let res = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
assert_eq!(res.state, ResourceState::Ready);
let img = res.data.as_ref();
img.unwrap();
}
/// Ensures that only one copy of the same thing made
#[test]
fn ensure_single() {
let mut man = ResourceManager::new();
let res = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
assert_eq!(Arc::strong_count(&res), 2);
let resagain = man.request::<Texture>(&get_image("squiggles.png")).unwrap();
assert_eq!(Arc::strong_count(&resagain), 3);
}
/// Ensures that an error is returned when a file that doesn't exist is requested
#[test]
fn ensure_none() {
let mut man = ResourceManager::new();
let res = man.request::<Texture>(&get_image("squigglesfff.png"));
let err = res.err().unwrap();
assert!(
match err {
// make sure the error is NotFound
RequestError::Loader(LoaderError::IOError(e)) if e.kind() == io::ErrorKind::NotFound => true,
_ => false
}
);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB