use super::{Id, TypedId};
fn admits<I: Id>() {}
#[test]
fn the_vocabulary_admits_its_members() {
admits::<i8>();
admits::<i16>();
admits::<i32>();
admits::<i64>();
admits::<i128>();
admits::<u8>();
admits::<u16>();
admits::<u32>();
admits::<u64>();
admits::<u128>();
admits::<core::num::NonZeroI8>();
admits::<core::num::NonZeroI16>();
admits::<core::num::NonZeroI32>();
admits::<core::num::NonZeroI64>();
admits::<core::num::NonZeroI128>();
admits::<core::num::NonZeroU8>();
admits::<core::num::NonZeroU16>();
admits::<core::num::NonZeroU32>();
admits::<core::num::NonZeroU64>();
admits::<core::num::NonZeroU128>();
admits::<uuid::Uuid>();
admits::<String>();
admits::<smol_bytes::Utf8Bytes>();
admits::<core::net::IpAddr>();
admits::<core::net::Ipv4Addr>();
admits::<core::net::Ipv6Addr>();
admits::<core::net::SocketAddr>();
admits::<core::net::SocketAddrV4>();
admits::<core::net::SocketAddrV6>();
admits::<[u8; 1]>();
admits::<[u8; 12]>();
admits::<[u8; 16]>();
admits::<[u8; 32]>();
}
#[cfg(feature = "smol_str")]
#[test]
fn the_text_member_under_its_feature_is_admitted() {
admits::<smol_str::SmolStr>();
}
#[cfg(feature = "mongo")]
#[test]
fn the_bson_members_are_admitted() {
admits::<bson::Uuid>();
admits::<bson::oid::ObjectId>();
}
#[test]
fn a_pointer_over_a_member_is_a_member() {
admits::<std::sync::Arc<u64>>();
admits::<std::rc::Rc<u64>>();
admits::<Box<u64>>();
admits::<std::sync::Arc<uuid::Uuid>>();
admits::<std::sync::Arc<String>>();
admits::<std::sync::Arc<[u8; 16]>>();
admits::<std::sync::Arc<core::net::IpAddr>>();
admits::<std::sync::Arc<Box<u64>>>();
}
#[test]
fn a_typed_id_the_entity_names_back_is_a_member() {
use crate::Identifiable;
struct Scene {
id: TypedId<uuid::Uuid, Scene>,
}
struct Slug {
id: TypedId<String, Slug>,
}
struct Held {
id: TypedId<std::sync::Arc<u64>, Held>,
}
impl Identifiable for Scene {
type Id = TypedId<uuid::Uuid, Scene>;
fn id_ref(&self) -> &Self::Id {
&self.id
}
}
impl Identifiable for Slug {
type Id = TypedId<String, Slug>;
fn id_ref(&self) -> &Self::Id {
&self.id
}
}
impl Identifiable for Held {
type Id = TypedId<std::sync::Arc<u64>, Held>;
fn id_ref(&self) -> &Self::Id {
&self.id
}
}
admits::<TypedId<uuid::Uuid, Scene>>();
admits::<TypedId<String, Slug>>();
admits::<TypedId<std::sync::Arc<u64>, Held>>();
}
#[test]
fn the_entity_marker_carries_no_auto_trait() {
fn is_send<T: Send>() {}
fn is_sync<T: Sync>() {}
is_send::<TypedId<u64, std::rc::Rc<()>>>();
is_sync::<TypedId<u64, std::rc::Rc<()>>>();
}
#[test]
fn the_entity_marker_implements_nothing() {
struct Marker;
fn is_copy<T: Copy>() {}
fn is_eq<T: Eq>() {}
fn is_hash<T: core::hash::Hash>() {}
fn is_debug<T: core::fmt::Debug>() {}
is_copy::<TypedId<u64, Marker>>();
is_eq::<TypedId<u64, Marker>>();
is_hash::<TypedId<u64, Marker>>();
is_debug::<TypedId<u64, Marker>>();
}
#[test]
fn the_wrapper_is_the_id_it_holds() {
struct Scene;
let seven = TypedId::<u64, Scene>::new(7);
assert_eq!(seven.id(), 7);
assert_eq!(seven.id_ref(), &7);
assert_eq!(seven, TypedId::new(7));
assert_ne!(seven, TypedId::new(8));
assert_eq!(format!("{seven:?}"), "TypedId(7)");
let mut rows = std::collections::HashMap::new();
rows.insert(seven, "a-scene");
assert_eq!(rows.get(&TypedId::<u64, Scene>::new(7)), Some(&"a-scene"));
}
#[test]
fn a_bare_id_cannot_tell_two_records_apart() {
use crate::{Identifiable, refers_to};
struct Take(u64);
struct Cut(u64);
impl Identifiable for Take {
type Id = u64;
fn id_ref(&self) -> &u64 {
&self.0
}
}
impl Identifiable for Cut {
type Id = u64;
fn id_ref(&self) -> &u64 {
&self.0
}
}
refers_to::<<Cut as Identifiable>::Id, Take>();
refers_to::<<Take as Identifiable>::Id, Cut>();
}