Skip to content

Commit

Permalink
Improve our testing of core functionality on NSObjectProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 7, 2023
1 parent 0a73287 commit 391134a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
60 changes: 57 additions & 3 deletions crates/objc2/src/declare/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,12 +760,15 @@ impl Drop for ProtocolBuilder {

#[cfg(test)]
mod tests {
use core::hash::Hasher;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hash;

use super::*;
use crate::mutability::Immutable;
use crate::rc::Id;
use crate::runtime::{NSObject, NSZone, __NSCopying as NSCopying};
use crate::test_utils;
use crate::{declare_class, msg_send, ClassType, ProtocolType};
use crate::runtime::{NSObject, NSObjectProtocol, NSZone, __NSCopying as NSCopying};
use crate::{declare_class, extern_methods, msg_send, test_utils, ClassType, ProtocolType};

#[test]
fn test_alignment() {
Expand Down Expand Up @@ -1157,4 +1160,55 @@ mod tests {

let _ = GenericDeclareClass::<()>::class();
}

#[test]
fn test_inherited_nsobject_methods_work() {
declare_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
struct Custom;

unsafe impl ClassType for Custom {
type Super = NSObject;
type Mutability = Immutable;
const NAME: &'static str = "TestInheritedNSObjectMethodsWork";
}
);

extern_methods!(
unsafe impl Custom {
#[method_id(new)]
fn new() -> Id<Self>;
}
);

let obj1 = Custom::new();
let obj2 = Custom::new();

// isEqual:
assert_eq!(obj1, obj1);
assert_ne!(obj1, obj2);

// description
let expected =
format!("Custom {{ __superclass: ManuallyDrop {{ value: <TestInheritedNSObjectMethodsWork: {obj1:p}> }} }}");
assert_eq!(format!("{obj1:?}"), expected);

// hash
let mut hashstate1 = DefaultHasher::new();
let mut hashstate2 = DefaultHasher::new();

obj1.hash(&mut hashstate1);
obj1.hash(&mut hashstate2);

assert_eq!(hashstate1.finish(), hashstate2.finish());

let mut hashstate2 = DefaultHasher::new();
obj2.hash(&mut hashstate2);
assert_ne!(hashstate1.finish(), hashstate2.finish());

// isKindOfClass:
assert!(obj1.is_kind_of::<NSObject>());
assert!(obj1.is_kind_of::<Custom>());
assert!(obj1.is_kind_of::<Custom>());
}
}
11 changes: 11 additions & 0 deletions crates/objc2/src/runtime/nsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,15 @@ mod tests {
assert!(obj.is_kind_of::<NSObject>());
assert!(obj.is_kind_of::<__RcTestObject>());
}

#[test]
fn test_retain_same() {
let obj1 = NSObject::new();
let ptr1 = Id::as_ptr(&obj1);

let obj2 = obj1.clone();
let ptr2 = Id::as_ptr(&obj2);

assert_eq!(ptr1, ptr2);
}
}

0 comments on commit 391134a

Please sign in to comment.