Supporting a button library.
This commit is contained in:
@@ -355,4 +355,143 @@ mod tests {
|
||||
let missing = unassigned_pins(&meta, &assigned);
|
||||
assert!(missing.is_empty());
|
||||
}
|
||||
|
||||
// ── Button library tests ────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_list_available_includes_button() {
|
||||
let libs = list_available();
|
||||
assert!(
|
||||
libs.iter().any(|l| l.name == "button"),
|
||||
"Should include button library, found: {:?}",
|
||||
libs.iter().map(|l| &l.name).collect::<Vec<_>>()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_library_button() {
|
||||
let meta = find_library("button").expect("button should exist");
|
||||
assert_eq!(meta.name, "button");
|
||||
assert_eq!(meta.bus, "digital");
|
||||
assert_eq!(meta.pins, vec!["signal"]);
|
||||
assert_eq!(meta.interface, "button.h");
|
||||
assert_eq!(meta.implementation, "button_digital.h");
|
||||
assert_eq!(meta.mock, "button_mock.h");
|
||||
assert!(meta.simulation.is_some());
|
||||
assert_eq!(meta.simulation.as_deref(), Some("button_sim.h"));
|
||||
assert_eq!(meta.test.as_deref(), Some("test_button.cpp"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_button_creates_files() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let written = extract_library("button", tmp.path()).unwrap();
|
||||
|
||||
assert!(!written.is_empty(), "Should write at least one file");
|
||||
|
||||
let driver_dir = tmp.path().join("lib/drivers/button");
|
||||
assert!(driver_dir.exists(), "Driver directory should exist");
|
||||
assert!(driver_dir.join("button.h").exists(), "Interface should exist");
|
||||
assert!(driver_dir.join("button_digital.h").exists(), "Implementation should exist");
|
||||
assert!(driver_dir.join("button_mock.h").exists(), "Mock should exist");
|
||||
assert!(driver_dir.join("button_sim.h").exists(), "Simulation should exist");
|
||||
assert!(
|
||||
tmp.path().join("test/test_button.cpp").exists(),
|
||||
"Test file should be in test/ directory"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extract_button_files_are_ascii() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
extract_library("button", tmp.path()).unwrap();
|
||||
|
||||
let driver_dir = tmp.path().join("lib/drivers/button");
|
||||
for entry in fs::read_dir(&driver_dir).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let content = fs::read_to_string(entry.path()).unwrap();
|
||||
for (line_num, line) in content.lines().enumerate() {
|
||||
for (col, ch) in line.chars().enumerate() {
|
||||
assert!(
|
||||
ch.is_ascii(),
|
||||
"Non-ASCII in {} at {}:{}: U+{:04X}",
|
||||
entry.file_name().to_string_lossy(),
|
||||
line_num + 1, col + 1, ch as u32
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_button_cleans_up() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
extract_library("button", tmp.path()).unwrap();
|
||||
|
||||
assert!(is_installed_on_disk("button", tmp.path()));
|
||||
assert!(tmp.path().join("test/test_button.cpp").exists());
|
||||
remove_library_files("button", tmp.path()).unwrap();
|
||||
assert!(!is_installed_on_disk("button", tmp.path()));
|
||||
assert!(!tmp.path().join("test/test_button.cpp").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wiring_summary_digital() {
|
||||
let meta = find_library("button").unwrap();
|
||||
let summary = meta.wiring_summary();
|
||||
assert!(summary.contains("digital"), "Should mention digital: {}", summary);
|
||||
assert!(summary.contains("1"), "Should mention 1 pin: {}", summary);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_mode_digital() {
|
||||
let meta = find_library("button").unwrap();
|
||||
assert_eq!(meta.default_mode(), "input");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_button_pin_roles() {
|
||||
let meta = find_library("button").unwrap();
|
||||
let roles = meta.pin_roles();
|
||||
assert_eq!(roles.len(), 1);
|
||||
assert_eq!(roles[0].0, "signal");
|
||||
assert_eq!(roles[0].1, "button_signal");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_button_unassigned_pins() {
|
||||
let meta = find_library("button").unwrap();
|
||||
let assigned: Vec<String> = vec![];
|
||||
let missing = unassigned_pins(&meta, &assigned);
|
||||
assert_eq!(missing, vec!["button_signal"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_button_unassigned_pins_when_assigned() {
|
||||
let meta = find_library("button").unwrap();
|
||||
let assigned = vec!["button_signal".to_string()];
|
||||
let missing = unassigned_pins(&meta, &assigned);
|
||||
assert!(missing.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_both_libraries_coexist() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
extract_library("tmp36", tmp.path()).unwrap();
|
||||
extract_library("button", tmp.path()).unwrap();
|
||||
|
||||
// Both driver directories exist
|
||||
assert!(tmp.path().join("lib/drivers/tmp36").is_dir());
|
||||
assert!(tmp.path().join("lib/drivers/button").is_dir());
|
||||
|
||||
// Both test files exist
|
||||
assert!(tmp.path().join("test/test_tmp36.cpp").exists());
|
||||
assert!(tmp.path().join("test/test_button.cpp").exists());
|
||||
|
||||
// Remove one, the other survives
|
||||
remove_library_files("tmp36", tmp.path()).unwrap();
|
||||
assert!(!is_installed_on_disk("tmp36", tmp.path()));
|
||||
assert!(is_installed_on_disk("button", tmp.path()));
|
||||
assert!(tmp.path().join("test/test_button.cpp").exists());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user