Skip to content

Commit

Permalink
Merge pull request #504 from madsmtm/more-tests
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
madsmtm authored Sep 7, 2023
2 parents 5e1054d + 7030050 commit 112d7b0
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 8 deletions.
41 changes: 36 additions & 5 deletions crates/icrate/tests/mutable_dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,47 @@ fn test_insert() {
}

#[test]
fn test_insert_retain_release() {
fn test_insert_key_copies() {
let mut dict = NSMutableDictionary::new();
dict.insert(NSNumber::new_i32(1), __RcTestObject::new());
let key1 = __RcTestObject::new();
let mut expected = __ThreadTestData::current();

let old = dict.insert(NSNumber::new_i32(1), __RcTestObject::new());
let _ = dict.insert(key1, NSNumber::new_i32(1));
// Create copy
expected.copy += 1;
expected.alloc += 1;
expected.init += 1;
expected.retain += 2;
expected.release += 2;

// Release passed-in key
expected.release += 1;
expected.dealloc += 1;
expected.assert_current();

dict.removeAllObjects();
// Release key
expected.release += 1;
expected.dealloc += 1;
expected.assert_current();
}

#[test]
fn test_insert_value_retain_release() {
let mut dict = NSMutableDictionary::new();
dict.insert(NSNumber::new_i32(1), __RcTestObject::new());
let to_insert = __RcTestObject::new();
let mut expected = __ThreadTestData::current();

let old = dict.insert(NSNumber::new_i32(1), to_insert);
// Grab old value
expected.retain += 1;

// Dictionary takes new value and overwrites the old one
expected.retain += 1;
expected.release += 1;

// Release passed-in `Id`
expected.release += 1;

expected.assert_current();

drop(old);
Expand Down
14 changes: 14 additions & 0 deletions crates/icrate/tests/mutable_set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "Foundation_NSMutableSet")]
#![cfg(feature = "Foundation_NSString")]
use objc2::rc::{__RcTestObject, __ThreadTestData};
use objc2::ClassType;

use icrate::Foundation::{self, ns_string, NSMutableSet, NSSet, NSString};

Expand Down Expand Up @@ -79,17 +80,30 @@ fn test_insert_retain_release() {
let mut set = NSMutableSet::new();
let obj1 = __RcTestObject::new();
let obj2 = __RcTestObject::new();
let obj2_copy = obj2.retain();
let mut expected = __ThreadTestData::current();

set.insert(obj1);
// Retain to store in set
expected.retain += 1;
// Release passed in object
expected.release += 1;
expected.assert_current();
assert_eq!(set.len(), 1);
assert_eq!(set.get_any(), set.get_any());

set.insert(obj2);
// Retain to store in set
expected.retain += 1;
// Release passed in object
expected.release += 1;
expected.assert_current();
assert_eq!(set.len(), 2);

set.insert(obj2_copy);
// No retain, since the object is already in the set
expected.retain += 0;
// Release passed in object
expected.release += 1;
expected.assert_current();
assert_eq!(set.len(), 2);
Expand Down
60 changes: 57 additions & 3 deletions crates/objc2/src/declare/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,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 @@ -1159,4 +1162,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>());
}
}
23 changes: 23 additions & 0 deletions crates/objc2/src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ pub unsafe fn catch<R>(
mod tests {
use alloc::format;
use alloc::string::ToString;
use core::panic::AssertUnwindSafe;

use super::*;
use crate::msg_send_id;
use crate::runtime::NSObject;

#[test]
Expand Down Expand Up @@ -332,6 +334,27 @@ mod tests {
assert!(result.unwrap_err().is_none());
}

#[test]
#[cfg_attr(
feature = "catch-all",
ignore = "Panics inside `catch` when catch-all is enabled"
)]
fn test_catch_unknown_selector() {
let obj = AssertUnwindSafe(NSObject::new());
let ptr = Id::as_ptr(&obj);
let result = unsafe {
catch(|| {
let _: Id<NSObject> = msg_send_id![&*obj, copy];
})
};
let err = result.unwrap_err().unwrap();

assert_eq!(
format!("{err}"),
format!("-[NSObject copyWithZone:]: unrecognized selector sent to instance {ptr:?}"),
);
}

#[test]
fn test_throw_catch_object() {
let obj = NSObject::new();
Expand Down
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 112d7b0

Please sign in to comment.