refactor(vm) checkup - ensure tests pass

This commit is contained in:
Rachel Powers
2024-05-09 14:41:32 -07:00
parent 096e272b07
commit 095e17a4fb
5 changed files with 86 additions and 8 deletions

1
Cargo.lock generated
View File

@@ -672,6 +672,7 @@ dependencies = [
name = "ic10emu"
version = "0.2.3"
dependencies = [
"color-eyre",
"const-crc32",
"getrandom",
"itertools",

View File

@@ -39,4 +39,5 @@ time = { version = "0.3.34", features = [
] }
[dev-dependencies]
color-eyre = "0.6.3"
serde_json = "1.0.117"

View File

@@ -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::<Operand>();
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::<u32>().is_ok());
assert_eq!(le.get_value(), value.unwrap().parse::<u32>().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(())
}
}

View File

@@ -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 = {

View File

@@ -245,3 +245,42 @@ pub struct ItemLogicMemoryTemplate {
pub slots: Vec<SlotInfo>,
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<String, ObjectTemplate>,
}
#[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(())
}
}