diff --git a/rust-bindings/rust/src/collection_ref/tests.rs b/rust-bindings/rust/src/collection_ref/tests.rs new file mode 100644 index 00000000..ac0a9e08 --- /dev/null +++ b/rust-bindings/rust/src/collection_ref/tests.rs @@ -0,0 +1,77 @@ +use super::*; +use std::hash::{Hash, Hasher}; +use std::collections::hash_map::DefaultHasher; + +fn hash(v: &impl Hash) -> u64 { + let mut s = DefaultHasher::new(); + v.hash(&mut s); + s.finish() +} + +#[test] +fn same_value_should_be_equal() { + let r = CollectionRef::new("io.gitlab.fkrull", "ref").unwrap(); + assert_eq!(r, r); +} + +#[test] +fn equal_values_should_be_equal() { + let a = CollectionRef::new("io.gitlab.fkrull", "ref").unwrap(); + let b = CollectionRef::new("io.gitlab.fkrull", "ref").unwrap(); + assert_eq!(a, b); +} + +#[test] +fn equal_values_without_collection_id_should_be_equal() { + let a = CollectionRef::new(None, "ref-name").unwrap(); + let b = CollectionRef::new(None, "ref-name").unwrap(); + assert_eq!(a, b); +} + +#[test] +fn different_values_should_not_be_equal() { + let a = CollectionRef::new("io.gitlab.fkrull", "ref1").unwrap(); + let b = CollectionRef::new("io.gitlab.fkrull", "ref2").unwrap(); + assert_ne!(a, b); +} + +#[test] +fn new_with_invalid_collection_id_should_return_none() { + let r = CollectionRef::new(".abc", "ref"); + assert_eq!(r, None); +} + +#[test] +fn hash_for_equal_values_should_be_equal() { + let a = CollectionRef::new("io.gitlab.fkrull", "ref").unwrap(); + let b = CollectionRef::new("io.gitlab.fkrull", "ref").unwrap(); + assert_eq!(hash(&a), hash(&b)); +} + +#[test] +fn hash_for_values_with_different_collection_id_should_be_different() { + let a = CollectionRef::new("io.gitlab.fkrull1", "ref").unwrap(); + let b = CollectionRef::new("io.gitlab.fkrull2", "ref").unwrap(); + assert_ne!(hash(&a), hash(&b)); +} + +#[test] +fn hash_for_values_with_different_ref_id_should_be_different() { + let a = CollectionRef::new("io.gitlab.fkrull", "ref-1").unwrap(); + let b = CollectionRef::new("io.gitlab.fkrull", "ref-2").unwrap(); + assert_ne!(hash(&a), hash(&b)); +} + +#[test] +fn hash_should_be_different_if_collection_id_is_absent() { + let a = CollectionRef::new("io.gitlab.fkrull", "ref").unwrap(); + let b = CollectionRef::new(None, "ref").unwrap(); + assert_ne!(hash(&a), hash(&b)); +} + +#[test] +fn clone_should_be_equal_to_original_value() { + let a = CollectionRef::new("io.gitlab.fkrull", "ref").unwrap(); + let b = a.clone(); + assert_eq!(a, b); +}