use quote::ToTokens as _;
use super::super::{BitWidth, FlagBit, FlagsModel};
fn mirror() -> FlagsModel {
FlagsModel {
remote: Some(syn::parse_quote!(upstream::Disposition)),
..declared()
}
}
fn declared() -> FlagsModel {
FlagsModel {
krate: syn::parse_quote!(::ingraph),
vis: syn::parse_quote!(pub),
name: ident("Disposition"),
width: BitWidth::U32,
bits: vec![
bit("Default", None, 0x1),
bit("Forced", None, 0x2),
bit("Hi", Some("HEARING_IMPAIRED"), 0x4),
],
attrs: vec![syn::parse_quote!(#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)])],
remote: None,
wire_name: None,
}
}
fn narrow() -> FlagsModel {
FlagsModel {
width: BitWidth::U8,
..mirror()
}
}
fn bit(name: &str, upstream: Option<&str>, value: u128) -> FlagBit {
FlagBit {
ident: ident(name),
rename: None,
upstream: upstream.map(ident),
value,
attrs: Vec::new(),
}
}
fn ident(name: &str) -> syn::Ident {
syn::Ident::new(name, proc_macro2::Span::call_site())
}
fn rendered(model: &FlagsModel) -> String {
model.emit().to_token_stream().to_string().replace(' ', "")
}
#[test]
fn a_declaration_naming_no_upstream_emits_what_it_always_did() {
let plain = rendered(&declared());
let mirrored = rendered(&mirror());
assert!(
mirrored.starts_with(&plain),
"the mirror is the declared expansion plus a block, not a different expansion"
);
assert!(
!plain.contains("__IngraphMirrorWidth"),
"and a declaration with no upstream carries not one token of the axis"
);
assert!(!plain.contains("::core::convert::From"));
}
#[test]
fn the_crossing_retains_a_bit_this_declaration_never_named() {
let emitted = rendered(&mirror());
assert!(emitted.contains("impl::core::convert::From<upstream::Disposition>forDisposition"));
assert!(emitted.contains("impl::core::convert::From<Disposition>forupstream::Disposition"));
assert_eq!(
emitted.matches("from_bits_retain(").count(),
2,
"one CALL per leg, and no other constructor — the trailing `(` is what keeps the width \
obligation's own prose, which quotes the word, out of the count"
);
assert!(
!emitted.contains("from_bits_truncate"),
"nothing truncates a bit on the way through"
);
}
#[test]
fn every_restated_bit_is_pinned_against_its_upstream_const() {
let emitted = rendered(&mirror());
assert_eq!(
emitted.matches("::core::assert!").count(),
3,
"one per bit and no more"
);
assert!(emitted.contains("Disposition::DEFAULT.bits()as::core::primitive::u128"));
assert!(emitted.contains("upstream::Disposition::DEFAULT.bits()as::core::primitive::u128"));
assert!(
emitted.contains("upstream::Disposition::HEARING_IMPAIRED.bits()"),
"the renamed bit is pinned against the upstream `const` it names"
);
assert!(
emitted.contains("Disposition::HI.bits()"),
"and against its OWN word, which the rename did not move"
);
}
#[test]
fn the_width_obligation_admits_the_declared_width_and_no_other() {
for (model, primitive, other) in [(mirror(), "u32", "u8"), (narrow(), "u8", "u32")] {
let emitted = rendered(&model);
assert!(emitted.contains(&format!(
"impl__IngraphMirrorWidthfor::core::primitive::{primitive}"
)));
assert!(
!emitted.contains(&format!(
"impl__IngraphMirrorWidthfor::core::primitive::{other}"
)),
"one width is admitted, and it is the declared one"
);
assert!(
emitted.contains("diagnostic::on_unimplemented"),
"and it reports itself"
);
}
}
#[test]
fn a_mirror_has_no_gate_beside_its_conversions() {
let emitted = rendered(&mirror());
assert!(!emitted.contains("fnremote"));
assert!(!emitted.contains("AsRef"));
assert!(!emitted.contains("Deref"));
assert!(!emitted.contains("Borrow"));
}