use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KindCountDrift {
pub kind_name: &'static str,
pub expected: u16,
pub actual: u16,
}
impl fmt::Display for KindCountDrift {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}::COUNT drifted: expected {} (last reviewed value), found {} -- re-review \
every exhaustive match over {} in crates/vj-codegen (see this module's doc \
comment for the specific match sites) before updating the expected count",
self.kind_name, self.expected, self.actual, self.kind_name
)
}
}
impl std::error::Error for KindCountDrift {}
pub fn assert_kind_count(
kind_name: &'static str,
actual: u16,
expected: u16,
) -> Result<(), KindCountDrift> {
if actual == expected {
Ok(())
} else {
Err(KindCountDrift {
kind_name,
expected,
actual,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn expression_kind_count_matches_pinned_sv_lang_version() {
assert_eq!(
assert_kind_count("ExpressionKind", sv_lang::kinds::ExpressionKind::COUNT, 41,),
Ok(()),
"sv_lang::kinds::ExpressionKind::COUNT no longer matches this module's pinned \
expected value -- re-review crates/vj-codegen/src/expr.rs::lower_expr's \
exhaustive match before updating the expected count"
);
}
#[test]
fn statement_kind_count_matches_pinned_sv_lang_version() {
assert_eq!(
assert_kind_count("StatementKind", sv_lang::kinds::StatementKind::COUNT, 32),
Ok(()),
"sv_lang::kinds::StatementKind::COUNT no longer matches this module's pinned \
expected value -- re-review crates/vj-codegen/src/func.rs::lower_stmt's \
exhaustive match before updating the expected count"
);
}
#[test]
fn timing_control_kind_count_matches_pinned_sv_lang_version() {
assert_eq!(
assert_kind_count(
"TimingControlKind",
sv_lang::kinds::TimingControlKind::COUNT,
10,
),
Ok(()),
"sv_lang::kinds::TimingControlKind::COUNT no longer matches this module's pinned \
expected value -- re-review crates/vj-sim/src/process/cfg.rs::CfgBuilder::\
walk_timed's dispatch before updating the expected count"
);
}
#[test]
fn symbol_kind_count_matches_pinned_sv_lang_version() {
assert_eq!(
assert_kind_count("SymbolKind", sv_lang::kinds::SymbolKind::COUNT, 101),
Ok(()),
"sv_lang::kinds::SymbolKind::COUNT no longer matches this module's pinned \
expected value -- re-review the netlist SymbolKind whitelist matches before \
updating the expected count"
);
}
#[test]
fn kind_count_guard_fires_when_a_bump_is_simulated() {
assert_eq!(
assert_kind_count("ExpressionKind", 42, 41),
Err(KindCountDrift {
kind_name: "ExpressionKind",
expected: 41,
actual: 42,
})
);
}
#[test]
fn assert_kind_count_is_ok_when_counts_genuinely_match() {
assert_eq!(assert_kind_count("X", 7, 7), Ok(()));
}
}