use super::{APP_ID, CASING, ENGINE, PHOSPHOR, SEAT, rgba, shapes, svg};
fn asset(name: &str) -> String {
let at = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.join("assets")
.join(name);
std::fs::read_to_string(&at).unwrap_or_else(|why| panic!("{}: {why}", at.display()))
}
#[test]
fn the_tracked_vector_source_is_exactly_what_the_generator_emits() {
assert_eq!(
asset(&format!("{APP_ID}.svg")),
svg(),
"assets/{APP_ID}.svg is stale — re-emit it with `make icon`, never by hand"
);
}
#[test]
fn the_app_id_the_desktop_entry_and_the_icon_name_are_one_word() {
let entry = asset(&format!("{APP_ID}.desktop"));
for line in [
format!("Icon={APP_ID}"),
format!("StartupWMClass={APP_ID}"),
format!("Exec={APP_ID}"),
] {
assert!(
entry.lines().any(|held| held == line),
"the desktop entry has no {line:?}:\n{entry}"
);
}
}
#[test]
fn the_tracked_entry_carries_exactly_one_exec_and_it_is_generic() {
let entry = asset(&format!("{APP_ID}.desktop"));
let execs: Vec<&str> = entry
.lines()
.filter(|line| line.starts_with("Exec="))
.collect();
assert_eq!(
execs,
vec![format!("Exec={APP_ID}")],
"the installed copy is where an absolute Exec belongs, and there is one \
of them — `make icon-seats` rewrites every ^Exec= line:\n{entry}"
);
}
#[test]
fn every_primitive_is_one_element_and_the_order_is_the_list_s() {
let source = svg();
let elements: Vec<&str> = source
.lines()
.filter(|line| line.starts_with(" <"))
.collect();
assert_eq!(elements.len(), shapes().len());
assert!(elements[0].starts_with(" <line "), "{source}");
assert!(elements[1].contains("fill=\"none\""), "{source}");
assert!(elements[2].contains("fill=\"#5fd1b4\""), "{source}");
assert!(source.ends_with("</svg>\n"), "{source}");
}
fn spot(px: &[u8], n: u16, x: i64, y: i64) -> [u8; 4] {
let at = |v: i64| usize::try_from(v * i64::from(n) / 1000).unwrap_or(0);
let i = (at(y) * usize::from(n) + at(x)) * 4;
let taken = px.get(i..i + 4).unwrap_or(&[0; 4]);
[taken[0], taken[1], taken[2], taken[3]]
}
#[test]
fn the_raster_paints_a_hollow_engine_a_solid_seat_and_a_wire_between() {
let n = 64_u16;
let px = rgba(n);
assert_eq!(px.len(), usize::from(n) * usize::from(n) * 4);
assert_eq!(spot(&px, n, 20, 20), [0; 4], "the canvas is transparent");
assert_eq!(
spot(&px, n, SEAT.0, SEAT.1),
[PHOSPHOR.red, PHOSPHOR.green, PHOSPHOR.blue, 255],
"the seat is solid"
);
assert_eq!(
spot(&px, n, ENGINE.0 + 73, ENGINE.1 + 68)[3],
0,
"the engine is a ring, so inside it is the canvas"
);
assert_eq!(
spot(&px, n, ENGINE.0, ENGINE.1),
[CASING.red, CASING.green, CASING.blue, 255],
"and the wire runs to its centre rather than stopping at its edge"
);
assert_eq!(
spot(&px, n, ENGINE.0, ENGINE.1 - 200),
[PHOSPHOR.red, PHOSPHOR.green, PHOSPHOR.blue, 255],
"and its stroke is on it"
);
assert_eq!(
spot(
&px,
n,
i64::midpoint(SEAT.0, ENGINE.0),
i64::midpoint(SEAT.1, ENGINE.1)
),
[CASING.red, CASING.green, CASING.blue, 255],
"the wire is the dimmer ink"
);
}
#[test]
fn a_raster_of_no_size_is_empty() {
assert!(rgba(0).is_empty());
}
#[test]
fn the_edges_are_antialiased_and_every_pixel_is_a_mean_of_the_two_inks() {
let n = 16_u16;
let px = rgba(n);
let mut soft = 0;
for pixel in px.chunks_exact(4) {
let (red, alpha) = (pixel.first(), pixel.get(3));
if alpha.copied().is_some_and(|a| a > 0 && a < 255) {
soft += 1;
}
if alpha.copied().is_some_and(|a| a > 0) {
let red = red.copied().unwrap_or(0);
assert!(
(CASING.red..=PHOSPHOR.red).contains(&red),
"a pixel is neither ink nor a mean of them: {pixel:?}"
);
}
}
assert!(soft > 0, "nothing on the mark's edges was ever split");
}