Skip to content

Commit 391134a

Browse files
committed
Improve our testing of core functionality on NSObjectProtocol
1 parent 0a73287 commit 391134a

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

crates/objc2/src/declare/mod.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,15 @@ impl Drop for ProtocolBuilder {
760760

761761
#[cfg(test)]
762762
mod tests {
763+
use core::hash::Hasher;
764+
use std::collections::hash_map::DefaultHasher;
765+
use std::hash::Hash;
766+
763767
use super::*;
764768
use crate::mutability::Immutable;
765769
use crate::rc::Id;
766-
use crate::runtime::{NSObject, NSZone, __NSCopying as NSCopying};
767-
use crate::test_utils;
768-
use crate::{declare_class, msg_send, ClassType, ProtocolType};
770+
use crate::runtime::{NSObject, NSObjectProtocol, NSZone, __NSCopying as NSCopying};
771+
use crate::{declare_class, extern_methods, msg_send, test_utils, ClassType, ProtocolType};
769772

770773
#[test]
771774
fn test_alignment() {
@@ -1157,4 +1160,55 @@ mod tests {
11571160

11581161
let _ = GenericDeclareClass::<()>::class();
11591162
}
1163+
1164+
#[test]
1165+
fn test_inherited_nsobject_methods_work() {
1166+
declare_class!(
1167+
#[derive(Debug, PartialEq, Eq, Hash)]
1168+
struct Custom;
1169+
1170+
unsafe impl ClassType for Custom {
1171+
type Super = NSObject;
1172+
type Mutability = Immutable;
1173+
const NAME: &'static str = "TestInheritedNSObjectMethodsWork";
1174+
}
1175+
);
1176+
1177+
extern_methods!(
1178+
unsafe impl Custom {
1179+
#[method_id(new)]
1180+
fn new() -> Id<Self>;
1181+
}
1182+
);
1183+
1184+
let obj1 = Custom::new();
1185+
let obj2 = Custom::new();
1186+
1187+
// isEqual:
1188+
assert_eq!(obj1, obj1);
1189+
assert_ne!(obj1, obj2);
1190+
1191+
// description
1192+
let expected =
1193+
format!("Custom {{ __superclass: ManuallyDrop {{ value: <TestInheritedNSObjectMethodsWork: {obj1:p}> }} }}");
1194+
assert_eq!(format!("{obj1:?}"), expected);
1195+
1196+
// hash
1197+
let mut hashstate1 = DefaultHasher::new();
1198+
let mut hashstate2 = DefaultHasher::new();
1199+
1200+
obj1.hash(&mut hashstate1);
1201+
obj1.hash(&mut hashstate2);
1202+
1203+
assert_eq!(hashstate1.finish(), hashstate2.finish());
1204+
1205+
let mut hashstate2 = DefaultHasher::new();
1206+
obj2.hash(&mut hashstate2);
1207+
assert_ne!(hashstate1.finish(), hashstate2.finish());
1208+
1209+
// isKindOfClass:
1210+
assert!(obj1.is_kind_of::<NSObject>());
1211+
assert!(obj1.is_kind_of::<Custom>());
1212+
assert!(obj1.is_kind_of::<Custom>());
1213+
}
11601214
}

crates/objc2/src/runtime/nsobject.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,15 @@ mod tests {
357357
assert!(obj.is_kind_of::<NSObject>());
358358
assert!(obj.is_kind_of::<__RcTestObject>());
359359
}
360+
361+
#[test]
362+
fn test_retain_same() {
363+
let obj1 = NSObject::new();
364+
let ptr1 = Id::as_ptr(&obj1);
365+
366+
let obj2 = obj1.clone();
367+
let ptr2 = Id::as_ptr(&obj2);
368+
369+
assert_eq!(ptr1, ptr2);
370+
}
360371
}

0 commit comments

Comments
 (0)