crates/nornir-humanize/src/selftest.rs
//! # Functional-status emit bridge — `nornir-humanize` → test matrix
//!
//! The single internal call site this crate uses to push a *functional* row into
//! the [`nornir_testmatrix`] matrix, so `nornir test --features testmatrix`
//! records whether `nornir-humanize`'s real surfaces (the exact-string byte /
//! count / duration / percent vectors and their boundary cases) ACTUALLY behave
//! — without eyeballing anything.
//!
//! ## Release strips everything
//! The `testmatrix` cargo feature gates the whole bridge: with it OFF (the
//! release default) [`emit`] is an `#[inline]` no-op and `nornir-testmatrix` is
//! not even linked. Lit ONLY under `cargo … --features testmatrix`.
/// The aspect tag every self-emitted row carries (matches `functional_row`).
pub const ASPECT_FUNCTIONAL: &str = "functional";
/// Push ONE functional-status row for a single real check (feature ON).
///
/// * `component` — the unit under test (becomes the row's `suite`).
/// * `check` — the specific assertion's name (becomes the row's `test_name`).
/// * `ok` — the **real** verdict.
/// * `detail` — the **measured** value.
#[cfg(feature = "testmatrix")]
#[inline]
pub fn emit(component: &str, check: &str, ok: bool, detail: &str) {
nornir_testmatrix::functional_status(component, check, ok, detail);
}
/// No-op stub when the `testmatrix` feature is OFF (release builds). Inlined and
/// empty so the call — and, with the wrap-the-call-site pattern, the detail
/// string-building — is stripped entirely.
#[cfg(not(feature = "testmatrix"))]
#[inline]
pub fn emit(_component: &str, _check: &str, _ok: bool, _detail: &str) {}
/// `assert!`-with-emit: assert on the real return value AND record the verdict as
/// a matrix row. The `assert!` is the test's actual gate; the emit is the matrix
/// observation.
#[macro_export]
macro_rules! assert_emit {
($component:expr, $check:expr, $ok:expr, $($detail:tt)+) => {{
let __ok: bool = $ok;
let __detail = ::std::format!($($detail)+);
$crate::selftest::emit($component, $check, __ok, &__detail);
::std::assert!(__ok, "{}::{} — {}", $component, $check, __detail);
}};
}
// ─── inject-and-assert wiring test (feature ON only) ─────────────────────────
#[cfg(all(test, feature = "testmatrix"))]
mod wiring {
use crate::{bytes, count, duration, percent};
use std::time::Duration;
/// Exercise `nornir-humanize`'s real surfaces, emit an honest row per
/// surface, then drain the process-global functional buffer and assert the
/// rows landed — proving the emit bridge is wired end to end.
#[test]
fn emits_honest_rows_for_humanize_surfaces() {
let kib = bytes(1536);
assert_emit!("nornir-humanize", "bytes_kib", kib == "1.5 KiB", "bytes(1536)={kib}");
// Boundary: 1024 is the first KiB, 1023 is still bytes — the RED guard.
let boundary = bytes(1024) == "1.0 KiB" && bytes(1023) == "1023 B";
assert_emit!(
"nornir-humanize",
"bytes_boundary_1023_1024",
boundary,
"1023->{} 1024->{}",
bytes(1023),
bytes(1024)
);
let m = count(3_400_000);
assert_emit!("nornir-humanize", "count_millions", m == "3.4M", "count(3.4e6)={m}");
let dur = duration(Duration::from_millis(123_000));
assert_emit!("nornir-humanize", "duration_2m03s", dur == "2m 03s", "duration={dur}");
let pct = percent(0.123);
assert_emit!("nornir-humanize", "percent_one_decimal", pct == "12.3%", "percent={pct}");
// RED surface: a WRONG expectation must be observed as false, proving the
// emitter records honest failures (verified by neutralizing the check).
let wrong = bytes(1024) == "1024 B";
crate::selftest::emit(
"nornir-humanize",
"binary_threshold_not_off_by_one",
!wrong,
&format!("bytes(1024)={}", bytes(1024)),
);
let rows = nornir_testmatrix::drain_functional_rows();
let has = |t: &str, pass: bool| {
rows.iter().any(|r| {
r.suite == "nornir-humanize" && r.test_name == t && (r.status == "pass") == pass
})
};
assert!(has("bytes_kib", true), "rows={rows:?}");
assert!(has("bytes_boundary_1023_1024", true), "rows={rows:?}");
assert!(has("count_millions", true), "rows={rows:?}");
assert!(has("duration_2m03s", true), "rows={rows:?}");
assert!(has("percent_one_decimal", true), "rows={rows:?}");
assert!(has("binary_threshold_not_off_by_one", true), "rows={rows:?}");
assert!(rows.iter().all(|r| r.aspect == "functional"), "rows={rows:?}");
}
}