From 095e17a4fb907b796b5f2e6f20b2a707f2184012 Mon Sep 17 00:00:00 2001 From: Rachel Powers <508861+Ryex@users.noreply.github.com> Date: Thu, 9 May 2024 14:41:32 -0700 Subject: [PATCH] refactor(vm) checkup - ensure tests pass --- Cargo.lock | 1 + ic10emu/Cargo.toml | 1 + ic10emu/src/grammar.rs | 37 +++++++++++++++++++++++----- ic10emu/src/interpreter.rs | 16 ++++++++++-- ic10emu/src/vm/object/templates.rs | 39 ++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 082a122..8a204c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -672,6 +672,7 @@ dependencies = [ name = "ic10emu" version = "0.2.3" dependencies = [ + "color-eyre", "const-crc32", "getrandom", "itertools", diff --git a/ic10emu/Cargo.toml b/ic10emu/Cargo.toml index cdeab67..a533408 100644 --- a/ic10emu/Cargo.toml +++ b/ic10emu/Cargo.toml @@ -39,4 +39,5 @@ time = { version = "0.3.34", features = [ ] } [dev-dependencies] +color-eyre = "0.6.3" serde_json = "1.0.117" diff --git a/ic10emu/src/grammar.rs b/ic10emu/src/grammar.rs index 0a72b65..8c51bd7 100644 --- a/ic10emu/src/grammar.rs +++ b/ic10emu/src/grammar.rs @@ -714,10 +714,22 @@ impl Number { #[cfg(test)] mod tests { + use color_eyre::eyre::Ok; + use strum::EnumProperty; + use super::*; + static INIT: std::sync::Once = std::sync::Once::new(); + + fn setup() { + INIT.call_once(|| { + let _ = color_eyre::install(); + }) + } + #[test] - fn parse_register() { + fn parse_register() -> color_eyre::Result<()> { + setup(); let op = "requestingot".parse::(); assert_eq!( op.unwrap(), @@ -725,10 +737,12 @@ mod tests { name: "requestingot".to_owned() }) ); + Ok(()) } #[test] - fn successful_parse() { + fn successful_parse() -> color_eyre::Result<()> { + setup(); let parsed = parse("s d0 Setting 0 # This is a comment\n"); dbg!(&parsed); assert_eq!( @@ -776,10 +790,12 @@ mod tests { comment: None, },], ); + Ok(()) } #[test] - fn parse_code_chunk() { + fn parse_code_chunk() -> color_eyre::Result<()> { + setup(); let code = "# This is a comment\n\ define a_def 10\n\ define a_hash HASH(\"This is a String\")\n\ @@ -1049,15 +1065,19 @@ mod tests { }, ], ); + Ok(()) } #[test] - fn test_operand_display() { + fn test_operand_display() -> color_eyre::Result<()> { + setup(); + #[track_caller] fn test_roundtrip(s: &str) { let o: Operand = s.parse().expect("test string should parse with FromStr"); assert_eq!(o.to_string(), s); } + test_roundtrip("r0"); test_roundtrip("r15"); test_roundtrip("rr4"); @@ -1093,10 +1113,12 @@ mod tests { test_roundtrip(r#"HASH("StructureFurnace")"#); test_roundtrip("$abcd"); test_roundtrip("%1001"); + Ok(()) } #[test] - fn all_generated_enums_have_value() { + fn all_generated_enums_have_value() -> color_eyre::Result<()> { + setup(); use strum::IntoEnumIterator; for lt in LogicType::iter() { println!("testing LogicType.{lt}"); @@ -1133,13 +1155,16 @@ mod tests { assert!(value.unwrap().parse::().is_ok()); assert_eq!(le.get_value(), value.unwrap().parse::().unwrap()); } + Ok(()) } #[test] - fn bad_parse_does_not_panic() { + fn bad_parse_does_not_panic() -> color_eyre::Result<()> { + setup(); let code = "move foo -"; let parsed = parse(code); assert!(parsed.is_err()); println!("{}", parsed.unwrap_err()); + Ok(()) } } diff --git a/ic10emu/src/interpreter.rs b/ic10emu/src/interpreter.rs index e6f1d32..6749348 100644 --- a/ic10emu/src/interpreter.rs +++ b/ic10emu/src/interpreter.rs @@ -2511,8 +2511,19 @@ mod tests { use super::*; + use color_eyre::eyre::Ok; + + static INIT: std::sync::Once = std::sync::Once::new(); + + fn setup() { + INIT.call_once(|| { + let _ = color_eyre::install(); + }) + } + #[test] - fn batch_modes() -> Result<(), VMError> { + fn batch_modes() -> color_eyre::Result<()> { + setup(); let mut vm = VM::new(); let ic = vm.add_ic(None).unwrap(); let ic_id = { @@ -2540,7 +2551,8 @@ mod tests { } #[test] - fn stack() -> Result<(), VMError> { + fn stack() -> color_eyre::Result<()> { + setup(); let mut vm = VM::new(); let ic = vm.add_ic(None).unwrap(); let ic_id = { diff --git a/ic10emu/src/vm/object/templates.rs b/ic10emu/src/vm/object/templates.rs index 1926939..615679a 100644 --- a/ic10emu/src/vm/object/templates.rs +++ b/ic10emu/src/vm/object/templates.rs @@ -245,3 +245,42 @@ pub struct ItemLogicMemoryTemplate { pub slots: Vec, pub memory: MemoryInfo, } + +#[cfg(test)] +mod tests { + + use serde_derive::Deserialize; + use serde_json; + use std::collections::BTreeMap; + use std::fs::File; + use std::io::BufReader; + use std::path::PathBuf; + + use super::ObjectTemplate; + + static INIT: std::sync::Once = std::sync::Once::new(); + + fn setup() { + INIT.call_once(|| { + let _ = color_eyre::install(); + }) + } + + #[derive(Debug, Deserialize)] + struct Database { + pub prefabs: BTreeMap, + } + + + #[test] + fn all_database_prefabs_parse() -> color_eyre::Result<()> { + setup(); + let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + d = d.parent().unwrap().join("data").join("database.json"); + println!("loading database from {}", d.display()); + + let database: Database = serde_json::from_reader(BufReader::new(File::open(d)?))?; + + Ok(()) + } +}