From aab8ade15bca97760bedca7035e48f5565afb8f6 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Mon, 23 Jul 2012 15:25:52 +0200 Subject: [PATCH] Refs #1629. Fixed: `aSet removeObject:` for an object not a member is not an error. --- Foundation/CPSet/_CPConcreteMutableSet.j | 16 +++++++++++----- Tests/Foundation/CPSetTest.j | 8 ++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Foundation/CPSet/_CPConcreteMutableSet.j b/Foundation/CPSet/_CPConcreteMutableSet.j index 96b99dba2..c5e3f9c91 100644 --- a/Foundation/CPSet/_CPConcreteMutableSet.j +++ b/Foundation/CPSet/_CPConcreteMutableSet.j @@ -108,13 +108,19 @@ var hasOwnProperty = Object.prototype.hasOwnProperty; */ - (void)removeObject:(id)anObject { - var object = [self member:anObject]; - - if (object === nil || object === undefined) + // Removing nil is an error. + if (anObject === nil || anObject === undefined) [CPException raise:CPInvalidArgumentException reason:@"attempt to remove nil or undefined"]; - delete _contents[[object UID]]; - _count--; + // anObject might be isEqual: another object in the set. We need the exact instance so we can remove it by UID. + var object = [self member:anObject]; + + // ...but removing an object not present in the set is not an error. + if (object !== nil) + { + delete _contents[[object UID]]; + _count--; + } } /* diff --git a/Tests/Foundation/CPSetTest.j b/Tests/Foundation/CPSetTest.j index a961f3f02..3a6afc80a 100644 --- a/Tests/Foundation/CPSetTest.j +++ b/Tests/Foundation/CPSetTest.j @@ -182,16 +182,20 @@ [set2 addObject:dict1]; [set2 removeObject:dict2]; [self assertTrue:[set2 count] === 0]; + + // Removing an object not in the set is not an error. + [set2 removeObject:dict2]; } - (void)testRemoveZeroObject { var set = [CPSet new]; + // In Objective-J this is equivalent to [set addObject:[CPNumber numberWithInt:0]]; [set addObject:0]; - // [self assertTrue:[set containsObject:0]]; + [self assertTrue:[set containsObject:0] message:@"adding 0 to a set should work"]; [set removeObject:0]; - [self assertFalse:[set containsObject:0]]; + [self assertFalse:[set containsObject:0] message:@"removing 0 from a set should work"]; } - (void)testAddNilObject