use tokitai_core::{assert_compatible_with, CORE_VERSION};
fn fmt(template: &str) -> String {
template.replace("{}", "")
}
fn two_component_prefix() -> String {
let dot = CORE_VERSION
.find('.')
.expect("CORE_VERSION must contain at least one '.'");
let after_first = &CORE_VERSION[dot + 1..];
let second_dot = after_first
.find('.')
.expect("CORE_VERSION must contain a second '.'");
CORE_VERSION[..dot + 1 + second_dot].to_string()
}
#[test]
fn positive_exact_match_passes_at_runtime() {
assert_compatible_with(CORE_VERSION);
}
#[test]
fn positive_prefix_match_passes_at_runtime() {
let prefix = two_component_prefix();
assert_compatible_with(&prefix);
}
#[test]
fn positive_v_prefix_is_accepted() {
let mut v_prefixed = String::with_capacity(CORE_VERSION.len() + 1);
v_prefixed.push('v');
v_prefixed.push_str(CORE_VERSION);
assert_compatible_with(&v_prefixed);
}
#[test]
fn positive_v_prefixed_two_component() {
let mut v_prefixed = String::with_capacity(20);
v_prefixed.push('v');
v_prefixed.push_str(&two_component_prefix());
assert_compatible_with(&v_prefixed);
}
#[test]
fn negative_mismatch_panics_at_runtime() {
let result = std::panic::catch_unwind(|| {
assert_compatible_with("999.0.0");
});
assert!(
result.is_err(),
"assert_compatible_with must panic on a major drift",
);
}
#[test]
fn negative_minor_drift_panics_at_runtime() {
let dot = CORE_VERSION.find('.').unwrap();
let after_first = &CORE_VERSION[dot + 1..];
let second_dot = after_first.find('.').unwrap();
let minor_str = &after_first[..second_dot];
let minor: u64 = minor_str.parse().unwrap_or(0);
let bumped = if minor == 0 { 999 } else { minor - 1 };
let literal = format!("{}.{}.0", &CORE_VERSION[..dot], bumped);
let result = std::panic::catch_unwind(|| {
assert_compatible_with(&literal);
});
assert!(
result.is_err(),
"assert_compatible_with must panic on a minor drift",
);
}
#[test]
fn negative_patch_drift_panics_at_runtime() {
let dot = CORE_VERSION.find('.').unwrap();
let after_first = &CORE_VERSION[dot + 1..];
let second_dot = after_first.find('.').unwrap();
let minor_str = &after_first[..second_dot];
let minor: u64 = minor_str.parse().unwrap_or(0);
let bumped = if minor == u64::MAX { 0 } else { minor + 1 };
let literal = format!("{}.{}.0", &CORE_VERSION[..dot], bumped);
let result = std::panic::catch_unwind(|| {
assert_compatible_with(&literal);
});
assert!(
result.is_err(),
"assert_compatible_with must panic on a patch drift (when expected arity is 3)",
);
}
#[test]
fn negative_malformed_literal_panics_at_runtime() {
let result = std::panic::catch_unwind(|| {
assert_compatible_with("not-a-version");
});
assert!(
result.is_err(),
"assert_compatible_with must panic on a malformed literal",
);
}
#[test]
fn negative_leading_zero_component_panics() {
let result = std::panic::catch_unwind(|| {
assert_compatible_with("01.0.0");
});
assert!(
result.is_err(),
"assert_compatible_with must panic on a non-canonical SemVer literal",
);
}
#[test]
fn core_version_is_non_empty_semver_like() {
assert!(!CORE_VERSION.is_empty());
let bytes = CORE_VERSION.as_bytes();
let mut saw_digit = false;
for b in bytes {
if b.is_ascii_digit() {
saw_digit = true;
}
}
assert!(
saw_digit,
"CORE_VERSION must contain at least one digit (got `{}`)",
CORE_VERSION,
);
}
#[test]
fn constant_format_uses_cargo_pkg_version() {
let manifest_version = env!("CARGO_PKG_VERSION");
assert_eq!(
CORE_VERSION, manifest_version,
"CORE_VERSION must match the manifest's CARGO_PKG_VERSION",
);
}
#[allow(dead_code)]
fn _format_smoke_test() {
let _ = fmt("hello");
}
const _: () = {
tokitai_core::assert_compatible_with("0.6");
};
const _: () = {
tokitai_core::assert_compatible_with("0");
};
const _: () = {
tokitai_core::assert_compatible_with("v0.6");
};