From 7a8194f9e7d32dd5e7c40e759adc7bf30df9cf29 Mon Sep 17 00:00:00 2001 From: Amjad Alsharafi Date: Thu, 20 Aug 2020 12:47:59 +0800 Subject: [PATCH] Cartridge: added tests for error types --- cartridge/src/tests.rs | 52 +++++++++++++++++++++ cartridge/tests/roms/test_large_file.nes | Bin 0 -> 40977 bytes cartridge/tests/roms/test_wrong_header.nes | Bin 0 -> 40976 bytes 3 files changed, 52 insertions(+) create mode 100644 cartridge/tests/roms/test_large_file.nes create mode 100644 cartridge/tests/roms/test_wrong_header.nes diff --git a/cartridge/src/tests.rs b/cartridge/src/tests.rs index 3f31375..1593a2a 100644 --- a/cartridge/src/tests.rs +++ b/cartridge/src/tests.rs @@ -2,6 +2,58 @@ mod cartridge_tests { use crate::{Cartridge, CartridgeError}; + #[test] + fn cartridge_file_not_found() { + let err = Cartridge::from_file("./file/does/not/exists.nes") + .err() + .expect("Should get an error as the cartridge file does not exists"); + + if let CartridgeError::FileError(file_err) = err { + assert_eq!(file_err.kind(), std::io::ErrorKind::NotFound); + } else { + panic!("Should get file not found error"); + } + } + + #[test] + fn cartridge_extension_error_file() { + let err = Cartridge::from_file("./file/does/not/exists.notnes") + .err() + .expect("Should get an error as the cartridge has extension error"); + + if let CartridgeError::ExtensionError = err { + // passed + } else { + panic!("Should get extension error"); + } + } + + #[test] + fn cartridge_wrong_header() { + let err = Cartridge::from_file("./tests/roms/test_wrong_header.nes") + .err() + .expect("Should get an error as the cartridge has wrong header"); + + if let CartridgeError::HeaderError = err { + // passed + } else { + panic!("Should get header error"); + } + } + + #[test] + fn cartridge_large_file() { + let err = Cartridge::from_file("./tests/roms/test_large_file.nes") + .err() + .expect("Should get an error as the cartridge file is larger than expected"); + + if let CartridgeError::TooLargeFile(exceeded_size) = err { + assert_eq!(exceeded_size, 1); + } else { + panic!("Should get too large file error"); + } + } + #[test] fn test_ines1_cartridge_read() -> Result<(), CartridgeError> { let cartridge = Cartridge::from_file("./tests/roms/test_creation.nes")?; diff --git a/cartridge/tests/roms/test_large_file.nes b/cartridge/tests/roms/test_large_file.nes new file mode 100644 index 0000000000000000000000000000000000000000..b9f360e6506fb3bed6f5f2cd84d07cbea07722ab GIT binary patch literal 40977 zcmeIuF%1A93;-}d#{BT|;NQY5Y#$y-S_wV-#+u_P3>YwAz<>b*1`HT5V8DO@0|pEj zFkrxd0RsjM7%*VKfB^#r3>YwAz<>b*1`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r z3>YwAz<>b*1`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r3>YwAz<>b*1`HT5V8DO@ z0|pEjFkrxd0RsjM7%*VKfPqR37%*VKfB^#r3>YwAz<>b*1`HT5V8DO@0|pEjFkrxd Hf&T{@$K^{U literal 0 HcmV?d00001 diff --git a/cartridge/tests/roms/test_wrong_header.nes b/cartridge/tests/roms/test_wrong_header.nes new file mode 100644 index 0000000000000000000000000000000000000000..cc51226e1f857bb40960eef95df8fe82f944a8b5 GIT binary patch literal 40976 zcmeIuF%1A93;-}d#{8gwAOF`ZY#$y-S_!@T#G2zN3>YwAz<>b*1`HT5V8DO@0|pEj zFkrxd0RsjM7%*VKfB^#r3>YwAz<>b*1`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r z3>YwAz<>b*1`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r3>YwAz<>b*1`HT5V8DO@ z0|pEjFkrxd0RsjM7%*VKfPqR37%*VKfB^#r3>YwAz<>b*1`HT5V8DO@0|pEjFkrxd Hf$s+zepX6H literal 0 HcmV?d00001