-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathAboutARC.m
83 lines (69 loc) · 2.59 KB
/
AboutARC.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// AboutAssertions.m
// ObjectiveCKoans
//
// Created by Curtis Schofield
// Copyright 2011 BlazingCloud, Curtis J Schofield
//
#if __has_feature(objc_arc)
// Extending NSObject
#import <Foundation/Foundation.h>
// Make reference to a Person and PhoneNumber before they exist
@class Person;
@class PhoneNumber;
@interface Person : NSObject
// Example : create the property thingy with 'strong' ARC property
//
// ARC Notes : http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html
//
// More on ARC from the compiler team : http://clang.llvm.org/docs/AutomaticReferenceCounting.html
//
// @property (nonatomic,strong) NSString *thingy;
@end
@interface PhoneNumber : NSObject
// We would explicitly tell the ARC system that we want a weak reference to Person
// ie: don't keep the Person around if it's only retained reference is weak
@property (nonatomic, weak) Person *owner;
@end
#import "Kiwi.h"
SPEC_BEGIN(AboutARC)
describe(@"About Automatic Reference Counting", ^{
context(@"PhoneNumber",^{
__block PhoneNumber * phoneNumber;
beforeAll(^{
phoneNumber = [[PhoneNumber alloc]
initWithCountryCode:@"1"
areaCode:@"604"
digits:@"334-3244"];
});
it(@"can be initalized with area code and digits and countryCode without an owner", ^{
[[theValue(phoneNumber.areaCode) should] equal:theValue(@"604")];
[[theValue(phoneNumber.digits) should] equal:theValue(@"334-3244")];
[[theValue(phoneNumber.countryCode) should] equal:theValue(@"1")];
[phoneNumber.owner shouldBeNil];
});
it(@"can have an owner set",^{
Person *aPerson = [[Person alloc] init];
[phoneNumber setOwner:aPerson];
[phoneNumber.owner shouldNotBeNil];
[[phoneNumber.owner should] beIdenticalTo:aPerson];
});
it(@"arc disables retain",^{
// [phoneNumber retain];
// bypassing ARC will create memory leaks
//[phoneNumber performSelector:NSSelectorFromString(@"retain")];
});
});
context(@"Person",^{
it(@"sets a phone number to be owned by this person", ^{
Person *me = [[Person alloc] init];
PhoneNumber *phoneNumber = [[PhoneNumber alloc] initWithCountryCode:@"1"
areaCode:@"555"
digits:@"444-1234"];
[me setPhoneNumber:phoneNumber];
[[phoneNumber.owner should] beIdenticalTo:me];
});
});
});
SPEC_END
#endif