60 lines
2.0 KiB
Rust
60 lines
2.0 KiB
Rust
use dever_runtime::value::uuid::Uuid;
|
|
|
|
const LOWER: &str = "550e8400-e29b-41d4-a716-446655440000";
|
|
const UPPER: &str = "550E8400-E29B-41D4-A716-446655440000";
|
|
|
|
#[test]
|
|
fn uuid_is_an_inline_canonical_value_and_map_key() {
|
|
let value = Uuid::parse(UPPER).unwrap();
|
|
assert_eq!(value.to_text(), LOWER);
|
|
assert_eq!(value, Uuid::parse(LOWER).unwrap());
|
|
assert_eq!(std::mem::size_of::<Uuid>(), 16);
|
|
assert!(!std::mem::needs_drop::<Uuid>());
|
|
let mut values = dever_runtime::collection::Map::with_capacity(1);
|
|
values.insert(value, 1);
|
|
values.insert(Uuid::parse(LOWER).unwrap(), 2);
|
|
assert_eq!(values.len(), 1);
|
|
assert_eq!(values.get(&value), Some(&2));
|
|
}
|
|
|
|
#[test]
|
|
fn malformed_hyphens_hex_and_alternative_forms_are_rejected_without_panics() {
|
|
for value in [
|
|
"",
|
|
"550e8400e29b41d4a716446655440000",
|
|
"{550e8400-e29b-41d4-a716-446655440000}",
|
|
"urn:uuid:550e8400-e29b-41d4-a716-446655440000",
|
|
"00000000-0000-0000-0000-00000000000-",
|
|
] {
|
|
assert_eq!(Uuid::parse(value), None, "{value}");
|
|
}
|
|
for index in 0..36 {
|
|
let mut text = LOWER.as_bytes().to_vec();
|
|
text[index] = if matches!(index, 8 | 13 | 18 | 23) {
|
|
b'0'
|
|
} else {
|
|
b'-'
|
|
};
|
|
assert!(
|
|
Uuid::parse(std::str::from_utf8(&text).unwrap()).is_none(),
|
|
"{index}"
|
|
);
|
|
}
|
|
assert!(Uuid::parse("ffffffff-ffff-ffff-ffff-ffffffffffff").is_some());
|
|
assert!(Uuid::parse("00000000-0000-0000-0000-000000000000").is_some());
|
|
assert!(Uuid::parse(&format!("é{}", &LOWER[2..])).is_none());
|
|
}
|
|
|
|
#[cfg(feature = "wire")]
|
|
#[test]
|
|
fn uuid_wire_preserves_the_strict_source_format() {
|
|
let value: Uuid = serde_json::from_str(&format!("\"{UPPER}\"")).unwrap();
|
|
assert_eq!(
|
|
serde_json::to_string(&value).unwrap(),
|
|
format!("\"{LOWER}\"")
|
|
);
|
|
for value in ["0", "null", r#""550e8400e29b41d4a716446655440000""#] {
|
|
assert!(serde_json::from_str::<Uuid>(value).is_err());
|
|
}
|
|
}
|