Skip to content

Commit

Permalink
Added NSNull ActAsNullObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
endSly committed Dec 16, 2013
1 parent 68f0206 commit 43b5933
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
20 changes: 20 additions & 0 deletions NullObjects.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0D149D65185F49D300A39AF6 /* NONull.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D149D64185F49D300A39AF6 /* NONull.m */; };
0D149D68185F614F00A39AF6 /* NSNull+NullObjects.m in Sources */ = {isa = PBXBuildFile; fileRef = 0D149D67185F614F00A39AF6 /* NSNull+NullObjects.m */; };
0DDBCE27185F414C00B42744 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0DDBCE26185F414C00B42744 /* Foundation.framework */; };
0DDBCE2C185F414C00B42744 /* NullObjects.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 0DDBCE2B185F414C00B42744 /* NullObjects.h */; };
0DDBCE35185F414C00B42744 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0DDBCE34185F414C00B42744 /* XCTest.framework */; };
Expand Down Expand Up @@ -44,6 +45,8 @@
/* Begin PBXFileReference section */
0D149D63185F49D300A39AF6 /* NONull.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NONull.h; sourceTree = "<group>"; };
0D149D64185F49D300A39AF6 /* NONull.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NONull.m; sourceTree = "<group>"; };
0D149D66185F614F00A39AF6 /* NSNull+NullObjects.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSNull+NullObjects.h"; sourceTree = "<group>"; };
0D149D67185F614F00A39AF6 /* NSNull+NullObjects.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSNull+NullObjects.m"; sourceTree = "<group>"; };
0DDBCE23185F414C00B42744 /* libNullObjects.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libNullObjects.a; sourceTree = BUILT_PRODUCTS_DIR; };
0DDBCE26185F414C00B42744 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
0DDBCE2A185F414C00B42744 /* NullObjects-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NullObjects-Prefix.pch"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -114,6 +117,8 @@
0DDBCE2B185F414C00B42744 /* NullObjects.h */,
0D149D63185F49D300A39AF6 /* NONull.h */,
0D149D64185F49D300A39AF6 /* NONull.m */,
0D149D66185F614F00A39AF6 /* NSNull+NullObjects.h */,
0D149D67185F614F00A39AF6 /* NSNull+NullObjects.m */,
0DDBCE29185F414C00B42744 /* Supporting Files */,
);
path = NullObjects;
Expand Down Expand Up @@ -227,6 +232,7 @@
buildActionMask = 2147483647;
files = (
0D149D65185F49D300A39AF6 /* NONull.m in Sources */,
0D149D68185F614F00A39AF6 /* NSNull+NullObjects.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -370,6 +376,13 @@
"$(inherited)",
);
INFOPLIST_FILE = "NullObjectsTests/NullObjectsTests-Info.plist";
OTHER_LDFLAGS = (
"$(inherited)",
"-framework",
XCTest,
"-ObjC",
"-all_load",
);
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = xctest;
};
Expand All @@ -387,6 +400,13 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "NullObjects/NullObjects-Prefix.pch";
INFOPLIST_FILE = "NullObjectsTests/NullObjectsTests-Info.plist";
OTHER_LDFLAGS = (
"$(inherited)",
"-framework",
XCTest,
"-ObjC",
"-all_load",
);
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = xctest;
};
Expand Down
1 change: 0 additions & 1 deletion NullObjects/NONull.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

NSString * const NONullDummyMethodBlock = @"NODummyMethodBlock";
NSString * const NONullBlackHole = @"NONullBlackHole";
NSString * const NOTraceable = @"NOTraceable";

id dummyMethod(id self, SEL _cmd) {
return nil;
Expand Down
17 changes: 17 additions & 0 deletions NullObjects/NSNull+NullObjects.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NSNull+NullObjects.h
// NullObjects
//
// Created by Endika Gutiérrez Salas on 16/12/13.
// Copyright (c) 2013 Endika Gutiérrez Salas. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSNull (NullObjects)

+ (void)actAsNullObject;
+ (void)actAsBlackhole;
+ (void)actAsNullObjectWithOptions:(NSDictionary *)options;

@end
32 changes: 32 additions & 0 deletions NullObjects/NSNull+NullObjects.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// NSNull+NullObjects.m
// NullObjects
//
// Created by Endika Gutiérrez Salas on 16/12/13.
// Copyright (c) 2013 Endika Gutiérrez Salas. All rights reserved.
//

#import "NSNull+NullObjects.h"

#import <objc/runtime.h>

#import "NONull.h"

@implementation NSNull (NullObjects)

+ (void)actAsNullObject
{
object_setClass([NSNull null], [NONull class]);
}

+ (void)actAsBlackhole
{
object_setClass([NSNull null], [[NONull blackhole] class]);
}

+ (void)actAsNullObjectWithOptions:(NSDictionary *)options
{
object_setClass([NSNull null], [NONull nullClassWithOptions:options]);
}

@end
1 change: 1 addition & 0 deletions NullObjects/NullObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
//

#import "NONull.h"
#import "NSNull+NullObjects.h"
31 changes: 31 additions & 0 deletions NullObjectsTests/NullObjectsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,35 @@ - (void)testBlackholeNull
XCTAssertEqual(blackhole[@"key"], blackhole, @"[NONull null] should return self for any method");
}

- (void)testNSNullActAsNullObject
{
[NSNull actAsNullObject];

id simpleNull = (id) [NSNull null];

XCTAssertNoThrow([simpleNull lastObject], @"[NSNull actAsNullObject] never should raise exception");
XCTAssertNoThrow([[simpleNull firstObject] stringValue], @"[NONull null] never should raise exception");
XCTAssertNoThrow([simpleNull objectForKey:@"key"], @"[NONull null] never should raise exception");

XCTAssertEqual((__bridge void *)[simpleNull firstObject], nil, @"[NONull null] should return nil for any method");
XCTAssertEqual((__bridge void *)[simpleNull objectForKey:@"key"], nil, @"[NONull null] should return nil for any method");
}

- (void)testNSNullActAsBlackholeNull
{
[NSNull actAsBlackhole];

id blackhole = (id) [NSNull null];

XCTAssertNoThrow([blackhole lastObject], @"[NONull blackhole] never should raise exception");
XCTAssertNoThrow([[blackhole firstObject] stringValue], @"[NONull blackhole] never should raise exception");
XCTAssertNoThrow([blackhole objectForKey:@"key"], @"[NONull blackhole] never should raise exception");

XCTAssertEqual([blackhole firstObject], blackhole, @"[NONull null] should return self for any method");
XCTAssertEqual([[[blackhole firstObject] firstObject] string], blackhole, @"[NONull null] should return self for any method");
XCTAssertEqual([blackhole objectForKey:@"key"], blackhole, @"[NONull null] should return self for any method");
XCTAssertEqual(blackhole[@"key"], blackhole, @"[NONull null] should return self for any method");
}


@end

0 comments on commit 43b5933

Please sign in to comment.